From d4fca905f5a61207ba69ba7fee96ae51f19e5352 Mon Sep 17 00:00:00 2001 From: Meet Mangukiya Date: Mon, 21 Mar 2022 20:34:02 +0530 Subject: [PATCH] feat(core/TypedTransaction): add helper for calculating max cost of tx (#1070) --- ethers-core/src/types/transaction/eip2718.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ethers-core/src/types/transaction/eip2718.rs b/ethers-core/src/types/transaction/eip2718.rs index 8fffc6ba..00a387b7 100644 --- a/ethers-core/src/types/transaction/eip2718.rs +++ b/ethers-core/src/types/transaction/eip2718.rs @@ -253,6 +253,16 @@ impl TypedTransaction { let encoded = self.rlp(); keccak256(encoded).into() } + + /// Max cost of the transaction + pub fn max_cost(&self) -> Option { + let gas_limit = self.gas(); + let gas_price = self.gas_price(); + match (gas_limit, gas_price) { + (Some(gas_limit), Some(gas_price)) => Some(gas_limit * gas_price), + _ => None, + } + } } /// Get a TypedTransaction directly from an rlp encoded byte stream