-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathmain.rs
More file actions
65 lines (51 loc) · 2.07 KB
/
main.rs
File metadata and controls
65 lines (51 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![allow(unused)]
use bitcoin::hex::DisplayHex;
use bitcoincore_rpc::bitcoin::Amount;
use bitcoincore_rpc::{Auth, Client, RpcApi};
use serde::Deserialize;
use serde_json::json;
use std::fs::File;
use std::io::Write;
// Node access params
const RPC_URL: &str = "http://127.0.0.1:18443"; // Default regtest RPC port
const RPC_USER: &str = "alice";
const RPC_PASS: &str = "password";
// You can use calls not provided in RPC lib API using the generic `call` function.
// An example of using the `send` RPC call, which doesn't have exposed API.
// You can also use serde_json `Deserialize` derivation to capture the returned json result.
fn send(rpc: &Client, addr: &str) -> bitcoincore_rpc::Result<String> {
let args = [
json!([{addr : 100 }]), // recipient address
json!(null), // conf target
json!(null), // estimate mode
json!(null), // fee rate in sats/vb
json!(null), // Empty option object
];
#[derive(Deserialize)]
struct SendResult {
complete: bool,
txid: String,
}
let send_result = rpc.call::<SendResult>("send", &args)?;
assert!(send_result.complete);
Ok(send_result.txid)
}
fn main() -> bitcoincore_rpc::Result<()> {
// Connect to Bitcoin Core RPC
let rpc = Client::new(
RPC_URL,
Auth::UserPass(RPC_USER.to_owned(), RPC_PASS.to_owned()),
)?;
// Get blockchain info
let blockchain_info = rpc.get_blockchain_info()?;
println!("Blockchain Info: {:?}", blockchain_info);
// Create/Load the wallets, named 'Miner' and 'Trader'. Have logic to optionally create/load them if they do not exist or not loaded already.
// Generate spendable balances in the Miner wallet. How many blocks needs to be mined?
// Load Trader wallet and generate a new address
// Send 20 BTC from Miner to Trader
// Check transaction in mempool
// Mine 1 block to confirm the transaction
// Extract all required transaction details
// Write the data to ../out.txt in the specified format given in readme.md
Ok(())
}