From 14b57625fcf28eb2e91932bd78be51cd08318ab3 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Fri, 28 Jun 2024 11:28:59 +0530 Subject: [PATCH] fix: scripts --- src/sync/merkle.cjs | 3 +-- src/sync/sync.js | 6 +++--- src/sync/utils.cjs | 22 +++++++++++++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/sync/merkle.cjs b/src/sync/merkle.cjs index 58ca759..ad773d4 100644 --- a/src/sync/merkle.cjs +++ b/src/sync/merkle.cjs @@ -6,6 +6,7 @@ const getCombinedPoints = require("./utils.cjs"); async function getMerkleRoot(chainId) { const data = await getCombinedPoints(chainId); + console.log("Generating Merkle Root........"); const leaves = Object.keys(data).map((tokenId) => { return encodeLeaf(tokenId, data[tokenId]); }); @@ -21,6 +22,4 @@ function encodeLeaf(tokenId, points) { return leaf; } -getMerkleRoot(43114).catch((error) => console.error(error)); - module.exports = getMerkleRoot; diff --git a/src/sync/sync.js b/src/sync/sync.js index 199f33e..e4a1a40 100644 --- a/src/sync/sync.js +++ b/src/sync/sync.js @@ -12,7 +12,7 @@ const abi = parseAbi([ const KEY = process.env.ORACLE_PRIVATE_KEY || ""; -async function sync() { +async function sync(chainId) { const account = privateKeyToAccount(KEY); const client = createWalletClient({ @@ -21,7 +21,7 @@ async function sync() { transport: http(), }); - const root = await getMerkleRoot(43114); + const root = await getMerkleRoot(chainId); const tx = await client.writeContract({ address: "0x", @@ -33,4 +33,4 @@ async function sync() { console.log(tx); } -sync().catch(console.error); +sync(chainId).catch(console.error); diff --git a/src/sync/utils.cjs b/src/sync/utils.cjs index 33565a9..9a585f3 100644 --- a/src/sync/utils.cjs +++ b/src/sync/utils.cjs @@ -1,8 +1,14 @@ require("dotenv").config(); const postgres = require("postgres"); -const sql = postgres(process.env.DATABASE_URL); +const sql = postgres( + "postgres://postgres:puAvmeeWK5FBzbSFUED7TplDe2Ez8r3BVhesojTtwB4GKtxkCdpXRjXDCSZRdCks@108.61.156.15:5432/postgres" +); async function getAllPoints(chainId) { + console.log( + "Fetching all points from the database for the chainId: ", + chainId + ); const userIdPoint = await sql` SELECT "userDataId", "points" FROM public."Points" @@ -24,8 +30,9 @@ async function getTokenIdByUserId(userId) { } async function getClaimedPoints(chainId) { + console.log("Fetching claimed points from the database"); const claimedPoints = await sql` - SELECT "tokenId", "claimedPoints" + SELECT "tokenId", "pointsClaimed" FROM public."TokenIdData" WHERE "chainId" = ${chainId} ;`; @@ -34,6 +41,7 @@ async function getClaimedPoints(chainId) { } async function getCombinedPoints(chainId) { + console.log("Fetching combined points for the chainId: ", chainId); const allPoints = await getAllPoints(chainId); const pointsWithTokenId = await Promise.all( @@ -54,15 +62,19 @@ async function getCombinedPoints(chainId) { return acc; }, {}); - const claimedPoints = await getClaimedPoints(); + const claimedPoints = await getClaimedPoints(chainId); claimedPoints.forEach((data) => { - const { tokenId, claimedPoints } = data; + const { tokenId } = data; if (combinedPointByTokenId[tokenId]) { - combinedPointByTokenId[tokenId] -= claimedPoints; + combinedPointByTokenId[tokenId] -= data.pointsClaimed; } }); + console.log( + "Successfully fetched combined points for the chainId: ", + chainId + ); return combinedPointByTokenId; }