Is it possible to schedule function calls to an Ethereum smart contract?

by ThePiachu   Last Updated November 08, 2018 21:27 PM

I am wondering - is it possible to schedule or somehow trigger function calls to an Ethereum smart contract based on the current block height or timestamp? This would involve some pre-payment of gas for execution understandably, but it might be beneficial to have something like:

  • If current block height is 1'000'000, send 1000 ether to the block miner
  • If it's 2016-01-01 00:00:00, release funds from escrow

Of course those functions could be triggered by transactions at an appropriate time, but some sort of "execution scheduling" might be a useful automation of the process.



Answers 5


It is not in "the" ethereum spec, so no. But it is a feature that has been discussed for "ethereum 2.0" or similar. It is possible that there may even be a hard fork that will enable it in the then-existing network, but there are none currently planned.

Nikolai
Nikolai
May 29, 2015 23:04 PM

This feature can be easily realised outside the Ethereum virtual machine (and thus keeping the VM simple): Create a (reusable) contract where contracts can register to be called at a certain time (and provide a reward as compensation for the gas costs). A script can poll this contract from outside and call it at the appropriate times. The contract could be similar to the following.

contract CallbackRegistry
{
  struct Entry { uint time; address addr; bytes data; uint reward; }
  mapping(bytes32 => Entry) entries;
  function register(uint _time, address _addr, bytes _data) external returns (bytes32 _hash)
  {
    _hash = sha3(_time, _addr, _data);
    entries[_hash].time = _time;
    entries[_hash].addr = _addr;
    entries[_hash].data = _data;
    entries[_hash].reward = msg.value;
  }
  function invoke(bytes32 _hash) external
  {
    if (entries[_hash].time > 0 && now >= entries[_hash].time)
    {
      msg.sender.send(entries[_hash].reward);
      entries[_hash].addr.call(entries[_hash].data);
      delete entries[_hash];
    }
  }     
}
chriseth
chriseth
May 31, 2015 14:56 PM

It is not, sadly. There was a proposal for an ALARM opcode, but it was marked WONTFIX -

https://github.com/ethereum/go-ethereum/issues/117

I can see why - timers require that you keep your contract topped up with Ether, and making them too easy to create could litter the blockchain with them and impose an overhead on miners.

You either have to have an external caller for your function (as in chriseth's solution), or use a PHP-style "poor man's cron" function that you call from every function in the contract to check and see which periodic tasks should fire -

https://stackoverflow.com/questions/1857741/run-php-script-every-5-10-minutes-without-cron-perl-etc

This kind of solution can also be seen in LambdaMOO code, where events are triggered periodically only when someone speaks, but that's a bit more esoteric. ;-)

Rob Myers
Rob Myers
June 21, 2015 00:29 AM

You can do this using http://www.ethereum-alarm-clock.com/

It's a contract on the network that facilitates call scheduling by providing a financial incentive for independent parties to execute them for you. Disclaimer that I'm the author of the service.

Piper Merriam
Piper Merriam
September 23, 2015 23:41 PM

The simplest way to do it is using Ethereum Alarm Clock. It's on mainnet and integrated in MyCrypto wallet. Here's tutorial to accomplish scheduling based on block height / timestamp, as you mentioned in the question: https://medium.com/@MyCrypto/schedule-your-ethereum-transactions-now-on-mycrypto-92a6305f7dcc.

Daniel Kmak
Daniel Kmak
November 08, 2018 20:35 PM

Related Questions


Updated September 03, 2017 05:27 AM

Updated July 28, 2017 09:27 AM

Updated October 07, 2017 03:27 AM

Updated October 25, 2017 15:27 PM

Updated December 27, 2017 17:27 PM