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/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); 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/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 8bbc48e..665d2cb 100644 --- a/utils.js +++ b/utils.js @@ -77,6 +77,24 @@ 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`; + + 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); +}; + 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 +191,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); } };