ethers-rs/examples/ethers-wasm/tests/contract_with_abi.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
1.4 KiB
Rust
Raw Normal View History

#![cfg(target_arch = "wasm32")]
2022-08-30 16:45:36 +00:00
use ethers::{
prelude::{Http, JsonRpcClient, LocalWallet, Provider, SignerMiddleware, Ws},
2022-08-30 16:45:36 +00:00
signers::Signer,
types::Chain,
};
use ethers_wasm::{utils, SimpleContract};
use std::sync::Arc;
2022-08-30 16:45:36 +00:00
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
async fn http_connect_and_deploy() {
console_log!("connecting http...");
let provider = Provider::<Http>::try_from("http://localhost:8545").unwrap();
deploy(provider, utils::key(0).with_chain_id(Chain::AnvilHardhat)).await;
}
#[wasm_bindgen_test]
async fn ws_connect_and_deploy() {
console_log!("connecting ws...");
let provider = Provider::new(Ws::connect("ws://localhost:8545").await.unwrap());
deploy(provider, utils::key(1).with_chain_id(Chain::AnvilHardhat)).await;
}
async fn deploy<T: JsonRpcClient>(provider: Provider<T>, wallet: LocalWallet) {
let client = Arc::new(SignerMiddleware::new(provider, wallet));
let expected = "Hello from Contract!";
let deploy_tx = SimpleContract::deploy(client, expected.to_string()).unwrap();
let contract: SimpleContract<_> = deploy_tx.send().await.unwrap();
let addr = contract.address();
console_log!("deployed to {addr}");
let value = contract.get_value().call().await.unwrap();
console_log!("value: {value:?}");
assert_eq!(value, expected);
}