Skip to content

Commit

Permalink
Improve utils-eth
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshw3 committed Aug 28, 2024
1 parent cb7dfd0 commit 313db2b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
Binary file added bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion components/daos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const getAllLiteOnlyDAOs = async (req, response) => {
}).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)
Expand Down Expand Up @@ -164,6 +163,7 @@ const updateTotalCount = async (req, response) => {
dao.network,
token.tokenAddress,
);
console.log(`Token holder count for ${token.tokenAddress} is ${count}`)
} else {
count = await getTokenHoldersCount(
dao.network,
Expand Down
17 changes: 14 additions & 3 deletions components/tokens/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// This will help us connect to the database
const mongoose = require("mongoose");
const mongodb = require("mongodb");
const dbo = require("../../db/conn");
const TokenModel = require("../../db/models/Token.model");
const DAOModel = require("../../db/models/Dao.model");
const { getUserTotalVotingPowerAtReferenceBlock } = require("../../utils");
const { getEthTokenMetadata } = require("../../utils-eth");

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

const ObjectId = mongodb.ObjectId;
const addToken = async (req, response) => {
const { daoID, tokenID, symbol, tokenAddress } = req.body;

Expand Down Expand Up @@ -35,7 +37,16 @@ const getTokenById = async (req, response) => {
const { id } = req.params;

try {
const result = await TokenModel.findOne({daoId: ObjectId(id)})
const result = await TokenModel.findOne({daoID: id}).lean()
if(result.tokenType === "ERC20") {
const linkedDao = await DAOModel.findById(result.daoID)
const tokenMeta = await getEthTokenMetadata(linkedDao?.network, result.tokenAddress)
if(tokenMeta) {
result.holders = tokenMeta?.holders
}else{
console.log(tokenMeta, linkedDao.address, result.tokenAddress)
}
}
response.json(result);
} catch (error) {
console.log("error: ", error);
Expand Down
5 changes: 5 additions & 0 deletions pm2.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
name: "homebase-api",
script: "server.js",
interpreter: "~/.bun/bin/bun",
};
57 changes: 38 additions & 19 deletions utils-eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,43 @@ const tokenAbiForErc20 = [
}
]

// ⚠️ To be Fixed
function _getEthProvider(network) {
return new JsonRpcProvider("https://node.ghostnet.etherlink.com");
// return new JsonRpcProvider("https://eth-sepolia.blockscout.com");
const rpcEndpoint = network.includes("test") ? "https://node.ghostnet.etherlink.com" : "https://node.mainnet.etherlink.com";
return new JsonRpcProvider(rpcEndpoint);
}

function _getEthRestEndpoint(network) {
const rpcEndpoint = network.includes("test") ? "https://testnet.explorer.etherlink.com/api/v2" : "https://node.mainnet.etherlink.com";
return rpcEndpoint;
}

async function _getEthTokenMetadataWithRpc(network, tokenAddress) {
const provider = _getEthProvider(network);
const tokenContract = new ethers.Contract(tokenAddress, tokenAbiForErc20, provider);
const symbol = await tokenContract.symbol();
const decimals = await tokenContract.decimals();
const name = await tokenContract.name();
const totalSupply = await tokenContract.totalSupply();
return {
name,
decimals: Number(decimals),
symbol,
totalSupply: BigNumber(totalSupply).toString(),
};
}

async function _getEthTokenMetadataWithRest(network, tokenAddress) {
const url = _getEthRestEndpoint(network)
const response = await fetch(`${url}/tokens/${tokenAddress}`)
const data = await response.json()
return {
name: data?.name,
decimals: data?.decimals,
symbol: data?.symbol,
totalSupply: data?.total_supply,
holders: data?.holders,
}
}
// ⚠️ To be Implemented
function verityEthSignture(signature, payloadBytes) {

Expand Down Expand Up @@ -360,19 +391,7 @@ function verityEthSignture(signature, payloadBytes) {

// ✅ Working
async function getEthTokenMetadata(network, tokenAddress) {
const provider = _getEthProvider(network);
const tokenContract = new ethers.Contract(tokenAddress, tokenAbiForErc20, provider);
const symbol = await tokenContract.symbol();
const decimals = await tokenContract.decimals();
const name = await tokenContract.name();
const totalSupply = await tokenContract.totalSupply();

return {
name,
decimals: Number(decimals),
symbol,
totalSupply: BigNumber(totalSupply).toString(),
};
return await _getEthTokenMetadataWithRest(network, tokenAddress)
}

// ✅ Working
Expand Down Expand Up @@ -432,9 +451,9 @@ async function getEthTokenHoldersCount(network, tokenAddress, block = 0) {
return holders.size;
}

getEthTokenMetadata("sepolia", "0x336bfd0356f6babec084f9120901c0296db1967e").then(console.log)
// getEthTokenMetadata("etherlink_testnet", "0x336bfd0356f6babec084f9120901c0296db1967e").then(console.log)

// getEthTokenHoldersCount("sepolia","0x336bfd0356f6babec084f9120901c0296db1967e").then(console.log)
// getEthTokenHoldersCount("etherlink_testnet","0x336bfd0356f6babec084f9120901c0296db1967e").then(console.log)

// ✅ Working
// getEthTotalSupply("sepoplia","0x336bfd0356f6babec084f9120901c0296db1967e").then((x)=>console.log("Total Suplpy",x))
Expand All @@ -445,7 +464,7 @@ getEthTokenMetadata("sepolia", "0x336bfd0356f6babec084f9120901c0296db1967e").the


// ✅ Working
// getEthCurrentBlock("sepolia").then(console.log)
// getEthCurrentBlock("etherlink_testnet").then(console.log)

console.log("from ETH")

Expand Down

0 comments on commit 313db2b

Please sign in to comment.