Solidity Contract:

contract MyContract {
    bytes32 public product;
    uint public amount;

    function setOrder(bytes32 _product, uint _reorderAmount,) public returns (bool success);   

Javascript web3js (without node.js):

<script src="./web3.min.js"></script>
web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/key"));
web3.eth.defaultAccount = "0x...address of my account...";
var myContract = web3.eth.contract([...abi...]);
var contractInstance = myContract.at('0x...contract address...');

This works when I have ganache-cli running, but not on a testnet (e.g. Ropsten). On a testnet I can only getter functions, but how do I call a setter function? What am I missing here?

  • what is the siloContract ? and what version of web3 you are using ? May 11 '18 at 12:23
  • This is from the original contract, changed it to MyContract. I am using 0.20.6
    – sunwarri0r
    May 11 '18 at 12:30
  • 1
    Your JS code look okey, but your contract method doesn't have a body ? You would need ether in your account(coinbase) to be able to call transactions on ropsten. May 11 '18 at 12:37

Transactions have to be signed with the private key of the sending account.

You're using Infura as your web3 provider and are asking it to send a transaction for you. It can't do that because it doesn't know the private key for your account.

You'll need to either write code to sign the transaction yourself or (the more typical approach) use a web3 provider like MetaMask in the browser. MetaMask holds the user's private keys and signs transactions on their behalf (after prompting for approval).

  • Thank you for the answer. I want to sign it, but I don't know how. Do you have a link with info or can you please put more details into your answer?
    – sunwarri0r
    May 12 '18 at 6:17

According to the doc, you have to change the contractInstance.setOrder(web3.toHex('ProductName'),3000); method to

Send a transaction: (Your case)

contractInstance.setOrder.sendTransaction(web3.toHex('ProductName'),3000, { from: ethaddress }, function(err, result){ ... })

If you want to just call a function which does not update the state (and cost nothing):

contractInstance.product.call(param1, , function(err, result){ ... }) 
  • Thank you. But my web3 (v0.20.6) doesn't show me methods after I write the instance into a browser console. Also don't I have to provide a key somehow to sign a state changing transaction? from: ethaddress can't be enough or is it?
    – sunwarri0r
    May 11 '18 at 12:37
  • Oh sorry, i thought that you were using web3 > 1, I updated my comment according to your version
    – oktapodia
    May 11 '18 at 13:27
  • Thanks, but where do I sign the transaction? I don't use metamask, only infura and I have to sign the data somehow
    – sunwarri0r
    May 12 '18 at 6:46

You need to use Web3js to sign your transaction on client side before sending it to Infura:

      to: contractInstance.address,
      data: contractInstance.methods.setOrder.encodeABI(
      gas: 100000

For more details see documentation

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.