2

I am trying to send the args on function from owner account but getting Returned error.

I am using web3 1.0 .

Function in contract :

/**
* @dev Adds single address to whitelist.
* @param trusted Address to be added to the whitelist
*/
function addToWhiteList(address trusted) public onlyOwner {
    require(!whitelist[trusted]);
    whitelist[trusted] = true;

    emit UserWhitelist(true);
}

Code on index.html :

// Add user on whitelist
abc.methods.addToWhiteList("0x9DA80947ACf5Fc16299311EdAabc37fF7d201C95")
.send({ from: "0x61A44075419C4402f6DE631341d875Ece6A3922e" })
.on("receipt", function(receipt) {
  console.log(receipt);
})
.on("error", function(error) {
  console.error(error);
});

ERROR : Returned error: The method eth_sendTransaction does not exist/is not available

Note : I add web3 in script tag :

<script language="javascript" type="text/javascript" src="web3.min.js"></script>

and init. like this :

web3 = new Web3(new Web3.providers.WebsocketProvider("wss://ropsten.infura.io/ws"));
5
  • Your code seems to be perfect. Maybe something is broken with Inura. I hope you are unlocking seders add before sending tx. May 16, 2018 at 19:20
  • @PrashantPrabhakarSingh, I didn't get you. I am sending tx. from owner account and I add the owner public key in from . May 17, 2018 at 3:54
  • You need to unlock the owner's account. Check personal.unlock(owner) May 17, 2018 at 5:07
  • @PrashantPrabhakarSingh, I tried but didn't get any success. Can you please let me know in separate answer so I can accept your answer also. May 17, 2018 at 5:36
  • Infura doesn't support sendTransaction you have to sign your own transactions and use sendSignedTransaction, or use a plugin like MetaMask to sign transactions.
    – Ismael
    May 17, 2018 at 16:53

1 Answer 1

1

I am not sure if this is the reason. But try unlockking account before sending transaction. The senders account needs to be unlocked. Generally you get please unlock your account error. But may be this is not allowing you to send tx. A sample code looks like:

    custom_func.prototype.SENDCNK=function(from,to,password,amount){
    return new Promise(function(fullfill,reject){
        /* add basic checks like if user has sufficient balance , address are valid etc*/
        web3.eth.personal.unlockAccount(from, password).then(function(result,e){
            console.log("Account "+from+" unlocked successfully");
            amount = parseFloat(amount);
            amount = amount *decimalMultiplier;
            cnkContract.methods.transfer(to ,amount).send({from:from})
                .on('transactionHash',function(txHash){
                    console.log("Transaction hash : "+txHash);
                    fullfill(txHash);
                }).catch(function(error){
                    console.log("Unable to send transaction. Error:"+error);
                    logger.error(error);
                    reject("Sending transaction on blockchain failed");
                });        
       }).catch(function(error){
            logger.error("Incorrect transaction password for account: "+from);
            reject("Incorrect transaction password.");
       });
    });    
}
5
  • I tried like this : web3.eth.personal.unlockAccount("0x61A44075419C4402f6DE631341d875Ece6A3922e", "12egjec308").then(function(result,e) { console.log("Account unlocked successfully"); }); but still getting error : Returned error: The method personal_unlockAccount does not exist/is not available May 17, 2018 at 6:10
  • The reason for this is you need you need to enable perosnal api over rpc (by default this is disabled for security reasons). When you set-up geth you enable this under rpc flag like --rpcapi web3,eth,net,personal,db. If you don't enable this you cann't access personal methods. Since you are using infura node, check if they have enabled the same. May 17, 2018 at 8:12
  • If you are doing testing and all, I shall recommed setup a private node which will be under yiur control to test your app better. You can check my medium post here for same. May 17, 2018 at 8:14
  • Thanks for your words ! But thing is that want to test on infura node ? May 17, 2018 at 8:32
  • Then you need to check if they are exposing personal over rpc. May 17, 2018 at 9:14

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