1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

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.
This commit is contained in:
Mark Stacey 2020-10-27 13:22:21 -02:30 committed by GitHub
parent 5e5d9e6c0a
commit 3bbc1d1fa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import {
fetchTradesInfo as defaultFetchTradesInfo, fetchTradesInfo as defaultFetchTradesInfo,
fetchSwapsFeatureLiveness as defaultFetchSwapsFeatureLiveness, fetchSwapsFeatureLiveness as defaultFetchSwapsFeatureLiveness,
} from '../../../ui/app/pages/swaps/swaps.util' } from '../../../ui/app/pages/swaps/swaps.util'
import { MAINNET_CHAIN_ID } from './network/enums'
const METASWAP_ADDRESS = '0x881d40237659c251811cec9c364ef91dc08d300c' const METASWAP_ADDRESS = '0x881d40237659c251811cec9c364ef91dc08d300c'
@ -91,7 +92,8 @@ export default class SwapsController {
this.indexOfNewestCallInFlight = 0 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() this._setupSwapsLivenessFetching()
} }

View File

@ -31,6 +31,9 @@ export function createTestProviderTools (opts = {}) {
// handle block tracker methods // handle block tracker methods
engine.push(providerAsMiddleware(GanacheCore.provider({ engine.push(providerAsMiddleware(GanacheCore.provider({
mnemonic: getTestSeed(), mnemonic: getTestSeed(),
network_id: opts.networkId,
_chainId: opts.chainId,
_chainIdRpc: opts.chainId,
}))) })))
// wrap in standard provider interface // wrap in standard provider interface
const provider = providerFromEngine(engine) const provider = providerFromEngine(engine)

View File

@ -119,7 +119,7 @@ describe('SwapsController', function () {
// by default, all accounts are external accounts (not contracts) // by default, all accounts are external accounts (not contracts)
eth_getCode: '0x', eth_getCode: '0x',
} }
provider = createTestProviderTools({ scaffold: providerResultStub }) provider = createTestProviderTools({ scaffold: providerResultStub, networkId: 1, chainId: 1 })
.provider .provider
}) })