Skip to content

Commit 0fa828d

Browse files
authored
Merge pull request #1 from SYS-Labs/update-lib
Update lib
2 parents 74645ba + 0172042 commit 0fa828d

File tree

6 files changed

+437
-81
lines changed

6 files changed

+437
-81
lines changed

Cargo.lock

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ serde_json = "1"
1717
reqwest = { version = "0.12.12", features = ["json"] }
1818
hex = "0.4"
1919
tokio = { version = "1", features = ["full"] }
20-
tracing = "0.1"
20+
tracing = "0.1"
21+
tracing-subscriber = "0.3.19"
2122
[dev-dependencies]
2223
mockito = "1.6.1"
2324
tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use std::time::Duration;
4444

4545
#[tokio::main]
4646
async fn main() -> Result<(), Box<dyn std::error::Error>> {
47-
let rpc_url = "http://localhost:8332";
47+
let rpc_url = "http://localhost:18370/wallet";
4848
let rpc_user = "your_rpc_user";
4949
let rpc_password = "your_rpc_password";
5050
let poda_url = "http://poda.example.com";

src/bin/main.rs

Lines changed: 101 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,121 @@
1+
use std::time::Duration;
2+
use tokio::time::sleep;
3+
use tracing::{info, debug, span, Level};
4+
use tracing::{Instrument};
5+
use tracing_subscriber::fmt;
16
use bitcoin_da_client::SyscoinClient;
27

38
type Error = Box<dyn std::error::Error + Send + Sync>;
49

510
#[tokio::main]
611
async fn main() -> Result<(), Error> {
7-
// Create a real RPC client
8-
println!("Starting");
9-
let rpc_url = "http://127.0.0.1:8370";
10-
let rpc_user = "u";
12+
// 🎛️ Initialize tracing: compact output, max DEBUG, no file/line info
13+
fmt()
14+
.with_max_level(Level::INFO)
15+
.with_file(false)
16+
.with_line_number(false)
17+
.with_target(false)
18+
.compact()
19+
.init();
20+
21+
info!("🚀 Starting Syscoin client application");
22+
23+
// 🔧 Configuration parameters
24+
let rpc_url = "http://127.0.0.1:8370";
25+
let rpc_user = "u";
1126
let rpc_password = "p";
12-
let poda_url = "http://poda.tanenbaum.io/vh/";
13-
let timeout = Some(std::time::Duration::from_secs(30));
27+
let poda_url = "https://poda.syscoin.org/vh/";
28+
let timeout = Some(Duration::from_secs(30));
29+
let wallet = "wallet200999";
30+
debug!(rpc_url, rpc_user, poda_url, timeout = ?timeout, wallet, "🔍 Config loaded");
1431

15-
// Initialize the client
32+
// 🔌 Initialize the Syscoin RPC client
33+
info!("🔌 Connecting to Syscoin node…");
1634
let client = SyscoinClient::new(
1735
rpc_url,
1836
rpc_user,
1937
rpc_password,
2038
poda_url,
2139
timeout,
40+
wallet,
2241
)?;
23-
24-
// Create/load wallet
25-
println!("Loading wallet");
26-
client.create_or_load_wallet("wallet12").await?;
42+
info!("✅ SyscoinClient initialized successfully");
43+
44+
// 💼 Create or load the wallet and ensure a stable funding address
45+
info!("🆕 Loading or creating wallet “{}”", wallet);
46+
client
47+
.create_or_load_wallet(wallet)
48+
.instrument(span!(Level::DEBUG, "create_or_load_wallet", wallet = wallet))
49+
.await?;
50+
let funding_label = "da_funding";
51+
let funding_address = client
52+
.ensure_address_by_label(funding_label)
53+
.instrument(span!(Level::DEBUG, "ensure_address_by_label", label = funding_label))
54+
.await?;
55+
info!("🏷️ Funding label '{}' is bound to address: {}", funding_label, funding_address);
56+
57+
// 📥 Fetch the current balance
58+
let mut balance = client
59+
.get_balance()
60+
.instrument(span!(Level::INFO, "get_balance_start"))
61+
.await?;
62+
debug!("📥 Balance fetched: {} SYS", balance);
63+
64+
// 💸 Funding flow if balance is zero
65+
if balance <= 0.0 {
66+
info!("⚠️ Balance empty, let's top you up!");
67+
let address = match client
68+
.fetch_address_by_label("podalabel")
69+
.instrument(span!(Level::DEBUG, "fetch_address_by_label", label = "podalabel"))
70+
.await?
71+
{
72+
Some(addr) => {
73+
info!("📍 Found existing funding address: {}", addr);
74+
addr
75+
}
76+
None => {
77+
info!("✨ No address yet—creating a fresh one…");
78+
let addr = client
79+
.get_new_address("podalabel")
80+
.instrument(span!(Level::DEBUG, "get_new_address", label = "podalabel"))
81+
.await?;
82+
info!("📍 New funding address: {}", addr);
83+
addr
84+
}
85+
};
86+
87+
info!("💌 Please send some SYS to: {}", address);
88+
89+
// 🔄 Poll until funds arrive
90+
while balance <= 0.0 {
91+
debug!("⏳ Waiting 10 seconds before checking balance again…");
92+
sleep(Duration::from_secs(10)).await;
93+
balance = client.get_balance().await?;
94+
info!("🔄 Checking… current balance: {} SYS", balance);
95+
}
96+
info!("🎉 Funds detected! Continuing…");
97+
}
2798

28-
println!("Checking balance");
29-
let balance = client.get_balance().await?;
30-
println!("Balance: {balance}");
99+
// 📤 Blob upload/retrieval flow
100+
let data_to_upload = [1, 2, 3, 4];
101+
info!("📤 Uploading blob data: {:?}", data_to_upload);
102+
let blob_hash = client
103+
.create_blob(&data_to_upload)
104+
.instrument(span!(Level::DEBUG, "create_blob", data = ?data_to_upload))
105+
.await?;
106+
info!("✅ Blob uploaded! Got hash: {}", blob_hash);
31107

32-
println!("Uploading blob data");
33-
let blob_hash = client.create_blob(&[1, 2, 3, 4]).await?;
34-
println!("Created blob: {blob_hash}");
108+
info!("📥 Fetching blob back by hash…");
109+
let blob_data = client
110+
.get_blob(&blob_hash)
111+
.instrument(span!(Level::DEBUG, "get_blob", hash = %blob_hash))
112+
.await?;
113+
info!("🗒️ Blob data retrieved: {:?}", blob_data);
35114

36-
println!("Fetching it back (RPC → cloud fallback)");
37-
let blob_data = client.get_blob(&blob_hash).await?;
38-
println!("Blob data: {blob_data:?}");
115+
// 🔗 Log the data availability (DA) link
116+
let da_link = format!("{}{}", poda_url, blob_hash);
117+
info!("🔗 Access your data here: {}", da_link);
39118

119+
info!("🏁 Syscoin client flow complete—have a great day!");
40120
Ok(())
41-
}
121+
}

0 commit comments

Comments
 (0)