How we set up a sports pool on the blockchain

Thomas de Wolf
Jul 3, 2018 · 9 min read

By now you’ve read about a “great” new blockchain application almost every day. Ranging from useful, like tracking containers in shipping to the almost ridiculous, like blockchain marriage apps. But from a developers perspective, how do you get started with an application? What needs to be considered and is there added value in using blockchain for your application?

For me (and perhaps many others), Bitcoin was introduced a couple of years ago as a really unnecessarily difficult way to buy pizza. A bit later the financial success stories of investing in cryptocurrencies started to appear and I started to read more about the underlying Distributed Ledger Technology. By now I run a company that specializes in blockchain software development and I get to see a lot of new developments and ideas. Unfortunately most of these ideas can not be shared openly, so we decided to build a Tour de France sports pool as an example and share the way it works here. With this article I hope to give some insight in blockchain software development related to a real world example.

A sports pool in the blockchain, why?

First of all we’ll have to introduce the example and perhaps some reasoning behind it. The added value of blockchain technology is often broken down into three points.

  1. Removing a central authority (the blockchain mediates)
  2. Security (through decentralization)
  3. Transparency (through an always visible ledger)

A blockchain sports pool therefore should give the participants more confidence in that what is happening is according to the rules and no fraud can be committed by the organizer. Of course we also see that this is not exactly a big issue in the sports pool market, but it does serve our demonstration purpose.

What we used to make this blockchain application

We’re going to focus on the Ethereum blockchain for this article, because at the moment this is the most popular for development of decentralized applications

We’re going to assume you have a basic understanding of web development and mainly outline the structure for the connection to the blockchain.

We used the following tools

  • An Ethereum wallet: You can create one on
  • An Ethereum node: Download from Ethereum developer website
  • A web browser plugin for the connection from web to wallet: MetaMask
  • An online Linux server with PHP, JavaScript,Node.js and MySQL database
  • web3 library for JavaScript to connect to wallets and the blockchain
  • Solidity as a language to write smart contracts

Basic outline of the functionality

The Tour de France sports pool we created has to do a couple of things. It has to allow players to register their Ethereum wallet (once), select a team of riders and finally commit this to the blockchain. Then we’ll need to have a way to insert scores daily and in after the event automatically decide who wins the competition and gets the prize Ethereum.

This is how we want things to happen in a technical sense:

  • A player with an Ethereum wallet and MetaMask installed arrives at the website
  • The player registers for the competition and gets a MetaMask message asking to pay the transaction fees (Gas fees) to enter the competition. The website calls the tourpool smart contract. Calling the function is a transaction on the blockchain and requires paying of the transaction fee.
  • The player then receives 100 tokens in their wallet that can be used to buy riders for the team they think will be getting the most points in the Tour de France
  • The website gives an overview of the token balance and the riders that can be selected. When the riders have been selected, the player can commit the team to the blockchain
  • The website calls the smart contract of the token, which checks and performs the transfer of tokens and team data from the player wallet to the tourpool contract. Upon receiving the tokens, the tourpool contract stores the player’s team in the blockchain.
  • The registration and commit team functions in the tourpool smart contract close at the exact timestamp that the Tour de France start. No players can change their team or register.
  • During the tour, the contract owning wallet can send the stage scores per rider once a day to the smart contract. This automatically awards points to the wallets based on a scoring table.
  • When the Tour de France finishes and the final stage score and overall scores are uploaded, the owner calls the contract to transfer the ethereum to the wallet with the highest score. The Ethereum in the contract can only be sent to the current highscore. In case of a tie, the first registration wins.

What is in the smart contracts?

The best way to start showing how things actually work is to start with the two smart contracts. A smart contract is in fact nothing more than a set of rules that have to be checked when a function of the contract is called. When certain conditions are met, the smart contract will reply accordingly. After a smart contract is in the blockchain, it can not be changed so you are absolutely sure that it will keep doing what is programmed to do.

Smart contracts for Ethereum are written in Solidity. An example (taken from the Solidity website) contract looks like this:

pragma solidity ^0.4.21;

