Menu

Monthly Archives: August 2016

So, now that we have a draw and a lottery orchestrator to keep track of it all, the next task was to put this all together so that, at regular intervals (say once a week), the draw is drawn and either winners paid or the pot rolled over to next week’s draw. To do that you have to execute the following actions in sequence:

Create a new draw for next week

Run the current draw, i.e. select a winning number and pay the winners if there are any

Transfer the pot (if there are no winners) to next week’s draw

Add the address of the new draw to the lottery orchestrator

Sleep until next week and do it all again!

All of the above are asynchronous operations on the Ethereum Blockchain, so it is necessary to wait for confirmation of one thing to execute the next one. In the following code, we do step 1, but only execute step 2 when the callback comes with confirmation that the contract has been created. And steps 3 and 4 are inside an Event callback that confirms that a winning number has been selected and the winners paid:

Automating it all

So how do you get this to happen without having to remember to do it yourself, and bearing in mind that it requires you to run code from within a geth console? To solve that problem we put all the code we wanted to run in a script and then used Expect to run it. Expect is a programme that “talks” to other interactive programmes according to a script. Our script is:

Finally, all you do is add a line to your crontab to execute the script once a week at midnight (or whenever)

0 0 * * 0 /home/path/to/weekly_deploy.exp >> /tmp/expect.txt

Why this is OK, but not perfect

This works fine but a confession is required: When we set out we wanted to create a totally trust-less lottery, i.e. one where you did not have to trust any third party to ensure that the lottery would run according to the rules.
By doing the above, you need to trust us that this script will run at the appointed time. And we need to trust that the people who supply the server where we keep the script will continue to supply it.
So it does somewhat break the rules.
But the alternative, to have contracts that knew when to run themselves and that knew how to spawn copies of themselves in order to transfer the pot and keep the thing going, was too hard for our current abilities. So it will have to wait, for now…
Once Lottereo is live we will run the scripts, we promise!