Skip to content

Commit

Permalink
Merge pull request #23 from dOrgTech/xtz-polls
Browse files Browse the repository at this point in the history
logic for xtz weighted polls
  • Loading branch information
fabiolalombardim authored Feb 9, 2024
2 parents 3bbfd90 + efdc805 commit cb50719
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
8 changes: 2 additions & 6 deletions components/choices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ const updateChoiceById = async (req, response) => {
dao.daoContract,
token.tokenID,
block,
address
address,
poll.isXTZ
);

if (!total) {
Expand Down Expand Up @@ -151,9 +152,6 @@ const updateChoiceById = async (req, response) => {
try {
await session.withTransaction(async () => {
const coll1 = db_connect.collection("Choices");
const coll2 = db_connect.collection("Polls");

// await coll2.updateOne({_id: poll._id}, { $set: { "votes" : values.length }})
// Important:: You must pass the session to the operations
await coll1.updateOne(
{ _id: ObjectId(oldVote._id) },
Expand Down Expand Up @@ -199,8 +197,6 @@ const updateChoiceById = async (req, response) => {
await session
.withTransaction(async () => {
const coll1 = db_connect.collection("Choices");
const coll2 = db_connect.collection("Polls");

await coll1.updateOne(
{
_id: choice._id,
Expand Down
2 changes: 2 additions & 0 deletions components/polls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const addPoll = async (req, response) => {
externalLink,
endTime,
votingStrategy,
isXTZ
} = values;

const author = getPkhfromPk(publicKey);
Expand Down Expand Up @@ -166,6 +167,7 @@ const addPoll = async (req, response) => {
choices: choicesPoll,
author,
votingStrategy,
isXTZ
};

let data = {
Expand Down
15 changes: 13 additions & 2 deletions components/tokens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,21 @@ const getVotingPowerAtLevel = async (req, response) => {
daoContract,
tokenID,
level,
userAddress
userAddress,
isXTZ = false
);

response.json({ votingWeight });
const votingXTZWeight = await getUserTotalVotingPowerAtReferenceBlock(
network,
address,
daoContract,
tokenID,
level,
userAddress,
isXTZ = true
);

response.json({ votingWeight, votingXTZWeight });
} catch (error) {
console.log("error: ", error);
response.status(400).send({
Expand Down
91 changes: 60 additions & 31 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ const getUserBalanceAtLevel = async (
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" });

if (response.status === 200) {
const result = response.data;
if (result) {
return new BigNumber(result);
} else {
return new BigNumber(0);
}
}

return new BigNumber(0);
};

const getUserDAODepositBalanceAtLevel = async (
accountAddress,
network,
Expand Down Expand Up @@ -117,49 +137,57 @@ const getUserTotalVotingPowerAtReferenceBlock = async (
daoContract,
tokenID,
level,
userAddress
userAddress,
isXTZ = false
) => {
try {
let userVotingPower = new BigNumber(0);

const isTokenDelegation = await isTokenDelegationSupported(
network,
address
);

if (isTokenDelegation) {
const userVotePower = await getUserTotalVotingWeightAtBlock(
if (!isXTZ) {
const isTokenDelegation = await isTokenDelegationSupported(
network,
address,
level,
userAddress
address
);
if (!userVotePower) {
throw new Error("Could Not get voting weight");
if (isTokenDelegation) {
const userVotePower = await getUserTotalVotingWeightAtBlock(
network,
address,
level,
userAddress
);
if (!userVotePower) {
throw new Error("Could Not get voting weight");
}
userVotingPower = userVotingPower.plus(userVotePower);
} else {
const selfBalance = await getUserBalanceAtLevel(
network,
address,
tokenID,
level,
userAddress
);
userVotingPower = userVotingPower.plus(selfBalance);

if (daoContract) {
const userDAODepositBalance = await getUserDAODepositBalanceAtLevel(
userAddress,
network,
daoContract,
level
);
userVotingPower = userVotingPower.plus(userDAODepositBalance);
}
}
userVotingPower = userVotingPower.plus(userVotePower);

return userVotingPower;
} else {
const selfBalance = await getUserBalanceAtLevel(
const selfBalance = await getUserXTZBalanceAtLevel(
network,
address,
tokenID,
level,
userAddress
);
userVotingPower = userVotingPower.plus(selfBalance);

if (daoContract) {
const userDAODepositBalance = await getUserDAODepositBalanceAtLevel(
userAddress,
network,
daoContract,
level
);
userVotingPower = userVotingPower.plus(userDAODepositBalance);
}
return userVotingPower.plus(selfBalance);
}

return userVotingPower;
} catch (error) {
console.log("error: ", error);
throw error;
Expand Down Expand Up @@ -200,4 +228,5 @@ module.exports = {
getUserTotalVotingPowerAtReferenceBlock,
getUserBalanceAtLevel,
getTokenHoldersCount,
getUserXTZBalanceAtLevel
};

0 comments on commit cb50719

Please sign in to comment.