0

I have initialized my contract and need to balanceOf the current logged in address

const contract = new web3.eth.contract(abi).at(address);

  contract.balanceOf(function (error, balance) {
        $('account-balance').html = balance;
     });

but now i have troubles accessing the functions within the contract.

I get this error for trying to get "balanceOf"

inpage.js:1 Uncaught Error: Invalid number of arguments to Solidity function
    at Object.InvalidNumberOfSolidityArgs (inpage.js:1)
    at u.validateArgs (inpage.js:1)
    at u.toPayload (inpage.js:1)
    at u.call (inpage.js:1)
    at u.execute (inpage.js:1)
    at exchange.js:59
3
  • You should pass to function balanceOf the account whose balance you seek to get. – goodvibration Jan 17 '20 at 19:00
  • how do i do that? – Coding Joe Jan 17 '20 at 19:04
  • i need the balance of the current logged-in address – Coding Joe Jan 17 '20 at 19:14
0

First get all the accounts:

const accounts = await web3.eth.getAccounts();

Then get balance of the first one, for example:

const balance = await web3.eth.getBalance(accounts[0]);

BTW: I used "await" in the example above, thus your function has to be an async one, e.g.

async function getUserBalance() {
  const accounts = await web3.eth.getAccounts();
  const balance = await web3.eth.getBalance(accounts[0]);
}

https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#getbalance

6
  • i dont need the balance from web3.eth - i need the token balance "balanceOf" the current logged in user/account. Please look at this code pastebin.com/H29q9px6 – Coding Joe Jan 17 '20 at 20:00
  • Sorry. Misunderstood. Does your solidity token contract have a "balanceOf" function? If yes: Check, what inputs this function expects. Should only be an address as @goodvibrations already wrote. If you found it use const nToken = await contract.methods.balanceOf(<user-address>).call(); – René K Jan 17 '20 at 20:12
  • contract.balanceOf('0x62f28320f688A7A4e0021c55d7ffD1acd770A133', function ( error) { $('#account-balance').html = balance; }); this gives me a eth_call is not a function error – Coding Joe Jan 17 '20 at 20:21
  • i should be able to pass in the accounts variable but neither works accounts variable nor the address itself. when pastin in accounts- it says accounts not defined- but i use the accounts variable further up in the code. SO i dont know whats the issue here pastebin.com/wAsCeNTG – Coding Joe Jan 17 '20 at 20:29
  • Can we see your Solidity code? The ABI alone is a bit hard (at least for me). – René K Jan 17 '20 at 20:33
0

