pub struct Transaction {
pub nonce: u128,
pub gas_price: u128,
pub gas: u128,
pub to: Option<[u8; 20]>,
pub value: u128,
pub data: Vec<u8>,
pub chain_id: u64,
}Fields§
§nonce: u128Nonce of your next transaction
gas_price: u128Gas price
gas: u128Gas or Gas_limit. So amount of gas you are willing to spend
to: Option<[u8; 20]>Address you want to transact with. If you want to deploy a contract, to should be None.
To convert your address from string to [u8; 20] you will have to use ethereum_types crate.
use ethereum_types::H160;
use std::str::FromStr;
let address: [u8; 20] = H160::from_str(&"/* your address */").unwrap().to_fixed_bytes();value: u128Amount of ether you want to send
data: Vec<u8>If you want to interact or deploy smart contract add the bytecode here
chain_id: u64Chain id for the target chain. Mainnet = 1
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn sign(&self, private_key: &[u8]) -> Vec<u8> ⓘ
pub fn sign(&self, private_key: &[u8]) -> Vec<u8> ⓘ
To use sign method you have to input your private key so it can sign the transaction.
It takes &[u8] as parameter. If you want to convert your private_key from string here is
how you can do that
use ethereum_types::H256;
use std::str::FromStr;
let private_key = H256::from_str("/*your private key*/").unwrap();
let tx = Transaction::default();
let signed_tx = tx.sign(private_key.as_bytes());This will convert your private key to &u8 from string
pub fn hash(&self) -> Vec<u8> ⓘ
Trait Implementations§
Source§impl Clone for Transaction
impl Clone for Transaction
Source§fn clone(&self) -> Transaction
fn clone(&self) -> Transaction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Transaction
impl Debug for Transaction
Source§impl Default for Transaction
impl Default for Transaction
Source§fn default() -> Self
fn default() -> Self
Implement Default trait so users can specify what what they need and rest will be added automatically.
use ethereum_types::H160;
use std::str::FromStr;
let address: [u8; 20] = H160::from_str(&"/* your address */").unwrap().to_fixed_bytes();
let tx = tx_from_scratch::Transaction {
nonce: 10,
to,
value: 10,
..Default::default(),
}If you don’t specify to the default is None so it will try to deploy a contract
Default is:
Transaction {
nonce: 0,
gas_price: 250,
gas: 21000,
to: None,
value: 0,
data: Vec::new(),
chain_id: 1,
}