3

I'm trying to automate the calling of a contract function. Every day, I would like to call a function in one of my contract that makes data available to the users of my DApp. I would like to automate this so that I don't have to manually call the function my self. Is it possible to automate this process on a webserver with a cron job? If not is there a better way to do this (preferably without having to run a vps)

1

Yes you can automate this. There are many different approaches. I'm going to assume you need to issue transactions that interact with your contract for the purposes of the example.

There are several key parts to the system you're trying to build. Each part has plenty of choices, which means a combinatorial explosion of build options. It would help if you specified a bit more about the:

  • Node: are you running an Ethereum node already, like geth or parity?
  • Web3 Connector: choose from javascript, java, C#, ruby, python, etc...
  • Core Logic: which connector language do you prefer?
  • Scheduler: you already chose cron, great

Until we get those answers, I'll give an example with my preferred options:

  • Node: geth with a funded, unlocked account.*
  • Web3 Connector: web3utils.py, over an IPC connection (the default).
  • Core Logic: a python script that loads the data and issues a transaction
  • Scheduler: cron

*A funded, unlocked account means that any process running on your computer has the ability to drain your account. You can minimize risk by keeping the balance low enough that you wouldn't miss it if it were stolen. You only need enough to pay for gas.

0

If your call doesn't modify the contract state then it should be pretty straightforward, you only need access to a geth synchronized and running. If you do not want to deploy a geth you can use a third party service like etherscan or infura.

If your call does modify the contract it still can be done, but you have to find a way to manage the private key of your own account, and also manage the balance since each operation have a cost. You can use ethereumjs-tx to create a sign your transaction, and use sendRawTransacrion to broadcast to the network.

0

You can automate this task, and as carver said, the stack will be important as it will define what scheduler/cron job package you use.

I have done a similar kind of thing, will share my stack and approach: I used geth client, Nodejs, Web3js, forever and cron.

  • forever : A simple CLI tool for ensuring that a given script runs continuously (i.e. forever).
  • The NPM cron package (mentioned above) is really good and i like it a lot, as it gives a lot of customization options.

The below approach can help you in getting the function hex and passing the parameters to call the function:

Use the web3.js library which helps in doing the same. Just download web3.js using NPM (or anywhere else) and get the modules required for the task, by:

const_ = require('lodash');
const SolidityFunction = require('web3/lib/web3/function');

You can use the web3.js library along with your ABI to find the function definition, using the below code:

var ABI = JSON.parse(<your_ABI>);
var functionDef = new SolidityFunction('', _.find(ABI, { name: '<your_function_name>' }), '');

After this, you can call the method toPayload which will help you converting the values that you want to pass to your function into HEX Data.

var payloadData = functionDef.toPayload([<value_for_var_1>, <value_for_var_2>, <value_for_var_3>, <value_for_var_4>]).data;

payloadData can be used as Value for the DATA property.

Example:

var rawTx = {
 to: <to_address>,
 data: payloadData,
 value: '0x0',
 from: <from_address>,
 nonce: nonce,
 gasLimit: gasLimit,
 gasPrice: gasPrice
}

You have a raw transaction object, you can use ethereumjs-tx to sign your transaction and use web3.sendRawTransaction to call your Contract Function.

Hope this helps.

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.