1

I created a contract (in the Ropsten Test Net) for the most basic example described in this article: http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html

pragma solidity ^0.4.0;

contract SimpleStorage {
    uint storedData;

    function set(uint x) {
        storedData = x;
    }

    function get() constant returns (uint) {
        return storedData;
    }
}

The following Javascript calls that contract:

var abi = [{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
var MyContract = web3.eth.contract(abi);
var myContractInstance = MyContract.at('0x51a5846DB9DEb1Cd72ccab36F825C06328a21b8C');

myContractInstance.set.call(1, function(error, result){
    if(!error) {
        console.log("#" + result + "#")
    } else {
        console.error(error);
    }
})

myContractInstance.get.call(function(error, result){
    if(!error) {
        console.log("##" + result + "##")
    } else {
        console.error(error);
    }
})

The "set" call does not work, because it requires "sendTransaction" instead of "call".

I have tried every combination of parameters in web3.eth.sendTransaction, and I continually get the following error message: "sendTransaction Invalid number of arguments to Solidity function"

I have read the API documentation (https://github.com/ethereum/wiki/wiki/JavaScript-API), which does not provide a good example for web3.eth.sendTransaction. Please do not provide just a theoretical explanation, or a requirement to install additional tools.

Please provide a specific javascript code example that has been tested, to show how I can use just javascript to execute the "set" function in the contract shown above.

2

You can directly call set function, like this :

myContractInstance.set(2,{from:eth.coinbase}, function(error, result){
    if(!error) {
        console.log("#" + result + "#")
    } else {
        console.error(error);
    }
})

Or call sendTransaction function, as follows:

myContractInstance.set.sendTransaction(1,{from:eth.coinbase}, function(error, result){
    if(!error) {
        console.log("#" + result + "#")
    } else {
        console.error(error);
    }
})

Hope it helps~

  • Why does your example show "from:eth.coinbase"? I am using Ropsten Test Net. – Tester Aug 31 '17 at 13:23
  • You can replace eth.coinbase with you account address , like this, myContractInstance.set(2,{from:"0x0b46c35d2e823f9b1e69ff616f9e9bf2d9d52dd0"}, function(error, result){ if(!error) { console.log("#" + result + "#") } else { console.error(error); } }) – BinGoBinBin Aug 31 '17 at 13:28
  • Is there any advantage of "set" vs. "set.sendTransaction"? Do both methods incur the same gas cost? – Tester Aug 31 '17 at 13:37
  • There is a 30 second lag between the "set" and the "get". Can you provide some simple JS code to show how my program can wait for the confirmation before executing the "get"? – Tester Aug 31 '17 at 13:39
  • "set.sendTransaction" costs more lesser. when calling set function, you must wait for being mined, only mined, this transaction would be valid. If your get depends on set logic, like this : – BinGoBinBin Aug 31 '17 at 13:48

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.