From 65162a0e8bd6f342f65429aa3f3b83f163a41f5a Mon Sep 17 00:00:00 2001 From: i1i1 Date: Mon, 3 Oct 2022 16:35:37 +0300 Subject: [PATCH] Add example --- Cargo.toml | 3 ++ examples/simple.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 examples/simple.rs diff --git a/Cargo.toml b/Cargo.toml index e0f83b11..eeb60bca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" [dependencies] libp2p-core = "0.36" + +[dev-dependencies] +tokio = { version = "1.21", features = ["rt-multi-thread", "macros"] } diff --git a/examples/simple.rs b/examples/simple.rs new file mode 100644 index 00000000..574681c4 --- /dev/null +++ b/examples/simple.rs @@ -0,0 +1,78 @@ +use subspace_sdk::{ + Farmer, FarmerBuilder, Network, Node, NodeBuilder, NodeMode, PlotDescription, RewardAddress, +}; + +#[tokio::main] +async fn main() { + let mut node: Node = NodeBuilder::new() + .mode(NodeMode::Full) + .network(Network::Gemini2a) + .name("i1i1") + .port(1337) + .directory("node") + .build() + .await + .expect("Failed to init a node"); + + node.sync().await; + + let reward_address = RewardAddress::from_raw([0; 32]); + let mut farmer: Farmer = FarmerBuilder::new() + .node(node.clone()) + .plot(PlotDescription::new("plot", 10 * 1024 * 1024 * 1024)) + .ws_rpc("127.0.0.1:9955".parse().unwrap()) + .listen_on("/ip4/0.0.0.0/tcp/40333".parse().unwrap()) + .reward_address(reward_address.clone()) + .build() + .await + .expect("Failed to init a farmer"); + + farmer.sync().await; + + farmer + .on_solution(|solution| async move { + eprintln!("Found solution: {solution:?}"); + }) + .await; + node.on_block(|block| async move { + eprintln!("New block: {block:?}"); + }) + .await; + + farmer.start_farming().await; + + dbg!(node.get_info().await); + dbg!(farmer.get_info().await); + + farmer.stop_farming().await; + farmer.close().await; + node.close().await; + + // Restarting + let mut node = NodeBuilder::new() + .mode(NodeMode::Full) + .network(Network::Gemini2a) + .directory("node") + .build() + .await + .expect("Failed to init a node"); + node.sync().await; + + let mut farmer = FarmerBuilder::new() + .node(node.clone()) + .plot(PlotDescription::new("plot", 10 * 1024 * 1024 * 1024)) + .reward_address(reward_address) + .build() + .await + .expect("Failed to init a farmer"); + + farmer.sync().await; + farmer.start_farming().await; + + // Delete everything + for plot in farmer.plots().await { + plot.delete().await; + } + farmer.wipe().await; + node.wipe().await; +}