1

I have the call working for a non-gas execution.

This code below does not seem to run. Should I add account in the call?

let addContract = async (inputValue) => {
    var abi=
  [{"constant":true,"inputs":[],"name":"contractWeight","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"jsonInput","type":"string"},{"name":"hashKey","type":"bytes32"}],"name":"addContract","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"weight","type":"uint256"}],"name":"addWeight","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"contracts","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]

    var web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/'));
    var contractaddress= "0xb316b4bf742818d3fb97586a2f3aea361b240f30";

    var hashVal = web3.utils.sha3(inputValue);
    console.log(hashVal);

    let commodityContract = new web3.eth.Contract(abi, contractaddress);
  //  let newContract =   commodityContract.at(this.state.contractAddress);
  //  this.setState({commodityContract: commodityContract});
    web3.eth.defaultAccount = web3.eth.coinbase;
  //  var testResult = commodityContract.contracts(test1);
    var result='';
    commodityContract.methods.addContract(inputValue,hashVal).call().then(
       (result) => console.log(result)
     );

    console.log(commodityContract);

}
  • You probably want send, not call. – user19510 Apr 2 '18 at 2:20
  • Yes send and also the account to charge the gas should be given. I am unsure how that is done. – Trevor Lee Oakley Apr 2 '18 at 5:21
  • I believe web3.eth.defaultAccount should take care of that. You can use send({ from: '0x123abc...' }) if you want to override the default. – user19510 Apr 2 '18 at 5:22
  • I am just checking into this. I was wondering about security. I am using Ropsten. So I am unsure what the default account will be. The system needs to prevent theft so there must be some kind of key system. – Trevor Lee Oakley Apr 2 '18 at 5:40
  • Oh, sorry, I see you're using Infura. You can't just use send then, because Infura doesn't know the private key for your account. (That has nothing to do with Ropsten vs. the main network, it's just that you're using a public node.) If you want to use a public node directly, you'll need to sign transactions locally (with a private key) and then send them via sendSignedTransaction. – user19510 Apr 2 '18 at 5:54
1

You can do it adding few parameters in call():

    const response2 = await contract.methods
       .createNewCertificate("11111111","gdsfgdsfgdsfg","gdsfgdsfg",["gdsfgdsfg"],["gdsfgdsfg","11111111111"])
       .send({
            from:accounts[0], 
            gasPrice : this.state.web3.utils.toWei("5", "gwei"), 
            gas : this.state.web3.utils.toWei("0.0000000000005", "ether")});

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.