1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 12:29:06 +01:00

Adding estimate fields to updateTransaction, createCancelTransaction and createSpeedUpTransaction (#12398)

This commit is contained in:
ryanml 2021-10-22 13:42:20 -07:00 committed by GitHub
parent 79b08fb803
commit a4ddeed799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 15 deletions

View File

@ -649,6 +649,14 @@ export default class TransactionController extends EventEmitter {
newGasParams.gas = customGasSettings?.gas ?? GAS_LIMITS.SIMPLE; newGasParams.gas = customGasSettings?.gas ?? GAS_LIMITS.SIMPLE;
} }
if (customGasSettings.estimateSuggested) {
newGasParams.estimateSuggested = customGasSettings.estimateSuggested;
}
if (customGasSettings.estimateUsed) {
newGasParams.estimateUsed = customGasSettings.estimateUsed;
}
if (isEIP1559Transaction(originalTxMeta)) { if (isEIP1559Transaction(originalTxMeta)) {
previousGasParams.maxFeePerGas = txParams.maxFeePerGas; previousGasParams.maxFeePerGas = txParams.maxFeePerGas;
previousGasParams.maxPriorityFeePerGas = txParams.maxPriorityFeePerGas; previousGasParams.maxPriorityFeePerGas = txParams.maxPriorityFeePerGas;
@ -1393,7 +1401,14 @@ export default class TransactionController extends EventEmitter {
status, status,
chainId, chainId,
origin: referrer, origin: referrer,
txParams: { gasPrice, gas: gasLimit, maxFeePerGas, maxPriorityFeePerGas }, txParams: {
gasPrice,
gas: gasLimit,
maxFeePerGas,
maxPriorityFeePerGas,
estimateSuggested,
estimateUsed,
},
metamaskNetworkId: network, metamaskNetworkId: network,
} = txMeta; } = txMeta;
const source = referrer === 'metamask' ? 'user' : 'dapp'; const source = referrer === 'metamask' ? 'user' : 'dapp';
@ -1407,6 +1422,14 @@ export default class TransactionController extends EventEmitter {
gasParams.gas_price = gasPrice; gasParams.gas_price = gasPrice;
} }
if (estimateSuggested) {
gasParams.estimate_suggested = estimateSuggested;
}
if (estimateUsed) {
gasParams.estimate_used = estimateUsed;
}
const gasParamsInGwei = this._getGasValuesInGWEI(gasParams); const gasParamsInGwei = this._getGasValuesInGWEI(gasParams);
this._trackMetaMetricsEvent({ this._trackMetaMetricsEvent({
@ -1441,6 +1464,8 @@ export default class TransactionController extends EventEmitter {
for (const param in gasParams) { for (const param in gasParams) {
if (isHexString(gasParams[param])) { if (isHexString(gasParams[param])) {
gasValuesInGwei[param] = hexWEIToDecGWEI(gasParams[param]); gasValuesInGwei[param] = hexWEIToDecGWEI(gasParams[param]);
} else {
gasValuesInGwei[param] = gasParams[param];
} }
} }
return gasValuesInGwei; return gasValuesInGwei;

View File

@ -999,6 +999,8 @@ describe('Transaction Controller', function () {
to: '0xB09d8505E1F4EF1CeA089D47094f5DD3464083d4', to: '0xB09d8505E1F4EF1CeA089D47094f5DD3464083d4',
gas: '0x5209', gas: '0x5209',
gasPrice: '0xa', gasPrice: '0xa',
estimateSuggested: 'medium',
estimateUsed: 'high',
}; };
txController.txStateManager._addTransactionsToState([ txController.txStateManager._addTransactionsToState([
{ {
@ -1698,6 +1700,8 @@ describe('Transaction Controller', function () {
maxPriorityFeePerGas: '0x77359400', maxPriorityFeePerGas: '0x77359400',
gas: '0x7b0d', gas: '0x7b0d',
nonce: '0x4b', nonce: '0x4b',
estimateSuggested: 'medium',
estimateUsed: 'high',
}, },
type: TRANSACTION_TYPES.SIMPLE_SEND, type: TRANSACTION_TYPES.SIMPLE_SEND,
origin: 'other', origin: 'other',
@ -1724,6 +1728,8 @@ describe('Transaction Controller', function () {
first_seen: 1624408066355, first_seen: 1624408066355,
transaction_envelope_type: 'fee-market', transaction_envelope_type: 'fee-market',
status: 'unapproved', status: 'unapproved',
estimate_suggested: 'medium',
estimate_used: 'high',
}, },
}; };
@ -1791,5 +1797,22 @@ describe('Transaction Controller', function () {
const result = txController._getGasValuesInGWEI(params); const result = txController._getGasValuesInGWEI(params);
assert.deepEqual(result, expectedParams); assert.deepEqual(result, expectedParams);
}); });
it('converts gas values in hex GWEi to dec GWEI, retains estimate fields', function () {
const params = {
max_fee_per_gas: '0x77359400',
max_priority_fee_per_gas: '0x77359400',
estimate_suggested: 'medium',
estimate_used: 'high',
};
const expectedParams = {
max_fee_per_gas: '2',
max_priority_fee_per_gas: '2',
estimate_suggested: 'medium',
estimate_used: 'high',
};
const result = txController._getGasValuesInGWEI(params);
assert.deepEqual(result, expectedParams);
});
}); });
}); });

