track dapp suggested gas fees (#11410)

feature/default_network_editable
Brad Decker 3 years ago committed by GitHub
parent 55502f212d
commit 4ba565e719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/scripts/controllers/transactions/index.js
  2. 36
      app/scripts/controllers/transactions/tx-state-manager.js
  3. 131
      app/scripts/controllers/transactions/tx-state-manager.test.js

@ -298,6 +298,7 @@ export default class TransactionController extends EventEmitter {
*/
let txMeta = this.txStateManager.generateTxMeta({
txParams: normalizedTxParams,
origin,
});
if (origin === 'metamask') {
@ -321,8 +322,6 @@ export default class TransactionController extends EventEmitter {
}
}
txMeta.origin = origin;
const { type, getCodeResponse } = await this._determineTransactionType(
txParams,
);

@ -72,12 +72,45 @@ export default class TransactionStateManager extends EventEmitter {
* overwriting default keys of the TransactionMeta
* @returns {TransactionMeta} the default txMeta object
*/
generateTxMeta(opts) {
generateTxMeta(opts = {}) {
const netId = this.getNetwork();
const chainId = this.getCurrentChainId();
if (netId === 'loading') {
throw new Error('MetaMask is having trouble connecting to the network');
}
let dappSuggestedGasFees = null;
// If we are dealing with a transaction suggested by a dapp and not
// an internally created metamask transaction, we need to keep record of
// the originally submitted gasParams.
if (
opts.txParams &&
typeof opts.origin === 'string' &&
opts.origin !== 'metamask'
) {
if (typeof opts.txParams.gasPrice !== 'undefined') {
dappSuggestedGasFees = {
gasPrice: opts.txParams.gasPrice,
};
} else if (
typeof opts.txParams.maxFeePerGas !== 'undefined' ||
typeof opts.txParams.maxPriorityFeePerGas !== 'undefined'
) {
dappSuggestedGasFees = {
maxPriorityFeePerGas: opts.txParams.maxPriorityFeePerGas,
maxFeePerGas: opts.txParams.maxFeePerGas,
};
}
if (typeof opts.txParams.gas !== 'undefined') {
dappSuggestedGasFees = {
...dappSuggestedGasFees,
gas: opts.txParams.gas,
};
}
}
return {
id: createId(),
time: new Date().getTime(),
@ -85,6 +118,7 @@ export default class TransactionStateManager extends EventEmitter {
metamaskNetworkId: netId,
chainId,
loadingDefaults: true,
dappSuggestedGasFees,
...opts,
};
}

@ -10,6 +10,7 @@ import {
RINKEBY_CHAIN_ID,
KOVAN_NETWORK_ID,
} from '../../../../shared/constants/network';
import { GAS_LIMITS } from '../../../../shared/constants/gas';
import TxStateManager from './tx-state-manager';
import { snapshotFromTxMeta } from './lib/tx-state-history-helpers';
@ -1074,6 +1075,136 @@ describe('TransactionStateManager', function () {
});
});
describe('#generateTxMeta', function () {
it('generates a txMeta object when supplied no parameters', function () {
// There are currently not safety checks for missing 'opts' but we should
// at least enforce txParams. This is done in the transaction controller
// before *calling* this method, but we should perhaps ensure that
// txParams is provided and validated in this method.
// TODO: this test should fail.
const generatedTransaction = txStateManager.generateTxMeta();
assert.ok(generatedTransaction);
});
it('generates a txMeta object with txParams specified', function () {
const txParams = {
gas: GAS_LIMITS.SIMPLE,
from: '0x0000',
to: '0x000',
value: '0x0',
gasPrice: '0x0',
};
const generatedTransaction = txStateManager.generateTxMeta({
txParams,
});
assert.ok(generatedTransaction);
assert.strictEqual(generatedTransaction.txParams, txParams);
});
it('generates a txMeta object with txParams specified using EIP-1559 fields', function () {
const txParams = {
gas: GAS_LIMITS.SIMPLE,
from: '0x0000',
to: '0x000',
value: '0x0',
maxFeePerGas: '0x0',
maxPriorityFeePerGas: '0x0',
};
const generatedTransaction = txStateManager.generateTxMeta({
txParams,
});
assert.ok(generatedTransaction);
assert.strictEqual(generatedTransaction.txParams, txParams);
});
it('records dappSuggestedGasFees when origin is provided and is not "metamask"', function () {
const eip1559GasFeeFields = {
maxFeePerGas: '0x0',
maxPriorityFeePerGas: '0x0',
gas: GAS_LIMITS.SIMPLE,
};
const legacyGasFeeFields = {
gasPrice: '0x0',
gas: GAS_LIMITS.SIMPLE,
};
const eip1559TxParams = {
from: '0x0000',
to: '0x000',
value: '0x0',
...eip1559GasFeeFields,
};
const legacyTxParams = {
from: '0x0000',
to: '0x000',
value: '0x0',
...legacyGasFeeFields,
};
const eip1559GeneratedTransaction = txStateManager.generateTxMeta({
txParams: eip1559TxParams,
origin: 'adappt.com',
});
const legacyGeneratedTransaction = txStateManager.generateTxMeta({
txParams: legacyTxParams,
origin: 'adappt.com',
});
assert.ok(
eip1559GeneratedTransaction,
'generated EIP1559 transaction should be truthy',
);
assert.deepStrictEqual(
eip1559GeneratedTransaction.dappSuggestedGasFees,
eip1559GasFeeFields,
'generated EIP1559 transaction should have appropriate dappSuggestedGasFees',
);
assert.ok(
legacyGeneratedTransaction,
'generated legacy transaction should be truthy',
);
assert.deepStrictEqual(
legacyGeneratedTransaction.dappSuggestedGasFees,
legacyGasFeeFields,
'generated legacy transaction should have appropriate dappSuggestedGasFees',
);
});
it('does not record dappSuggestedGasFees when transaction origin is "metamask"', function () {
const txParams = {
gas: GAS_LIMITS.SIMPLE,
from: '0x0000',
to: '0x000',
value: '0x0',
maxFeePerGas: '0x0',
maxPriorityFeePerGas: '0x0',
};
const generatedTransaction = txStateManager.generateTxMeta({
txParams,
origin: 'metamask',
});
assert.ok(generatedTransaction);
assert.strictEqual(generatedTransaction.dappSuggestedGasFees, null);
});
it('does not record dappSuggestedGasFees when transaction origin is not provided', function () {
const txParams = {
gas: GAS_LIMITS.SIMPLE,
from: '0x0000',
to: '0x000',
value: '0x0',
maxFeePerGas: '0x0',
maxPriorityFeePerGas: '0x0',
};
const generatedTransaction = txStateManager.generateTxMeta({
txParams,
});
assert.ok(generatedTransaction);
assert.strictEqual(generatedTransaction.dappSuggestedGasFees, null);
});
});
describe('#clearUnapprovedTxs', function () {
it('removes unapproved transactions', function () {
const txMetas = [

Loading…
Cancel
Save