I want to write a gambling smart contract, that will be based on time (on block number in the ethereum blockchain).

What is the best way to do some work when current network block will archive some number? For example, I want to close the current round and send all awards to winners when current network block will archive 6320132 number?

  • "block will archive 6320132 number" <-- you can't guarantee that, miners might not include your transaction at this block number if , for example, you use lower transaction fee
    – Nulik
    Sep 12 '18 at 20:55

You have to do it without relying on anything that triggers at that moment.

You use the block.number to decide if bets are allowed.

function isOpen() {
  return(block.number <= deadline);

function bet( ... {

You determine a winner retroactively.

function didIWin( ... {
  if(!isOpen()) return false;
  return winner() == msg.sender;

The tricky part is creating a fair game. This might involve concealing moves from other players or trying to introduce randomness. Those are non-trivial challenges.

function winner() { ...

Hope it helps.


Using block.number will return the current block number.

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.