From 929c792d1debc8e9bf50eb0603f50f4e8185d8a2 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Date: Sat, 15 Nov 2025 17:46:05 +0000 Subject: [PATCH 1/3] added userXTZBAlanceAtLevelviaRPC --- db/mongoose-connection.js | 1 + netlify/functions/api.js | 1 + package.json | 3 ++- utils.js | 22 +++++++++++++++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/db/mongoose-connection.js b/db/mongoose-connection.js index 16ea510..d7f5ae4 100644 --- a/db/mongoose-connection.js +++ b/db/mongoose-connection.js @@ -44,3 +44,4 @@ async function connectToMongoose() { module.exports = { connectToMongoose }; + diff --git a/netlify/functions/api.js b/netlify/functions/api.js index 3bf1534..f8222c2 100644 --- a/netlify/functions/api.js +++ b/netlify/functions/api.js @@ -18,3 +18,4 @@ exports.handler = async (event, context) => { return serverlessHandler(event, context); }; + diff --git a/package.json b/package.json index 488e43c..e365f28 100644 --- a/package.json +++ b/package.json @@ -46,5 +46,6 @@ "mongodb-memory-server": "^9.1.4", "nodemon": "^3.1.0", "supertest": "^6.3.3" - } + }, + "packageManager": "pnpm@9.12.2+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228" } diff --git a/utils.js b/utils.js index 8bbc48e..4422a4e 100644 --- a/utils.js +++ b/utils.js @@ -77,6 +77,19 @@ const getUserBalanceAtLevel = async ( return new BigNumber(0); }; +const getUserXTZBalanceAtLevelViaRpc = async (network, level, userAddress) => { + const rpcUrl = rpcNodes[network]; + const url = `${rpcUrl}/chains/main/blocks/${level}/context/contracts/${userAddress}/full_balance`; + console.log("url: ", url); + const response = await axios({ url, method: "GET" }); + + if (response.status === 200) { + return new BigNumber(response.data); + } + + return new BigNumber(0); +}; + const getUserXTZBalanceAtLevel = async (network, level, userAddress) => { const url = `https://api.${network}.tzkt.io/v1/accounts/${userAddress}/balance_history/${level}`; const response = await axios({ url, method: "GET" }); @@ -173,11 +186,18 @@ const getUserTotalVotingPowerAtReferenceBlock = async ( return userVotingPower; } else { - const selfBalance = await getUserXTZBalanceAtLevel( + const selfBalance = await getUserXTZBalanceAtLevelViaRpc( + network, + level, + userAddress + ); + const selfBalanceLegacy = await getUserXTZBalanceAtLevel( network, level, userAddress ); + console.log("selfBalance: ", selfBalance); + console.log("selfBalanceLegacy: ", selfBalanceLegacy); return userVotingPower.plus(selfBalance); } }; From fa2ffc846fdca0bc82a3cd5823d1af106f7046f6 Mon Sep 17 00:00:00 2001 From: eightrice Date: Thu, 4 Dec 2025 18:27:10 +0200 Subject: [PATCH 2/3] fix: use archive RPC node and add error handling for staked XTZ balance - Switch RPC nodes to rpc.tzkt.io (archive nodes with full history) - Add try/catch with fallback to TzKT API if RPC fails - Ensures staked XTZ is included in voting power via full_balance endpoint - Fixes voting power queries for proposals older than 3 days --- .gitignore | 6 ++++++ services/index.js | 4 ++-- utils.js | 17 +++++++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 336df45..d2de37f 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,9 @@ bun.lockb # Local Netlify folder .netlify + +# Claude +.claude/ + +# Misc +deno.lock diff --git a/services/index.js b/services/index.js index 1327bac..5f19099 100644 --- a/services/index.js +++ b/services/index.js @@ -9,8 +9,8 @@ const networkNameMap = { }; const rpcNodes = { - mainnet: "https://mainnet.api.tez.ie", - ghostnet: "https://ghostnet.smartpy.io", + mainnet: "https://rpc.tzkt.io/mainnet", + ghostnet: "https://rpc.tzkt.io/ghostnet", }; const getTokenMetadata = async (contractAddress, network, tokenId) => { diff --git a/utils.js b/utils.js index 4422a4e..665d2cb 100644 --- a/utils.js +++ b/utils.js @@ -80,13 +80,18 @@ const getUserBalanceAtLevel = async ( const getUserXTZBalanceAtLevelViaRpc = async (network, level, userAddress) => { const rpcUrl = rpcNodes[network]; const url = `${rpcUrl}/chains/main/blocks/${level}/context/contracts/${userAddress}/full_balance`; - console.log("url: ", url); - const response = await axios({ url, method: "GET" }); - - if (response.status === 200) { - return new BigNumber(response.data); + + try { + const response = await axios({ url, method: "GET" }); + if (response.status === 200) { + return new BigNumber(response.data); + } + } catch (error) { + // RPC failed (likely historical block not available), fall back to TzKT + console.warn(`RPC failed for block ${level}, falling back to TzKT API`); + return await getUserXTZBalanceAtLevel(network, level, userAddress); } - + return new BigNumber(0); }; From 27f481c35d9bd3ce3746c824780ec267604749d4 Mon Sep 17 00:00:00 2001 From: eightrice Date: Thu, 4 Dec 2025 18:54:25 +0200 Subject: [PATCH 3/3] fix: resolve null reference when re-voting on polls - Use existing Choice document from query instead of redundant findById - Get payloadBytes directly from wallet vote object with optional chaining - Fixes 400 error when users attempt to update their vote --- components/choices/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/choices/index.js b/components/choices/index.js index 376f5a0..c232ba8 100644 --- a/components/choices/index.js +++ b/components/choices/index.js @@ -260,9 +260,10 @@ const updateChoiceById = async (req, response) => { if (isVoted.length > 0) { const oldVoteObj = isVoted[0].walletAddresses.find(x => x.address === address); - oldVote = await ChoiceModel.findById(oldVoteObj.choiceId); + // isVoted[0] is already the Choice document containing the old vote + oldVote = isVoted[0]; - const oldSignaturePayload = oldVote.walletAddresses[0].payloadBytes + const oldSignaturePayload = oldVoteObj?.payloadBytes; if (oldSignaturePayload) { const oldSignatureDate = getTimestampFromPayloadBytes(oldSignaturePayload);