Skip to content

Commit fcfb452

Browse files
authored
Merge pull request #35 from dOrgTech/feat/staked-balance
feat:Include Staked Balance
2 parents 7cb1d4b + 27f481c commit fcfb452

7 files changed

Lines changed: 41 additions & 6 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ bun.lockb
3737

3838
# Local Netlify folder
3939
.netlify
40+
41+
# Claude
42+
.claude/
43+
44+
# Misc
45+
deno.lock

components/choices/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ const updateChoiceById = async (req, response) => {
260260

261261
if (isVoted.length > 0) {
262262
const oldVoteObj = isVoted[0].walletAddresses.find(x => x.address === address);
263-
oldVote = await ChoiceModel.findById(oldVoteObj.choiceId);
263+
// isVoted[0] is already the Choice document containing the old vote
264+
oldVote = isVoted[0];
264265

265-
const oldSignaturePayload = oldVote.walletAddresses[0].payloadBytes
266+
const oldSignaturePayload = oldVoteObj?.payloadBytes;
266267
if (oldSignaturePayload) {
267268
const oldSignatureDate =
268269
getTimestampFromPayloadBytes(oldSignaturePayload);

db/mongoose-connection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ async function connectToMongoose() {
4444

4545
module.exports = { connectToMongoose };
4646

47+

netlify/functions/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ exports.handler = async (event, context) => {
1818
return serverlessHandler(event, context);
1919
};
2020

21+

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
"mongodb-memory-server": "^9.1.4",
4747
"nodemon": "^3.1.0",
4848
"supertest": "^6.3.3"
49-
}
49+
},
50+
"packageManager": "pnpm@9.12.2+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228"
5051
}

services/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const networkNameMap = {
99
};
1010

1111
const rpcNodes = {
12-
mainnet: "https://mainnet.api.tez.ie",
13-
ghostnet: "https://ghostnet.smartpy.io",
12+
mainnet: "https://rpc.tzkt.io/mainnet",
13+
ghostnet: "https://rpc.tzkt.io/ghostnet",
1414
};
1515

1616
const getTokenMetadata = async (contractAddress, network, tokenId) => {

utils.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ const getUserBalanceAtLevel = async (
7777
return new BigNumber(0);
7878
};
7979

80+
const getUserXTZBalanceAtLevelViaRpc = async (network, level, userAddress) => {
81+
const rpcUrl = rpcNodes[network];
82+
const url = `${rpcUrl}/chains/main/blocks/${level}/context/contracts/${userAddress}/full_balance`;
83+
84+
try {
85+
const response = await axios({ url, method: "GET" });
86+
if (response.status === 200) {
87+
return new BigNumber(response.data);
88+
}
89+
} catch (error) {
90+
// RPC failed (likely historical block not available), fall back to TzKT
91+
console.warn(`RPC failed for block ${level}, falling back to TzKT API`);
92+
return await getUserXTZBalanceAtLevel(network, level, userAddress);
93+
}
94+
95+
return new BigNumber(0);
96+
};
97+
8098
const getUserXTZBalanceAtLevel = async (network, level, userAddress) => {
8199
const url = `https://api.${network}.tzkt.io/v1/accounts/${userAddress}/balance_history/${level}`;
82100
const response = await axios({ url, method: "GET" });
@@ -173,11 +191,18 @@ const getUserTotalVotingPowerAtReferenceBlock = async (
173191

174192
return userVotingPower;
175193
} else {
176-
const selfBalance = await getUserXTZBalanceAtLevel(
194+
const selfBalance = await getUserXTZBalanceAtLevelViaRpc(
195+
network,
196+
level,
197+
userAddress
198+
);
199+
const selfBalanceLegacy = await getUserXTZBalanceAtLevel(
177200
network,
178201
level,
179202
userAddress
180203
);
204+
console.log("selfBalance: ", selfBalance);
205+
console.log("selfBalanceLegacy: ", selfBalanceLegacy);
181206
return userVotingPower.plus(selfBalance);
182207
}
183208
};

0 commit comments

Comments
 (0)