Skip to content

Commit

Permalink
Use Ethers for AssetsContractController (#845)
Browse files Browse the repository at this point in the history
* Basic Ethers implementation for AssetsContractController

* Fix some tests

* Remove web3 from deps

* Fix lint

* Simplify a bit and fix more tests

* Fix more tests

* Fix tests by matching previous output types

* Fix issues after rebase

* Fix a bunch of tests and simplify

* Fix tests

* Fix lint

* Simplify

* Fix PR comments

* Update coverage

* Remove web3 types

* Remove use of then()

* Simplify decimals()

* Update coverage
  • Loading branch information
FrederikBolding committed Nov 14, 2022
1 parent 0704312 commit 9b82201
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 392 deletions.
8 changes: 4 additions & 4 deletions packages/assets-controllers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 88.86,
functions: 96.71,
lines: 96.62,
statements: 96.69,
branches: 88.66,
functions: 96.45,
lines: 96.55,
statements: 96.63,
},
},

Expand Down
5 changes: 2 additions & 3 deletions packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"@ethersproject/abi": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.0",
"@metamask/base-controller": "workspace:~",
Expand All @@ -46,14 +47,12 @@
"immer": "^9.0.6",
"multiformats": "^9.5.2",
"single-call-balance-checker-abi": "^1.0.0",
"uuid": "^8.3.2",
"web3": "^0.20.7"
"uuid": "^8.3.2"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.0.0",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.31",
"@types/web3": "^1.0.6",
"deepmerge": "^4.2.2",
"ethjs-provider-http": "^0.1.6",
"jest": "^26.4.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ describe('AssetsContractController', () => {
ERC20_UNI_ADDRESS,
'0x202637dAAEfbd7f131f90338a4A6c69F6Cd5CE91',
);
expect(UNIBalance.toNumber()).not.toStrictEqual(0);
expect(UNINoBalance.toNumber()).toStrictEqual(0);
expect(UNIBalance.toString(16)).not.toStrictEqual('0');
expect(UNINoBalance.toString(16)).toStrictEqual('0');
messenger.clearEventSubscriptions('NetworkController:stateChange');
});

Expand Down
61 changes: 26 additions & 35 deletions packages/assets-controllers/src/AssetsContractController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BN } from 'ethereumjs-util';
import Web3 from 'web3';
import abiSingleCallBalancesContract from 'single-call-balance-checker-abi';
import { Contract } from '@ethersproject/contracts';
import { Web3Provider } from '@ethersproject/providers';
import {
BaseController,
BaseConfig,
Expand Down Expand Up @@ -63,7 +64,7 @@ export class AssetsContractController extends BaseController<
AssetsContractConfig,
BaseState
> {
private web3: any;
private _provider?: Web3Provider;

private erc721Standard?: ERC721Standard;

Expand Down Expand Up @@ -129,10 +130,10 @@ export class AssetsContractController extends BaseController<
* @property provider - Provider used to create a new underlying Web3 instance
*/
set provider(provider: any) {
this.web3 = new Web3(provider);
this.erc721Standard = new ERC721Standard(this.web3);
this.erc1155Standard = new ERC1155Standard(this.web3);
this.erc20Standard = new ERC20Standard(this.web3);
this._provider = new Web3Provider(provider);
this.erc721Standard = new ERC721Standard(this._provider);
this.erc1155Standard = new ERC1155Standard(this._provider);
this.erc20Standard = new ERC20Standard(this._provider);
}

get provider() {
Expand Down Expand Up @@ -336,7 +337,7 @@ export class AssetsContractController extends BaseController<
userAddress: string,
nftAddress: string,
nftId: string,
): Promise<number> {
): Promise<BN> {
if (this.erc1155Standard === undefined) {
throw new Error(MISSING_PROVIDER_ERROR);
}
Expand Down Expand Up @@ -395,34 +396,24 @@ export class AssetsContractController extends BaseController<
const contractAddress =
SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID[this.config.chainId];

const contract = this.web3.eth
.contract(abiSingleCallBalancesContract)
.at(contractAddress);
return new Promise<BalanceMap>((resolve, reject) => {
contract.balances(
[selectedAddress],
tokensToDetect,
(error: Error, result: BN[]) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
const nonZeroBalances: BalanceMap = {};
/* istanbul ignore else */
if (result.length > 0) {
tokensToDetect.forEach((tokenAddress, index) => {
const balance: BN = result[index];
/* istanbul ignore else */
if (String(balance) !== '0') {
nonZeroBalances[tokenAddress] = balance;
}
});
}
resolve(nonZeroBalances);
},
);
});
const contract = new Contract(
contractAddress,
abiSingleCallBalancesContract,
this._provider,
);
const result = await contract.balances([selectedAddress], tokensToDetect);
const nonZeroBalances: BalanceMap = {};
/* istanbul ignore else */
if (result.length > 0) {
tokensToDetect.forEach((tokenAddress, index) => {
const balance: BN = result[index];
/* istanbul ignore else */
if (String(balance) !== '0') {
nonZeroBalances[tokenAddress] = balance;
}
});
}
return nonZeroBalances;
}
}

Expand Down
Loading

0 comments on commit 9b82201

Please sign in to comment.