Skip to content

Commit

Permalink
Merge branch 'main' into query-erc721-name-symbol-without-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 authored May 18, 2022
2 parents 6634058 + fdc1dd7 commit 10b2c1e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"devDependencies": {
"@keystonehq/bc-ur-registry-eth": "^0.9.0",
"@lavamoat/allow-scripts": "^2.0.2",
"@metamask/auto-changelog": "^2.5.0",
"@metamask/auto-changelog": "^2.6.0",
"@metamask/eslint-config": "^9.0.0",
"@metamask/eslint-config-jest": "^9.0.0",
"@metamask/eslint-config-nodejs": "^9.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/assets/AssetsContractController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ describe('AssetsContractController', () => {

it('should get ERC-20 token decimals', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const symbol = await assetsContract.getERC20TokenDecimals(
const decimals = await assetsContract.getERC20TokenDecimals(
ERC20_DAI_ADDRESS,
);
expect(Number(symbol)).toStrictEqual(18);
expect(Number(decimals)).toStrictEqual(18);
});

it('should get ERC-721 collectible ownership', async () => {
Expand Down
45 changes: 45 additions & 0 deletions src/assets/Standards/ERC20Standard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MAINNET_PROVIDER = new HttpProvider(
'https://mainnet.infura.io/v3/341eacb578dd44a1a049cbc5f6fd4035',
);
const ERC20_MATIC_ADDRESS = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0';
const MKR_ADDRESS = '0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2';

describe('ERC20Standard', () => {
let erc20Standard: ERC20Standard;
Expand Down Expand Up @@ -71,4 +72,48 @@ describe('ERC20Standard', () => {
);
expect(maticDecimals.toString()).toStrictEqual('18');
});

it('should support non-standard ERC20 symbols and decimals', async () => {
nock('https://mainnet.infura.io:443', { encodedQueryParams: true })
.post('/v3/341eacb578dd44a1a049cbc5f6fd4035', {
jsonrpc: '2.0',
id: 3,
method: 'eth_call',
params: [
{
to: MKR_ADDRESS,
data: '0x313ce567',
},
'latest',
],
})
.reply(200, {
jsonrpc: '2.0',
id: 3,
result:
'0x0000000000000000000000000000000000000000000000000000000000000012',
})
.post('/v3/341eacb578dd44a1a049cbc5f6fd4035', {
jsonrpc: '2.0',
id: 4,
method: 'eth_call',
params: [
{
to: MKR_ADDRESS,
data: '0x95d89b41',
},
'latest',
],
})
.reply(200, {
jsonrpc: '2.0',
id: 4,
result:
'0x4d4b520000000000000000000000000000000000000000000000000000000000',
});
const decimals = await erc20Standard.getTokenDecimals(MKR_ADDRESS);
const symbol = await erc20Standard.getTokenSymbol(MKR_ADDRESS);
expect(decimals).toBe('18');
expect(symbol).toBe('MKR');
});
});
37 changes: 31 additions & 6 deletions src/assets/Standards/ERC20Standard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { abiERC20 } from '@metamask/metamask-eth-abis';
import { BN } from 'ethereumjs-util';
import { BN, toUtf8 } from 'ethereumjs-util';
import { AbiCoder } from 'ethers/lib/utils';
import { ERC20 } from '../../constants';
import { Web3 } from './standards-types';

Expand Down Expand Up @@ -40,13 +41,13 @@ export class ERC20Standard {
async getTokenDecimals(address: string): Promise<string> {
const contract = this.web3.eth.contract(abiERC20).at(address);
return new Promise<string>((resolve, reject) => {
contract.decimals((error: Error, result: string) => {
contract.decimals((error: Error, result: BN | string) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);
resolve(result.toString());
});
});
}
Expand All @@ -58,15 +59,39 @@ export class ERC20Standard {
* @returns Promise resolving to the 'symbol'.
*/
async getTokenSymbol(address: string): Promise<string> {
const contract = this.web3.eth.contract(abiERC20).at(address);
// Signature for calling `symbol()`
const payload = { to: address, data: '0x95d89b41' };
return new Promise<string>((resolve, reject) => {
contract.symbol((error: Error, result: string) => {
this.web3.eth.call(payload, undefined, (error: Error, result: string) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);

const abiCoder = new AbiCoder();

// Parse as string
try {
const decoded = abiCoder.decode(['string'], result)[0];
if (decoded) {
resolve(decoded);
return;
}
} catch {
// Ignore error
}

// Parse as bytes
try {
const utf8 = toUtf8(result);
resolve(utf8);
return;
} catch {
// Ignore error
}

reject(new Error('Failed to parse token symbol'));
});
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/assets/Standards/standards-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ type Contract = {

export type Web3 = {
eth: {
call(
payload: { to: string; data: string },
block: undefined,
callback: (error: Error, result: string) => void,
): void;
contract(
abi: typeof abiERC20 | typeof abiERC721 | typeof abiERC1155,
): Contract;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1128,10 +1128,10 @@
"@npmcli/run-script" "^1.8.1"
yargs "^16.2.0"

"@metamask/auto-changelog@^2.5.0":
version "2.5.0"
resolved "https://registry.yarnpkg.com/@metamask/auto-changelog/-/auto-changelog-2.5.0.tgz#078f38142a3086fdb5556c758969a015c71dfdc9"
integrity sha512-39FeU98Poll3eTqv/bggqo3Yisza0WQJ8l9IiYloMVa2LV8XqTNqVkS4cNEU/5yq62n47JSAv6lZBtWCqeAjZQ==
"@metamask/auto-changelog@^2.6.0":
version "2.6.0"
resolved "https://registry.yarnpkg.com/@metamask/auto-changelog/-/auto-changelog-2.6.0.tgz#3cef14905635b144b0fc86aed8957e0942a91cc4"
integrity sha512-YzZ/Uc/3nfvDxZxZT9YMa0CJn8/kpxif/2y8TXi5blzxBiaYop9zUh9a+7PV3C4EQVUNZuTvkE816Ua3AZ0jHA==
dependencies:
diff "^5.0.0"
execa "^5.1.1"
Expand Down

0 comments on commit 10b2c1e

Please sign in to comment.