Skip to content

Commit 699987d

Browse files
committed
fix(client): always fetch decimals for ERC20 transfer formatting
getErc20Transfers now fetches all token metadata and builds a decimals lookup map, so amounts are properly formatted even when no specific contract address filter is provided (e.g. the initial transfers list).
1 parent cf0f174 commit 699987d

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

bins/torii-tokens/client/shared/src/queries.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ export async function getErc20Transfers(
181181
if (query.wallet) filter.wallet = hexToBytes(query.wallet);
182182
if (query.contractAddress) filter.tokens = [hexToBytes(query.contractAddress)];
183183

184-
// Fetch decimals if filtering by a specific contract
185-
let decimals: number | undefined;
186-
if (query.contractAddress) {
187-
const metadata = await getErc20TokenMetadata(client, query.contractAddress);
188-
decimals = metadata[0]?.decimals;
184+
// Fetch all token metadata to build a decimals lookup
185+
const allMetadata = await getErc20TokenMetadata(client, query.contractAddress || undefined);
186+
const decimalsMap = new Map<string, number>();
187+
for (const m of allMetadata) {
188+
if (m.decimals != null) {
189+
decimalsMap.set(m.token, m.decimals);
190+
}
189191
}
190192

191193
const response = await client.call(
@@ -198,15 +200,19 @@ export async function getErc20Transfers(
198200
const transfers = response.transfers;
199201
const list = Array.isArray(transfers) ? transfers : transfers ? [transfers] : [];
200202

201-
return list.map((t: Record<string, unknown>) => ({
202-
token: bytesToHex(t.token as string | Uint8Array | undefined),
203-
from: bytesToHex(t.from as string | Uint8Array | undefined),
204-
to: bytesToHex(t.to as string | Uint8Array | undefined),
205-
amount: formatU256(t.amount as string | Uint8Array | undefined, decimals),
206-
blockNumber: Number(t.blockNumber ?? 0),
207-
txHash: bytesToHex(t.txHash as string | Uint8Array | undefined),
208-
timestamp: Number(t.timestamp ?? 0),
209-
}));
203+
return list.map((t: Record<string, unknown>) => {
204+
const token = bytesToHex(t.token as string | Uint8Array | undefined);
205+
const decimals = decimalsMap.get(token);
206+
return {
207+
token,
208+
from: bytesToHex(t.from as string | Uint8Array | undefined),
209+
to: bytesToHex(t.to as string | Uint8Array | undefined),
210+
amount: formatU256(t.amount as string | Uint8Array | undefined, decimals),
211+
blockNumber: Number(t.blockNumber ?? 0),
212+
txHash: bytesToHex(t.txHash as string | Uint8Array | undefined),
213+
timestamp: Number(t.timestamp ?? 0),
214+
};
215+
});
210216
}
211217

212218
export async function getErc721Transfers(

0 commit comments

Comments
 (0)