.getTokenPrice endpoint doesnt work when i try to fetch data from bigger addresses. i got error message that TypeError: Converting circular structure to JSON . its not working when i try to fetch price of 0xd8da6bf26964af9d7eed9e03e53415d37aa96045 this address and working fine when i fetch 0x9233d7ce2740d5400e95c1f441e5b575bdd38d82 this address. if you need code i can provide. reply me asap.
Hey @Web3Kid,
Can you provide the code for this to be tested out
Also let us know what inputs are you using, e.g. chains, exchanges, etc, so we can test it out on our end~
const Moralis = require("moralis").default;
const express = require("express");
const app = express();
const cors = require("cors");
const port = 8080;
require("dotenv").config();
app.use(cors());
app.get("/", (req, res) => {
res.send("Working!");
});
app.listen(port, () => {
console.log(`CryptoKing app is listening on port ${port}`);
});
Moralis.start({
apiKey: process.env.MORALIS_API_KEY,
});
// -----------------------GET NATIVE BALANCE OF USER ENDPOINT-----------------------------
const native_Balance = () => {
app.get("/nativeBalance", async (req, res) => {
// await Moralis.start({
// apiKey: process.env.MORALIS_API_KEY,
// });
try {
const { address, chain } = req.query;
const response = await Moralis.EvmApi.balance.getNativeBalance({
address: address,
chain: chain,
});
const nativeBalance = response.raw;
// GET TOKEN PRICE OF ERC20 TOKEN WITH THE WETH AND WMATIC
let nativeCurrency;
if (chain === "0x1") {
// Wrapped ETHEREUM Address (WETH)
nativeCurrency = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
} else if (chain === "0x89") {
// Wrapped MATIC Address (WMATIC)
nativeCurrency = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270";
// Binance address (BNB)
}else if (chain === "0x38") {
nativeCurrency = "0x3a0d9d7764FAE860A659eb96A500F1323b411e68"
// nativeCurrency = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
}
// ------------------GET TOKEN PRICE ENDPOINT--------------------
try {
const nativePrice = await Moralis.EvmApi.token.getTokenPrice({
address: nativeCurrency,
chain: chain,
});
// nativeBalance.usd = nativePrice.data.usdPrice;
nativeBalance.usd = nativePrice.raw.usdPrice;
// console.log("THIS IS RESPONSE raw:");
console.log("----------------USD PRICE OF 1ETH OR 1MATIC:" + nativePrice.raw.usdPrice);
} catch (eror) {
console.error(eror);
}
res.send(nativeBalance);
console.log(response.toJSON());
console.log("Native Balances is Running To FETCH BALANCE");
} catch (e) {
res.send(e);
}
});
};
// native_Balance();
// -----------------------DISPLAY USER'S TOKENS AND FILTERING TOKENS-----------------------------
const token_Balances = () => {
app.get("/tokenBalances", async (req, res) => {
try {
const { address, chain } = req.query;
const response = await Moralis.EvmApi.token.getWalletTokenBalances({
address: address,
chain: chain,
});
res.send(response);
console.log(response);
// console.log(response.raw);
// res.send(response);
// ---------------FILTER SCAM TOKENS--- --------------
// let tokens = response.raw;
// let legitTokens = [];
// // starting the loop to get all tokens
// for (let i = 0; i < tokens.length; i++) {
// const priceResponse = await Moralis.EvmApi.token.getTokenPrice({
// address: tokens[i].token_address,
// chain: chain,
// });
// console.log(priceResponse);
// // condition to fetch scam coin
// if (priceResponse.raw.usdPrice > 0.01) {
// tokens[i].usd = priceResponse.raw.usdPrice;
// legitTokens.push(tokens[i]);
// } else {
// console.log("💩 Coin");
// }
// }
// res.send(legitTokens);
console.log("Token Balances is Running to GET TOKENS");
} catch (e) {
console.error(e);
res.send(e);
}
});
};
// token_Balances();
// --------------------------------FETCH ERC 20 TOKEN TRANSFERS-------------------------------------
const token_transfers = () => {
app.get("/tokenTransfers", async (req, res) => {
try {
const { address, chain } = req.query;
const response = await Moralis.EvmApi.token.getWalletTokenTransfers({
chain: chain,
address: address,
});
const userTrans = response.raw.result;
let userTransDetails = [];
for (let i = 0; i < userTrans.length; i++) {
try {
const metaResponse = await Moralis.EvmApi.token.getTokenMetadata({
addresses: [userTrans[i].address],
chain: chain,
});
if (metaResponse.raw) {
userTrans[i].decimals = metaResponse.raw[0].decimals;
userTrans[i].symbol = metaResponse.raw[0].symbol;
userTransDetails.push(userTrans[i]);
} else {
console.log("no details for coin");
}
} catch (e) {
console.log(e);
}
}
res.send(userTransDetails);
console.log("Token Transfer is Running to Fetch TRANSFER HISTORY");
} catch (e) {
console.error(e);
res.send(e);
}
});
};
// token_transfers();
// --------------------------------Get wallet NFT-------------------------------------
const nft_Balance = () => {
app.get("/nftBalance", async (req, res) => {
try {
const { address, chain } = req.query;
const response = await Moralis.EvmApi.nft.getWalletNFTs({
chain: chain,
address: address,
});
res.send(response.raw);
console.log("NFT balance is Running to FETCH NFT");
} catch (e) {
res.send(e);
console.error(e);
}
});
};
// nft_Balance();
const apilimit = async () =>{
const response = await Moralis.EvmApi.utils.endpointWeights();
console.log(response?.toJSON());
}
function test() {
native_Balance();
token_Balances();
token_transfers();
nft_Balance();
// apilimit()
console.log("");
}
test();
Hi @Web3Kid
Thanks for sharing the code. May I know how to reproduce the error that you are seeing?
I tried making the following requests with the address you shared and it seems to return the resposne without any error.
http://localhost:8080/nativeBalance?address=0xd8da6bf26964af9d7eed9e03e53415d37aa96045&chain=0x1
// returns
{
"balance": "4119613592188184579410",
"usd": 1839.2597660674764
}
http://localhost:8080/nativeBalance?address=0x9233d7ce2740d5400e95c1f441e5b575bdd38d82&chain=0x89
// returns
{
"balance": "714561055631116018",
"usd": 0.7625801691489564
}
did you tried with my code ? i am getting error of TypeError: Converting circular structure to JSON and even when the code is working i cant get usd value of binance chain.try it with my code. also is there any timezone limit ? of moralis api ? cause it is not working sometimes
I used the same code which you shared, also working on BSC chain.
There are no limits in timezones. It should work on all the timezones similarly.
If you know which line is causing the error, can you try adding a console.log to debug more?
already did console log many times. I noticed when I use get token price i got TypeError: Converting circular structure to JSON.
I am not sure why I could not reproduce the error with the same code. Am I missing anything?
I think main problem is in when i fetch the price -priceResponse (getTokenPrice) line - and where i am filtering scam tokens. can you go through that code once ?
i usually get error when i comment out that part.
Hi @Web3Kid
Is it possible to share the updated code so we will be on same page. It is not clear which code to comment out.
// -----------------------DISPLAY USER'S TOKENS AND FILTERING TOKENS-----------------------------
const token_Balances = () => {
app.get("/tokenBalances", async (req, res) => {
// try {
const { address, chain } = req.query;
const response = await Moralis.EvmApi.token.getWalletTokenBalances({
address: address,
chain: chain,
});
// res.send(response);
// console.log(response);
// console.log(response.raw);
// res.send(response);
// ---------------FILTER SCAM TOKENS-----------------
let tokens = response.raw;
let legitTokens = [];
// starting the loop to get all tokens
for (let i = 0; i < tokens.length; i++) {
const priceResponse = await Moralis.EvmApi.token.getTokenPrice({
address: tokens[i].token_address,
chain: chain,
});
// console.log(priceResponse);
// condition to fetch scam coin
if (priceResponse.raw.usdPrice > 0.01) {
tokens[i].usd = priceResponse.raw.usdPrice;
legitTokens.push(tokens[i]);
} else {
console.log("💩 Coin");
}
}
res.send(legitTokens);
// res.send(response);
console.log("Token Balances is Running to GET TOKENS");
// } catch (e) {
// console.error(e);
// res.send(e);
// }
});
};
token_Balances();
message: 'No pools found with enough liquidity, to calculate the price'
This error mean that we cannot get price of the token as there are no pools available to get the price.
I can see this error now when running /tokenBalances
endpoint. Use try catch syntax to handle this error so the code will not break.
If there are no pools with enough liquidity for a token then we don’t return the price.
i tested on other portfolio tracker they show that this address have tokens with particular chain but when i use that address in my created tracker i got no pools error or circular json error.
Did you find any solution for these? i have to complete my project of my internship.
Hi, what error are you still getting? what token are you checking and on what chain?
i am facing error when i get the token price in usd. when i enter 0xd8da6bf26964af9d7eed9e03e53415d37aa96045 this address in my zapper clone it returns with error saying that circular json and 2nd error is no pools found with enough liquidity, to calculate the price …
I was not able to replicate the first error about “TypeError: Converting circular structure to JSON”.
For the second error about the liq pool, you can use try catch and skip the token if it doesnt have enough liquidity
can you provide me a practical implementation ?