Assuming there is a contract deployed at 0x332a5fbdfd0b3ba6dd1d35e4e3c86562024f737d whose Owner is 0x0136d0ccac4464f920eeab664e847f4e69bd5fcf. The contract has a ownerOnly method named incrementNumber(), how should we go about executing this method from a standalone Node.js server?

I'm using testrpc in mac for testing this - I have access to the private keys (not sure what the password of the accounts are in Testrpc)


With web3 v1 installed, you can try this:

let abi = fs.readFileSync("YourContract.abi").toString();
let contract = new web3.eth.Contract(JSON.parse(abi), CONTRACT_ADDRESS);
let transaction = contract.methods.yourMethod(yourArguments);

let options = {
    to  : transaction._parent._address,
    data: transaction.encodeABI(),
    gas : await transaction.estimateGas({from: OWNER_PUBLIC_KEY}),

let signedTransaction = await web3.eth.accounts.signTransaction(options, OWNER_PRIVATE_KEY);
let transactionReceipt = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);

Please note that for the last line, waiting for the transaction-receipt is not necessarily the best course of action. You might prefer to get the transaction-hash and then use it in order to wait for the transaction-receipt elsewhere in your server. See here for a more detailed description of this issue.


I presume the owner is one of the accounts generated by testrpc, let's say accounts[0]. These accounts are accessible from your web3 provider in your node.js script. For example you could have:

var Web3 = require('web3')
if (typeof web3 != 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
var accounts = web3.eth.accounts;
var owner = accounts[0];
[...] // fetch contract instance at given address
contract.method(arg1, arg2, {from: owner, gas: 999999});

where of course localhost:8545 should be the hostname and port your testrpc instance is listening to.

  • But doesn't that need private key / transaction signing? The setup will be deployed in a remote server and connecting to mainnet – Sam Oct 27 '17 at 14:17
  • you mentioned testrpc in your question, so that's what I assumed you were using; in that case no signing is required, as accounts come already unlocked. If you are using a regular ethereum client to connect to the mainnet, of course you need to have the private key to sign transactions; everything would work exactly in the same way except that you would need to (assuming you use geth) geth attach to it and personal.unlockAccount() with the password, or similarly for other clients. – manuhalo Oct 27 '17 at 14:29
  • how do we go about for signing a transaction? – Sam Oct 27 '17 at 14:42
  • it's done automatically by web3, provided you unlocked your account in your ethereum client. Alternatively you can use other libraries to sign a transaction manually, see this question: ethereum.stackexchange.com/questions/6368/… – manuhalo Oct 27 '17 at 15: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.