Skip to content

Commit

Permalink
feat: fetch hub in a loop, register new root only if neccesary
Browse files Browse the repository at this point in the history
  • Loading branch information
yum0e committed Jul 25, 2023
1 parent 8f53c77 commit 28b8a1b
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 36 deletions.
4 changes: 3 additions & 1 deletion front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "NODE_OPTIONS='--max-http-header-size=24576' next dev -p 3000 & nodemon --watch ../src/Airdrop.sol --exec 'npm run deploy-airdrop && sleep 3 && ./script/log.sh'",
"dev": "NODE_OPTIONS='--max-http-header-size=24576' next dev -p 3000 & nodemon --watch ../src/Airdrop.sol --exec 'npm run deploy-airdrop && sleep 3 && ./script/log.sh' & nodemon --watch ./script/register-root/latest-root.txt --exec 'yarn register-root' & nodemon --exec 'yarn fetch-hub'",
"build": "next build",
"start": "NODE_OPTIONS='--max-http-header-size=24576' next start",
"lint": "next lint",
"fetch-hub": "npx ts-node script/register-root/fetch-hub.ts",
"register-root": "npx ts-node script/register-root/register-root.ts",
"deploy-airdrop": "forge script DeployAirdrop --rpc-url http://localhost:8545 -vv --mnemonics 'test test test test test test test test test test test junk' --sender '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266' --broadcast && ./script/generate-abi-from-front.sh"
},
"browser": {
Expand Down
17 changes: 0 additions & 17 deletions front/script/fetch-hub.ts

This file was deleted.

30 changes: 30 additions & 0 deletions front/script/register-root/fetch-hub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const axios = require("axios");
const fs = require("fs");
const path = require("path");

const latestRootFilePath = path.join(__dirname, "latest-root.txt");

const fetchHub = async () => {
while (true) {
try {
const res = (await axios(
" https://hub.sismo.io/available-data/gnosis/hydra-s2?latest=true&isOnChain=true"
)) as { data: { items: [{ identifier: string }] } };
const root = res.data?.items?.[0].identifier;
const latestRootSaved = fs.readFileSync(latestRootFilePath, "utf-8") as string;
if (latestRootSaved !== root) {
fs.writeFileSync(latestRootFilePath, root);
console.log(`New latest root ${root} on Gnosis`);
}
} catch (e) {
console.error(
"Error while fetching the latest root on Gnosis network, you seem to have lost your internet connection."
);
}
await new Promise((resolve) => setTimeout(resolve, 10000));
}
};

fetchHub();

module.exports = { latestRootFilePath, path };
1 change: 1 addition & 0 deletions front/script/register-root/latest-root.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x1e66c7019d3ee0187b65d1cbd242fa5f9d2cea21d5d38661fc73b30258e30d3e
14 changes: 14 additions & 0 deletions front/script/register-root/register-root.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

availableRootsRegistryContractAddress=$1
lastRootOnGnosis=$2

# get the owner of the roots registry contract
# first remove the 0x prefix, then remove the leading zeros with awk
rootsRegistryContractOwner=$(echo $(cast call "$availableRootsRegistryContractAddress" "owner()") | awk '{sub(/^0x*/,"");}1' | awk '{sub(/^0*/,"");}1')

# impersonate the owner of the roots registry contract
cast rpc anvil_impersonateAccount $rootsRegistryContractOwner

# register the root
cast send $availableRootsRegistryContractAddress 'registerRoot(uint256)' $lastRootOnGnosis --from $rootsRegistryContractOwner --unlocked
34 changes: 34 additions & 0 deletions front/script/register-root/register-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Script used to fetch the latest root sent on Gnosis
// and register it in the local fork chain

const { readFileSync } = require("fs");
const { spawnSync } = require("child_process");
const fetchHubExports = require("./fetch-hub");

// on Mumbai
const availableRootsRegistryContractAddress = "0x51B3ec080D1459232dbea86B751F75b5204a4abC";

const registerRoot = (root: string) => {
const registerRootScriptPath = fetchHubExports.path.join(__dirname, "register-root.sh");
const child = spawnSync(
`${registerRootScriptPath} ${availableRootsRegistryContractAddress} ${root}`,
{
shell: true,
}
);

if (child.status !== 0) {
console.error(child.stderr.toString());
throw new Error("Error while registering root on the local fork");
}

console.group(`Root ${root} successfully registered on the local fork`);
};

const main = async () => {
const root = readFileSync(fetchHubExports.latestRootFilePath, "utf-8") as string;
console.log(`Registering root ${root} on the local fork...`);
registerRoot(root === "" ? "0x" : root);
};

main();
22 changes: 4 additions & 18 deletions front/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"compilerOptions": {
"target": "es2020",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -15,7 +11,6 @@
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
// "isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
Expand All @@ -24,19 +19,10 @@
}
],
"paths": {
"@/*": [
"./src/*"
]
"@/*": ["./src/*"]
},
"isolatedModules": true
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 28b8a1b

Please sign in to comment.