1

I need to send transaction at a specified time (block number). How to write smart contract which fires at specified block number without input from outside?

Sergey
  • 11
  • 1
  • Related: https://ethereum.stackexchange.com/questions/42/how-can-a-contract-run-itself-at-a-later-time – eth May 14 '17 at 05:50

1 Answers1

3

You need input from the outside to initiate a transaction.

However, what you can do is check block.number in your contract to make sure that if a transaction is sent before the specified time, it will have no effect. (This transaction will use gas and be considered spent, so it the same transaction can't be sent again and you'll have to send a new one.)

You can then use a service like the Ethereum Alarm Clock to incentivize some random person on the internet to send the transaction for you at the right time, and receive a fee in return. This can be scheduled by a call from your contract.

There is also a plan to abstract out the initial transaction validation that happens before a transaction is included in a block, which in theory would make it possible to pre-prepare a transaction that is only allowed to be included in a block from a specific block.number and you wouldn't have to spend gas if a miner checked it and found out that the time hadn't come yet. This is how Bitcoin's nLockTime works: A transaction whose time how not yet come is simply considered invalid, and somebody can hang onto it and keep resending it at no cost until its time finally comes.

Note that there is no way, and no prospect of there being a way, to ensure that a transaction is included in a particular block. The miner of that block decides what transactions it will include. So your contract can enforce "don't run this logic unless it's exactly block x" or "don't run this logic unless it's after block x", but it can't enforce "this must be done at block x".

Edmund Edgar
  • 16,897
  • 1
  • 29
  • 58