[SOLVED] EthOnline hackathon help; created token; how to get totalSupply

const options = { chain: ‘rinkeby’, address: ‘0x6E33d7d875C21D37283ABC13F53195C9428B26d3’ }
const rinkebyBalance = await Moralis.Web3API.account.getTokenBalances(options);
console.log((rinkebyBalance.balance / 1e18).toFixed(5) + " ETH");
console.log(rinkebyBalance.balance);

let content = 
`
<table class="table">
<thead>
    <tr>
        <th scope="col">Chain</th>
        <th scope="col">Balance</th>
    </tr>
</thead>
<tbody>
    <tr>
        <th>Rinkeby</th>
        <td>${(rinkebyBalance.balance / 1e18).toFixed(5) + " BRGT"}</td>
    </tr>
</tbody>
</table>  
`
displayBraineumNetDaoLink.innerHTML = content;`Preformatted text`
1 Like

@rbensonevans, it looks like that contract was only created, https://rinkeby.etherscan.io/address/0x6E33d7d875C21D37283ABC13F53195C9428B26d3
it doesn’t have any eth balance, what do you expect to see there?

you can use Moralis.Web3API.account.getNativeBalance(options) to get the native balance, that it will return 0 ETH.

I get NaN. Isn’t token balance different from native “eth” balance??

Yes, those are totally different things, Moralis.Web3API.account.getTokenBalances will return the list of tokens for that address and native eth balance you can get it with Moralis.Web3API.account.getNativeBalance

How do I get the totalSupply. To get a balance on the token I would have to provide and account?

in order to get the totalSupply, you have to call a function on that contract, you need to know its ABI, and you can use this function from Moralis SDK: https://docs.moralis.io/moralis-server/web3/web3#executefunction

I don’t know what you mean by ‘to get a balance on the token’

I see. I’ve only created the token contract. I would need to sent an amount to some address to create the “token”.
But actually what I wanted was the totalSupply.

abi_totalSupply = [{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]

options_5 = {
                contractAddress: "0x6E33d7d875C21D37283ABC13F53195C9428B26d3",
                functionName: "totalSupply",
                abi: abi_totalSupply                
            };

total_supply = await Moralis.executeFunction(options_5);

=>
‘10000000000000000000000000000’

You’ll also need to have metamask connected to rinkeby network when you run this

Oh wow great. Will test it now. Had to get breakfast.

This worked. Thanks.

I did the following and I’m prompted to authenticate each time via Metamask.
Is there a way to do this only once in the code???

options_5 = {
contractAddress: “0x6E33d7d875C21D37283ABC13F53195C9428B26d3”,
functionName: “totalSupply”,
abi: abi_totalSupply
};
await Moralis.authenticate().then(async function (user) {
total_supply = await Moralis.executeFunction(options_5);

I think that you don’t need to authenticate for this, you can remove that authentication part from there

Thanks again. I removed the authenticate. This works.

await Moralis.enable();
total_supply = await Moralis.executeFunction(options_5);
console.log((total_supply / 1e18).toFixed(5) + " BRVT");

1 Like