Skip to content

Commit

Permalink
Merge pull request #31 from dOrgTech/feat/offchain-dao-etherlink
Browse files Browse the repository at this point in the history
Feat/offchain dao etherlink
  • Loading branch information
EightRice authored Sep 26, 2024
2 parents 473edbd + 313db2b commit 6056e4f
Show file tree
Hide file tree
Showing 21 changed files with 1,975 additions and 406 deletions.
Binary file added bun.lockb
Binary file not shown.
580 changes: 361 additions & 219 deletions components/choices/index.js

Large diffs are not rendered by default.

132 changes: 122 additions & 10 deletions components/daos/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
const ObjectId = require("mongodb").ObjectId;

const mongoose = require("mongoose");
const { getTokenMetadata } = require("../../services");
const {
getInputFromSigPayload,
getCurrentBlock,
getUserBalanceAtLevel,
getTokenHoldersCount,
} = require("../../utils");
const {
getEthTokenHoldersCount,
getEthCurrentBlock,
getEthUserBalanceAtLevel,
getEthTokenMetadata,
} = require("../../utils-eth");

const dbo = require("../../db/conn");
const { response } = require("express");
const { getPkhfromPk } = require("@taquito/utils");
const DaoModel = require("../../db/models/Dao.model");
const TokenModel = require("../../db/models/Token.model");

const getAllLiteOnlyDAOs = async (req, response) => {
const { network } = req.body;
const network = req.body?.network || req.query.network;

// Implementation with Mongoose with go live with Etherlink
if (req.method === 'GET') {
const sortOrder = req.query.order || "desc";
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();
// 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
}
});

return response.json(results);
}

try {
let db_connect = dbo.getDb();
Expand Down Expand Up @@ -85,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 @@ -119,12 +157,20 @@ 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,
);
console.log(`Token holder count for ${token.tokenAddress} is ${count}`)
} else {
count = await getTokenHoldersCount(
dao.network,
token.tokenAddress,
token.tokenID
);
}

let data = {
$set: {
Expand Down Expand Up @@ -169,8 +215,74 @@ 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

// Creating Offchain DAO on Etherlink
if (network && network?.startsWith("etherlink")) {
const payload = req.payloadObj;
const {
tokenAddress,
symbol: tokenSymbol,
network,
name,
description,
linkToTerms,
picUri,
requiredTokenOwnership,
allowPublicAccess,
daoContract,
decimals
} = payload;

// const tokenData = await getEthTokenMetadata(tokenAddress, network);

const address = publicKey

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

// if (userBalanceAtCurrentLevel.eq(0)) {
// throw new Error("User does not have balance for this DAO token");
// }

const ethDaoData = {
name,
description,
linkToTerms,
picUri,
members: [address],
polls: [],
tokenAddress,
tokenType: "ERC20",
requiredTokenOwnership,
allowPublicAccess,
network,
daoContract,
votingAddressesCount: 0,
};

console.log({ ethDaoData })
const createdDao = await DaoModel.create(ethDaoData);
const createdToken = await TokenModel.create({
tokenAddress,
tokenType: "ERC20",
symbol: tokenSymbol,
daoID: createdDao._id,
decimals: Number(decimals),
});

return response.json({
dao: createdDao,
token: createdToken
})
}
try {
const values = getInputFromSigPayload(payloadBytes);
const {
Expand Down
Loading

0 comments on commit 6056e4f

Please sign in to comment.