View File

@ -19,6 +19,8 @@ const normalizers = {
maxFeePerGas: addHexPrefix, maxFeePerGas: addHexPrefix,
maxPriorityFeePerGas: addHexPrefix, maxPriorityFeePerGas: addHexPrefix,
type: addHexPrefix, type: addHexPrefix,
estimateSuggested: (estimate) => estimate,
estimateUsed: (estimate) => estimate,
}; };
export function normalizeAndValidateTxParams(txParams, lowerCase = true) { export function normalizeAndValidateTxParams(txParams, lowerCase = true) {

View File

@ -323,6 +323,8 @@ describe('txUtils', function () {
gasPrice: '1', gasPrice: '1',
maxFeePerGas: '1', maxFeePerGas: '1',
maxPriorityFeePerGas: '1', maxPriorityFeePerGas: '1',
estimateSuggested: 'medium',
estimateUsed: 'high',
type: '1', type: '1',
}; };
@ -377,6 +379,17 @@ describe('txUtils', function () {
'0x1', '0x1',
'type should be hex-prefixed', 'type should be hex-prefixed',
); );
assert.equal(
normalizedTxParams.estimateSuggested,
'medium',
'estimateSuggested should be the string originally provided',
);
assert.equal(
normalizedTxParams.estimateUsed,
'high',
'estimateSuggested should be the string originally provided',
);
}); });
}); });

View File

@ -133,20 +133,21 @@ export default function EditGasPopover({
closePopover(); closePopover();
} }
const newGasSettings = supportsEIP1559 const newGasSettings = {
? { gas: decimalToHex(gasLimit),
gas: decimalToHex(gasLimit), gasLimit: decimalToHex(gasLimit),
gasLimit: decimalToHex(gasLimit), estimateSuggested: defaultEstimateToUse,
maxFeePerGas: decGWEIToHexWEI(maxFeePerGas ?? gasPrice), estimateUsed: estimateToUse,
maxPriorityFeePerGas: decGWEIToHexWEI( };
maxPriorityFeePerGas ?? maxFeePerGas ?? gasPrice,
), if (supportsEIP1559) {
} newGasSettings.maxFeePerGas = decGWEIToHexWEI(maxFeePerGas ?? gasPrice);
: { newGasSettings.maxPriorityFeePerGas = decGWEIToHexWEI(
gas: decimalToHex(gasLimit), maxPriorityFeePerGas ?? maxFeePerGas ?? gasPrice,
gasLimit: decimalToHex(gasLimit), );
gasPrice: decGWEIToHexWEI(gasPrice), } else {
}; newGasSettings.gasPrice = decGWEIToHexWEI(gasPrice);
}
const cleanTransactionParams = { ...updatedTransaction.txParams }; const cleanTransactionParams = { ...updatedTransaction.txParams };
@ -205,6 +206,7 @@ export default function EditGasPopover({
supportsEIP1559, supportsEIP1559,
estimateToUse, estimateToUse,
estimatedBaseFee, estimatedBaseFee,
defaultEstimateToUse,
]); ]);
let title = t('editGasTitle'); let title = t('editGasTitle');