0

I've this contract that gets 'address' type variable as input parameter to function and sets it to a variable. This works fine in Remix. I'm trying to call this using web3js and am getting an Uncaught Error: invalid address error. Is there any work arounds / best design practices to get this done?

Contract:

mapping (address => bool) public lakeApprover;

function addForestApprover(address _forestApprover) public onlyOwner {
    forestApprover[_forestApprover] = true;
}

Web3js function

function addLakeApprover() {

    var MyContract = web3.eth.contract(abi);
    var myContractInstance = MyContract.at(contractAddress);

    var accountToAdd = document.getElementById('idAccount').value;      
    var accountToSubmit = web3.personal.unlockAccount(web3.eth.accounts[0],'',300);

    var txHash = myContractInstance.addForestApprover(accountToAdd, {'from':accountToSubmit,'gas':47000}, 
        function (err, res) { 
        if(!err) {
            console.log("Error: " + err + "\n Response: " + res); 
            var txnReceipt = web3.eth.getTransactionReceipt(res);
            document.getElementById("idHashDetails").innerHTML = "<br> Block Number: " + txnReceipt.blockNumber
                                                                + "<br> Status: " + txnReceipt.status
                                                                //+ "<br> Status-ascii: " + web3.toAscii(txnReceipt.status)
                                                                + "<br> Transaction Hash: " + JSON.stringify(res);
                                                                ;
        }
    });
}   

Hardcoded value and console print

    function addLakeApprover() {

        var MyContract = web3.eth.contract(abi);
        var myContractInstance = MyContract.at(contractAddress);

        var accountToAdd = "0x0B4a064e35f51aB6884E90B8F3953074a8C7F292";
        var accountToSubmitNOC = web3.personal.unlockAccount(web3.eth.accounts[0],'',300);

        console.log("accountToAdd value = "  + accountToAdd);
        var txHash = myContractInstance.addForestApprover ( accountToAdd, 
        {'from':accountToSubmitNOC,'gas':47000} , 
            function (err, res) { 
            if(!err) {
                console.log("Error: " + err + "\n Response: " + res); 
                }
        });
    }

Response for hardcoded value code

accountToAdd 0x0B4a064e35f51aB6884E90B8F3953074a8C7F292
web3.js:3914 Uncaught Error: invalid address
    at inputAddressFormatter (web3.js:3914)
    at inputTransactionFormatter (web3.js:3740)
    at web3.js:5015
    at Array.map (<anonymous>)
    at Method.formatInput (web3.js:5014)
    at Method.toPayload (web3.js:5040)
    at Eth.send [as sendTransaction] (web3.js:5065)
    at SolidityFunction.sendTransaction (web3.js:4124)
    at SolidityFunction.execute (web3.js:4207)
    at addLakeApprover (Admin_screen.html:39)
  • @Ismael is right, the unlockAccount function doesn't return the unlocked account because unlocking accounts is done inside your node. just use the account you unlocked web3.eth.accounts[0] – Kaki Master Of Time Apr 19 '18 at 15:45
  • @Ismael 'from':accountToSubmit <- is the owner account that created contract and since there is a state change, a from and gas values are provided. I'm trying to 'add' the 'accountToAdd' (which is selected from dropdown in html page) into the lakeApprover variable in contract. – Matheswaran Kanagarajan Apr 19 '18 at 15:53
  • Can you confirm the web3 version you are using? – Prashant Prabhakar Singh Apr 19 '18 at 16:39
  • 1
    I assume accountToAdd doesn't have the value you expect it to. Please log the value and take a look. (Share here if you can.) Try hardcoding var accountToAdd = '0x123abc...'; to confirm. – user19510 Apr 19 '18 at 17:03
  • 1
    (Try using web3.eth.accounts[0] as the from address instead.) – user19510 Apr 20 '18 at 15:45

Your Answer

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

Browse other questions tagged or ask your own question.