I am fetching contract counters for evmexplorer.com and noticed some bugs.
Sometimes the data returned is full "0" strings.
Another bug is that token transfers are non zeros but the gas usage returned is "0".
The code to fix this for my project is
export async function fetchContractCounters(
address: string,
chainId?: number
): Promise<CountersContract> {
const chainPrefix: string = getBlockScoutPrefix(chainId);
const query: string =
https://${chainPrefix}.blockscout.com/api/v2/addresses/${address}/counters
;
const response: Response = await fetch(query);
if (response.status === 200) {
const body: CountersContract = await response.json();
if (
body.gas_usage_count === "0" &&
body.token_transfers_count === "0" &&
body.transactions_count === "0" &&
body.validations_count === "0"
) {
throw new Error("BlockScout Contract Counters response empty");
}
if (body.gas_usage_count === "0" && body.token_transfers_count !== "0") {
throw new Error("BlockScout Contract Counters response faulty");
}
return body;
}
throw new Error("BlockScout Contract Counters response undefined");
}