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 Sep 18, 2022
1 parent e52fcca commit 0b56299
Show file tree
Hide file tree
Showing 2 changed files with 93 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"] }
90 changes: 90 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#![allow(unreachable_code, unused_variables, clippy::diverging_sub_expression)]

use std::{future::Future, pin::Pin};

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 = todo!("Can't construct reward address right now :(");
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)
.build()
.await
.expect("Failed to init a farmer");

farmer.sync().await;

farmer
.on_solution(|solution| {
// Yeah very cumbersome, but rust can't coerce types here.
// TODO: look into https://doc.rust-lang.org/stable/std/ops/trait.CoerceUnsized.html
let fut: Pin<Box<dyn Future<Output = ()>>> = Box::pin(async move {
eprintln!("Found solution: {solution:?}");
});
fut
})
.await;
node.on_block(|block| {
let fut: Pin<Box<dyn Future<Output = ()>>> = Box::pin(async move {
eprintln!("New block: {block:?}");
});
fut
})
.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 0b56299

Please sign in to comment.