Skip to content

Commit

Permalink
Merge pull request #12 from dOrgTech/voting-changes
Browse files Browse the repository at this point in the history
Delegate Voting power and DAO balance support
  • Loading branch information
Man-Jain authored Sep 20, 2023
2 parents 237ccab + 402078c commit d541aa2
Show file tree
Hide file tree
Showing 11 changed files with 641 additions and 245 deletions.
319 changes: 166 additions & 153 deletions components/choices/index.js

Large diffs are not rendered by default.

185 changes: 122 additions & 63 deletions components/daos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { getTokenMetadata } = require("../../services");
const { getInputFromSigPayload } = require("../../utils");

const dbo = require("../../db/conn");
const { response } = require("express");

const getAllLiteOnlyDAOs = async (req, response) => {
const { network } = req.body;
Expand Down Expand Up @@ -36,7 +37,7 @@ const getAllLiteOnlyDAOs = async (req, response) => {
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Error retrieving the list of communities ",
message: error.message,
});
}
};
Expand Down Expand Up @@ -71,7 +72,7 @@ const getDAOFromContractAddress = async (req, response) => {
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Error retrieving the community for that DAO ",
message: error.message,
});
}
};
Expand All @@ -89,74 +90,130 @@ const getDAOById = async (req, response) => {
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Community not found ",
message: error.message,
});
}
};

const createDAO = async (req, response) => {
const { payloadBytes } = req.body;
const updateTotalCount = async (req, response) => {
const { id } = req.params;
const { count } = req.body;
try {
let db_connect = dbo.getDb();
let data = {
$set: {
votingAddressesCount: count,
},
};
let communityId = { _id: ObjectId(id) };
const res = await db_connect
.collection("DAOs")
.updateOne(communityId, data, { upsert: true });

const values = getInputFromSigPayload(payloadBytes);
let db_connect = dbo.getDb();

const mongoClient = dbo.getClient();
const session = mongoClient.startSession();

const original_id = ObjectId();

const tokenAddress = values.tokenAddress;
const tokenID = values.tokenID;

const tokenData = await getTokenMetadata(
tokenAddress,
values.network,
tokenID
);

let DAOData = {
name: values.name,
description: values.description,
linkToTerms: values.linkToTerms,
picUri: values.picUri,
members: values.members,
polls: values.polls,
tokenAddress: values.tokenAddress,
tokenType: tokenData.standard,
requiredTokenOwnership: values.requiredTokenOwnership,
allowPublicAccess: values.allowPublicAccess,
_id: original_id,
network: values.network,
daoContract: values?.daoContract,
};
response.json(res);
} catch (error) {
console.log("error: ", error);
response.status(500).send({
message: "Community votingAddressesCount could not be updated ",
});
}
};

const updateTotalHolders = async (req, response) => {
try {
await session
.withTransaction(async () => {
const DAOCollection = db_connect.collection("DAOs");
const TokenCollection = db_connect.collection("Tokens");
// Important:: You must pass the session to the operations
await DAOCollection.insertOne(DAOData, { session });

await TokenCollection.insertOne(
{
tokenAddress,
tokenType: tokenData.standard,
symbol: tokenData.metadata.symbol,
tokenID: Number(tokenID),
daoID: original_id,
decimals: Number(tokenData.metadata.decimals),
let db_connect = dbo.getDb();
const DAOCollection = db_connect.collection("DAOs");

const result = await DAOCollection.find({}).forEach(function (item) {
DAOCollection.updateOne(
{ _id: ObjectId(item._id) },
{
$set: {
votingAddressesCount: item.members ? item.members.length : 0,
},
{ session }
);
})
.then((res) => response.json(res));
} catch (e) {
result = e.Message;
console.warn(result);
await session.abortTransaction();
} finally {
await session.endSession();
}
);
});
response.json(result);
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: error.message,
});
}
};

const createDAO = async (req, response) => {
const { payloadBytes } = req.body;

try {
const values = getInputFromSigPayload(payloadBytes);
let db_connect = dbo.getDb();

const mongoClient = dbo.getClient();
const session = mongoClient.startSession();

const original_id = ObjectId();

const tokenAddress = values.tokenAddress;
const tokenID = values.tokenID;

const tokenData = await getTokenMetadata(
tokenAddress,
values.network,
tokenID
);

let DAOData = {
name: values.name,
description: values.description,
linkToTerms: values.linkToTerms,
picUri: values.picUri,
members: values.members,
polls: values.polls,
tokenAddress: values.tokenAddress,
tokenType: tokenData.standard,
requiredTokenOwnership: values.requiredTokenOwnership,
allowPublicAccess: values.allowPublicAccess,
_id: original_id,
network: values.network,
daoContract: values?.daoContract,
votingAddressesCount: values.votingAddressesCount,
};

try {
await session
.withTransaction(async () => {
const DAOCollection = db_connect.collection("DAOs");
const TokenCollection = db_connect.collection("Tokens");
// Important:: You must pass the session to the operations
await DAOCollection.insertOne(DAOData, { session });

await TokenCollection.insertOne(
{
tokenAddress,
tokenType: tokenData.standard,
symbol: tokenData.metadata.symbol,
tokenID: Number(tokenID),
daoID: original_id,
decimals: Number(tokenData.metadata.decimals),
},
{ session }
);
})
.then((res) => response.json(res));
} catch (e) {
result = e.Message;
console.log(e);
await session.abortTransaction();
} finally {
await session.endSession();
}
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: error.message,
});
}
};

