Skip to content

Commit

Permalink
Merge branch 'main' of github.com:trustwallet/trust-web3-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
y3fers0n committed Jan 6, 2025
2 parents 33a9acb + 2ab7fb8 commit e011d5f
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions packages/ethereum/MobileAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,62 @@ export class MobileAdapter {
const { options, type } = args.params as unknown as IWatchAsset;
const { address, symbol, decimals } = options;

let fetchedSymbol;
let fetchedDecimals;

if (!symbol) {
try {
// call for symbol() = 0x95d89b41
const result = await this.contractCall(address, '0x95d89b41');
const hexString = result.slice(2);

// The offset is the position where the dynamic data starts
const offset = parseInt(hexString.slice(0, 64), 16);

// The length of the string is at the offset position
const length = parseInt(
hexString.slice(offset * 2, offset * 2 + 64),
16,
);

// The actual string data starts right after the length field
const stringDataStart = offset * 2 + 64;
const stringDataHex = hexString.slice(
stringDataStart,
stringDataStart + length * 2,
);

fetchedSymbol = '';

for (let i = 0; i < stringDataHex.length; i += 2) {
const hexChar = stringDataHex.slice(i, i + 2);
const char = String.fromCharCode(parseInt(hexChar, 16));

if (char === '\x00') break;
fetchedSymbol += char;
}
} catch (e) {
console.error(e);
}
}

if (!decimals) {
try {
// call for decimals() = 0x313ce567
const result = await this.contractCall(address, '0x313ce567');
fetchedDecimals = parseInt(result, 16);
} catch (e) {
console.error(e);
}
}

return this.provider.internalRequest({
method: 'watchAsset',
params: {
type,
contract: address,
symbol,
decimals: decimals || 0,
symbol: symbol || fetchedSymbol,
decimals: decimals || fetchedDecimals,
},
});
}
Expand Down Expand Up @@ -247,4 +296,18 @@ export class MobileAdapter {
},
});
}

contractCall(address: string, method: string) {
return this.provider.getRPC().call({
method: 'eth_call',
jsonrpc: '2.0',
params: [
{
to: address,
data: method,
},
'latest',
],
});
}
}

0 comments on commit e011d5f

Please sign in to comment.