mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Converting gas params for tx metrics to decimal GWEI (#11482)
This commit is contained in:
parent
0df928efa9
commit
609ef9a09c
@ -1,7 +1,7 @@
|
|||||||
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 Transaction from 'ethereumjs-tx';
|
import Transaction from 'ethereumjs-tx';
|
||||||
|
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';
|
||||||
@ -19,6 +19,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,
|
||||||
@ -1081,6 +1082,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',
|
||||||
@ -1096,7 +1099,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,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -1106,6 +1109,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);
|
||||||
|
@ -1214,7 +1214,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',
|
||||||
@ -1259,7 +1259,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',
|
||||||
@ -1306,7 +1306,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',
|
||||||
@ -1358,8 +1358,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',
|
||||||
@ -1410,4 +1410,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…
Reference in New Issue
Block a user