[Ethereum] Can you call a contract function via a cron job

contract-invocation

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)

Best Answer

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: web3.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.

Related Topic