Just for for you:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <div id="account-address"></div>
  <div id="number-of-token"></div>
  <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>
  <script>
    const address = '0xE462CbEE0cd420f6c199B0194B1D8D93Fb5e7720';
    const abi = [{ "constant": true, "inputs": [{ "name": "_customerAddress", "type": "address" }], "name": "dividendsOf", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "name", "outputs": [{ "name": "", "type": "string" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [{ "name": "_agiToSpend", "type": "uint256" }], "name": "calculateTokensReceived", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [{ "name": "_tokensToSell", "type": "uint256" }], "name": "calculateAgiReceived", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "onlyAmbassadors", "outputs": [{ "name": "", "type": "bool" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [{ "name": "", "type": "uint8" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [{ "name": "_amount", "type": "uint256" }, { "name": "_customerAddress", "type": "address" }, { "name": "_referredBy", "type": "address" }], "name": "buyFor", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "withdraw", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "sellPrice", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "stakingRequirement", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [{ "name": "_includeReferralBonus", "type": "bool" }], "name": "myDividends", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [{ "name": "_customerAddress", "type": "address" }], "name": "balanceOf", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [{ "name": "_amount", "type": "uint256" }, { "name": "_referredBy", "type": "address" }], "name": "buy", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "buyPrice", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "owner", "outputs": [{ "name": "", "type": "address" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [{ "name": "_amount", "type": "uint256" }], "name": "distribute", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "myTokens", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [{ "name": "", "type": "string" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [{ "name": "_toAddress", "type": "address" }, { "name": "_amountOfTokens", "type": "uint256" }], "name": "transfer", "outputs": [{ "name": "", "type": "bool" }], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [{ "name": "_symbol", "type": "string" }], "name": "setSymbol", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "getInvested", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [{ "name": "_name", "type": "string" }], "name": "setName", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalAgiBalance", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [{ "name": "maintenance", "type": "address" }], "name": "updateMaintenanceAddress2", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [{ "name": "maintenance", "type": "address" }], "name": "updateMaintenanceAddress1", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [{ "name": "_amountOfTokens", "type": "uint256" }], "name": "sell", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "exit", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalDonation", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "totalHolder", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [{ "name": "newOwner", "type": "address" }], "name": "transferOwnership", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "reinvest", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "payable": true, "stateMutability": "payable", "type": "fallback" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "customerAddress", "type": "address" }, { "indexed": false, "name": "price", "type": "uint256" }], "name": "onDistribute", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "customerAddress", "type": "address" }, { "indexed": false, "name": "incomingAGI", "type": "uint256" }, { "indexed": false, "name": "tokensMinted", "type": "uint256" }, { "indexed": true, "name": "referredBy", "type": "address" }, { "indexed": false, "name": "timestamp", "type": "uint256" }], "name": "onTokenPurchase", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "customerAddress", "type": "address" }, { "indexed": false, "name": "tokensBurned", "type": "uint256" }, { "indexed": false, "name": "agiEarned", "type": "uint256" }, { "indexed": false, "name": "timestamp", "type": "uint256" }], "name": "onTokenSell", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "customerAddress", "type": "address" }, { "indexed": false, "name": "agiReinvested", "type": "uint256" }, { "indexed": false, "name": "tokensMinted", "type": "uint256" }], "name": "onReinvestment", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "customerAddress", "type": "address" }, { "indexed": false, "name": "agiWithdrawn", "type": "uint256" }], "name": "onWithdraw", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "tokens", "type": "uint256" }], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "previousOwner", "type": "address" }, { "indexed": true, "name": "newOwner", "type": "address" }], "name": "OwnershipTransferred", "type": "event" }];
    window.addEventListener('load', async function () {
      // Wait for loading completion to avoid race conditions with web3 injection timing.
      if (window.ethereum) {
        console.log(1);
        window.web3 = new Web3(window['ethereum']);
        try {
          // Request account access if needed
          await window.ethereum.enable();
          getUserBalance();
        } catch (error) {
          console.error(error);
        }
      } else if (window.web3) {
        console.log(2);
        // Legacy dapp browsers...
        // Use Mist/MetaMask's provider.
        window.web3 = window.web3;
        console.log('Injected web3 detected.');
      } else {
        console.log(3);
        // Fallback to localhost; use dev console port by default...
        const provider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/9bee77f147884c73bb2852e269dacece');
        window.web3 = new Web3(provider);
        console.log('No web3 instance injected, using Local web3.');
      }
    });

    async function getUserBalance() {
      const web3 = window.web3;
      const accounts = await web3.eth.getAccounts(); //This is an array of all accounts. Usually you want the first one ([0]).
      document.getElementById('account-address').innerHTML = accounts[0];

      const contract = new web3.eth.Contract(abi, address); //Load contract
      const nToken = await contract.methods.balanceOf(accounts[0]).call(); //With call() you are just reading blockchain data
      document.getElementById('number-of-token').innerHTML = nToken;
    }
  </script>
</body>

</html>
4
  • i dont know, its not printing anything and i cant access any of that stuff in there – Coding Joe Jan 17 '20 at 21:01
  • i cant access accounts and i cant access nToken – Coding Joe Jan 17 '20 at 21:03
  • Updated my answer. I think you already have a good understanding of the basics. Nonetheless I would propose a web development crash course (HTML, CSS, JS) - possibly from "Traversy Media" or "Net Ninja" on YouTube. Once you feel more or less comfortable check out the YouTube channel "dApp University" for Ethereum dApp development. – René K Jan 17 '20 at 22:08
  • none of these courses cover UIs. I dont like none of their tutorials – Coding Joe Jan 17 '20 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.