Skip to content

Commit 7e722a8

Browse files
authored
Merge pull request OneKeyHQ#34 from taimanhui/OK-6218
fix(eth): handle non-standard token name/symbol call result
2 parents 7960381 + 2c0e027 commit 7e722a8

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/provider/chains/eth/geth.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ type EIP1559Fee = {
2929
others?: Array<EIP1559Price>;
3030
};
3131

32+
function decodeStringCallResult(hexResult: string): string {
33+
if (hexResult.length <= 2) {
34+
throw new Error('Invalid hex result.');
35+
}
36+
try {
37+
return defaultAbiCoder.decode(['string'], hexResult)[0];
38+
} catch (e) {
39+
console.error(e);
40+
// Non-standard name or symbol, type of bytes32.
41+
// See 0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2
42+
return Buffer.from(hexResult.slice(2), 'hex')
43+
.toString()
44+
.replace(/\0+$/, '');
45+
}
46+
}
47+
3248
class Geth extends BaseClient {
3349
static readonly __LAST_BLOCK__ = 'latest';
3450
private _mmFee!: MmFee;
@@ -215,8 +231,8 @@ class Geth extends BaseClient {
215231
}
216232

217233
try {
218-
const [symbol] = defaultAbiCoder.decode(['string'], symbolHex);
219-
const [name] = defaultAbiCoder.decode(['string'], nameHex);
234+
const symbol = decodeStringCallResult(symbolHex);
235+
const name = decodeStringCallResult(nameHex);
220236
const decimals = parseInt(decimalsHex, 16);
221237
check(!isNaN(decimals));
222238

0 commit comments

Comments
 (0)