From 3bbc1d1fa46b1431f3cc65bf71719d676494325d Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Tue, 27 Oct 2020 13:22:21 -0230 Subject: [PATCH] Fix fetching of swap quotes when initial network was testnet (#9726) If the initial network when the extension is started is something other than Mainnet, the swaps controller will never successfully retrieve swap quotes. This is because the `ethers` provider used by the swaps controller doesn't allow network changes by default - it assumes that the network remains the same as when the provider was initialized. This was fixed by hard-coding Mainnet as the initial chain ID for this `ethers` provider used by the swaps controller. Some adjustments needed to be made to the `provider` stub to allow setting `1` as the network ID and chain ID in unit tests. --- app/scripts/controllers/swaps.js | 4 +++- test/stub/provider.js | 3 +++ test/unit/app/controllers/swaps-test.js | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/scripts/controllers/swaps.js b/app/scripts/controllers/swaps.js index a464729c1..6cb06cca9 100644 --- a/app/scripts/controllers/swaps.js +++ b/app/scripts/controllers/swaps.js @@ -18,6 +18,7 @@ import { fetchTradesInfo as defaultFetchTradesInfo, fetchSwapsFeatureLiveness as defaultFetchSwapsFeatureLiveness, } from '../../../ui/app/pages/swaps/swaps.util' +import { MAINNET_CHAIN_ID } from './network/enums' const METASWAP_ADDRESS = '0x881d40237659c251811cec9c364ef91dc08d300c' @@ -91,7 +92,8 @@ export default class SwapsController { this.indexOfNewestCallInFlight = 0 - this.ethersProvider = new ethers.providers.Web3Provider(provider) + // The chain ID is hard-coded as Mainnet because swaps is only used on Mainnet + this.ethersProvider = new ethers.providers.Web3Provider(provider, parseInt(MAINNET_CHAIN_ID, 16)) this._setupSwapsLivenessFetching() } diff --git a/test/stub/provider.js b/test/stub/provider.js index 6cbcb2700..4ff63297a 100644 --- a/test/stub/provider.js +++ b/test/stub/provider.js @@ -31,6 +31,9 @@ export function createTestProviderTools (opts = {}) { // handle block tracker methods engine.push(providerAsMiddleware(GanacheCore.provider({ mnemonic: getTestSeed(), + network_id: opts.networkId, + _chainId: opts.chainId, + _chainIdRpc: opts.chainId, }))) // wrap in standard provider interface const provider = providerFromEngine(engine) diff --git a/test/unit/app/controllers/swaps-test.js b/test/unit/app/controllers/swaps-test.js index ff54a3fd2..e473bd185 100644 --- a/test/unit/app/controllers/swaps-test.js +++ b/test/unit/app/controllers/swaps-test.js @@ -119,7 +119,7 @@ describe('SwapsController', function () { // by default, all accounts are external accounts (not contracts) eth_getCode: '0x', } - provider = createTestProviderTools({ scaffold: providerResultStub }) + provider = createTestProviderTools({ scaffold: providerResultStub, networkId: 1, chainId: 1 }) .provider })