Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"typescript": "^5.1.0"
},
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.0",
"@metamask/base-controller": "^4.1.1",
"@metamask/controller-utils": "^8.0.2",
"@metamask/eth-query": "^4.0.0",
Expand All @@ -79,8 +81,7 @@
"async-mutex": "^0.4.1",
"bignumber.js": "^9.0.1",
"bn.js": "^5.2.1",
"human-standard-token-abi": "^2.0.0",
"web3": "^4.2.2"
"human-standard-token-abi": "^2.0.0"
},
"lavamoat": {
"allowScripts": {
Expand Down
26 changes: 9 additions & 17 deletions src/SwapsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,12 @@ jest.mock('@metamask/eth-query', () =>
}),
);

// Mock implementation of web3
jest.mock('web3', () => {
jest.mock('@ethersproject/contracts', () => {
return {
Web3: jest.fn(() => ({
eth: {
Contract: jest.fn(() => ({
methods: {
allowance: jest.fn(() => ({
call: jest.fn().mockResolvedValue('1000000000000000000'), // Mocked allowance value
})),
},
})),
},
Contract: jest.fn(() => ({
allowance: jest.fn(() => ({
call: jest.fn().mockResolvedValue('1000000000000000000'), // Mocked allowance value
})),
})),
};
});
Expand Down Expand Up @@ -317,17 +310,16 @@ describe('SwapsController', () => {

describe('provider', () => {
it('should set provider', () => {
// Shape of provider object from Mobile@7.29.0 and network-controller@^19.0.0
const provider = {
name: 'test',
type: 'test',
chainId: '0x1',
rpcUrl: 'test',
__UNINITIALIZED__: undefined,
sendAsync: jest.fn()
};
expect(swapsController.defaultConfig.provider).toBeUndefined();
swapsController.configure({
provider,
});
expect(swapsController.defaultConfig.provider.name).toBe(provider.name);
expect(swapsController.defaultConfig.provider.sendAsync).not.toBeUndefined();
});
});

Expand Down
20 changes: 9 additions & 11 deletions src/SwapsController.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Contract } from '@ethersproject/contracts';
import { Web3Provider } from '@ethersproject/providers';
import type { BaseConfig, BaseState } from '@metamask/base-controller';
import { BaseControllerV1 } from '@metamask/base-controller';
import {
Expand All @@ -21,8 +23,6 @@ import type { Hex } from '@metamask/utils';
import { Mutex } from 'async-mutex';
import { BigNumber } from 'bignumber.js';
import abiERC20 from 'human-standard-token-abi';
import * as web3 from 'web3';
import type { Web3 as Web3Type } from 'web3';

import type {
APIAggregatorMetadata,
Expand Down Expand Up @@ -59,9 +59,6 @@ import {
shouldEnableDirectWrapping,
} from './swapsUtil';

// Hack to fix the issue with the web3 import that works different in app vs tests
const Web3 = web3.Web3 === undefined ? web3.default : web3.Web3;

// Functions to determine type of the return value from GasFeeController

/**
Expand Down Expand Up @@ -225,7 +222,7 @@ export default class SwapsController extends BaseControllerV1<
> {
private handle?: NodeJS.Timeout;

private web3: Web3Type;
private web3Provider: Web3Provider;

private ethQuery: any;

Expand Down Expand Up @@ -549,17 +546,18 @@ export default class SwapsController extends BaseControllerV1<
contractAddress: string,
walletAddress: string,
): Promise<BigNumber> {
const contract = new this.web3.eth.Contract(abiERC20, contractAddress);
const contract = new Contract(contractAddress, abiERC20, this.web3Provider);
const allowanceTimeout = new Promise<BigNumber>((_, reject) => {
setTimeout(() => {
reject(new Error(SwapsError.SWAPS_ALLOWANCE_TIMEOUT));
}, 10000);
});

const allowancePromise = async () => {
const result: bigint = await contract.methods
.allowance(walletAddress, getSwapsContractAddress(this.config.chainId))
.call();
const result = await contract.allowance(
walletAddress,
getSwapsContractAddress(this.config.chainId),
);
return new BigNumber(result.toString());
};

Expand Down Expand Up @@ -906,7 +904,7 @@ export default class SwapsController extends BaseControllerV1<
set provider(provider: any) {
if (provider) {
this.ethQuery = new EthQuery(provider);
this.web3 = new Web3(provider);
this.web3Provider = new Web3Provider(provider);
}
}

Expand Down
Loading