Skip to content

Commit

Permalink
Merge pull request #2489 from b-zee/docs-example-rust-code
Browse files Browse the repository at this point in the history
docs(autonomi): add example Rust code
  • Loading branch information
b-zee authored Dec 4, 2024
2 parents f0775e0 + a465459 commit 9a9e09e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cross-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:

- name: Cargo check for WASM
# Allow clippy lints (these can be pedantic on WASM), but deny regular Rust warnings
run: cargo clippy --target=wasm32-unknown-unknown --package=autonomi --all-targets -- --allow=clippy::all --deny=warnings
run: cargo clippy --target=wasm32-unknown-unknown --package=autonomi --lib --tests -- --allow=clippy::all --deny=warnings
timeout-minutes: 30

websocket:
Expand Down
44 changes: 41 additions & 3 deletions autonomi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,47 @@ Connect to and build on the Autonomi network.

Add the autonomi crate to your `Cargo.toml`:

```toml
[dependencies]
autonomi = { path = "../autonomi", version = "0.1.0" }
```sh
# `cargo add` adds dependencies to your Cargo.toml manifest file
cargo add autonomi
```

### Example

```rust
use autonomi::{Bytes, Client, Wallet};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Default wallet of testnet.
let key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";

let client = Client::connect(&["/ip4/127.0.0.1/udp/1234/quic-v1".parse()?]).await?;
let wallet = Wallet::new_from_private_key(Default::default(), key)?;

// Put and fetch data.
let data_addr = client
.data_put(Bytes::from("Hello, World"), (&wallet).into())
.await?;
let _data_fetched = client.data_get(data_addr).await?;

// Put and fetch directory from local file system.
let dir_addr = client.dir_upload("files/to/upload".into(), &wallet).await?;
client
.dir_download(dir_addr, "files/downloaded".into())
.await?;

Ok(())
}
```

In the above example the wallet is setup to use the default EVM network (Arbitrum One). Instead we can use a different network:
```rust
use autonomi::{EvmNetwork, Wallet};
// Arbitrum Sepolia
let wallet = Wallet::new_from_private_key(EvmNetwork::ArbitrumSepolia, key)?;
// Custom (e.g. local testnet)
let wallet = Wallet::new_from_private_key(EvmNetwork::new_custom("<rpc URL>", "<payment token address>", "<data payment address>"), key)?;
```

## Running tests
Expand Down
24 changes: 24 additions & 0 deletions autonomi/examples/put_and_dir_upload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use autonomi::{Bytes, Client, Wallet};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Default wallet of testnet.
let key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";

let client = Client::connect(&["/ip4/127.0.0.1/udp/1234/quic-v1".parse()?]).await?;
let wallet = Wallet::new_from_private_key(Default::default(), key)?;

// Put and fetch data.
let data_addr = client
.data_put(Bytes::from("Hello, World"), (&wallet).into())
.await?;
let _data_fetched = client.data_get(data_addr).await?;

// Put and fetch directory from local file system.
let dir_addr = client.dir_upload("files/to/upload".into(), &wallet).await?;
client
.dir_download(dir_addr, "files/downloaded".into())
.await?;

Ok(())
}

0 comments on commit 9a9e09e

Please sign in to comment.