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

Fix issues with empty responses in ERC20Standard #927

Merged
merged 4 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix an issue with empty ERC20 responses in ERC20Standard
  • Loading branch information
FrederikBolding committed Oct 5, 2022
commit 6ea628e26e703d32e149c43e6fa50d4cd105ad81
40 changes: 40 additions & 0 deletions src/assets/Standards/ERC20Standard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const MAINNET_PROVIDER = new HttpProvider(
);
const ERC20_MATIC_ADDRESS = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0';
const MKR_ADDRESS = '0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2';
const AMBIRE_ADDRESS = '0xa07D75aacEFd11b425AF7181958F0F85c312f143';

describe('ERC20Standard', () => {
let erc20Standard: ERC20Standard;
Expand Down Expand Up @@ -116,4 +117,43 @@ describe('ERC20Standard', () => {
expect(decimals).toBe('18');
expect(symbol).toBe('MKR');
});

it('should fail on on empty responses', async () => {
nock('https://mainnet.infura.io:443', { encodedQueryParams: true })
.post('/v3/341eacb578dd44a1a049cbc5f6fd4035', {
jsonrpc: '2.0',
id: 5,
method: 'eth_call',
params: [
{
to: AMBIRE_ADDRESS,
data: '0x95d89b41',
},
'latest',
],
})
.reply(200, { jsonrpc: '2.0', id: 5, result: '0x' })
.post('/v3/341eacb578dd44a1a049cbc5f6fd4035', {
jsonrpc: '2.0',
id: 6,
method: 'eth_call',
params: [
{
to: AMBIRE_ADDRESS,
data: '0x313ce567',
},
'latest',
],
})
.reply(200, { jsonrpc: '2.0', id: 6, result: '0x' });

// 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',
);

await expect(
erc20Standard.getTokenDecimals(AMBIRE_ADDRESS),
).rejects.toThrow('Failed to parse token decimals');
});
});
16 changes: 12 additions & 4 deletions src/assets/Standards/ERC20Standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ export class ERC20Standard {
reject(error);
return;
}
resolve(result.toString());

const resultString = result.toString();
if (resultString.length > 0 && resultString !== '0') {
resolve(resultString);
}

reject(new Error('Failed to parse token decimals'));
});
});
}
Expand All @@ -74,7 +80,7 @@ export class ERC20Standard {
// Parse as string
try {
const decoded = abiCoder.decode(['string'], result)[0];
if (decoded) {
if (decoded?.length > 0) {
resolve(decoded);
return;
}
Expand All @@ -85,8 +91,10 @@ export class ERC20Standard {
// Parse as bytes
try {
const utf8 = toUtf8(result);
resolve(utf8);
return;
if (utf8.length > 0) {
resolve(utf8);
return;
}
} catch {
// Ignore error
}
Expand Down