Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add update-deposits script to CI #35

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function Dashboard() {
} else if (connectionAttempted && !account.isConnected) {
router.push("/");
}
}, [account.isConnecting, connectionAttempted, account.isConnected]);
}, [account.isConnecting, connectionAttempted, account.isConnected, router]);

const handleCopyAddress = async () => {
if (account.address) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"name": "deposit-ui",
"version": "0.1.0",
"type": "module",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"update-deposits": "node ./scripts/update-deposits.js"
"update-deposits": "node ./scripts/update-deposits.mjs"
},
"dependencies": {
"@headlessui/react": "^1.7.18",
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions scripts/update-deposits.js → scripts/update-deposits.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { Contract, providers } from "ethers";
import PQueue from "p-queue";

// Network config and ABI
import depositABI from "./deposits.js";
import { NETWORKS } from "./contracts.js";
import depositABI from "./deposits.mjs";
import { NETWORKS } from './contracts.mjs';


// Config
const stepSize = 25_000;
Expand Down
178 changes: 89 additions & 89 deletions scripts/update-deposits.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,89 @@
import path from "path";
import { readFile, writeFile, mkdir } from "fs/promises";

// Network config and ABI
import CONTRACTS from "../utils/contracts.ts";
import depositABI from "../utils/abis/deposit.ts";
import { getPublicClient } from "wagmi/actions";
import { config } from "../wagmi.ts";
import { Address } from "viem";

// Config
const __dirname = path.dirname(new URL(import.meta.url).pathname);

const BLOCK_RANGE_SIZE = 5000;
const client = getPublicClient(config);

async function fetchAllEvents(depositAddress: Address, fromBlock: bigint, toBlock: bigint) {

let currentBlock = BigInt(fromBlock);
const endBlock = BigInt(toBlock);
let allEvents = [];

while (currentBlock <= endBlock) {
const nextBlock = currentBlock + BigInt(BLOCK_RANGE_SIZE) > endBlock ? endBlock : currentBlock + BigInt(BLOCK_RANGE_SIZE);

console.log(`Fetching from block ${currentBlock} to ${nextBlock}`);

const events = await client.getContractEvents({
abi: depositABI,
address: depositAddress,
eventName: "DepositEvent",
fromBlock: currentBlock,
toBlock: nextBlock,
});

allEvents.push(...events);
currentBlock = nextBlock + BigInt(1);
}

return allEvents;
}

async function readCurrentDeposits(filePath: string) {
try {
const content = await readFile(filePath, "utf8");
return JSON.parse(content);
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}

return {};
}

async function updateNetwork(networkId: number) {
const network = CONTRACTS[networkId];
if (!network) {
throw new Error(`Network with ID ${networkId} does not exist`);
}

const filePath = path.resolve(__dirname, `../data/${networkId}/deposits.json`);

let { lastBlock = network.depositStartBlockNumber, deposits = [] } = await readCurrentDeposits(filePath);

console.log("Fetching existing deposits");
const fromBlock = lastBlock;
const toBlock = await client.getBlockNumber();
const events = await fetchAllEvents(CONTRACTS[networkId]!.addresses.deposit, fromBlock, toBlock);
let pks = events.map((e) => e.topics[1]);

deposits = deposits.concat(pks);
lastBlock = toBlock;

// Write the new deposits to file
const newContent = JSON.stringify({ lastBlock, deposits });
await mkdir(path.dirname(filePath), { recursive: true });
await writeFile(filePath, newContent, "utf8");

console.log(`Added ${pks.length} new cached pubkeys, new total is ${deposits.length}.`);
}

async function main() {
for (const networkId of Object.keys(CONTRACTS)) {
await updateNetwork(Number(networkId));
}
}

main();
// import path from "path";
// import { readFile, writeFile, mkdir } from "fs/promises";

// // Network config and ABI
// import CONTRACTS from "../utils/contracts.ts";
// import depositABI from "../utils/abis/deposit.ts";
// import { getPublicClient } from "wagmi/actions";
// import { config } from "../wagmi.ts";
// import { Address } from "viem";

// // Config
// const __dirname = path.dirname(new URL(import.meta.url).pathname);

// const BLOCK_RANGE_SIZE = 5000;
// const client = getPublicClient(config);

// async function fetchAllEvents(depositAddress: Address, fromBlock: bigint, toBlock: bigint) {

// let currentBlock = BigInt(fromBlock);
// const endBlock = BigInt(toBlock);
// let allEvents = [];

// while (currentBlock <= endBlock) {
// const nextBlock = currentBlock + BigInt(BLOCK_RANGE_SIZE) > endBlock ? endBlock : currentBlock + BigInt(BLOCK_RANGE_SIZE);

// console.log(`Fetching from block ${currentBlock} to ${nextBlock}`);

// const events = await client.getContractEvents({
// abi: depositABI,
// address: depositAddress,
// eventName: "DepositEvent",
// fromBlock: currentBlock,
// toBlock: nextBlock,
// });

// allEvents.push(...events);
// currentBlock = nextBlock + BigInt(1);
// }

// return allEvents;
// }

// async function readCurrentDeposits(filePath: string) {
// try {
// const content = await readFile(filePath, "utf8");
// return JSON.parse(content);
// } catch (err: any) {
// if (err.code !== "ENOENT") {
// throw err;
// }
// }

// return {};
// }

// async function updateNetwork(networkId: number) {
// const network = CONTRACTS[networkId];
// if (!network) {
// throw new Error(`Network with ID ${networkId} does not exist`);
// }

// const filePath = path.resolve(__dirname, `../data/${networkId}/deposits.json`);

// let { lastBlock = network.depositStartBlockNumber, deposits = [] } = await readCurrentDeposits(filePath);

// console.log("Fetching existing deposits");
// const fromBlock = lastBlock;
// const toBlock = await client.getBlockNumber();
// const events = await fetchAllEvents(CONTRACTS[networkId]!.addresses.deposit, fromBlock, toBlock);
// let pks = events.map((e) => e.topics[1]);

// deposits = deposits.concat(pks);
// lastBlock = toBlock;

// // Write the new deposits to file
// const newContent = JSON.stringify({ lastBlock, deposits });
// await mkdir(path.dirname(filePath), { recursive: true });
// await writeFile(filePath, newContent, "utf8");

// console.log(`Added ${pks.length} new cached pubkeys, new total is ${deposits.length}.`);
// }

// async function main() {
// for (const networkId of Object.keys(CONTRACTS)) {
// await updateNetwork(Number(networkId));
// }
// }

// main();
Loading