1
votes

Presently, I have a smart contract successfully deployed to the Rinkeby testnet, I'm having trouble accessing the method in question using web3 version 1.0.

Here's my web3 code, which instantiates a contract instance and calls a contract method:

const contractInstance = new web3.eth.Contract(abiDefinition, contractAddress);
var value = web3.utils.toWei('1', 'ether')
var sentTransaction = contractInstance.methods.initiateScoreRetrieval().send({value: value, from: fromAddress})

console.log('event sent, now set listeners')

sentTransaction.on('confirmation', function(confirmationNumber, receipt){
  console.log('method confirmation', confirmationNumber, receipt)
})
sentTransaction.on('error', console.error);

And here is my smart contract, or rather a version of it stripped down to the relevant bits:

contract myContract {

  address private txInitiator;
  uint256 private amount;


  function initiateScoreRetrieval() public payable returns(bool) {
    require(msg.value >= coralFeeInEth);
    amount = msg.value;
    txInitiator = msg.sender;
    return true;
  }


}

I am not able to get to the console.log that is setting the event listeners on the web3 side, and I am not getting an error of any kind thrown. I'm certainly not getting the consoles from the actual event listeners. I am guessing something is wrong with the way I'm sending the transaction, but I think I am correctly following the pattern documented below: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

Does anyone have any insight on how to use web3 1.0 to make contract method calls correctly? Am I doing something wrong with how I'm passing options, etc.?

Thanks!

2
Change your event emitter to check for transactionHash and receipt. The callback for the transaction hash will be the first response you get back. If you get that, but never get a receipt, then your transaction isn’t being mined. I would also explicitly include gasPrice and gas values. Finally, have you confirmed on Etherscan that your contract was deployed correctly?Adam Kipnis

2 Answers

1
votes

I believe you forgot to specify your HttpProvider for your web3, thus you're not connecting to live Rinkeby network, and by default web3 is running on you local host, which is why even if you provide the right contract address, there's nothing there.

To connect to live network, I would strongly encourage you to use Infura Node by ConsenSys.

const Web3 = require("web3");
const web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io"));

Then by now, everything should work perfectly fine.

0
votes

First, you need to generate your transaction ABI using encodeABI(), here is an example:

let tx_builder = contractInstance.methods.myMethod(arg1, arg2, ...);
let encoded_tx = tx_builder.encodeABI();
let transactionObject = {
    gas: amountOfGas,
    data: encoded_tx,
    from: from_address,
    to: contract_address
};

Then you have to sign the transaction using signTransaction() using private key of sender. Later you can sendSignedTransaction()

web3.eth.accounts.signTransaction(transactionObject, private_key, function (error, signedTx) {
        if (error) {
        console.log(error);
        // handle error
        } else {
            web3.eth.sendSignedTransaction(signedTx.rawTransaction)
              .on('receipt', function (receipt) {
              //do something
             });
    }