From 609ef9a09c208de013b5f2c2505c9aa3378ecf10 Mon Sep 17 00:00:00 2001 From: ryanml Date: Mon, 12 Jul 2021 10:54:39 -0700 Subject: [PATCH] Converting gas params for tx metrics to decimal GWEI (#11482) --- app/scripts/controllers/transactions/index.js | 17 +++++++-- .../controllers/transactions/index.test.js | 36 ++++++++++++++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index cfc11c335..518e463ea 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -1,7 +1,7 @@ import EventEmitter from 'safe-event-emitter'; import { ObservableStore } from '@metamask/obs-store'; -import { bufferToHex, keccak, toBuffer } from 'ethereumjs-util'; import Transaction from 'ethereumjs-tx'; +import { bufferToHex, keccak, toBuffer, isHexString } from 'ethereumjs-util'; import EthQuery from 'ethjs-query'; import { ethErrors } from 'eth-rpc-errors'; import abi from 'human-standard-token-abi'; @@ -19,6 +19,7 @@ import { } from '../../lib/util'; import { TRANSACTION_NO_CONTRACT_ERROR_KEY } from '../../../../ui/helpers/constants/error-keys'; import { getSwapsTokensReceivedFromTxMeta } from '../../../../ui/pages/swaps/swaps.util'; +import { hexWEIToDecGWEI } from '../../../../ui/helpers/utils/conversions.util'; import { TRANSACTION_STATUSES, TRANSACTION_TYPES, @@ -1081,6 +1082,8 @@ export default class TransactionController extends EventEmitter { gasParams.gas_price = gasPrice; } + const gasParamsInGwei = this._getGasValuesInGWEI(gasParams); + this._trackMetaMetricsEvent({ event, category: 'Transactions', @@ -1096,7 +1099,7 @@ export default class TransactionController extends EventEmitter { : 'legacy', first_seen: time, gas_limit: gasLimit, - ...gasParams, + ...gasParamsInGwei, ...extraParams, }, }); @@ -1106,6 +1109,16 @@ export default class TransactionController extends EventEmitter { return Math.round((Date.now() - submittedTime) / 1000).toString(); } + _getGasValuesInGWEI(gasParams) { + const gasValuesInGwei = {}; + for (const param in gasParams) { + if (isHexString(gasParams[param])) { + gasValuesInGwei[param] = hexWEIToDecGWEI(gasParams[param]); + } + } + return gasValuesInGwei; + } + _failTransaction(txId, error) { this.txStateManager.setTxStatusFailed(txId, error); const txMeta = this.txStateManager.getTransaction(txId); diff --git a/app/scripts/controllers/transactions/index.test.js b/app/scripts/controllers/transactions/index.test.js index 45d7afc65..1a5d6697d 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -1214,7 +1214,7 @@ describe('Transaction Controller', function () { category: 'Transactions', sensitiveProperties: { chain_id: '0x2a', - gas_price: '0x77359400', + gas_price: '2', gas_limit: '0x7b0d', first_seen: 1624408066355, transaction_envelope_type: 'legacy', @@ -1259,7 +1259,7 @@ describe('Transaction Controller', function () { category: 'Transactions', sensitiveProperties: { chain_id: '0x2a', - gas_price: '0x77359400', + gas_price: '2', gas_limit: '0x7b0d', first_seen: 1624408066355, transaction_envelope_type: 'legacy', @@ -1306,7 +1306,7 @@ describe('Transaction Controller', function () { baz: 3.0, foo: 'bar', chain_id: '0x2a', - gas_price: '0x77359400', + gas_price: '2', gas_limit: '0x7b0d', first_seen: 1624408066355, transaction_envelope_type: 'legacy', @@ -1358,8 +1358,8 @@ describe('Transaction Controller', function () { baz: 3.0, foo: 'bar', chain_id: '0x2a', - max_fee_per_gas: '0x77359400', - max_priority_fee_per_gas: '0x77359400', + max_fee_per_gas: '2', + max_priority_fee_per_gas: '2', gas_limit: '0x7b0d', first_seen: 1624408066355, transaction_envelope_type: 'fee-market', @@ -1410,4 +1410,30 @@ describe('Transaction Controller', function () { assert.equal(result, '21'); }); }); + + describe('#_getGasValuesInGWEI', function () { + it('converts gas values in hex GWEi to dec GWEI (EIP-1559)', function () { + const params = { + max_fee_per_gas: '0x77359400', + max_priority_fee_per_gas: '0x77359400', + }; + const expectedParams = { + max_fee_per_gas: '2', + max_priority_fee_per_gas: '2', + }; + const result = txController._getGasValuesInGWEI(params); + assert.deepEqual(result, expectedParams); + }); + + it('converts gas values in hex GWEi to dec GWEI (non EIP-1559)', function () { + const params = { + gas_price: '0x37e11d600', + }; + const expectedParams = { + gas_price: '15', + }; + const result = txController._getGasValuesInGWEI(params); + assert.deepEqual(result, expectedParams); + }); + }); });