-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} |