3

I have the following function and don't want to manually sign the transaction every time it's called, how would I go about setting up a way of signing this transaction with an object. I've seen sendRawTransaction and account.signTransaction but can't figure out a way to implement these in my issue.

this.contractObject = this.contractObject = {from: this.account, gas:300000};

this.contract.methods.createCountry(arg1, 1rg2, arg3).send(this.contractObject, (err, res) => {
                if (err) {
                    throw err;
                } else {
                    console.log(res);
                }
            })

The following function gets added in a batch, I don't know if this will matter.

4

First, you need to generate your transaction, 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 can sign the transaction, using the address and the private key of this specific address. When it is signed, you can send it to the node:

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

Edit: After signing it, you don't have to send it. You can add it to the batch request.

  • This is exactly what I was looking for! I'll test the solution and get back at you with the result. – jasper May 17 '18 at 14:55
  • I still get an error on the web3.eth.sendSignedTransaction() function, the thing is that the error is really vague so I have no idea what is going on.. Do you have any idea? errors.js:29 Uncaught (in promise) Error: Returned error: {"id":14,"jsonrpc":"2.0","error":{"code":-32603}} – jasper May 18 '18 at 7:25
  • Is your node having an RPC endpoint opoenned? – Florian Castelain May 18 '18 at 10:29
  • I have ganache open and all other functions that make use of Ganache do work. – jasper May 18 '18 at 10:35
  • I created a new question for this issue: ethereum.stackexchange.com/questions/48807/… – jasper May 18 '18 at 10:36

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.