Skip to content

Commit

Permalink
WIP - Offchain DAO Almost Works.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshpw committed Aug 13, 2024
1 parent fdd9200 commit 821ddc2
Show file tree
Hide file tree
Showing 12 changed files with 885 additions and 393 deletions.
580 changes: 361 additions & 219 deletions components/choices/index.js

Large diffs are not rendered by default.

48 changes: 31 additions & 17 deletions components/daos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
getTokenHoldersCount,
} = require("../../utils");
const {
getEthTokenMetadata,
getEthTokenHoldersCount,
getEthCurrentBlock,
getEthUserBalanceAtLevel,
} = require("../../utils-eth");
Expand All @@ -22,22 +22,24 @@ const getAllLiteOnlyDAOs = async (req, response) => {
const network = req.body?.network || req.query.network;

// Implementation with Mongoose with go live with Etherlink
if(req.method === 'GET'){
if (req.method === 'GET') {
const sortOrder = req.query.order || "desc";
const allDaos = await DaoModel.find({network}).sort({
const allDaos = await DaoModel.find({ network }).sort({
_id: sortOrder
}).lean();

const allDaoIds = allDaos.map(dao => new mongoose.Types.ObjectId(dao._id));

const allTokens = await TokenModel.find({daoID: {$in: allDaoIds}}).lean();
const allTokens = await TokenModel.find({ daoID: { $in: allDaoIds } }).lean();
// console.log('All Tokens DAO', [...new Set(allTokens.map(token => token.daoID))])
// console.log('Found Tokens',allDaoIds, allTokens.length)

const results = allDaos.map(dao => {
const token = allTokens.find(token => token.daoID.toString() === dao._id.toString());
if (token) delete token._id;
// console.log('Token', token)
return {
_id: dao._id,
...dao,
...token
}
Expand Down Expand Up @@ -116,6 +118,11 @@ const getDAOFromContractAddress = async (req, response) => {

const getDAOById = async (req, response) => {
const { id } = req.params;
const daoDao = await DaoModel.findById(id);
console.log({ id, daoDao })
if (daoDao) {
return response.json(daoDao);
}

try {
let db_connect = dbo.getDb();
Expand Down Expand Up @@ -150,12 +157,19 @@ const updateTotalCount = async (req, response) => {
if (!token) {
throw new Error("DAO Token Does not exist in system");
}

const count = await getTokenHoldersCount(
dao.network,
token.tokenAddress,
token.tokenID
);
let count = 0;
if (dao.network?.startsWith("etherlink")) {
count = await getEthTokenHoldersCount(
dao.network,
token.tokenAddress,
);
} else {
count = await getTokenHoldersCount(
dao.network,
token.tokenAddress,
token.tokenID
);
}

let data = {
$set: {
Expand Down Expand Up @@ -200,14 +214,14 @@ const updateTotalHolders = async (req, response) => {
};

const createDAO = async (req, response) => {
const { payloadBytes, publicKey, } = req.body;
const { payloadBytes, publicKey, } = req.body;
const network = req.body.network
if(network && network?.startsWith("etherlink")) {
if (network && network?.startsWith("etherlink")) {
const payload = req.payloadObj;
const {
tokenAddress,
tokenID,
symbol:tokenSymbol,
symbol: tokenSymbol,
network,
name,
description,
Expand All @@ -230,14 +244,14 @@ const createDAO = async (req, response) => {
const address = publicKey

const block = await getEthCurrentBlock(network);
console.log({block})
console.log({ block })
const userBalanceAtCurrentLevel = await getEthUserBalanceAtLevel(
network,
address,
tokenAddress,
block,
);
console.log({userBalanceAtCurrentLevel})
console.log({ userBalanceAtCurrentLevel })

// if (userBalanceAtCurrentLevel.eq(0)) {
// throw new Error("User does not have balance for this DAO token");
Expand All @@ -259,7 +273,7 @@ const createDAO = async (req, response) => {
votingAddressesCount: 0,
};

console.log({ethDaoData})
console.log({ ethDaoData })
const createdDao = await DaoModel.create(ethDaoData);
const createdToken = await TokenModel.create({
tokenAddress,
Expand Down
Loading

0 comments on commit 821ddc2

Please sign in to comment.