Skip to content

Commit

Permalink
Merge pull request #2356 from b-zee/feat-autonomi-set-evm-network
Browse files Browse the repository at this point in the history
feat(autonomi): add method to get custom evm net
  • Loading branch information
mickvandijke authored Oct 30, 2024
2 parents 17febdc + d6a7338 commit 0f98b24
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
13 changes: 11 additions & 2 deletions autonomi/WASM_docs.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# JavaScript Autonomi API Documentation
# Autonomi JS API

Note that this is a first version and will be subject to change.
Note: the JS API is experimental and will be subject to change.

The entry point for connecting to the network is {@link Client.connect}.

Expand All @@ -12,6 +12,8 @@ For addresses (chunk, data, archives, etc) we're using hex-encoded strings conta

## Example

Note: `getEvmNetwork` will use hardcoded EVM network values that should be set during compilation of this library.

```javascript
import init, { Client, Wallet, getEvmNetwork } from 'autonomi';

Expand All @@ -28,3 +30,10 @@ console.log("Data stored at:", result);
let fetchedData = await client.get(result);
console.log("Data retrieved:", fetchedData);
```

## Funded wallet from custom local network

```js
const evmNetwork = getEvmNetworkCustom("http://localhost:4343", "<payment token addr>", "<data payments addr>");
const wallet = getFundedWalletWithCustomNetwork(evmNetwork, "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80");
```
32 changes: 32 additions & 0 deletions autonomi/src/client/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,27 @@ pub fn evm_network() -> Result<JsValue, JsError> {
Ok(js_value)
}

/// Create an `EvmNetwork` with custom values.
///
/// # Example
///
/// ```js
/// const [quotes, quotePayments, free_chunks] = await client.getQuotes(data);
/// const evmNetwork = getEvmNetworkCustom("http://localhost:4343", "<payment token addr>", "<data payments addr>");
/// const payForQuotesCalldata = getPayForQuotesCalldata(evmNetwork, quotePayments);
/// ```
#[wasm_bindgen(js_name = getEvmNetworkCustom)]
pub fn evm_network_custom(
rpc_url: String,
payment_token_address: String,
data_payments_address: String,
) -> Result<JsValue, JsError> {
let evm_network =
evmlib::utils::get_evm_network(&rpc_url, &payment_token_address, &data_payments_address);
let js_value = serde_wasm_bindgen::to_value(&evm_network)?;
Ok(js_value)
}

#[wasm_bindgen(js_name = Wallet)]
pub struct JsWallet(evmlib::wallet::Wallet);

Expand All @@ -431,6 +452,17 @@ pub fn funded_wallet() -> JsWallet {
JsWallet(wallet)
}

/// Get a funded wallet with a custom network.
#[wasm_bindgen(js_name = getFundedWalletWithCustomNetwork)]
pub fn funded_wallet_with_custom_network(
network: JsValue,
private_key: String,
) -> Result<JsWallet, JsError> {
let network: evmlib::Network = serde_wasm_bindgen::from_value(network)?;
let wallet = evmlib::wallet::Wallet::new_from_private_key(network, &private_key)?;
Ok(JsWallet(wallet))
}

/// Enable tracing logging in the console.
///
/// A level could be passed like `trace` or `warn`. Or set for a specific module/crate
Expand Down
13 changes: 13 additions & 0 deletions evmlib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ pub fn get_evm_testnet_csv_path() -> Result<PathBuf, Error> {
Ok(file)
}

/// Create a custom `Network` from the given values
pub fn get_evm_network(
rpc_url: &str,
payment_token_address: &str,
data_payments_address: &str,
) -> Network {
Network::Custom(CustomNetwork::new(
rpc_url,
payment_token_address,
data_payments_address,
))
}

/// Get the `Network` from environment variables
/// Returns an error if we cannot obtain the network from any means.
pub fn get_evm_network_from_env() -> Result<Network, Error> {
Expand Down

0 comments on commit 0f98b24

Please sign in to comment.