Skip to content

Commit

Permalink
fix: scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
royvardhan committed Jun 28, 2024
1 parent 8f87400 commit 14b5762
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/sync/merkle.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand All @@ -21,6 +22,4 @@ function encodeLeaf(tokenId, points) {
return leaf;
}

getMerkleRoot(43114).catch((error) => console.error(error));

module.exports = getMerkleRoot;
6 changes: 3 additions & 3 deletions src/sync/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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",
Expand All @@ -33,4 +33,4 @@ async function sync() {
console.log(tx);
}

sync().catch(console.error);
sync(chainId).catch(console.error);
22 changes: 17 additions & 5 deletions src/sync/utils.cjs
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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}
;`;
Expand All @@ -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(
Expand 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;
}

Expand Down

0 comments on commit 14b5762

Please sign in to comment.