Converting gas params for tx metrics to decimal GWEI (#11482)

feature/default_network_editable
ryanml 3 years ago committed by GitHub
parent bf4cdb0b5c
commit e88c069398
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      app/scripts/controllers/transactions/index.js
  2. 36
      app/scripts/controllers/transactions/index.test.js

@ -1,6 +1,6 @@
import EventEmitter from 'safe-event-emitter'; import EventEmitter from 'safe-event-emitter';
import { ObservableStore } from '@metamask/obs-store'; 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 EthQuery from 'ethjs-query';
import { ethErrors } from 'eth-rpc-errors'; import { ethErrors } from 'eth-rpc-errors';
import abi from 'human-standard-token-abi'; import abi from 'human-standard-token-abi';
@ -20,6 +20,7 @@ import {
} from '../../lib/util'; } from '../../lib/util';
import { TRANSACTION_NO_CONTRACT_ERROR_KEY } from '../../../../ui/helpers/constants/error-keys'; import { TRANSACTION_NO_CONTRACT_ERROR_KEY } from '../../../../ui/helpers/constants/error-keys';
import { getSwapsTokensReceivedFromTxMeta } from '../../../../ui/pages/swaps/swaps.util'; import { getSwapsTokensReceivedFromTxMeta } from '../../../../ui/pages/swaps/swaps.util';
import { hexWEIToDecGWEI } from '../../../../ui/helpers/utils/conversions.util';
import { import {
TRANSACTION_STATUSES, TRANSACTION_STATUSES,
TRANSACTION_TYPES, TRANSACTION_TYPES,
@ -1232,6 +1233,8 @@ export default class TransactionController extends EventEmitter {
gasParams.gas_price = gasPrice; gasParams.gas_price = gasPrice;
} }
const gasParamsInGwei = this._getGasValuesInGWEI(gasParams);
this._trackMetaMetricsEvent({ this._trackMetaMetricsEvent({
event, event,
category: 'Transactions', category: 'Transactions',
@ -1247,7 +1250,7 @@ export default class TransactionController extends EventEmitter {
: 'legacy', : 'legacy',
first_seen: time, first_seen: time,
gas_limit: gasLimit, gas_limit: gasLimit,
...gasParams, ...gasParamsInGwei,
...extraParams, ...extraParams,
}, },
}); });
@ -1257,6 +1260,16 @@ export default class TransactionController extends EventEmitter {
return Math.round((Date.now() - submittedTime) / 1000).toString(); 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) { _failTransaction(txId, error) {
this.txStateManager.setTxStatusFailed(txId, error); this.txStateManager.setTxStatusFailed(txId, error);
const txMeta = this.txStateManager.getTransaction(txId); const txMeta = this.txStateManager.getTransaction(txId);

@ -1219,7 +1219,7 @@ describe('Transaction Controller', function () {
category: 'Transactions', category: 'Transactions',
sensitiveProperties: { sensitiveProperties: {
chain_id: '0x2a', chain_id: '0x2a',
gas_price: '0x77359400', gas_price: '2',
gas_limit: '0x7b0d', gas_limit: '0x7b0d',
first_seen: 1624408066355, first_seen: 1624408066355,
transaction_envelope_type: 'legacy', transaction_envelope_type: 'legacy',
@ -1264,7 +1264,7 @@ describe('Transaction Controller', function () {
category: 'Transactions', category: 'Transactions',
sensitiveProperties: { sensitiveProperties: {
chain_id: '0x2a', chain_id: '0x2a',
gas_price: '0x77359400', gas_price: '2',
gas_limit: '0x7b0d', gas_limit: '0x7b0d',
first_seen: 1624408066355, first_seen: 1624408066355,
transaction_envelope_type: 'legacy', transaction_envelope_type: 'legacy',
@ -1311,7 +1311,7 @@ describe('Transaction Controller', function () {
baz: 3.0, baz: 3.0,
foo: 'bar', foo: 'bar',
chain_id: '0x2a', chain_id: '0x2a',
gas_price: '0x77359400', gas_price: '2',
gas_limit: '0x7b0d', gas_limit: '0x7b0d',
first_seen: 1624408066355, first_seen: 1624408066355,
transaction_envelope_type: 'legacy', transaction_envelope_type: 'legacy',
@ -1363,8 +1363,8 @@ describe('Transaction Controller', function () {
baz: 3.0, baz: 3.0,
foo: 'bar', foo: 'bar',
chain_id: '0x2a', chain_id: '0x2a',
max_fee_per_gas: '0x77359400', max_fee_per_gas: '2',
max_priority_fee_per_gas: '0x77359400', max_priority_fee_per_gas: '2',
gas_limit: '0x7b0d', gas_limit: '0x7b0d',
first_seen: 1624408066355, first_seen: 1624408066355,
transaction_envelope_type: 'fee-market', transaction_envelope_type: 'fee-market',
@ -1415,4 +1415,30 @@ describe('Transaction Controller', function () {
assert.equal(result, '21'); 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);
});
});
}); });

Loading…
Cancel
Save