Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat: add helpers for operating on the txs enum
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Aug 8, 2021
1 parent ee9e955 commit b4f1790
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions ethers-core/src/types/transaction/eip2718.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,131 @@ pub enum TypedTransaction {
}

impl TypedTransaction {
pub fn from(&self) -> Option<&Address> {
use TypedTransaction::*;
match self {
Legacy(inner) => inner.from.as_ref(),
Eip2930(inner) => inner.tx.from.as_ref(),
Eip1559(inner) => inner.from.as_ref(),
}
}

pub fn set_from(&mut self, from: Address) {
use TypedTransaction::*;
match self {
Legacy(inner) => inner.from = Some(from),
Eip2930(inner) => inner.tx.from = Some(from),
Eip1559(inner) => inner.from = Some(from),
};
}

pub fn to(&self) -> Option<&NameOrAddress> {
use TypedTransaction::*;
match self {
Legacy(inner) => inner.to.as_ref(),
Eip2930(inner) => inner.tx.to.as_ref(),
Eip1559(inner) => inner.to.as_ref(),
}
}

pub fn set_to<T: Into<NameOrAddress>>(&mut self, to: T) {
let to = to.into();
use TypedTransaction::*;
match self {
Legacy(inner) => inner.to = Some(to),
Eip2930(inner) => inner.tx.to = Some(to),
Eip1559(inner) => inner.to = Some(to),
};
}

pub fn nonce(&self) -> Option<&U256> {
use TypedTransaction::*;
match self {
Legacy(inner) => inner.nonce.as_ref(),
Eip2930(inner) => inner.tx.nonce.as_ref(),
Eip1559(inner) => inner.nonce.as_ref(),
}
}

pub fn set_nonce<T: Into<U256>>(&mut self, nonce: T) {
let nonce = nonce.into();
use TypedTransaction::*;
match self {
Legacy(inner) => inner.nonce = Some(nonce),
Eip2930(inner) => inner.tx.nonce = Some(nonce),
Eip1559(inner) => inner.nonce = Some(nonce),
};
}

pub fn value(&self) -> Option<&U256> {
use TypedTransaction::*;
match self {
Legacy(inner) => inner.value.as_ref(),
Eip2930(inner) => inner.tx.value.as_ref(),
Eip1559(inner) => inner.value.as_ref(),
}
}

pub fn set_value<T: Into<U256>>(&mut self, value: T) {
let value = value.into();
use TypedTransaction::*;
match self {
Legacy(inner) => inner.value = Some(value),
Eip2930(inner) => inner.tx.value = Some(value),
Eip1559(inner) => inner.value = Some(value),
};
}

pub fn gas(&self) -> Option<&U256> {
use TypedTransaction::*;
match self {
Legacy(inner) => inner.gas.as_ref(),
Eip2930(inner) => inner.tx.gas.as_ref(),
Eip1559(inner) => inner.gas.as_ref(),
}
}

pub fn set_gas<T: Into<U256>>(&mut self, gas: T) {
let gas = gas.into();
use TypedTransaction::*;
match self {
Legacy(inner) => inner.gas = Some(gas),
Eip2930(inner) => inner.tx.gas = Some(gas),
Eip1559(inner) => inner.gas = Some(gas),
};
}

pub fn set_gas_price<T: Into<U256>>(&mut self, gas_price: T) {
let gas_price = gas_price.into();
use TypedTransaction::*;
match self {
Legacy(inner) => inner.gas_price = Some(gas_price),
Eip2930(inner) => inner.tx.gas_price = Some(gas_price),
Eip1559(inner) => {
inner.max_fee_per_gas = Some(gas_price);
inner.max_priority_fee_per_gas = Some(gas_price);
}
};
}

pub fn data(&self) -> Option<&Bytes> {
use TypedTransaction::*;
match self {
Legacy(inner) => inner.data.as_ref(),
Eip2930(inner) => inner.tx.data.as_ref(),
Eip1559(inner) => inner.data.as_ref(),
}
}

pub fn set_data(&mut self, data: Bytes) {
use TypedTransaction::*;
match self {
Legacy(inner) => inner.data = Some(data),
Eip2930(inner) => inner.tx.data = Some(data),
Eip1559(inner) => inner.data = Some(data),
};
}

pub fn rlp_signed<T: Into<U64>>(&self, chain_id: T, signature: &Signature) -> Bytes {
use TypedTransaction::*;
let mut encoded = vec![];
Expand Down

0 comments on commit b4f1790

Please sign in to comment.