Can a smart contract run on every block?

After a contract has been uploaded, when will the script run?

  • Only on a certain block specified when initialized?
  • Only when triggered by some user-action, e.g. when asset sent to the contract address?
  • On every X blocks where X is specified when initialized?
  • On every single block?
  • Other?

Interesting questions. I think lookups (“current block height”) should be possible (if not directly, then indirectly), but soon we’ll be able to find out experimentally and before that it’s best to ask @rubensayshi.

A specific case I have in mind is a lottery. Every 100 blocks it makes a payout. If the script runs every block, it can simply begin with

if (CURRENT_BLOCK_HEIGHT % 100 != 0) return;

I.e. 99 out of 100 blocks it stops executing after this one line. Every 100th block it does the calculations.

Of course, a little bit of gas would be burned for every block but I suppose it’s so little it’s negligible (?)

no it can’t,

you can get the block height (and timestamp) in a contract, the way the example EVM contracts that desire similar functionality (ie; a crowdfund) work is that the contract is ‘pinged’ by executing a method and then the contract has stored the last block height when it ran and it loops from there onwards.

though it could be an interesting feature to create a contract that runs every block and pays gas from XCP that it owns, not sure why it’s not already in the EVM.

1 Like