Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(autonomi): add example Rust code #2489

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.clone().into())
b-zee marked this conversation as resolved.
Show resolved Hide resolved
.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";
Dismissed Show dismissed Hide dismissed

let client = Client::connect(&["/ip4/127.0.0.1/udp/1234/quic-v1".parse()?]).await?;
Dismissed Show dismissed Hide dismissed
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.clone().into())
b-zee marked this conversation as resolved.
Show resolved Hide resolved
.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(())
}
Loading