Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0842f9c
chore: updating dependencies to match mobile current versions
nikoferro Jul 18, 2024
2c03773
Merge branch 'rc-v10' of github.com:MetaMask/swaps-controller into de…
nikoferro Aug 14, 2024
31d63a1
feat: basecontrollerv2 refactor
nikoferro Aug 19, 2024
680c9d2
Merge branch 'rc-v10' of github.com:MetaMask/swaps-controller into ba…
nikoferro Aug 19, 2024
647573d
fix: linter fix
nikoferro Aug 20, 2024
f9fbd54
fix: test fix
nikoferro Aug 20, 2024
eec54a6
chore: exposing types of actions and events
nikoferro Aug 20, 2024
7a89c1d
fix: removing provider from state since its not serializable
nikoferro Aug 22, 2024
9087137
fix: fix for resetting state while keeping the current chain and last…
nikoferro Aug 22, 2024
edf5bb5
chore: export controller types
nikoferro Aug 22, 2024
bcb4f89
Update src/types.ts
nikoferro Aug 23, 2024
7405e7e
Update src/types.ts
nikoferro Aug 23, 2024
19c8455
Update src/types.ts
nikoferro Aug 23, 2024
e8e32af
Update src/SwapsController.ts
nikoferro Aug 23, 2024
ae687e8
Update src/constants.ts
nikoferro Aug 23, 2024
cd88c0c
Update src/types.ts
nikoferro Aug 23, 2024
762db09
Update src/swapsUtil.ts
nikoferro Aug 23, 2024
9cbf039
Update src/SwapsController.ts
nikoferro Aug 23, 2024
70ccc69
Update src/SwapsController.ts
nikoferro Aug 23, 2024
6f1f627
Update src/SwapsController.ts
nikoferro Aug 23, 2024
be4f51f
Update src/SwapsController.ts
nikoferro Aug 23, 2024
dc80499
Update src/SwapsController.ts
nikoferro Aug 23, 2024
7d3cc24
Update src/types.ts
nikoferro Aug 23, 2024
e240fa3
Update src/SwapsController.ts
nikoferro Aug 23, 2024
3e7283a
Update src/types.ts
nikoferro Aug 23, 2024
81dd446
Update src/SwapsController.ts
nikoferro Aug 23, 2024
f184eae
chore: pr feedback changes
nikoferro Aug 23, 2024
c84386d
chore: make explicit which properties are already anonymous
nikoferro Aug 23, 2024
160ffed
chore: default value for state as an optional parameter
nikoferro Aug 23, 2024
5fd1d18
chore: remove web3 package
infiniteflower Aug 22, 2024
ae1e8fe
chore: add @ethersproject/contracts
infiniteflower Aug 22, 2024
163ba05
chore: add @ethersproject/providers
infiniteflower Aug 23, 2024
3732338
chore: remove web3 and use ethers contracts instead
infiniteflower Aug 23, 2024
c48237d
fix: broken tests
infiniteflower Aug 23, 2024
d26a9b3
Merge branch 'rc-v10' into fix/android-startup-times-remove-web3
infiniteflower Aug 26, 2024
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 @@ -69,6 +69,8 @@
"typescript": "^5.1.0"
},
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.0",
"@metamask/base-controller": "^5.0.0",
"@metamask/controller-utils": "^10.0.0",
"@metamask/eth-query": "^4.0.0",
Expand All @@ -77,8 +79,7 @@
"async-mutex": "^0.5.0",
"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
21 changes: 5 additions & 16 deletions src/SwapsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,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 @@ -383,12 +376,10 @@ describe('SwapsController', () => {
rpcUrl: 'test',
} as unknown as Provider;

expect(swapsController.__test__getInternal('#web3')).toBeUndefined();
expect(swapsController.__test__getInternal('#ethQuery')).toBeUndefined();

swapsController.setProvider(provider);

expect(swapsController.__test__getInternal('#web3')).toBeDefined();
expect(swapsController.__test__getInternal('#ethQuery')).toBeDefined();
});
});
Expand All @@ -402,15 +393,13 @@ describe('SwapsController', () => {
rpcUrl: 'test',
} as unknown as Provider;

expect(swapsController.__test__getInternal('#web3')).toBeUndefined();
expect(swapsController.__test__getInternal('#ethQuery')).toBeUndefined();

swapsController.setProvider(provider, {
chainId: '0x23',
pollCountLimit: 10,
});

expect(swapsController.__test__getInternal('#web3')).toBeDefined();
expect(swapsController.__test__getInternal('#ethQuery')).toBeDefined();
expect(swapsController.__test__getInternal('#chainId')).toBe('0x23');
expect(swapsController.__test__getInternal('#pollCountLimit')).toBe(10);
Expand Down
34 changes: 19 additions & 15 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 { StateMetadata } from '@metamask/base-controller';
import { BaseController } from '@metamask/base-controller';
import {
Expand All @@ -22,8 +24,6 @@ import {
import { Mutex } from 'async-mutex';
import { BigNumber } from 'bignumber.js';
import abiERC20 from 'human-standard-token-abi';
import type { Web3 as Web3Type } from 'web3';
import * as web3 from 'web3';

import {
AVALANCHE_CHAIN_ID,
Expand Down Expand Up @@ -70,9 +70,6 @@ import type {
TxParams,
} from './types';

// 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;

const metadata: StateMetadata<SwapsControllerState> = {
quotes: { persist: false, anonymous: false },
quoteValues: { persist: false, anonymous: false },
Expand Down Expand Up @@ -124,8 +121,6 @@ export default class SwapsController extends BaseController<

#supportedChainIds: Hex[];

#web3: Web3Type;

#chainId: Hex;

// TODO: Remove once GasFeeController exports this action type
Expand Down Expand Up @@ -228,17 +223,30 @@ export default class SwapsController extends BaseController<
contractAddress: string,
walletAddress: string,
): Promise<BigNumber> {
const contract = new this.#web3.eth.Contract(abiERC20, contractAddress);
const networkClientId = this.messagingSystem.call(
'NetworkController:findNetworkClientIdByChainId',
this.#chainId,
);
const { provider } = this.messagingSystem.call(
'NetworkController:getNetworkClientById',
networkClientId,
);
const web3provider = new Web3Provider(provider as any);

const contract = new Contract(contractAddress, abiERC20, 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.#chainId))
.call();
const result = await contract.allowance(
walletAddress,
getSwapsContractAddress(this.#chainId),
);

return new BigNumber(result.toString());
};

Expand Down Expand Up @@ -1125,8 +1133,6 @@ export default class SwapsController extends BaseController<
provider: Provider,
opts?: { chainId: Hex; pollCountLimit: number },
): void {
// @ts-expect-error TODO: align `Web3` with EIP-1193 provider
this.#web3 = new Web3(provider);
this.#ethQuery = new EthQuery(provider);

if (opts?.chainId) {
Expand Down Expand Up @@ -1202,8 +1208,6 @@ export default class SwapsController extends BaseController<
return this.#supportedChainIds;
case '#clientId':
return this.#clientId;
case '#web3':
return this.#web3;
case '#ethQuery':
return this.#ethQuery;
case '#handle':
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type {
GasFeeEstimates,
GasFeeState,
} from '@metamask/gas-fee-controller';
import type {
NetworkControllerFindNetworkClientIdByChainIdAction,
NetworkControllerGetNetworkClientByIdAction,
} from '@metamask/network-controller';
import type { Hex, JsonRpcError } from '@metamask/utils';

import type SwapsController from './SwapsController';
Expand Down Expand Up @@ -325,7 +329,9 @@ export type SwapsControllerStateChangeEvent = ControllerStateChangeEvent<
* The external actions available to the {@link SwapsController}.
* TODO: Add GasFeeControllerFetchGasFeeEstimates once GasFeeController exports this action type
*/
export type AllowedActions = never;
export type AllowedActions =
| NetworkControllerFindNetworkClientIdByChainIdAction
| NetworkControllerGetNetworkClientByIdAction;

/**
* The internal actions available to the SwapsController.
Expand Down
Loading