use primitive_types::U256;
use web3::types::U64;
#[derive(Debug, Default, Eq, PartialEq)]
pub struct ResolvedTransactionGasPrice {
pub gas_price: Option<U256>,
pub max_fee_per_gas: Option<U256>,
pub max_priority_fee_per_gas: Option<U256>,
pub transaction_type: Option<U64>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GasPrice {
Legacy(U256),
Eip1559 {
max_fee_per_gas: U256,
max_priority_fee_per_gas: U256,
},
}
impl GasPrice {
pub fn resolve_for_transaction(&self) -> ResolvedTransactionGasPrice {
match self {
GasPrice::Legacy(value) => ResolvedTransactionGasPrice {
gas_price: Some(*value),
..Default::default()
},
GasPrice::Eip1559 {
max_fee_per_gas,
max_priority_fee_per_gas,
} => ResolvedTransactionGasPrice {
max_fee_per_gas: Some(*max_fee_per_gas),
max_priority_fee_per_gas: Some(*max_priority_fee_per_gas),
transaction_type: Some(2.into()),
..Default::default()
},
}
}
}
impl From<U256> for GasPrice {
fn from(value: U256) -> Self {
GasPrice::Legacy(value)
}
}
impl From<f64> for GasPrice {
fn from(value: f64) -> Self {
U256::from_f64_lossy(value).into()
}
}
impl From<(U256, U256)> for GasPrice {
fn from(value: (U256, U256)) -> Self {
GasPrice::Eip1559 {
max_fee_per_gas: value.0,
max_priority_fee_per_gas: value.1,
}
}
}
impl From<(f64, f64)> for GasPrice {
fn from(value: (f64, f64)) -> Self {
(U256::from_f64_lossy(value.0), U256::from_f64_lossy(value.1)).into()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_for_transaction_legacy() {
let resolved_gas_price = GasPrice::Legacy(100.into()).resolve_for_transaction();
assert_eq!(resolved_gas_price.gas_price, Some(100.into()));
assert_eq!(resolved_gas_price.transaction_type, None);
}
#[test]
fn resolve_for_transaction_eip1559() {
let resolved_gas_price = GasPrice::Eip1559 {
max_fee_per_gas: 100.into(),
max_priority_fee_per_gas: 50.into(),
}
.resolve_for_transaction();
assert_eq!(resolved_gas_price.max_fee_per_gas, Some(100.into()));
assert_eq!(resolved_gas_price.max_priority_fee_per_gas, Some(50.into()));
assert_eq!(resolved_gas_price.transaction_type, Some(2.into()));
}
#[test]
fn gas_price_convertor_u256() {
let legacy_transaction_type: GasPrice = U256::from(100).into();
assert_eq!(legacy_transaction_type, GasPrice::Legacy(100.into()));
let legacy_transaction_type: GasPrice = 100.0.into();
assert_eq!(legacy_transaction_type, GasPrice::Legacy(100.into()));
}
#[test]
fn gas_price_convertor_u256_u256() {
let eip1559_transaction_type: GasPrice = (U256::from(100), U256::from(50)).into();
assert_eq!(
eip1559_transaction_type,
GasPrice::Eip1559 {
max_fee_per_gas: 100.into(),
max_priority_fee_per_gas: 50.into()
}
);
let eip1559_transaction_type: GasPrice = (100.0, 50.0).into();
assert_eq!(
eip1559_transaction_type,
GasPrice::Eip1559 {
max_fee_per_gas: 100.into(),
max_priority_fee_per_gas: 50.into()
}
);
}
}