From e88c06939826126ffce29bb8cbd52a81165b3dd5 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 adebd4204..56ccfa286 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -1,6 +1,6 @@ import EventEmitter from 'safe-event-emitter'; import { ObservableStore } from '@metamask/obs-store'; -import { bufferToHex, keccak, toBuffer } from 'ethereumjs-util'; +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'; @@ -20,6 +20,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, @@ -1232,6 +1233,8 @@ export default class TransactionController extends EventEmitter { gasParams.gas_price = gasPrice; } + const gasParamsInGwei = this._getGasValuesInGWEI(gasParams); + this._trackMetaMetricsEvent({ event, category: 'Transactions', @@ -1247,7 +1250,7 @@ export default class TransactionController extends EventEmitter { : 'legacy', first_seen: time, gas_limit: gasLimit, - ...gasParams, + ...gasParamsInGwei, ...extraParams, }, }); @@ -1257,6 +1260,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 2f4c96f19..b04132850 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -1219,7 +1219,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', @@ -1264,7 +1264,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', @@ -1311,7 +1311,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', @@ -1363,8 +1363,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', @@ -1415,4 +1415,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); + }); + }); });