0

I have my contract object and this has attributes like

contract.balanceOf
contract.myDividends
contract.totalSupply
contract.myTokens

I get an error this._eth.call is not a function when i try to call them like this

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



contract.buy
contract.sell
contract.withdraw
contract.reinvest

For above calls i get this error

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.sendTransaction (inpage.js:1)
    at u.execute (inpage.js:1)

Please have a look at my code ond tell me what i do wrong https://pastebin.com/wAsCeNTG

  • What is your web3 library version? You can find this out by running in the terminal npm view web3 version. – Miroslav Nedelchev Jan 17 at 21:23
  • Version = 1.2.5-rc.0 – Coding Joe Jan 17 at 21:29
0

I'm able to execute balanceOf and sell methods like this:

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

window.addEventListener('load', async function() {
    // Wait for loading completion to avoid race conditions with web3 injection timing.
    var web3;
    var globalState = {};

    console.log(web3, 'web3');
    if(window.ethereum) {
        web3 = new Web3(window['ethereum']);
        try {
            // Request account access if needed
            await window.ethereum.enable();

            web3.eth.getAccounts(function (error, accounts) {
                document.getElementById('account-address').innerHTML = accounts[0];
            });

            // Acccounts now exposed
            window.ethereum.on('accountsChanged', function () {
                web3.eth.getAccounts(function (error, accounts) {
                    document.getElementById('account-address').innerHTML = accounts[0];
                });

                window.ethereum.on('connect', function () {
                    //    console.log('connect');
                });
            });
        } catch (error) {
            console.error(error);
        }
    } else if (window.web3) {
        console.log(2);
        // Legacy dapp browsers...
        // Use Mist/MetaMask's provider.
        const web3 = window.web3;
    } 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');
        web3 = new Web3(provider);
    }

    // get token balance of current account
    function balanceOf() {
        web3.eth.getAccounts(function (error, accounts) {
            contract.balanceOf(accounts[0], function(err, result) {
                if(err) {
                    console.log(err, 'err');
                } else {
                    console.log(parseInt(result), 'result');
                }
            });
        });
    }
    balanceOf();

    // sell method
    function sell() {
        web3.eth.getAccounts(function (error, accounts) {
            contract.sell(555, {from: accounts[0], gas: 250000}, function(err, result) {
                if(err) {
                    console.log(err, 'err');
                } else {
                    console.log(parseInt(result), 'result');
                }
            });
        });
    }
    sell();
});

The rest methods you can create them by your just watch out for passing the correct parameters to the functions.

| improve this answer | |
  • Thanks, balanceOf works but it doesnt update when address is changed and about the sell button i worry about a bit later- there are so many things that ar confusing to me with this stupid ethereum web3- but thanks. – Coding Joe Jan 17 at 22:27
  • everythign i try myself i get some errors i never seen before and all those functions dont work either- i tried the same code for totalAGIBalance etc- this ethereum is some bullshit- this is unbelievable – Coding Joe Jan 17 at 22:44
  • @CodingJoe the update balance is not happening on account change, because you are not doing this logic at method which is listening for account change. I think you should dive a bit more into javascript basics and event handlers so you can build you dApp applications easier. – Miroslav Nedelchev Jan 18 at 12:30
  • i can get all these calls now to work in the console - but what i dont understand is why can i not access these variables like current logged in account? – Coding Joe Jan 18 at 12:43

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.