Skip to content

Commit

Permalink
docs(autonomi): add example code
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Dec 4, 2024
1 parent ef3a2b1 commit 569364f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
35 changes: 32 additions & 3 deletions autonomi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,38 @@ 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, EvmNetwork, 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(EvmNetwork::ArbitrumSepolia, key)?;

// Put and fetch data.
let data_addr = client
.data_put(Bytes::from("Hello, World"), wallet.clone().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(())
}
```

## 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, EvmNetwork, Wallet};

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

Check failure

Code scanning / devskim

A token or key was found in source code. If this represents a secret, it should be moved somewhere else. Error

Do not store tokens or keys in source code.

let client = Client::connect(&["/ip4/127.0.0.1/udp/1234/quic-v1".parse()?]).await?;

Check notice

Code scanning / devskim

Accessing localhost could indicate debug code, or could hinder scaling. Note

Do not leave debug code in production
let wallet = Wallet::new_from_private_key(EvmNetwork::ArbitrumSepolia, key)?;

// Put and fetch data.
let data_addr = client
.data_put(Bytes::from("Hello, World"), wallet.clone().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 569364f

Please sign in to comment.