Skip to content

Commit

Permalink
feat: use recursive logs for alchemy (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra authored Feb 15, 2024
1 parent 21eb530 commit eab9a7e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/ipfs/parseIpfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function getProposalMetadata(
},
});
if (!ipfsResponse.ok) throw Error(`IPFS: error fetching ${ipfsPath}`);
const clone = await ipfsResponse.clone();
const clone = ipfsResponse.clone();
try {
const response = await ipfsResponse.json();
const { content, data } = matter(response.description);
Expand All @@ -35,8 +35,6 @@ export async function getProposalMetadata(
description: content,
...data,
};
// matter will error in case the proposal is not valid yaml (like on proposal 0)
// therefore in the case of an error we just inline the complete ipfs content
} catch (e) {
const { content, data } = matter(await clone.text());
return {
Expand Down
15 changes: 15 additions & 0 deletions src/rpc/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getAbiItem } from 'viem';
import { IPoolV1_ABI } from './mocks/IPoolV1';
import { CHAIN_ID_CLIENT_MAP } from './clients.ts';
import { mainnet } from 'viem/chains';
import { getLogs } from 'viem/actions';

describe('helpers', () => {
it(
Expand Down Expand Up @@ -57,4 +58,18 @@ describe('helpers', () => {
},
{ timeout: 60000 },
);

// it(
// 'getLogs should use alchemy range',
// async () => {
// const logs = await strategicGetLogs({
// client: CHAIN_ID_CLIENT_MAP[mainnet.id],
// events: [getAbiItem({ abi: IPoolV1_ABI, name: 'Borrow' })],
// address: '0x398eC7346DcD622eDc5ae82352F02bE94C62d119', // v1 pool
// fromBlock: 9241022n,
// toBlock: await CHAIN_ID_CLIENT_MAP[mainnet.id].getBlockNumber(),
// });
// },
// { timeout: 120000 },
// );
});
35 changes: 23 additions & 12 deletions src/rpc/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, GetLogsReturnType, Client } from 'viem';
import { Address, GetLogsReturnType, Client, fromHex, Hex } from 'viem';
import type { AbiEvent } from 'abitype';
import { PromisePool } from '@supercharge/promise-pool';
import { getBlock, getBytecode, getLogs } from 'viem/actions';
Expand Down Expand Up @@ -141,17 +141,7 @@ export async function strategicGetLogs<
if (/quiknode/.test(url)) batchSize = 10_000;
// alchemy behaves different to other rpcs as it allows querying with infinite block range as long as the response size is below a certain threshold
if (/alchemy/.test(url)) {
try {
// TODO: better error handling as alchemy suggests proper ranges
return await getLogs(client, {
fromBlock,
toBlock,
events,
address,
});
} catch (e) {
batchSize = 2_000;
}
getLogsRecursive({ client, events, address, fromBlock, toBlock });
}
if (batchSize > 0) {
return getLogsInBatches({
Expand Down Expand Up @@ -179,6 +169,7 @@ export async function getLogsRecursive<
fromBlock,
toBlock,
}: GetLogsArgs<TAbiEvents>): Promise<GetLogsReturnType<undefined, TAbiEvents>> {
console.log('recursions', fromBlock, toBlock);
if (fromBlock <= toBlock) {
try {
const logs = await getLogs(client, {
Expand All @@ -189,6 +180,26 @@ export async function getLogsRecursive<
});
return logs;
} catch (error: any) {
// for alchemy part of the details string contains sth like: [0x8d01be, 0x948ce4]
const rangeMatch = (error.details as string)?.match(/.*\[(.*),\s*(.*)\]/);
if (rangeMatch?.length === 3) {
const maxBlock = fromHex(rangeMatch[2] as Hex, 'bigint');
const arr1 = await getLogsRecursive({
client,
events,
address,
fromBlock,
toBlock: maxBlock,
});
const arr2 = await getLogsRecursive({
client,
events,
address,
fromBlock: maxBlock + BigInt(1),
toBlock,
});
return [...arr1, ...arr2];
}
// divide & conquer when issue/limit is now known
const midBlock = BigInt(fromBlock + toBlock) >> BigInt(1);
const arr1 = await getLogsRecursive({
Expand Down

0 comments on commit eab9a7e

Please sign in to comment.