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

update ERC20Standard to use @metamask/abi-utils instead of @ethersproject/abi #985

Merged
merged 14 commits into from
Dec 21, 2022
2 changes: 1 addition & 1 deletion packages/assets-controllers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ module.exports = merge(baseConfig, {
},

// We rely on `window` to make requests
testEnvironment: 'jsdom',
testEnvironment: '<rootDir>/jest.environment.js',
});
18 changes: 18 additions & 0 deletions packages/assets-controllers/jest.environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable */
const JSDOMEnvironment = require('jest-environment-jsdom');

// Custom test environment copied from https://github.com/jsdom/jsdom/issues/2524
// in order to add TextEncoder to jsdom. TextEncoder is expected by jose.

module.exports = class CustomTestEnvironment extends JSDOMEnvironment {
async setup() {
await super.setup();
if (typeof this.global.TextEncoder === 'undefined') {
const { TextEncoder, TextDecoder } = require('util');
adonesky1 marked this conversation as resolved.
Show resolved Hide resolved
this.global.TextEncoder = TextEncoder;
this.global.TextDecoder = TextDecoder;
this.global.ArrayBuffer = ArrayBuffer;
this.global.Uint8Array = Uint8Array;
}
}
};
4 changes: 3 additions & 1 deletion packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@ethersproject/abi": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.0",
"@metamask/abi-utils": "^1.1.0",
adonesky1 marked this conversation as resolved.
Show resolved Hide resolved
"@metamask/base-controller": "workspace:^",
"@metamask/contract-metadata": "^2.1.0",
"@metamask/controller-utils": "workspace:^",
"@metamask/metamask-eth-abis": "3.0.0",
"@metamask/network-controller": "workspace:^",
"@metamask/preferences-controller": "workspace:^",
"@metamask/utils": "^3.3.1",
"@types/uuid": "^8.3.0",
"abort-controller": "^3.0.0",
"async-mutex": "^0.2.6",
Expand All @@ -58,6 +59,7 @@
"deepmerge": "^4.2.2",
"ethjs-provider-http": "^0.1.6",
"jest": "^26.4.2",
"jest-environment-jsdom": "^26.4.2",
"nock": "^13.0.7",
"sinon": "^9.2.4",
"ts-jest": "^26.5.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('ERC20Standard', () => {

// Some proxy contracts don't revert when requesting symbol() and decimals(), this test makes sure we handle those cases.
await expect(erc20Standard.getTokenSymbol(AMBIRE_ADDRESS)).rejects.toThrow(
'Failed to parse token symbol',
'Value must be a hexadecimal string, starting with "0x".',
);

await expect(
Expand Down
10 changes: 5 additions & 5 deletions packages/assets-controllers/src/Standards/ERC20Standard.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Contract } from '@ethersproject/contracts';
import { abiERC20 } from '@metamask/metamask-eth-abis';
import { BN, toUtf8 } from 'ethereumjs-util';
import { AbiCoder } from '@ethersproject/abi';
import { decodeSingle } from '@metamask/abi-utils';

import { Web3Provider } from '@ethersproject/providers';
import { ERC20 } from '@metamask/controller-utils';
import { assertIsStrictHexString } from '@metamask/utils';
import { ethersBigNumberToBN } from '../assetsUtil';

export class ERC20Standard {
Expand Down Expand Up @@ -56,12 +58,10 @@ export class ERC20Standard {
// Signature for calling `symbol()`
const payload = { to: address, data: '0x95d89b41' };
const result = await this.provider.call(payload);

const abiCoder = new AbiCoder();

assertIsStrictHexString(result);
// Parse as string - treat empty string as failure
try {
const decoded = abiCoder.decode(['string'], result)[0];
const decoded = decodeSingle('string', result);
if (decoded?.length > 0) {
adonesky1 marked this conversation as resolved.
Show resolved Hide resolved
return decoded;
}
Expand Down
Loading