chore(sdk): Impl alloy_consensus::Transaction for TransactionSigned (#11843)

This commit is contained in:
Emilia Hane
2024-10-17 18:29:17 +02:00
committed by GitHub
parent 2131c87edb
commit 9db28d91a4
5 changed files with 71 additions and 10 deletions

View File

@@ -1351,6 +1351,68 @@ impl TransactionSigned {
}
}
impl alloy_consensus::Transaction for TransactionSigned {
fn chain_id(&self) -> Option<ChainId> {
self.deref().chain_id()
}
fn nonce(&self) -> u64 {
self.deref().nonce()
}
fn gas_limit(&self) -> u64 {
self.deref().gas_limit()
}
fn gas_price(&self) -> Option<u128> {
self.deref().gas_price()
}
fn max_fee_per_gas(&self) -> u128 {
self.deref().max_fee_per_gas()
}
fn max_priority_fee_per_gas(&self) -> Option<u128> {
self.deref().max_priority_fee_per_gas()
}
fn max_fee_per_blob_gas(&self) -> Option<u128> {
self.deref().max_fee_per_blob_gas()
}
fn priority_fee_or_price(&self) -> u128 {
self.deref().priority_fee_or_price()
}
fn to(&self) -> TxKind {
alloy_consensus::Transaction::to(self.deref())
}
fn value(&self) -> U256 {
self.deref().value()
}
fn input(&self) -> &[u8] {
self.deref().input()
}
fn ty(&self) -> u8 {
self.deref().ty()
}
fn access_list(&self) -> Option<&AccessList> {
self.deref().access_list()
}
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
alloy_consensus::Transaction::blob_versioned_hashes(self.deref())
}
fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
self.deref().authorization_list()
}
}
impl From<TransactionSignedEcRecovered> for TransactionSigned {
fn from(recovered: TransactionSignedEcRecovered) -> Self {
recovered.signed_transaction
@@ -2181,8 +2243,8 @@ mod tests {
let tx = TransactionSigned::decode_2718(&mut data.as_slice()).unwrap();
let sender = tx.recover_signer().unwrap();
assert_eq!(sender, address!("001e2b7dE757bA469a57bF6b23d982458a07eFcE"));
assert_eq!(tx.to(), Some(address!("D9e1459A7A482635700cBc20BBAF52D495Ab9C96")));
assert_eq!(tx.input().as_ref(), hex!("1b55ba3a"));
assert_eq!(tx.to(), Some(address!("D9e1459A7A482635700cBc20BBAF52D495Ab9C96")).into());
assert_eq!(tx.input(), hex!("1b55ba3a"));
let encoded = tx.encoded_2718();
assert_eq!(encoded.as_ref(), data.to_vec());
}

View File

@@ -48,9 +48,7 @@ pub trait TransactionCompat: Send + Sync + Unpin + Clone + fmt::Debug {
// baseFee`
let gas_price = base_fee
.and_then(|base_fee| {
signed_tx
.effective_tip_per_gas(Some(base_fee))
.map(|tip| tip + base_fee as u128)
signed_tx.effective_tip_per_gas(base_fee).map(|tip| tip + base_fee as u128)
})
.unwrap_or_else(|| signed_tx.max_fee_per_gas());

View File

@@ -37,8 +37,9 @@ where
let GasPrice { gas_price, max_fee_per_gas } =
Self::gas_price(&signed_tx, base_fee.map(|fee| fee as u64));
let input = signed_tx.input().to_vec().into();
let chain_id = signed_tx.chain_id();
let blob_versioned_hashes = signed_tx.blob_versioned_hashes();
let blob_versioned_hashes = signed_tx.blob_versioned_hashes().map(|hs| hs.to_vec());
let access_list = signed_tx.access_list().cloned();
let authorization_list = signed_tx.authorization_list().map(|l| l.to_vec());
@@ -60,7 +61,7 @@ where
max_priority_fee_per_gas: signed_tx.max_priority_fee_per_gas(),
signature: Some(signature),
gas: signed_tx.gas_limit(),
input: signed_tx.input().clone(),
input,
chain_id,
access_list,
transaction_type: Some(signed_tx.tx_type() as u8),

View File

@@ -101,7 +101,7 @@ where
entry.insert(
tx.nonce().to_string(),
TxpoolInspectSummary {
to: tx.to(),
to: tx.to().into(),
value: tx.value(),
gas: tx.gas_limit() as u128,
gas_price: tx.transaction.max_fee_per_gas(),

View File

@@ -1183,7 +1183,7 @@ impl PoolTransaction for EthPooledTransaction {
/// For EIP-1559 transactions: `min(max_fee_per_gas - base_fee, max_priority_fee_per_gas)`.
/// For legacy transactions: `gas_price - base_fee`.
fn effective_tip_per_gas(&self, base_fee: u64) -> Option<u128> {
self.transaction.effective_tip_per_gas(Some(base_fee))
self.transaction.effective_tip_per_gas(base_fee)
}
/// Returns the max priority fee per gas if the transaction is an EIP-1559 transaction, and
@@ -1199,7 +1199,7 @@ impl PoolTransaction for EthPooledTransaction {
}
fn input(&self) -> &[u8] {
self.transaction.input().as_ref()
self.transaction.input()
}
/// Returns a measurement of the heap usage of this type and all its internals.