1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

Switch hardfork in getCommonConfiguration when EIP-1559 support is detected (#11385)

This commit is contained in:
Brad Decker 2021-06-30 09:39:00 -05:00 committed by GitHub
parent b9cf309404
commit 27c140355e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 6 deletions

View File

@ -26,6 +26,7 @@ import {
import { METAMASK_CONTROLLER_EVENTS } from '../../metamask-controller';
import { GAS_LIMITS } from '../../../../shared/constants/gas';
import {
HARDFORKS,
MAINNET,
NETWORK_TYPE_RPC,
} from '../../../../shared/constants/network';
@ -35,8 +36,6 @@ import TxGasUtil from './tx-gas-utils';
import PendingTransactionTracker from './pending-tx-tracker';
import * as txUtils from './lib/util';
const HARDFORK = 'berlin';
const hstInterface = new ethers.utils.Interface(abi);
const MAX_MEMSTORE_TX_LIST_SIZE = 100; // Number of transactions (by unique nonces) to keep in memory
@ -82,6 +81,7 @@ export default class TransactionController extends EventEmitter {
this.networkStore = opts.networkStore || new ObservableStore({});
this._getCurrentChainId = opts.getCurrentChainId;
this.getProviderConfig = opts.getProviderConfig;
this.getEIP1559Compatibility = opts.getEIP1559Compatibility;
this.preferencesStore = opts.preferencesStore || new ObservableStore({});
this.provider = opts.provider;
this.getPermittedAccounts = opts.getPermittedAccounts;
@ -171,14 +171,23 @@ export default class TransactionController extends EventEmitter {
* transaction type to use.
* @returns {Common} common configuration object
*/
getCommonConfiguration() {
async getCommonConfiguration() {
const { type, nickname: name } = this.getProviderConfig();
const supportsEIP1559 = await this.getEIP1559Compatibility();
// This logic below will have to be updated each time a hardfork happens
// that carries with it a new Transaction type. It is inconsequential for
// hardforks that do not include new types.
const hardfork = supportsEIP1559 ? HARDFORKS.LONDON : HARDFORKS.BERLIN;
// type will be one of our default network names or 'rpc'. the default
// network names are sufficient configuration, simply pass the name as the
// chain argument in the constructor.
if (type !== NETWORK_TYPE_RPC) {
return new Common({ chain: type, hardfork: HARDFORK });
return new Common({
chain: type,
hardfork,
});
}
// For 'rpc' we need to use the same basic configuration as mainnet,
@ -203,7 +212,7 @@ export default class TransactionController extends EventEmitter {
networkId: networkId === 'loading' ? 0 : parseInt(networkId, 10),
};
return Common.forCustomChain(MAINNET, customChainParams, HARDFORK);
return Common.forCustomChain(MAINNET, customChainParams, hardfork);
}
/**
@ -637,7 +646,7 @@ export default class TransactionController extends EventEmitter {
};
// sign tx
const fromAddress = txParams.from;
const common = this.getCommonConfiguration();
const common = await this.getCommonConfiguration();
const unsignedEthTx = TransactionFactory.fromTxData(txParams, { common });
const signedEthTx = await this.signEthTx(unsignedEthTx, fromAddress);

View File

@ -50,6 +50,7 @@ describe('Transaction Controller', function () {
return '0xee6b2800';
},
networkStore: new ObservableStore(currentNetworkId),
getEIP1559Compatibility: () => Promise.resolve(true),
txHistoryLimit: 10,
blockTracker: blockTrackerStub,
signTransaction: (ethTx) =>

View File

@ -336,6 +336,9 @@ export default class MetamaskController extends EventEmitter {
getProviderConfig: this.networkController.getProviderConfig.bind(
this.networkController,
),
getEIP1559Compatibility: this.networkController.getEIP1559Compatibility.bind(
this.networkController,
),
networkStore: this.networkController.networkStore,
getCurrentChainId: this.networkController.getCurrentChainId.bind(
this.networkController,

View File

@ -120,3 +120,24 @@ export const NATIVE_CURRENCY_TOKEN_IMAGE_MAP = {
};
export const INFURA_BLOCKED_KEY = 'countryBlocked';
/**
* Hardforks are points in the chain where logic is changed significantly
* enough where there is a fork and the new fork becomes the active chain.
* These constants are presented in chronological order starting with BERLIN
* because when we first needed to track the hardfork we had launched support
* for EIP-2718 (where transactions can have types and different shapes) and
* EIP-2930 (optional access lists), which were included in BERLIN.
*
* BERLIN - forked at block number 12,244,000, included typed transactions and
* optional access lists
* LONDON - future, upcoming fork that introduces the baseFeePerGas, an amount
* of the ETH transaction fees that will be burned instead of given to the
* miner. This change necessitated the third type of transaction envelope to
* specify maxFeePerGas and maxPriorityFeePerGas moving the fee bidding system
* to a second price auction model.
*/
export const HARDFORKS = {
BERLIN: 'berlin',
LONDON: 'london',
};