0

An api will call the smart contract function. It should sign the transaction on its own instead of human intervention.

I think raw transaction can work here. Are there any other options?

Any help is much appreciated. Thanks!

--Edit-- The question is updated!

Shubham Chadokar
  • 339
  • 4
  • 15

2 Answers2

1

Try this (using web3.js v1):

let Web3 = require("web3");
let web3 = new Web3(NODE_ADDRESS);

async function send(transaction, reestimate = function(gas) {return gas;}) {
    let gas = await transaction.estimateGas({from: PUBLIC_KEY});
    let options = {
        to  : transaction._parent._address,
        data: transaction.encodeABI(),
        gas : reestimate(gas)
    };
    let signedTransaction = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
    return await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
}

Usage example #1:

let receipt = await send(myContract.methods.myFunc(arg1, arg2, arg3));
...

Usage example #2:

let reestimate = function(gas) {return gas * 2;};
let receipt = await send(myContract.methods.myFunc(arg1, arg2, arg3), reestimate);
...
goodvibration
  • 26,003
  • 5
  • 46
  • 86
0

A contract can not sign transaction by its own. The transaction must be signed by externally owned accounts. If a transaction is not signed by an externally owned account then it won't be included in the blockchain.

Soham Lawar
  • 2,567
  • 14
  • 38
  • This is why I'm pretty sure that this user doesn't really mean to ask how the contract can sign its own transaction, but how the (external) API should sign the transaction (even though he/she stated the opposite in response to your comment). An evidence to that is the fact that the question is tagged under web3.js. In addition, there's no sense in asking how to sign a transaction from the on-chain, because signing a transaction is performed from the off-chain by definition. – goodvibration Sep 14 '18 at 11:40
  • @goodvibration I agree with you. Even I was confused after reading the question and asked for more clarification. – Soham Lawar Sep 14 '18 at 12:51