7 releases

new 0.2.3 Nov 6, 2024
0.2.2 Oct 29, 2024
0.1.2 Oct 7, 2024
0.1.0 Sep 24, 2024

#316 in Web programming

Download history 145/week @ 2024-09-23 163/week @ 2024-09-30 166/week @ 2024-10-07 27/week @ 2024-10-14 267/week @ 2024-10-21 140/week @ 2024-10-28

603 downloads per month
Used in 2 crates

GPL-3.0 license

1MB
15K SLoC

autonomi - Autonomi client API

Crates.io docs.rs

Connect to and build on the Autonomi network.

Usage

Add the autonomi crate to your Cargo.toml:

[dependencies]
autonomi = { path = "../autonomi", version = "0.1.0" }

Running tests

Using a local EVM testnet

  1. If you haven't, install Foundry, to be able to run Anvil nodes: https://book.getfoundry.sh/getting-started/installation
  2. Run a local EVM node:
cargo run --bin evm_testnet
  1. Run a local network with the local feature and use the local evm node.
cargo run --bin=safenode-manager --features=local -- local run --build --clean --rewards-address <ETHEREUM_ADDRESS> evm-local
  1. Then run the tests with the local feature and pass the EVM params again:
EVM_NETWORK=local cargo test --package=autonomi --features=local
# Or with logs
RUST_LOG=autonomi EVM_NETWORK=local cargo test --package=autonomi --features=local -- --nocapture

Using a live testnet or mainnet

Using the hardcoded Arbitrum One option as an example, but you can also use the command flags of the steps above and point it to a live network.

  1. Run a local network with the local feature:
cargo run --bin=safenode-manager --features=local -- local run --build --clean --rewards-address <ETHEREUM_ADDRESS> evm-arbitrum-one
  1. Then run the tests with the local feature. Make sure that the wallet of the private key you pass has enough gas and payment tokens on the network (in this case Arbitrum One):
EVM_NETWORK=arbitrum-one EVM_PRIVATE_KEY=<PRIVATE_KEY> cargo test --package=autonomi --features=local
# Or with logs
RUST_LOG=autonomi EVM_NETWORK=arbitrum-one EVM_PRIVATE_KEY=<PRIVATE_KEY> cargo test --package=autonomi --features=local -- --nocapture

WebAssembly

To run a WASM test

  • Install wasm-pack
  • Make sure your Rust supports the wasm32-unknown-unknown target. (If you have rustup: rustup target add wasm32-unknown-unknown.)
  • Pass a bootstrap peer via SAFE_PEERS. This has to be the websocket address, e.g. /ip4/<ip>/tcp/<port>/ws/p2p/<peer ID>.
    • As well as the other environment variables needed for EVM payments (e.g. RPC_URL).
  • Optionally specify the specific test, e.g. -- put to run put() in wasm.rs only.

Example:

SAFE_PEERS=/ip4/<ip>/tcp/<port>/ws/p2p/<peer ID> wasm-pack test --release --firefox autonomi --features=data,files --test wasm -- put

Test from JS in the browser

wasm-pack test does not execute JavaScript, but runs mostly WebAssembly. Again make sure the environment variables are set and build the JS package:

wasm-pack build --dev --target=web autonomi --features=vault

Then cd into autonomi/tests-js, and use npm to install and serve the test html file.

cd autonomi/tests-js
npm install
npm run serve

Then go to http://127.0.0.1:8080/tests-js in the browser. Here, enter a ws multiaddr of a local node and press ' run'.

MetaMask example

There is a MetaMask example for doing a simple put operation.

Build the package with the external-signer feature (and again with the env variables) and run a webserver, e.g. with Python:

wasm-pack build --dev --target=web autonomi --features=external-signer
python -m http.server --directory=autonomi 8000

Then visit http://127.0.0.1:8000/examples/metamask in your (modern) browser.

Here, enter a ws multiaddr of a local node and press 'run'.

Faucet (local)

There is no faucet server, but instead you can use the Deployer wallet private key printed in the EVM node output to initialise a wallet from with almost infinite gas and payment tokens. Example:

let rpc_url = "http://localhost:54370/";
let payment_token_address = "0x5FbDB2315678afecb367f032d93F642f64180aa3";
let data_payments_address = "0x8464135c8F25Da09e49BC8782676a84730C318bC";
let private_key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";

let network = Network::Custom(CustomNetwork::new(
rpc_url,
payment_token_address,
data_payments_address,
));

let deployer_wallet = Wallet::new_from_private_key(network, private_key).unwrap();
let receiving_wallet = Wallet::new_with_random_wallet(network);

// Send 10 payment tokens (atto)
let _ = deployer_wallet
.transfer_tokens(receiving_wallet.address(), Amount::from(10))
.await;

Alternatively, you can provide the wallet address that should own all the gas and payment tokens to the EVM testnet startup command using the --genesis-wallet flag:

cargo run --bin evm_testnet -- --genesis-wallet <ETHEREUM_ADDRESS>
*************************
* Ethereum node started *
*************************
RPC URL: http://localhost:60093/
Payment token address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Chunk payments address: 0x8464135c8F25Da09e49BC8782676a84730C318bC
Deployer wallet private key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
Genesis wallet balance: (tokens: 20000000000000000000000000, gas: 9998998011366954730202)

Dependencies

~67–105MB
~2M SLoC