1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00

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

This commit is contained in:
ryanml 2021-07-12 10:54:39 -07:00 committed by GitHub
parent bf4cdb0b5c
commit e88c069398
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 7 deletions

View File

@ -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);

View File

@ -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);
});
});
});