Expand Down Expand Up @@ -196,7 +253,7 @@ const joinDAO = async (req, response) => {
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Could not join community",
message: error.message,
});
}
};
Expand All @@ -207,4 +264,6 @@ module.exports = {
getDAOById,
createDAO,
joinDAO,
updateTotalHolders,
updateTotalCount,
};
34 changes: 29 additions & 5 deletions components/polls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {
getInputFromSigPayload,
getCurrentBlock,
getTotalSupplyAtCurrentBlock,
getUserBalanceAtLevel,
getUserTotalVotingPowerAtReferenceBlock,
} = require("../../utils");

const ObjectId = require("mongodb").ObjectId;
Expand All @@ -20,7 +22,7 @@ const getPollById = async (req, response) => {
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Error retrieving poll",
message: error.message,
});
}
};
Expand All @@ -41,7 +43,7 @@ const getPollsById = async (req, response) => {
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Error retrieving the list of polls",
message: error.message,
});
}
};
Expand Down Expand Up @@ -74,19 +76,40 @@ const addPoll = async (req, response) => {
const dao = await db_connect
.collection("DAOs")
.findOne({ _id: ObjectId(values.daoID) });
if (!dao) {
throw new Error("DAO Does not exist");
}

const token = await db_connect
.collection("Tokens")
.findOne({ tokenAddress: dao.tokenAddress });
if (!token) {
throw new Error("DAO Token Does not exist in system");
}

const block = await getCurrentBlock(dao.network);
const total = await getTotalSupplyAtCurrentBlock(
dao.network,
dao.tokenAddress,
token.tokenID,
block
token.tokenID
);

const userVotingPowerAtCurrentLevel =
await getUserTotalVotingPowerAtReferenceBlock(
dao.network,
dao.tokenAddress,
dao.daoContract,
token.tokenID,
block,
values.author
);

if (userVotingPowerAtCurrentLevel.eq(0) && dao.requiredTokenOwnership) {
throw new Error(
"User Doesnt have balance at this level to create proposal"
);
}

if (!total) {
await session.abortTransaction();
}
Expand Down Expand Up @@ -132,13 +155,14 @@ const addPoll = async (req, response) => {
result = e.Message;
console.log(e);
await session.abortTransaction();
throw new Error(e);
} finally {
await session.endSession();
}
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Could not create new poll",
message: error.message,
});
}
};
Expand Down
46 changes: 44 additions & 2 deletions components/tokens/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This will help us connect to the database
const dbo = require("../../db/conn");
const { getUserTotalVotingPowerAtReferenceBlock } = require("../../utils");

const ObjectId = require("mongodb").ObjectId;

Expand All @@ -23,7 +24,7 @@ const addToken = async (req, response) => {
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Error saving token",
message: error.message,
});
}
};
Expand All @@ -41,12 +42,53 @@ const getTokenById = async (req, response) => {
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: "Error retrieving community token",
message: error.message,
});
}
};

const getVotingPowerAtLevel = async (req, response) => {
const { network, address, tokenID } = req.params;
const { userAddress, level } = req.query;

try {
let db_connect = dbo.getDb();

const TokensCollection = db_connect.collection("Tokens");
const DAOCollection = db_connect.collection("DAOs");

let tokenAddress = { tokenAddress: address };
const token = await TokensCollection.findOne(tokenAddress);

if (!token) {
throw new Error("Could not find token");
}

let daoId = { _id: ObjectId(token.daoID) };
const dao = await DAOCollection.findOne(daoId);

const daoContract = dao?.daoContract;

const votingWeight = await getUserTotalVotingPowerAtReferenceBlock(
network,
address,
daoContract,
tokenID,
level,
userAddress
);

response.json({ votingWeight });
} catch (error) {
console.log("error: ", error);
response.status(400).send({
message: error.message,
});
}
};

module.exports = {
addToken,
getTokenById,
getVotingPowerAtLevel,
};
Loading

0 comments on commit d541aa2

Please sign in to comment.