|
| 1 | +import { ethers } from "ethers"; |
| 2 | +import { mainnet } from "wagmi/chains"; |
| 3 | +import contracts from "~~/generated/hardhat_contracts"; |
| 4 | +import scaffoldConfig from "~~/scaffold.config"; |
| 5 | + |
| 6 | +const { address: cohortAddress, abi } = contracts[1][0].contracts.SandGardenStreams; |
| 7 | + |
| 8 | +export type WithdrawEvent = { |
| 9 | + id: string; |
| 10 | + builder: string; |
| 11 | + amount: string; |
| 12 | + reason: string; |
| 13 | + timestamp: number; |
| 14 | +}; |
| 15 | + |
| 16 | +export type CohortEvents = { |
| 17 | + withdrawals: WithdrawEvent[]; |
| 18 | + builders: string[]; |
| 19 | +}; |
| 20 | + |
| 21 | +const fetchCohortEvents = async (): Promise<CohortEvents> => { |
| 22 | + const provider = new ethers.providers.JsonRpcBatchProvider( |
| 23 | + `${mainnet.rpcUrls.alchemy.http[0]}/${scaffoldConfig.alchemyApiKey}`, |
| 24 | + ); |
| 25 | + const contractInterface = new ethers.utils.Interface(abi); |
| 26 | + |
| 27 | + const logs = await provider.getLogs({ |
| 28 | + address: cohortAddress, |
| 29 | + fromBlock: scaffoldConfig.contracts.SandGardenStreams.fromBlock, |
| 30 | + toBlock: "latest", |
| 31 | + }); |
| 32 | + |
| 33 | + const withdrawLogs: { log: ethers.providers.Log; args: ethers.utils.Result }[] = []; |
| 34 | + const builders: string[] = []; |
| 35 | + |
| 36 | + logs.forEach(log => { |
| 37 | + let parsed; |
| 38 | + try { |
| 39 | + parsed = contractInterface.parseLog(log); |
| 40 | + } catch (e) { |
| 41 | + // Event not in the ABI (or malformed). Skip it. |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (parsed.name === "Withdraw") { |
| 46 | + withdrawLogs.push({ log, args: parsed.args }); |
| 47 | + } else if (parsed.name === "AddBuilder" && !builders.includes(parsed.args.to)) { |
| 48 | + builders.push(parsed.args.to); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + // Withdraw has no timestamp, so we read it from the block. |
| 53 | + const blockNumbers = Array.from(new Set(withdrawLogs.map(({ log }) => log.blockNumber))); |
| 54 | + const blocks = await Promise.all(blockNumbers.map(blockNumber => provider.getBlock(blockNumber))); |
| 55 | + const timestampByBlock = new Map(blocks.map(block => [block.number, block.timestamp])); |
| 56 | + |
| 57 | + const withdrawals = withdrawLogs |
| 58 | + .map(({ log, args }) => ({ |
| 59 | + id: `${log.transactionHash}-${log.logIndex}`, |
| 60 | + builder: args.to as string, |
| 61 | + amount: ethers.utils.formatEther(args.amount), |
| 62 | + reason: args.reason as string, |
| 63 | + timestamp: timestampByBlock.get(log.blockNumber) ?? 0, |
| 64 | + })) |
| 65 | + .sort((a, b) => b.timestamp - a.timestamp); |
| 66 | + |
| 67 | + return { withdrawals, builders }; |
| 68 | +}; |
| 69 | + |
| 70 | +let cohortEventsPromise: Promise<CohortEvents> | null = null; |
| 71 | + |
| 72 | +/** |
| 73 | + * Reads every cohort event from the chain once per browser session. |
| 74 | + */ |
| 75 | +export const getCohortEvents = () => { |
| 76 | + if (!cohortEventsPromise) { |
| 77 | + cohortEventsPromise = fetchCohortEvents().catch(error => { |
| 78 | + cohortEventsPromise = null; |
| 79 | + throw error; |
| 80 | + }); |
| 81 | + } |
| 82 | + return cohortEventsPromise; |
| 83 | +}; |
0 commit comments