From a9297ce3d0af4728ebc39824d2ca226692aa1026 Mon Sep 17 00:00:00 2001 From: Vinicius Stevam Date: Thu, 17 Aug 2023 13:11:33 +0100 Subject: [PATCH 1/5] add baseFeePerGas to transaction state --- .../src/TransactionController.test.ts | 11 +++++++++++ .../src/TransactionController.ts | 7 +++++++ packages/transaction-controller/src/types.ts | 2 ++ 3 files changed, 20 insertions(+) diff --git a/packages/transaction-controller/src/TransactionController.test.ts b/packages/transaction-controller/src/TransactionController.test.ts index 321733851fd..46ba0a6b8d1 100644 --- a/packages/transaction-controller/src/TransactionController.test.ts +++ b/packages/transaction-controller/src/TransactionController.test.ts @@ -104,6 +104,7 @@ jest.mock('@metamask/eth-query', () => gasUsed: '0x5208', status: '0x1', transactionIndex: 1337, + blockHash: '1337', }, { transactionHash: '1111', @@ -117,6 +118,16 @@ jest.mock('@metamask/eth-query', () => ); callback(undefined, tx); }, + getBlockByHash: (_blockHash: any, callback: any) => { + const blocks: any = [ + { hash: '1337', number: '0x1', baseFeePerGas: '0x14' }, + { hash: '1338', number: '0x2' }, + ]; + const block: any = blocks.find( + (element: any) => element.hash === _blockHash, + ); + callback(undefined, block); + }, }; }), ); diff --git a/packages/transaction-controller/src/TransactionController.ts b/packages/transaction-controller/src/TransactionController.ts index fa048e2c2d8..6fd47c7da68 100644 --- a/packages/transaction-controller/src/TransactionController.ts +++ b/packages/transaction-controller/src/TransactionController.ts @@ -1155,9 +1155,16 @@ export class TransactionController extends BaseController< return [meta, false]; } + const txBlock = await query(this.ethQuery, 'getBlockByHash', [ + txReceipt.blockHash, + ]); + meta.verifiedOnBlockchain = true; meta.transaction.gasUsed = txReceipt.gasUsed; meta.txReceipt = txReceipt; + if (txBlock && txBlock.baseFeePerGas) { + meta.baseFeePerGas = txBlock.baseFeePerGas; + } // According to the Web3 docs: // TRUE if the transaction was successful, FALSE if the EVM reverted the transaction. diff --git a/packages/transaction-controller/src/types.ts b/packages/transaction-controller/src/types.ts index 743a97ca852..3945cc41940 100644 --- a/packages/transaction-controller/src/types.ts +++ b/packages/transaction-controller/src/types.ts @@ -4,6 +4,7 @@ import type { Hex } from '@metamask/utils'; * @type TransactionMeta * * TransactionMeta representation + * @property baseFeePerGas - The estimated block baseFeePerGas that will be burned. Introduced in EIP 1559. Value in hex wei * @property error - Synthesized error information for failed transactions * @property id - Generated UUID associated with this transaction * @property networkID - Network code as per EIP-155 for this transaction @@ -25,6 +26,7 @@ export type TransactionMeta = | ({ status: TransactionStatus.failed; error: Error } & TransactionMetaBase); type TransactionMetaBase = { + baseFeePerGas?: Hex; isTransfer?: boolean; transferInformation?: { symbol: string; From e8ec38d3324d000bce590bdc6a33ed6ab8d6af0c Mon Sep 17 00:00:00 2001 From: Vinicius Stevam Date: Thu, 17 Aug 2023 15:55:11 +0100 Subject: [PATCH 2/5] checks baseFeePerGas unit test --- .../transaction-controller/src/TransactionController.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/transaction-controller/src/TransactionController.test.ts b/packages/transaction-controller/src/TransactionController.test.ts index 46ba0a6b8d1..1f4c01245b5 100644 --- a/packages/transaction-controller/src/TransactionController.test.ts +++ b/packages/transaction-controller/src/TransactionController.test.ts @@ -1279,6 +1279,7 @@ describe('TransactionController', () => { const transactionMeta = controller.state.transactions[0]; expect(transactionMeta.verifiedOnBlockchain).toBe(true); expect(transactionMeta.transaction.gasUsed).toBe('0x5208'); + expect(transactionMeta.baseFeePerGas).toBe('0x14'); expect(transactionMeta.txReceipt?.transactionIndex).toBe(1337); }); }); From 8d29a8d89f7937ce4f18f782100238fb2f0e3563 Mon Sep 17 00:00:00 2001 From: Vinicius Stevam <45455812+vinistevam@users.noreply.github.com> Date: Mon, 21 Aug 2023 10:00:07 +0100 Subject: [PATCH 3/5] Update baseFeePerGas property documentation Co-authored-by: Matthew Walsh --- packages/transaction-controller/src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transaction-controller/src/types.ts b/packages/transaction-controller/src/types.ts index 3945cc41940..c026aaccfc4 100644 --- a/packages/transaction-controller/src/types.ts +++ b/packages/transaction-controller/src/types.ts @@ -4,7 +4,7 @@ import type { Hex } from '@metamask/utils'; * @type TransactionMeta * * TransactionMeta representation - * @property baseFeePerGas - The estimated block baseFeePerGas that will be burned. Introduced in EIP 1559. Value in hex wei + * @property baseFeePerGas - Base fee of the block as a hex value, introduced in EIP-1559. * @property error - Synthesized error information for failed transactions * @property id - Generated UUID associated with this transaction * @property networkID - Network code as per EIP-155 for this transaction From e05d641553d035fa9c6c2df080277d1328dbc98e Mon Sep 17 00:00:00 2001 From: Vinicius Stevam Date: Mon, 21 Aug 2023 10:12:39 +0100 Subject: [PATCH 4/5] simplifying check baseFeePerGas property --- .../src/TransactionController.ts | 4 +- packages/transaction-controller/src/types.ts | 44 +++++++++---------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/packages/transaction-controller/src/TransactionController.ts b/packages/transaction-controller/src/TransactionController.ts index 6fd47c7da68..f2f4bf387a7 100644 --- a/packages/transaction-controller/src/TransactionController.ts +++ b/packages/transaction-controller/src/TransactionController.ts @@ -1162,9 +1162,7 @@ export class TransactionController extends BaseController< meta.verifiedOnBlockchain = true; meta.transaction.gasUsed = txReceipt.gasUsed; meta.txReceipt = txReceipt; - if (txBlock && txBlock.baseFeePerGas) { - meta.baseFeePerGas = txBlock.baseFeePerGas; - } + meta.baseFeePerGas = txBlock?.baseFeePerGas; // According to the Web3 docs: // TRUE if the transaction was successful, FALSE if the EVM reverted the transaction. diff --git a/packages/transaction-controller/src/types.ts b/packages/transaction-controller/src/types.ts index c026aaccfc4..8b7eb253856 100644 --- a/packages/transaction-controller/src/types.ts +++ b/packages/transaction-controller/src/types.ts @@ -5,19 +5,19 @@ import type { Hex } from '@metamask/utils'; * * TransactionMeta representation * @property baseFeePerGas - Base fee of the block as a hex value, introduced in EIP-1559. - * @property error - Synthesized error information for failed transactions - * @property id - Generated UUID associated with this transaction - * @property networkID - Network code as per EIP-155 for this transaction - * @property origin - Origin this transaction was sent from - * @property deviceConfirmedOn - string to indicate what device the transaction was confirmed - * @property rawTransaction - Hex representation of the underlying transaction - * @property status - String status of this transaction - * @property time - Timestamp associated with this transaction - * @property toSmartContract - Whether transaction recipient is a smart contract - * @property transaction - Underlying Transaction object - * @property txReceipt - Transaction receipt - * @property transactionHash - Hash of a successful transaction - * @property blockNumber - Number of the block where the transaction has been included + * @property error - Synthesized error information for failed transactions. + * @property id - Generated UUID associated with this transaction. + * @property networkID - Network code as per EIP-155 for this transaction. + * @property origin - Origin this transaction was sent from. + * @property deviceConfirmedOn - string to indicate what device the transaction was confirmed. + * @property rawTransaction - Hex representation of the underlying transaction. + * @property status - String status of this transaction. + * @property time - Timestamp associated with this transaction. + * @property toSmartContract - Whether transaction recipient is a smart contract. + * @property transaction - Underlying Transaction object. + * @property txReceipt - Transaction receipt. + * @property transactionHash - Hash of a successful transaction. + * @property blockNumber - Number of the block where the transaction has been included. */ export type TransactionMeta = | ({ @@ -27,23 +27,23 @@ export type TransactionMeta = type TransactionMetaBase = { baseFeePerGas?: Hex; - isTransfer?: boolean; - transferInformation?: { - symbol: string; - contractAddress: string; - decimals: number; - }; + blockNumber?: string; + chainId?: Hex; + deviceConfirmedOn?: WalletDevice; id: string; + isTransfer?: boolean; networkID?: string; - chainId?: Hex; origin?: string; rawTransaction?: string; time: number; toSmartContract?: boolean; transaction: Transaction; transactionHash?: string; - blockNumber?: string; - deviceConfirmedOn?: WalletDevice; + transferInformation?: { + contractAddress: string; + decimals: number; + symbol: string; + }; verifiedOnBlockchain?: boolean; txReceipt?: TransactionReceipt; }; From 3edbc4929ecb77bcafa512b477fef45213ca9a80 Mon Sep 17 00:00:00 2001 From: Vinicius Stevam Date: Tue, 22 Aug 2023 13:48:35 +0100 Subject: [PATCH 5/5] unit test coverage --- packages/transaction-controller/jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transaction-controller/jest.config.js b/packages/transaction-controller/jest.config.js index ec30bc001e4..85e88d35881 100644 --- a/packages/transaction-controller/jest.config.js +++ b/packages/transaction-controller/jest.config.js @@ -17,7 +17,7 @@ module.exports = merge(baseConfig, { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - branches: 84.02, + branches: 83.91, functions: 92.68, lines: 95.35, statements: 95.46,