From 3429586747035d518ee6bf1118817af8d3a24dd3 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Sun, 10 Jul 2022 18:20:32 -0300 Subject: [PATCH] feat: send raw transaction --- examples/raw_transaction/.gitignore | 10 ++++ examples/raw_transaction/Cargo.toml | 13 ++++++ examples/raw_transaction/src/main.rs | 69 ++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 examples/raw_transaction/.gitignore create mode 100644 examples/raw_transaction/Cargo.toml create mode 100644 examples/raw_transaction/src/main.rs diff --git a/examples/raw_transaction/.gitignore b/examples/raw_transaction/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/examples/raw_transaction/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/examples/raw_transaction/Cargo.toml b/examples/raw_transaction/Cargo.toml new file mode 100644 index 0000000..b47c388 --- /dev/null +++ b/examples/raw_transaction/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rust-ethereum-examples" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +web3 = { git = "https://github.com/tomusdrw/rust-web3" } +tokio = { version = "1.19.2", features = ["full"] } +ethereum-tx-sign = "0.0.2" +ethereum-types = "0.4" +hex = "0.3.1" diff --git a/examples/raw_transaction/src/main.rs b/examples/raw_transaction/src/main.rs new file mode 100644 index 0000000..be4e141 --- /dev/null +++ b/examples/raw_transaction/src/main.rs @@ -0,0 +1,69 @@ +extern crate web3; +extern crate ethereum_tx_sign; +extern crate ethereum_types; +extern crate hex; + +use web3::types::Bytes; +use ethereum_tx_sign::RawTransaction; +use ethereum_types::{H160,H256,U256}; + +fn convert_u256(value: web3::types::U256) -> U256 { + let web3::types::U256(ref arr) = value; + let mut ret = [0; 4]; + ret[0] = arr[0]; + ret[1] = arr[1]; + U256(ret) +} + +fn convert_account(value: web3::types::H160) -> H160 { + H160::from(value.0) +} + +fn get_private_key() -> H256 { + // Hardcoded for this example -- you should never expose a private key! + let private_key = hex::decode( + "4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d").unwrap(); + + return H256(to_array(private_key.as_slice())); +} + +fn to_array(bytes: &[u8]) -> [u8; 32] { + let mut array = [0; 32]; + let bytes = &bytes[..array.len()]; + array.copy_from_slice(bytes); + array +} + +#[tokio::main] +async fn main() -> web3::contract::Result<()> { + let transport = web3::transports::Http::new("http://localhost:8545").unwrap(); + + let web3 = web3::Web3::new(transport); + + let accounts = web3.eth().accounts().await?; + + let balance_before = web3.eth().balance(accounts[1], None).await?; + + let nonce = web3.eth().transaction_count(accounts[0], None).await?; + + let tx = RawTransaction { + nonce: convert_u256(nonce), + to: Some(convert_account(accounts[1])), + value: U256::from(10000), + gas_price: U256::from(1000000000), + gas: U256::from(21000), + data: Vec::new() + }; + + let signed_tx = tx.sign(&get_private_key()); + + let tx_hash = web3.eth().send_raw_transaction(Bytes::from(signed_tx)).await?; + + let balance_after = web3.eth().balance(accounts[1], None).await?; + + println!("TX Hash: {:?}", tx_hash); + println!("Balance before: {}", balance_before); + println!("Balance after: {}", balance_after); + + Ok(()) +}