Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/use etherjs specific imports #15461

Merged
merged 6 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/scripts/controllers/detect-tokens.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Web3Provider } from '@ethersproject/providers';
import { warn } from 'loglevel';
import { MINUTE } from '../../../shared/constants/time';
import { CHAIN_IDS } from '../../../shared/constants/network';
Expand Down Expand Up @@ -114,6 +115,7 @@ export default class DetectTokensController {
: tokenList;

const tokensToDetect = [];
this.ethersProvider = new Web3Provider(this._network._provider);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I don't know if this file needs a provider any more?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you're right! Will remove

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here

for (const tokenAddress in tokenListUsed) {
if (
!this.tokenAddresses.find((address) =>
Expand Down Expand Up @@ -231,6 +233,7 @@ export default class DetectTokensController {
return;
}
this._network = network;
this.ethersProvider = new Web3Provider(network._provider);
this._network.store.subscribe(() => {
if (this.chainId !== this.getChainIdFromNetworkStore(network)) {
const chainId = this.getChainIdFromNetworkStore(network);
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/controllers/ens/ens.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers';
import { Web3Provider } from '@ethersproject/providers';
import ensNetworkMap from 'ethereum-ens-network-map';
import { NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP } from '../../../../shared/constants/network';

Expand All @@ -10,7 +10,7 @@ export default class Ens {
constructor({ network, provider } = {}) {
const networkName = NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP[network];
const ensAddress = ensNetworkMap[network];
const ethProvider = new ethers.providers.Web3Provider(provider, {
const ethProvider = new Web3Provider(provider, {
chainId: parseInt(network, 10),
name: networkName,
ensAddress,
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObservableStore } from '@metamask/obs-store';
import { normalize as normalizeAddress } from 'eth-sig-util';
import { ethers } from 'ethers';
import { Web3Provider } from '@ethersproject/providers';
import { IPFS_DEFAULT_GATEWAY_URL } from '../../../shared/constants/network';
import { isPrefixedFormattedHexString } from '../../../shared/modules/network.utils';
import { LedgerTransportTypes } from '../../../shared/constants/hardware-wallets';
Expand Down Expand Up @@ -73,7 +73,7 @@ export default class PreferencesController {
};

this.network = opts.network;
this.ethersProvider = new ethers.providers.Web3Provider(opts.provider);
this.ethersProvider = new Web3Provider(opts.provider);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar comment as above, what is this provider used for?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here

this.store = new ObservableStore(initState);
this.store.setMaxListeners(12);
this.openPopup = opts.openPopup;
Expand Down
13 changes: 5 additions & 8 deletions app/scripts/controllers/swaps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from 'ethers';
import { Web3Provider } from '@ethersproject/providers';
import { Contract } from '@ethersproject/contracts';
import log from 'loglevel';
import BigNumber from 'bignumber.js';
import { ObservableStore } from '@metamask/obs-store';
Expand Down Expand Up @@ -134,12 +135,12 @@ export default class SwapsController {

this.indexOfNewestCallInFlight = 0;

this.ethersProvider = new ethers.providers.Web3Provider(provider);
this.ethersProvider = new Web3Provider(provider);
this._currentNetwork = networkController.store.getState().network;
networkController.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, (network) => {
if (network !== 'loading' && network !== this._currentNetwork) {
this._currentNetwork = network;
this.ethersProvider = new ethers.providers.Web3Provider(provider);
this.ethersProvider = new Web3Provider(provider);
}
});
}
Expand Down Expand Up @@ -895,11 +896,7 @@ export default class SwapsController {
}

async _getERC20Allowance(contractAddress, walletAddress, chainId) {
const contract = new ethers.Contract(
contractAddress,
abi,
this.ethersProvider,
);
const contract = new Contract(contractAddress, abi, this.ethersProvider);
return await contract.allowance(
walletAddress,
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[chainId],
Expand Down
20 changes: 10 additions & 10 deletions app/scripts/controllers/swaps.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { strict as assert } from 'assert';
import sinon from 'sinon';

import { ethers } from 'ethers';
import { BigNumber } from '@ethersproject/bignumber';
import { mapValues } from 'lodash';
import BigNumber from 'bignumber.js';
import BigNumberjs from 'bignumber.js';
import { CHAIN_IDS, NETWORK_IDS } from '../../../shared/constants/network';
import { ETH_SWAPS_TOKEN_OBJECT } from '../../../shared/constants/swaps';
import { createTestProviderTools } from '../../../test/stub/provider';
Expand Down Expand Up @@ -373,7 +373,7 @@ describe('SwapsController', function () {
assert.strictEqual(gasEstimate, bufferedGasLimit);
assert.strictEqual(
gasEstimateWithRefund,
`0x${new BigNumber(maxGas, 10)
`0x${new BigNumberjs(maxGas, 10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use the ethers version of BigNumber here too? And get rid of bignumber.js here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will cause a test to fail because ethers BigNumber adds 0 when we covert to hex value.
image

.minus(estimatedRefund, 10)
.toString(16)}`,
);
Expand Down Expand Up @@ -667,7 +667,7 @@ describe('SwapsController', function () {
// Make it so approval is not required
sandbox
.stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(1));
.resolves(BigNumber.from(1));

const [newQuotes] = await swapsController.fetchAndSetQuotes(
MOCK_FETCH_PARAMS,
Expand Down Expand Up @@ -711,7 +711,7 @@ describe('SwapsController', function () {
// Make it so approval is not required
const allowanceStub = sandbox
.stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(1));
.resolves(BigNumber.from(1));

await swapsController.fetchAndSetQuotes(
MOCK_FETCH_PARAMS,
Expand All @@ -734,7 +734,7 @@ describe('SwapsController', function () {
// Ensure approval is required
sandbox
.stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(0));
.resolves(BigNumber.from(0));

const timedoutGasReturnResult = { gasLimit: 1000000 };
const timedoutGasReturnStub = sandbox
Expand All @@ -759,7 +759,7 @@ describe('SwapsController', function () {
// Make it so approval is not required
sandbox
.stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(1));
.resolves(BigNumber.from(1));

const [newQuotes, topAggId] = await swapsController.fetchAndSetQuotes(
MOCK_FETCH_PARAMS,
Expand All @@ -777,7 +777,7 @@ describe('SwapsController', function () {
const bestQuote = {
...getMockQuotes()[TEST_AGG_ID_1],
aggregator: bestAggId,
destinationAmount: ethers.BigNumber.from(
destinationAmount: BigNumber.from(
getMockQuotes()[TEST_AGG_ID_1].destinationAmount,
)
.add((100e18).toString())
Expand All @@ -789,7 +789,7 @@ describe('SwapsController', function () {
// Make it so approval is not required
sandbox
.stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(1));
.resolves(BigNumber.from(1));

const [newQuotes, topAggId] = await swapsController.fetchAndSetQuotes(
MOCK_FETCH_PARAMS,
Expand All @@ -806,7 +806,7 @@ describe('SwapsController', function () {
// Make it so approval is not required
sandbox
.stub(swapsController, '_getERC20Allowance')
.resolves(ethers.BigNumber.from(1));
.resolves(BigNumber.from(1));

swapsController.getTokenRatesState = () => ({
contractExchangeRates: {},
Expand Down
10 changes: 5 additions & 5 deletions app/scripts/lib/account-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import EthQuery from 'eth-query';
import { ObservableStore } from '@metamask/obs-store';
import log from 'loglevel';
import pify from 'pify';
import { ethers } from 'ethers';
import { Web3Provider } from '@ethersproject/providers';
import { Contract } from '@ethersproject/contracts';
import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi';
import {
CHAIN_IDS,
Expand Down Expand Up @@ -82,8 +83,6 @@ export default class AccountTracker {
this.preferencesController = opts.preferencesController;
this.onboardingController = opts.onboardingController;

this.ethersProvider = new ethers.providers.Web3Provider(this._provider);

this.onboardingController.store.subscribe(
previousValueComparator(async (prevState, currState) => {
const { completedOnboarding: prevCompletedOnboarding } = prevState;
Expand All @@ -109,6 +108,7 @@ export default class AccountTracker {
}
}, this.onboardingController.store.getState()),
);
this.ethersProvider = new Web3Provider(this._provider);
}

start() {
Expand Down Expand Up @@ -391,9 +391,9 @@ export default class AccountTracker {
newAccounts[address] = { address, balance: null };
}
});
this.ethersProvider = new ethers.providers.Web3Provider(this._provider);
this.ethersProvider = new Web3Provider(this._provider);

const ethContract = await new ethers.Contract(
const ethContract = await new Contract(
deployedContractAddress,
SINGLE_CALL_BALANCES_ABI,
this.ethersProvider,
Expand Down
Loading