diff --git a/Cargo.lock b/Cargo.lock index 14440afd..396ec836 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1215,7 +1215,7 @@ dependencies = [ "cbc", "chacha20poly1305", "dashmap", - "ethers", + "ethers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "eyre", "futures", "hex", @@ -1257,7 +1257,7 @@ dependencies = [ "cosmos-sdk-proto", "cosmrs", "defi-wallet-core-proto", - "ethers", + "ethers 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "eyre", "futures", "futures-util", @@ -1301,7 +1301,7 @@ dependencies = [ "cxx-build", "defi-wallet-core-common", "defi-wallet-core-proto", - "ethers", + "ethers 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "hex", "keyring", "serde", @@ -1697,14 +1697,29 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "839a392641e746a1ff365ef7c901238410b5c6285d240cf2409ffaaa7df9a78a" dependencies = [ - "ethers-addressbook", - "ethers-contract", - "ethers-core", - "ethers-etherscan", - "ethers-middleware", - "ethers-providers", - "ethers-signers", - "ethers-solc", + "ethers-addressbook 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-contract 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-etherscan 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-middleware 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-providers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-signers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-solc 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethers" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "ethers-addressbook 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-contract 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-etherscan 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-middleware 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-providers 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-signers 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-solc 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", ] [[package]] @@ -1713,7 +1728,18 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1e010165c08a2a3fa43c0bb8bc9d596f079a021aaa2cc4e8d921df09709c95" dependencies = [ - "ethers-core", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "once_cell", + "serde", + "serde_json", +] + +[[package]] +name = "ethers-addressbook" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "once_cell", "serde", "serde_json", @@ -1725,10 +1751,28 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be33fd47a06cc8f97caf614cf7cf91af9dd6dcd767511578895fa884b430c4b8" dependencies = [ - "ethers-contract-abigen", - "ethers-contract-derive", - "ethers-core", - "ethers-providers", + "ethers-contract-abigen 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-contract-derive 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-providers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util", + "hex", + "once_cell", + "pin-project", + "serde", + "serde_json", + "thiserror", +] + +[[package]] +name = "ethers-contract" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "ethers-contract-abigen 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-contract-derive 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-providers 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "futures-util", "hex", "once_cell", @@ -1747,8 +1791,35 @@ dependencies = [ "Inflector", "cfg-if", "dunce", - "ethers-core", - "ethers-etherscan", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-etherscan 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eyre", + "getrandom 0.2.10", + "hex", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "reqwest", + "serde", + "serde_json", + "syn 1.0.109", + "tokio", + "toml", + "url", + "walkdir", +] + +[[package]] +name = "ethers-contract-abigen" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "Inflector", + "cfg-if", + "dunce", + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-etherscan 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "eyre", "getrandom 0.2.10", "hex", @@ -1772,8 +1843,23 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "001b33443a67e273120923df18bab907a0744ad4b5fef681a8b0691f2ee0f3de" dependencies = [ - "ethers-contract-abigen", - "ethers-core", + "ethers-contract-abigen 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eyre", + "hex", + "proc-macro2", + "quote", + "serde_json", + "syn 1.0.109", +] + +[[package]] +name = "ethers-contract-derive" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "ethers-contract-abigen 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "eyre", "hex", "proc-macro2", @@ -1816,14 +1902,64 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ethers-core" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "arrayvec", + "bytes", + "cargo_metadata", + "chrono", + "convert_case", + "elliptic-curve", + "ethabi", + "generic-array 0.14.7", + "getrandom 0.2.10", + "hex", + "k256", + "num_enum", + "once_cell", + "open-fastrlp", + "proc-macro2", + "rand 0.8.5", + "rlp", + "rlp-derive", + "serde", + "serde_json", + "strum", + "syn 1.0.109", + "tempfile", + "thiserror", + "tiny-keccak", + "unicode-xid", +] + [[package]] name = "ethers-etherscan" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d769437fafd0b47ea8b95e774e343c5195c77423f0f54b48d11c0d9ed2148ad" dependencies = [ - "ethers-core", - "ethers-solc", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-solc 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "getrandom 0.2.10", + "reqwest", + "semver", + "serde", + "serde-aux", + "serde_json", + "thiserror", + "tracing", +] + +[[package]] +name = "ethers-etherscan" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-solc 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "getrandom 0.2.10", "reqwest", "semver", @@ -1842,11 +1978,36 @@ checksum = "a7dd311b76eab9d15209e4fd16bb419e25543709cbdf33079e8923dfa597517c" dependencies = [ "async-trait", "auto_impl", - "ethers-contract", - "ethers-core", - "ethers-etherscan", - "ethers-providers", - "ethers-signers", + "ethers-contract 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-etherscan 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-providers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-signers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-locks", + "futures-util", + "instant", + "reqwest", + "serde", + "serde_json", + "thiserror", + "tokio", + "tracing", + "tracing-futures", + "url", +] + +[[package]] +name = "ethers-middleware" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "async-trait", + "auto_impl", + "ethers-contract 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-etherscan 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-providers 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "ethers-signers 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "futures-locks", "futures-util", "instant", @@ -1870,7 +2031,7 @@ dependencies = [ "auto_impl", "base64 0.21.2", "enr", - "ethers-core", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures-channel", "futures-core", "futures-timer", @@ -1898,6 +2059,42 @@ dependencies = [ "ws_stream_wasm", ] +[[package]] +name = "ethers-providers" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "async-trait", + "auto_impl", + "base64 0.21.2", + "enr", + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", + "futures-core", + "futures-timer", + "futures-util", + "getrandom 0.2.10", + "hashers", + "hex", + "http", + "once_cell", + "parking_lot 0.11.2", + "pin-project", + "reqwest", + "serde", + "serde_json", + "thiserror", + "tokio", + "tokio-tungstenite", + "tracing", + "tracing-futures", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-timer", + "web-sys", + "ws_stream_wasm", +] + [[package]] name = "ethers-signers" version = "2.0.0" @@ -1909,7 +2106,25 @@ dependencies = [ "coins-bip39", "elliptic-curve", "eth-keystore", - "ethers-core", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hex", + "rand 0.8.5", + "sha2 0.10.7", + "thiserror", + "tracing", +] + +[[package]] +name = "ethers-signers" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "async-trait", + "coins-bip32", + "coins-bip39", + "elliptic-curve", + "eth-keystore", + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "hex", "rand 0.8.5", "sha2 0.10.7", @@ -1925,7 +2140,38 @@ checksum = "e5500989f6abfc751a660a3090c7d66790300ff59b7744cedb89e23dd1179d83" dependencies = [ "cfg-if", "dunce", - "ethers-core", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "getrandom 0.2.10", + "glob", + "hex", + "home", + "md-5", + "num_cpus", + "once_cell", + "path-slash", + "rayon", + "regex", + "semver", + "serde", + "serde_json", + "solang-parser", + "svm-rs", + "thiserror", + "tiny-keccak", + "tokio", + "tracing", + "walkdir", + "yansi", +] + +[[package]] +name = "ethers-solc" +version = "2.0.0" +source = "git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2#e8b8c4b6079a018002c28db3153acee73ba61ac2" +dependencies = [ + "cfg-if", + "dunce", + "ethers-core 2.0.0 (git+https://github.com/leejw51crypto/ethers-rs?rev=e8b8c4b6079a018002c28db3153acee73ba61ac2)", "getrandom 0.2.10", "glob", "hex", @@ -1965,15 +2211,15 @@ dependencies = [ "defi-wallet-connect", "defi-wallet-core-common", "defi-wallet-core-cpp", - "ethers", - "ethers-addressbook", - "ethers-contract", - "ethers-core", - "ethers-etherscan", - "ethers-middleware", - "ethers-providers", - "ethers-signers", - "ethers-solc", + "ethers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-addressbook 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-contract 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-core 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-etherscan 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-middleware 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-providers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-signers 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethers-solc 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "eyre", "hex", "hex-literal", diff --git a/wallet-connect/examples/web3_v2.rs b/wallet-connect/examples/web3_v2.rs index f8e952f8..6fdc1463 100644 --- a/wallet-connect/examples/web3_v2.rs +++ b/wallet-connect/examples/web3_v2.rs @@ -1,9 +1,11 @@ +use ethers::abi::Address; +use ethers::core::types::transaction::eip2718::TypedTransaction; use eyre::Result; use image::Luma; use qrcode::QrCode; - -use ethers::abi::Address; -use ethers::core::types::transaction::eip2718::TypedTransaction; +use std::fs; +use std::io; +use std::io::Write; //use ethers::ethers_providers::Middleware; use defi_wallet_connect::v2::WCMiddleware; use ethers::prelude::*; @@ -14,6 +16,12 @@ use defi_wallet_connect::v2::{Client, ClientOptions, Metadata, RequiredNamespace use std::error::Error; use std::io::BufRead; +const RPC_URL: &str = env!("MY_RPC_URL"); +// convert string to u64 +const CHAIN_ID: &str = env!("MY_CHAIN_ID"); +const FROM_ADDRESS: &str = env!("MY_FROM_ADDRESS"); +const TO_ADDRESS: &str = env!("MY_TO_ADDRESS"); + #[derive(Debug, Default)] pub struct WalletConnectTxCommon { pub gas_limit: String, // decimal string, "1" @@ -36,18 +44,20 @@ pub struct WalletConnectTxEip155 { async fn make_client( callback_sender: Option>, ) -> Result { + let mychain = format!("eip155:{}", CHAIN_ID); let opts = ClientOptions { relay_server: "wss://relay.walletconnect.com".parse().expect("url"), project_id: std::env::args().skip(1).next().expect("project_id"), required_namespaces: RequiredNamespaces::new( vec![ "eth_sendTransaction".to_owned(), + "eth_sendRawTransaction".to_owned(), "eth_signTransaction".to_owned(), "eth_sign".to_owned(), "personal_sign".to_owned(), "eth_signTypedData".to_owned(), ], - vec!["eip155:5".to_owned()], + vec![mychain.to_owned()], vec!["chainChanged".to_owned(), "accountsChanged".to_owned()], ), client_meta: Metadata { @@ -62,15 +72,24 @@ async fn make_client( Client::new(opts).await } -async fn load() -> eyre::Result { +async fn load() -> Result { let file_path = "session.bin"; - let contents = tokio::fs::read(file_path).await?; + let contents = fs::read(file_path)?; + let session_info: SessionInfo = bincode::deserialize(&contents)?; Ok(session_info) } -async fn save(info: &SessionInfo) -> eyre::Result<()> { + +async fn save(info: &SessionInfo) -> Result<()> { let binary_data = bincode::serialize(&info)?; - tokio::fs::write("session.bin", binary_data).await?; + + let file_path = "session.bin"; + let file = fs::File::create(file_path)?; + let mut writer = io::BufWriter::new(file); + + writer.write_all(&binary_data)?; + writer.flush()?; + Ok(()) } @@ -79,12 +98,13 @@ async fn sign_typed_tx( tx: &TypedTransaction, address: Address, ) -> Result { - let middleware = WCMiddleware::new(client); + let middleware: WCMiddleware> = WCMiddleware::new(client); let signature = middleware.sign_transaction(tx, address).await?; Ok(signature) } async fn send_typed_tx(client: Client, tx: TypedTransaction, address: Address) -> Result { + println!("send_typed_tx tx = {:?} address {:?}", tx, address); let middleware = WCMiddleware::new(client).with_sender(address); let receipt = middleware.send_transaction(tx, None).await?.tx_hash(); Ok(receipt) @@ -145,6 +165,7 @@ pub async fn send_eip155_transaction_blocking( userinfo: &WalletConnectTxEip155, address: [u8; 20], ) -> Result> { + println!("send_eip155_transaction_blocking userinfo= {:?}", userinfo); let signeraddress = Address::from_slice(&address); let mut tx = Eip1559TransactionRequest::new(); @@ -155,6 +176,8 @@ pub async fn send_eip155_transaction_blocking( if !userinfo.data.is_empty() { tx = tx.data(userinfo.data.as_slice().to_vec()); } + tx = tx.data(vec![]); + if !userinfo.common.gas_limit.is_empty() { tx = tx.gas(U256::from_dec_str(&userinfo.common.gas_limit)?); } @@ -177,6 +200,7 @@ pub async fn send_eip155_transaction_blocking( let typedtx = TypedTransaction::Eip1559(tx); // print typedtx + println!("-------------------"); println!("typedtx: {:?}", typedtx); println!("send tx: {:?}", typedtx); let tx_bytes = send_typed_tx(newclient, typedtx, signeraddress) @@ -197,9 +221,47 @@ async fn make_qrcode(uri: &str) -> Result<()> { Ok(()) } + +// Function to create, sign, and send an Ethereum transaction +async fn send_ethereum_transaction( + client: &mut Client, + from: &str, + to: &str, + amount: U256, +) -> Result { + let from_address = Address::from_str(from)?; + let to_address = Address::from_str(to)?; + + let provider = Provider::::try_from(RPC_URL)?; + let chain_id = provider.get_chainid().await?; + let nonce = provider.get_transaction_count(from_address, None).await?; + // Construct the transaction + let tx_request = Eip1559TransactionRequest::new() + .from(from_address) + .to(NameOrAddress::Address(to_address)) + .value(amount) + .gas(U256::from_dec_str("2100000")?) + .max_priority_fee_per_gas(U256::from_dec_str("17646859727182")?) + .max_fee_per_gas(U256::from_dec_str("17646852851231")?) + .nonce(nonce) + .data(vec![]) + .access_list(vec![]) + .chain_id(chain_id.as_u64()); + + let typed_tx = TypedTransaction::Eip1559(tx_request); + println!( + "typed_tx json= {}", + serde_json::to_string_pretty(&typed_tx)? + ); + // Send the transaction + let tx_hash = send_typed_tx(client.clone(), typed_tx, from_address).await?; + + Ok(tx_hash) +} + #[tokio::main] async fn main() -> Result<(), Box> { - println!("walletconnect v2.0"); + println!("walletconnect v2.0a"); let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::(); tokio::spawn(async move { while let Some(msg) = rx.recv().await { @@ -217,8 +279,9 @@ async fn main() -> Result<(), Box> { }; let test_ping = false; - let test_signing = false; - let test_tx = true; + let test_personal_signing = false; + let test_tx = false; + let test_send_tx = true; let test_event_listening = false; let uri = client.get_connection_string().await; @@ -237,7 +300,7 @@ async fn main() -> Result<(), Box> { let response = client.send_ping().await?; println!("ping response= {}", response); } - if test_signing { + if test_personal_signing { // 0xaddress let address1 = namespaces.get_ethereum_addresses()[0].address.clone(); let sig1 = client.personal_sign("Hello Crypto", &address1).await?; @@ -246,13 +309,16 @@ async fn main() -> Result<(), Box> { if test_tx { // read env MYTOADDRESS - let to = std::env::var("MYTOADDRESS").expect("MYTOADDRESS not set"); - let fromaddress = namespaces.get_ethereum_addresses()[0].address.clone(); + let fromaddress = FROM_ADDRESS.to_string(); + let to = TO_ADDRESS.to_string(); + // convert fromaddress to 20 fixed byte array + let fromaddress = Address::from_str(&fromaddress)?; + // print fromaddress println!("fromaddress= {:?}", fromaddress); let txinfo = WalletConnectTxEip155 { common: WalletConnectTxCommon { - chainid: 5, + chainid: CHAIN_ID.parse::()?, gas_limit: "21000".into(), gas_price: "1000000000".into(), nonce: "0".into(), @@ -262,12 +328,8 @@ async fn main() -> Result<(), Box> { data: vec![], value: "1000".into(), }; - let sig = sign_eip155_transaction_blocking( - &mut client, - &txinfo, - (*fromaddress.as_fixed_bytes()).into(), - ) - .await?; + let sig = + sign_eip155_transaction_blocking(&mut client, &txinfo, fromaddress.into()).await?; let sig_hex = hex::encode(sig.as_slice()); let sig_hex_length = sig_hex.len(); println!("signature length {sig_hex_length} 0x{sig_hex}"); @@ -285,5 +347,14 @@ async fn main() -> Result<(), Box> { } } + if test_send_tx { + let amount = U256::from_dec_str("1000012345678912")?; // 1 ETH for example + + let tx_hash = + send_ethereum_transaction(&mut client, FROM_ADDRESS, TO_ADDRESS, amount).await?; + + println!("Transaction sent with hash: {:?}", tx_hash); + } + Ok(()) } diff --git a/wallet-connect/src/v2/client.rs b/wallet-connect/src/v2/client.rs index 5d495d24..49aa7249 100644 --- a/wallet-connect/src/v2/client.rs +++ b/wallet-connect/src/v2/client.rs @@ -1,8 +1,7 @@ -use std::collections::HashMap; -use std::str::FromStr; - use async_trait::async_trait; +use ethers::prelude::PendingTransaction; use ethers::types::transaction::eip2718::TypedTransaction; +use ethers::types::BlockId; use ethers::{ prelude::{ Address, Bytes, JsonRpcClient, Middleware, MiddlewareError, NameOrAddress, Provider, @@ -13,17 +12,18 @@ use ethers::{ use eyre::eyre; use eyre::Context; use serde::{de::DeserializeOwned, Serialize}; +use std::collections::HashMap; +use std::str::FromStr; use std::sync::Arc; use thiserror::Error; use tokio::sync::RwLock; use url::Url; -use crate::{hex, ClientError}; - use super::core::Connector; use super::protocol::{Namespaces, RequiredNamespaces}; use super::session::SessionInfo; use super::Metadata; +use crate::{hex, ClientError}; use relay_client::Error; #[derive(Debug)] @@ -62,7 +62,7 @@ impl Default for ClientOptions { "personal_sign".to_owned(), "eth_signTypedData".to_owned(), ], - vec!["eip155:5".to_owned()], + vec!["eip155:338".to_owned()], vec!["chainChanged".to_owned(), "accountsChanged".to_owned()], ), client_meta: Metadata { @@ -317,4 +317,47 @@ impl Middleware for WCMiddleware> { )))) } } + async fn send_transaction + Send + Sync>( + &self, + tx: T, + _block: Option, + ) -> Result, Self::Error> { + let tx: TypedTransaction = tx.into(); + + let mut tx_obj = HashMap::new(); + if let Some(from) = tx.from() { + let addr = *from; + tx_obj.insert("from", format!("{addr:?}")); + } + if let Some(to) = tx.to() { + let addr = match to { + NameOrAddress::Address(addr) => *addr, + NameOrAddress::Name(n) => self.resolve_name(n).await?, + }; + tx_obj.insert("to", format!("{addr:?}")); + } + if let Some(data) = tx.data() { + tx_obj.insert("data", format!("0x{}", hex::encode(data))); + } + if let Some(gas) = tx.gas() { + tx_obj.insert("gasLimit", append_hex(pad_zero(format!("{gas:x}")))); + } + if let Some(gas_price) = tx.gas_price() { + tx_obj.insert("gasPrice", append_hex(pad_zero(format!("{gas_price:x}")))); + } + if let Some(value) = tx.value() { + tx_obj.insert("value", append_hex(pad_zero(format!("{value:x}")))); + } + if let Some(nonce) = tx.nonce() { + tx_obj.insert("nonce", append_hex(pad_zero(format!("{nonce:x}")))); + } + + let tx_hash = self + .0 + .request("eth_sendTransaction", vec![tx_obj]) + .await + .map_err(|e| WCError::ClientError(ClientError::Eyre(eyre!(e))))?; + + Ok(PendingTransaction::new(tx_hash, self.provider())) + } }