From 8f8740031b5bf22801ac0794216dacbcca6d5169 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 27 Jun 2024 10:44:42 +0530 Subject: [PATCH] feat: add test sync script --- .env.example | 3 ++- src/sync/merkle.cjs | 2 ++ src/sync/sync.js | 36 ++++++++++++++++++++++++++++++++++++ tsconfig.json | 7 ++++++- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/sync/sync.js diff --git a/.env.example b/.env.example index 79ff790..68a1a36 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ DATABASE_URL= MAX_BLOCK_RANGE= -AVALANCHE_RPC= \ No newline at end of file +AVALANCHE_RPC= +ORACLE_PRIVATE_KEY= \ No newline at end of file diff --git a/src/sync/merkle.cjs b/src/sync/merkle.cjs index 0a1e45c..58ca759 100644 --- a/src/sync/merkle.cjs +++ b/src/sync/merkle.cjs @@ -22,3 +22,5 @@ function encodeLeaf(tokenId, points) { } getMerkleRoot(43114).catch((error) => console.error(error)); + +module.exports = getMerkleRoot; diff --git a/src/sync/sync.js b/src/sync/sync.js new file mode 100644 index 0000000..199f33e --- /dev/null +++ b/src/sync/sync.js @@ -0,0 +1,36 @@ +import { createWalletClient, http } from "viem"; +import { privateKeyToAccount } from "viem/accounts"; +import { mainnet } from "viem/chains"; +import getMerkleRoot from "./merkle.cjs"; + +import { parseAbi } from "viem"; + +const abi = parseAbi([ + "function setMerkleRoot(bytes32 _merkleRoot)", + "function s_merkleRoot() view returns (bytes32)", +]); + +const KEY = process.env.ORACLE_PRIVATE_KEY || ""; + +async function sync() { + const account = privateKeyToAccount(KEY); + + const client = createWalletClient({ + account, + chain: mainnet, + transport: http(), + }); + + const root = await getMerkleRoot(43114); + + const tx = await client.writeContract({ + address: "0x", + abi: abi, + functionName: "setMerkleRoot", + args: [root], + }); + + console.log(tx); +} + +sync().catch(console.error); diff --git a/tsconfig.json b/tsconfig.json index cb7fa8c..bfc2afc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,6 +21,11 @@ // Skip type checking for node modules "skipLibCheck": true }, - "include": ["./**/*.ts", "src/sync/merkle.cjs", "src/sync/utils.cjs"], + "include": [ + "./**/*.ts", + "src/sync/merkle.mjs", + "src/sync/utils.cjs", + "src/sync/sync.cjs" + ], "exclude": ["node_modules"] }