I have an array of tokens fetched by useEvmWalletTokenBalances()
named tokens.
I then retrieve pricing and receive the error in the heading of this question.
I want to remove all tokens which fail on retrieving the price to be removed (tokens.splice(i,1))
however for some reason the try catch isn’t working for me. Could anyone fix my code to solve this?
const { fetch: native, data: nation } = useEvmNativeBalance()
const { fetch: tok } = useEvmTokenPrice()
const price = async () => {
const call = []
for (let i = 0; i < tokens?.length; i++) {
call.push({ chain: eth, include: "percent_change", address: tokens[i]?.token?.contractAddress })
}
const ethPrice = await tok({ chain: eth, include: "percent_change", address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" })
const ethe = [{ balance: Number(native?.balance?.ether), price: native?.usdPrice, name: "ETH", change: native?.["24hrPercentChange"] }]
setETH(ethe)
for (let i = 0; i < call?.length; i++) {
const tik = await tok(call[i])
console.log(tik)
tokens[i].change = tik?.["24hrPercentChange"]
tokens[i].pricy = tik?.usdPrice
}
var walletVal = 0
for (let i = 0; i < tokens.length; i++) {
walletVal +=tokens[i]?.pricy * Number(tokens[i].value)
}
console.log(walletVal)
setWalBal(walletVal + (Number(ethe[0]?.balance) * Number(ethe[0]?.price)))
}