Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
i1i1 committed Oct 3, 2022
1 parent 372a3cb commit 65162a0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ edition = "2021"

[dependencies]
libp2p-core = "0.36"

[dev-dependencies]
tokio = { version = "1.21", features = ["rt-multi-thread", "macros"] }
78 changes: 78 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 65162a0

Please sign in to comment.