I’m trying to fetch the current user address into ABI and to output the token balance.
Stuck on this for two days straight now, reached the far lands of github, circled google - any advice ?
import { useMoralis, useMoralisWeb3Api } from "react-moralis"
import {useEffect, useState} from "react"
function AVAXToken() {
const web3api = useMoralisWeb3Api();
const [balance, setBalance] = useState(0);
//const user = Moralis.User.current();
//const { account, isAuthenticated } = useMoralis();
//const [address, setAddress] = useState();
//const { Moralis, chainId } = useMoralis();
//const web3 = await Moralis.enableWeb3();
const { user } = useMoralis();
const ABI = [{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}];
const options = {
chain: "avalanche",
function_name: "balanceOf",
address: "0x95c8c21c261e3855b62f45121197c5a533a8a4a3",
abi: ABI,
params: {account: user.get("ethAddress")} //Here
}
useEffect(() = {
web3api.native.runContractFunction(options).then(result = {
setBalance(result)
})
},[]);
const ABI2 = [
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]
const options2 = {
chain: "avalanche",
function_name: "totalSupply",
address: "0x95c8c21c261e3855b62f45121197c5a533a8a4a3",
abi: ABI2
}
const [supply, setSupply] = useState(0);
useEffect(() = {
web3api.native.runContractFunction(options2).then(result = {
setSupply(result);
})
},[])
return (
<div className="text-center p-5"
<div{`My Balance: ${balance}`}</div
<div{`Token supply: ${supply}`}</div
<div{`My percentage: ${balance/supply*100}%`}</div
</div
)
}
export default AVAXToken