From 28b8a1b4a90e9242094f0489547b6378baa07cfb Mon Sep 17 00:00:00 2001 From: bigq Date: Tue, 25 Jul 2023 16:07:18 +0200 Subject: [PATCH] feat: fetch hub in a loop, register new root only if neccesary --- front/package.json | 4 ++- front/script/fetch-hub.ts | 17 ----------- front/script/register-root/fetch-hub.ts | 30 ++++++++++++++++++ front/script/register-root/latest-root.txt | 1 + front/script/register-root/register-root.sh | 14 +++++++++ front/script/register-root/register-root.ts | 34 +++++++++++++++++++++ front/tsconfig.json | 22 +++---------- 7 files changed, 86 insertions(+), 36 deletions(-) delete mode 100644 front/script/fetch-hub.ts create mode 100644 front/script/register-root/fetch-hub.ts create mode 100644 front/script/register-root/latest-root.txt create mode 100755 front/script/register-root/register-root.sh create mode 100644 front/script/register-root/register-root.ts diff --git a/front/package.json b/front/package.json index 09fe612..d732dcf 100644 --- a/front/package.json +++ b/front/package.json @@ -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": { diff --git a/front/script/fetch-hub.ts b/front/script/fetch-hub.ts deleted file mode 100644 index eefd039..0000000 --- a/front/script/fetch-hub.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Script used to fetch the latest root sent on Gnosis -// and register it in the local fork chain - -const axios = require("axios"); - -const main = async () => { - while (true) { - await new Promise((resolve) => setTimeout(resolve, 10000)); - 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; - console.log("Latest Gnosis root:", root); - } -}; - -main(); diff --git a/front/script/register-root/fetch-hub.ts b/front/script/register-root/fetch-hub.ts new file mode 100644 index 0000000..0087c5d --- /dev/null +++ b/front/script/register-root/fetch-hub.ts @@ -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 }; diff --git a/front/script/register-root/latest-root.txt b/front/script/register-root/latest-root.txt new file mode 100644 index 0000000..ea5fa0d --- /dev/null +++ b/front/script/register-root/latest-root.txt @@ -0,0 +1 @@ +0x1e66c7019d3ee0187b65d1cbd242fa5f9d2cea21d5d38661fc73b30258e30d3e \ No newline at end of file diff --git a/front/script/register-root/register-root.sh b/front/script/register-root/register-root.sh new file mode 100755 index 0000000..4663141 --- /dev/null +++ b/front/script/register-root/register-root.sh @@ -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 \ No newline at end of file diff --git a/front/script/register-root/register-root.ts b/front/script/register-root/register-root.ts new file mode 100644 index 0000000..1ace32b --- /dev/null +++ b/front/script/register-root/register-root.ts @@ -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(); diff --git a/front/tsconfig.json b/front/tsconfig.json index 4d23956..34dc437 100644 --- a/front/tsconfig.json +++ b/front/tsconfig.json @@ -1,11 +1,7 @@ { "compilerOptions": { "target": "es2020", - "lib": [ - "dom", - "dom.iterable", - "esnext" - ], + "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, @@ -15,7 +11,6 @@ "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, - // "isolatedModules": true, "jsx": "preserve", "incremental": true, "plugins": [ @@ -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"] }