contract Coin {
// The keyword "public" makes those variables
// readable from outside.
address public minter;
mapping (address => uint) public balances;

// Events allow light clients to react on
// changes efficiently.
event Sent(address from, address to, uint amount);

// This is the constructor whose code is
// run only when the contract is created.
function Coin() public {
minter = msg.sender;

function mint(address receiver, uint amount) public {
if (msg.sender != minter) return;
balances[receiver] += amount;

function send(address receiver, uint amount) public {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);

This is a contract that creates a basic coin. The smart contract makes it possible to create a coin and to only allow the creator to “mint” or issue new coins. It also allows anyone with the coin to send them to someone else. Making contracts like this is not so difficult, but making secure contracts that do not break does require more experience.

The sports pool we’re building requires two smart contracts. The token one is similar to the one above. It creates a token and when someone calls the contract it will check the wallet address and if it has not been issued before, the wallet holder will receive a 100 tokens to be spent on riders later.

Registering a player

The second smart contract, the Tourpool contract is a bit more complicated. It holds definitions for what a player is and what a rider is, deadlines for registration, uploading of scores and the automatic pay out of the winner after certain conditions have been met.

Committing a team

Basically any function can be created in Solidity that checks certain variables and many examples can be found on the website. Often smart contracts are made publicly available, to show everyone how things work and that there is no way to commit fraud and everything is transparent. When the contract is finished, you compile the Solidity file to create bytecode. The bytecode can be deployed to the blockchain using an Ethereum wallet. When deployed you get a link to view the actual contract online and the ABI, which is basically a JSON file that explains how to call functions in a contract. Take a look at Karl Floersch’s Solidity tutorial for a great way to learn more about writing smart contracts.

What’s in the database then?

With all the possibilities in the smart contracts it seems easy to add as much as possible to them. There is a catch however, any data that is sent to a smart contract will cost you transaction fees (Gas fees). That is because the adaptation has to be registered and a new block has to be created to hold this new state of the contract. Changing a lot of data is therefore expensive and it takes time. The trick is to limit the amount of changes to a minimum, without losing the added value of the blockchain in terms of security and transparency.

So we make sure the data in the contracts is only changed when:

  • Someone registers and receives coins
  • Someone commits or changes a team in the blockchain
  • Scores are uploaded for a stage

That means that other useful data has to be stored somewhere else, in this case a MySQL Database. The database holds all the riders, their price and team information so they can easily be selected. Only the rider number and their price is actually uploaded to the blockchain.

Connecting the dots

Now that we have smart contracts in the blockchain and a database with riders it is time to start making a connection with the website to tie it all together. We build a JavaScript website with Node.js and install the web3 library. The website will basically wait until there is a web3 compatible browser, with the MetaMask plugin you can make Chrome, Opera or Firefox web3 compatible. With web3 you can create a connection to the registration smart contract, using the ABI created earlier and a wallet address that you can get from the current user if they allow it. When the user clicks a button they can execute one of the contracts functions and they get a pop up from MetaMask asking them if they want to call that function and what it will cost them. If you want to know more about writing the web3 connection for JavaScript you can take a look at Dan Finley’s Medium post about it.

In our case this button will register the user in the token contract and send them a 100 tokens that can be spent later on selecting riders. After that we show the user a new page where they can actually select the riders and see the balance of their tokens. Basically we just show a table of the rider database and allow them to select riders in some way. The actual token costs is calculated after each change and reflected in the balance. There is a final commit button that is only active when all requirements are met. The commit button is a call to the team contract that sends the selected team information to the blockchain. Again a popup is shown by MetaMask to confirm this transaction and when accepted, the team is registered in the blockchain.

And then?

Of course we’re not finished now that the information is in the blockchain. We’ll also need a way to upload scores after each stage. When there are scores we need to be able to show them to players and what happens when they are too late to register?

As we mentioned the tourpool smart contract contains the intelligence. There‘s a deadline in there for the end of the registration period. The contract has a function to check if these time constraints have been passed and they are automatically checked when someone tries to register. In that way we can catch that information in JavaScript and show a message accordingly. If a wallet was already registered and we are past the registration deadline, we’ll show the user their current score and standing in the competition.

Sending data to a contract as owner

The score can be calculated per wallet when a user is on the website, as long as there are scores for the riders. As organizer we need to call the upload score function in the contract after a stage has been finished and the results have been communicated by the official Tour organization. To do this we create a separate webpage and visit it using MetaMask with the wallet that also is owner of the smart contract. The web page will have a form where we can enter the number of every rider that has obtained a certain score. When we press commit we see an overview of the riders and their scores, double check it and then pay the gas fees to increase the scores for these riders. In the smart contract we limited this function to be called only once per day to make sure that we can’t do this more than once. This transaction is visible for anyone on the blockchain, so fraud can be easily checked by participants.

Hopefully this article has given you a basic idea of what is needed to set up an application like this, the rationale behind it and also the links to tools and explanations to do something like this yourself.

The demonstrator can be found on, so take a look at the end result of this article.

Written by Thomas de Wolf, Co-founder of diip Blockchain a company that develops blockchain software for customers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade