2022-09-19 17:49:27 +00:00
|
|
|
use crate::utils::SIMPLECONTRACT_BIN;
|
2021-08-23 09:56:44 +00:00
|
|
|
use ethers::{
|
|
|
|
contract::abigen,
|
2021-08-23 22:28:05 +00:00
|
|
|
prelude::{ContractFactory, Provider, SignerMiddleware},
|
|
|
|
providers::Ws,
|
2021-08-23 09:56:44 +00:00
|
|
|
};
|
2022-09-19 17:49:27 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
use web_sys::console;
|
2021-08-23 22:28:05 +00:00
|
|
|
|
|
|
|
pub mod utils;
|
2021-08-23 09:56:44 +00:00
|
|
|
|
|
|
|
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
|
|
|
// allocator.
|
|
|
|
#[cfg(feature = "wee_alloc")]
|
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|
|
|
|
|
|
|
macro_rules! log {
|
|
|
|
( $( $t:tt )* ) => {
|
|
|
|
web_sys::console::log_1(&format!( $( $t )* ).into());
|
|
|
|
}
|
|
|
|
}
|
2022-09-19 17:49:27 +00:00
|
|
|
|
2021-08-23 09:56:44 +00:00
|
|
|
abigen!(
|
|
|
|
SimpleContract,
|
|
|
|
"./../contract_abi.json",
|
|
|
|
event_derives(serde::Deserialize, serde::Serialize)
|
|
|
|
);
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2021-08-23 22:28:05 +00:00
|
|
|
pub async fn deploy() {
|
2021-08-23 09:56:44 +00:00
|
|
|
utils::set_panic_hook();
|
|
|
|
|
|
|
|
console::log_2(
|
2021-08-23 22:28:05 +00:00
|
|
|
&"SimpleContract ABI: ".into(),
|
2022-09-19 17:49:27 +00:00
|
|
|
&serde_wasm_bindgen::to_value(&*SIMPLECONTRACT_ABI).unwrap(),
|
2021-08-23 09:56:44 +00:00
|
|
|
);
|
2021-08-23 22:28:05 +00:00
|
|
|
|
|
|
|
let wallet = utils::key(0);
|
2021-08-23 09:56:44 +00:00
|
|
|
log!("Wallet: {:?}", wallet);
|
|
|
|
|
|
|
|
let endpoint = "ws://127.0.0.1:8545";
|
2022-09-19 17:49:27 +00:00
|
|
|
let provider = Provider::<Ws>::connect(endpoint).await.unwrap();
|
2021-08-23 09:56:44 +00:00
|
|
|
let client = Arc::new(SignerMiddleware::new(provider, wallet));
|
2022-09-19 17:49:27 +00:00
|
|
|
log!("Connected to: `{}`", endpoint);
|
2021-08-23 09:56:44 +00:00
|
|
|
|
|
|
|
let bytecode = hex::decode(SIMPLECONTRACT_BIN).unwrap();
|
|
|
|
let factory = ContractFactory::new(SIMPLECONTRACT_ABI.clone(), bytecode.into(), client.clone());
|
2021-08-23 22:28:05 +00:00
|
|
|
|
|
|
|
log!("Deploying contract...");
|
2021-12-03 17:27:55 +00:00
|
|
|
let contract = factory.deploy("hello WASM!".to_string()).unwrap().send().await.unwrap();
|
2021-08-23 09:56:44 +00:00
|
|
|
let addr = contract.address();
|
2021-08-23 22:28:05 +00:00
|
|
|
log!("Deployed contract with address: {:?}", addr);
|
2021-08-23 09:56:44 +00:00
|
|
|
|
|
|
|
let contract = SimpleContract::new(addr, client.clone());
|
|
|
|
|
2021-08-23 22:28:05 +00:00
|
|
|
let value = "bye from WASM!";
|
|
|
|
log!("Setting value... `{}`", value);
|
2021-12-03 17:27:55 +00:00
|
|
|
let receipt = contract.set_value(value.to_owned()).send().await.unwrap().await.unwrap();
|
2022-09-19 17:49:27 +00:00
|
|
|
console::log_2(&"Set value receipt: ".into(), &serde_wasm_bindgen::to_value(&receipt).unwrap());
|
2021-08-23 22:28:05 +00:00
|
|
|
|
|
|
|
log!("Fetching logs...");
|
2021-12-03 17:27:55 +00:00
|
|
|
let logs = contract.value_changed_filter().from_block(0u64).query().await.unwrap();
|
2021-08-23 09:56:44 +00:00
|
|
|
|
|
|
|
let value = contract.get_value().call().await.unwrap();
|
|
|
|
|
2021-08-23 22:28:05 +00:00
|
|
|
console::log_2(
|
2022-11-07 23:43:11 +00:00
|
|
|
&format!("Value: `{value}`. Logs: ").into(),
|
2022-09-19 17:49:27 +00:00
|
|
|
&serde_wasm_bindgen::to_value(&logs).unwrap(),
|
2021-08-23 09:56:44 +00:00
|
|
|
);
|
|
|
|
}
|