2021-12-03 17:27:55 +00:00
|
|
|
#![allow(clippy::all)]
|
|
|
|
|
2021-08-23 22:28:05 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
use web_sys::console;
|
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
|
|
|
};
|
2021-08-23 22:28:05 +00:00
|
|
|
|
|
|
|
use crate::utils::SIMPLECONTRACT_BIN;
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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(),
|
2021-08-23 09:56:44 +00:00
|
|
|
&JsValue::from_serde(&*SIMPLECONTRACT_ABI).unwrap(),
|
|
|
|
);
|
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";
|
|
|
|
let provider = Provider::new(Ws::connect(endpoint).await.unwrap());
|
|
|
|
let client = Arc::new(SignerMiddleware::new(provider, wallet));
|
2021-08-23 22:28:05 +00:00
|
|
|
log!("Provider 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();
|
|
|
|
console::log_2(&"Set value receipt: ".into(), &JsValue::from_serde(&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(
|
|
|
|
&format!("Value: `{}`. Logs: ", value).into(),
|
|
|
|
&JsValue::from_serde(&logs).unwrap(),
|
2021-08-23 09:56:44 +00:00
|
|
|
);
|
|
|
|
}
|