@ -4,6 +4,7 @@ import sinon from 'sinon'
import { ethers } from 'ethers'
import { ethers } from 'ethers'
import BigNumber from 'bignumber.js'
import BigNumber from 'bignumber.js'
import ObservableStore from 'obs-store'
import ObservableStore from 'obs-store'
import { ROPSTEN _NETWORK _ID , MAINNET _NETWORK _ID } from '../../../../app/scripts/controllers/network/enums'
import { createTestProviderTools } from '../../../stub/provider'
import { createTestProviderTools } from '../../../stub/provider'
import SwapsController , { utils } from '../../../../app/scripts/controllers/swaps'
import SwapsController , { utils } from '../../../../app/scripts/controllers/swaps'
@ -75,6 +76,19 @@ const MOCK_GET_BUFFERED_GAS_LIMIT = async () => ({
simulationFails : undefined ,
simulationFails : undefined ,
} )
} )
function getMockNetworkController ( ) {
return {
store : {
getState : ( ) => {
return {
network : ROPSTEN _NETWORK _ID ,
}
} ,
} ,
on : sinon . stub ( ) . withArgs ( 'networkDidChange' ) . callsArgAsync ( 1 ) ,
}
}
const EMPTY _INIT _STATE = {
const EMPTY _INIT _STATE = {
swapsState : {
swapsState : {
quotes : { } ,
quotes : { } ,
@ -104,6 +118,7 @@ describe('SwapsController', function () {
const getSwapsController = ( ) => {
const getSwapsController = ( ) => {
return new SwapsController ( {
return new SwapsController ( {
getBufferedGasLimit : MOCK _GET _BUFFERED _GAS _LIMIT ,
getBufferedGasLimit : MOCK _GET _BUFFERED _GAS _LIMIT ,
networkController : getMockNetworkController ( ) ,
provider ,
provider ,
getProviderConfig : MOCK _GET _PROVIDER _CONFIG ,
getProviderConfig : MOCK _GET _PROVIDER _CONFIG ,
tokenRatesStore : MOCK _TOKEN _RATES _STORE ,
tokenRatesStore : MOCK _TOKEN _RATES _STORE ,
@ -141,6 +156,78 @@ describe('SwapsController', function () {
MOCK _GET _PROVIDER _CONFIG ,
MOCK _GET _PROVIDER _CONFIG ,
)
)
} )
} )
it ( 'should replace ethers instance when network changes' , function ( ) {
const networkController = getMockNetworkController ( )
const swapsController = new SwapsController ( {
getBufferedGasLimit : MOCK _GET _BUFFERED _GAS _LIMIT ,
networkController ,
provider ,
getProviderConfig : MOCK _GET _PROVIDER _CONFIG ,
tokenRatesStore : MOCK _TOKEN _RATES _STORE ,
fetchTradesInfo : fetchTradesInfoStub ,
fetchSwapsFeatureLiveness : fetchSwapsFeatureLivenessStub ,
} )
const currentEthersInstance = swapsController . ethersProvider
const onNetworkDidChange = networkController . on . getCall ( 0 ) . args [ 1 ]
onNetworkDidChange ( MAINNET _NETWORK _ID )
const newEthersInstance = swapsController . ethersProvider
assert . notStrictEqual (
currentEthersInstance ,
newEthersInstance ,
'Ethers provider should be replaced' ,
)
} )
it ( 'should not replace ethers instance when network changes to loading' , function ( ) {
const networkController = getMockNetworkController ( )
const swapsController = new SwapsController ( {
getBufferedGasLimit : MOCK _GET _BUFFERED _GAS _LIMIT ,
networkController ,
provider ,
getProviderConfig : MOCK _GET _PROVIDER _CONFIG ,
tokenRatesStore : MOCK _TOKEN _RATES _STORE ,
fetchTradesInfo : fetchTradesInfoStub ,
fetchSwapsFeatureLiveness : fetchSwapsFeatureLivenessStub ,
} )
const currentEthersInstance = swapsController . ethersProvider
const onNetworkDidChange = networkController . on . getCall ( 0 ) . args [ 1 ]
onNetworkDidChange ( 'loading' )
const newEthersInstance = swapsController . ethersProvider
assert . strictEqual (
currentEthersInstance ,
newEthersInstance ,
'Ethers provider should not be replaced' ,
)
} )
it ( 'should not replace ethers instance when network changes to the same network' , function ( ) {
const networkController = getMockNetworkController ( )
const swapsController = new SwapsController ( {
getBufferedGasLimit : MOCK _GET _BUFFERED _GAS _LIMIT ,
networkController ,
provider ,
getProviderConfig : MOCK _GET _PROVIDER _CONFIG ,
tokenRatesStore : MOCK _TOKEN _RATES _STORE ,
fetchTradesInfo : fetchTradesInfoStub ,
fetchSwapsFeatureLiveness : fetchSwapsFeatureLivenessStub ,
} )
const currentEthersInstance = swapsController . ethersProvider
const onNetworkDidChange = networkController . on . getCall ( 0 ) . args [ 1 ]
onNetworkDidChange ( ROPSTEN _NETWORK _ID )
const newEthersInstance = swapsController . ethersProvider
assert . strictEqual (
currentEthersInstance ,
newEthersInstance ,
'Ethers provider should not be replaced' ,
)
} )
} )
} )
describe ( 'API' , function ( ) {
describe ( 'API' , function ( ) {