Transaction

Struct Transaction 

Source
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: u128

Nonce of your next transaction

§gas_price: u128

Gas price

§gas: u128

Gas 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: u128

Amount of ether you want to send

§data: Vec<u8>

If you want to interact or deploy smart contract add the bytecode here

§chain_id: u64

Chain id for the target chain. Mainnet = 1

Implementations§

Source§

impl Transaction

Source

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

Source

pub fn hash(&self) -> Vec<u8>

Trait Implementations§

Source§

impl Clone for Transaction

Source§

fn clone(&self) -> Transaction

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Transaction

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Transaction

Source§

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,
}
Source§

impl Encodable for Transaction

Source§

fn rlp_append(&self, s: &mut RlpStream)

Implement Encodable trait for simpler Rlp Encoding

Source§

fn rlp_bytes(&self) -> BytesMut

Get rlp-encoded bytes for this instance
Source§

impl PartialEq for Transaction

Source§

fn eq(&self, other: &Transaction) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Transaction

Source§

impl StructuralPartialEq for Transaction

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.