From 8f53c773b1d77b946c9c891a6d1f4d6eb180fb99 Mon Sep 17 00:00:00 2001 From: bigq Date: Sat, 22 Jul 2023 23:04:07 +0200 Subject: [PATCH 1/2] feat: fetch gnosis root from hub --- front/package.json | 1 + front/script/fetch-hub.ts | 17 ++++++++++++ front/yarn.lock | 54 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 front/script/fetch-hub.ts diff --git a/front/package.json b/front/package.json index 21e9e0c..09fe612 100644 --- a/front/package.json +++ b/front/package.json @@ -20,6 +20,7 @@ "@types/node": "20.2.3", "@types/react": "18.2.7", "@types/react-dom": "18.2.4", + "axios": "^1.4.0", "eslint": "8.41.0", "eslint-config-next": "13.4.3", "next": "13.4.3", diff --git a/front/script/fetch-hub.ts b/front/script/fetch-hub.ts new file mode 100644 index 0000000..eefd039 --- /dev/null +++ b/front/script/fetch-hub.ts @@ -0,0 +1,17 @@ +// 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/yarn.lock b/front/yarn.lock index ba66268..24ec360 100644 --- a/front/yarn.lock +++ b/front/yarn.lock @@ -2954,6 +2954,11 @@ async-mutex@^0.2.6: dependencies: tslib "^2.0.0" +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== + atomic-sleep@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/atomic-sleep/-/atomic-sleep-1.0.0.tgz#eb85b77a601fc932cfe432c5acd364a9e2c9075b" @@ -2976,6 +2981,15 @@ axios@^0.21.0: dependencies: follow-redirects "^1.14.0" +axios@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.4.0.tgz#38a7bf1224cd308de271146038b551d725f0be1f" + integrity sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA== + dependencies: + follow-redirects "^1.15.0" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + axobject-query@^3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-3.1.1.tgz#3b6e5c6d4e43ca7ba51c5babf99d22a9c68485e1" @@ -3280,6 +3294,13 @@ colorette@^2.0.7: resolved "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz#9eb793e6833067f7235902fcd3b09917a000a95a" integrity sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w== +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== + dependencies: + delayed-stream "~1.0.0" + commander@^2.20.3: version "2.20.3" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" @@ -3468,6 +3489,11 @@ delay@^5.0.0: resolved "https://registry.yarnpkg.com/delay/-/delay-5.0.0.tgz#137045ef1b96e5071060dd5be60bf9334436bd1d" integrity sha512-ReEBKkIfe4ya47wlPYf/gu5ib6yUG0/Aez0JQZQz94kiWtRQvZIQbTiehsnwHvLSWJnQdhVeqYue7Id1dKr0qw== +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== + depd@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df" @@ -4149,7 +4175,7 @@ flatted@^3.1.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787" integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ== -follow-redirects@^1.14.0: +follow-redirects@^1.14.0, follow-redirects@^1.15.0: version "1.15.2" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== @@ -4161,6 +4187,15 @@ for-each@^0.3.3: dependencies: is-callable "^1.1.3" +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + fs-readdir-recursive@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz#e32fc030a2ccee44a6b5371308da54be0b397d27" @@ -4993,6 +5028,18 @@ micromatch@^4.0.4: braces "^3.0.2" picomatch "^2.3.1" +mime-db@1.52.0: + version "1.52.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" + integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== + +mime-types@^2.1.12: + version "2.1.35" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" + integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== + dependencies: + mime-db "1.52.0" + mimic-fn@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b" @@ -5507,6 +5554,11 @@ proxy-compare@2.5.1: resolved "https://registry.yarnpkg.com/proxy-compare/-/proxy-compare-2.5.1.tgz#17818e33d1653fbac8c2ec31406bce8a2966f600" integrity sha512-oyfc0Tx87Cpwva5ZXezSp5V9vht1c7dZBhvuV/y3ctkgMVUmiAGDVeeB0dKhGSyT0v1ZTEQYpe/RXlBVBNuCLA== +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + pstree.remy@^1.1.8: version "1.1.8" resolved "https://registry.yarnpkg.com/pstree.remy/-/pstree.remy-1.1.8.tgz#c242224f4a67c21f686839bbdb4ac282b8373d3a" From 28b8a1b4a90e9242094f0489547b6378baa07cfb Mon Sep 17 00:00:00 2001 From: bigq Date: Tue, 25 Jul 2023 16:07:18 +0200 Subject: [PATCH 2/2] 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"] }