2021-08-23 09:56:44 +00:00
|
|
|
#![cfg(target_arch = "wasm32")]
|
|
|
|
|
2022-08-30 16:45:36 +00:00
|
|
|
use ethers::{
|
|
|
|
prelude::{
|
|
|
|
abigen, ContractFactory, Http, JsonRpcClient, LocalWallet, Provider, SignerMiddleware, Ws,
|
|
|
|
},
|
|
|
|
signers::Signer,
|
|
|
|
types::Chain,
|
2021-08-23 09:56:44 +00:00
|
|
|
};
|
2021-12-03 17:27:55 +00:00
|
|
|
use std::{convert::TryFrom, sync::Arc};
|
2022-08-30 16:45:36 +00:00
|
|
|
use wasm_bindgen_test::*;
|
2021-08-23 09:56:44 +00:00
|
|
|
|
|
|
|
wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
|
|
|
|
// Generate the type-safe contract bindings by providing the ABI
|
|
|
|
// definition in human readable format
|
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
|
|
|
"../contract_abi.json",
|
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
2021-08-23 22:28:05 +00:00
|
|
|
async fn http_connect_and_deploy() {
|
|
|
|
console_log!("connecting http...");
|
|
|
|
let provider = Provider::<Http>::try_from("http://localhost:8545").unwrap();
|
2022-08-30 16:45:36 +00:00
|
|
|
deploy(provider, ethers_wasm::utils::key(0).with_chain_id(Chain::AnvilHardhat)).await;
|
2021-08-23 22:28:05 +00:00
|
|
|
}
|
2021-08-23 09:56:44 +00:00
|
|
|
|
2021-08-23 22:28:05 +00:00
|
|
|
#[wasm_bindgen_test]
|
|
|
|
async fn ws_connect_and_deploy() {
|
|
|
|
console_log!("connecting ws...");
|
2021-08-23 09:56:44 +00:00
|
|
|
let provider = Provider::new(Ws::connect("ws://localhost:8545").await.unwrap());
|
2022-08-30 16:45:36 +00:00
|
|
|
deploy(provider, ethers_wasm::utils::key(1).with_chain_id(Chain::AnvilHardhat)).await;
|
2021-08-23 22:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn deploy<T: JsonRpcClient>(provider: Provider<T>, wallet: LocalWallet) {
|
2021-08-23 09:56:44 +00:00
|
|
|
let client = Arc::new(SignerMiddleware::new(provider, wallet));
|
|
|
|
|
|
|
|
let bytecode = hex::decode(ethers_wasm::utils::SIMPLECONTRACT_BIN).unwrap();
|
|
|
|
let factory = ContractFactory::new(SIMPLECONTRACT_ABI.clone(), bytecode.into(), client.clone());
|
2021-12-03 17:27:55 +00:00
|
|
|
let contract =
|
|
|
|
factory.deploy("Hello from Contract!".to_string()).unwrap().send().await.unwrap();
|
2021-08-23 09:56:44 +00:00
|
|
|
let addr = contract.address();
|
|
|
|
console_log!("deployed to {}", addr);
|
|
|
|
|
|
|
|
let contract = SimpleContract::new(addr, client.clone());
|
|
|
|
let value = contract.get_value().call().await.unwrap();
|
|
|
|
|
2021-08-23 22:28:05 +00:00
|
|
|
console_log!("value: {:?}", value);
|
2021-08-23 09:56:44 +00:00
|
|
|
}
|