Christmas Present in the Blockchain (technical approach)

Author
Vincent Rutten
Categories
Publish date

This year at VI Company we decided to do something different regarding the Christmas gifts we give to colleagues, relations and friends. Instead of the usual umbrella or chocolate we came up with the idea to give Ether, a cryptocurrency running on the Ethereum network. This article will shed some light on how we technically managed to register gifts in the blockchain and distribute them to the receivers.

During a hackathon earlier this month (start and do as much work on a project in one day), we started working on our Christmas gift for 2017. We worked with smart contracts and the Ethereum blockchain, bundled together into multiple .NET Core projects.

Smart contract
A smart contract is a piece of code stored in the blockchain containing functions which can be executed. The cool thing about it is that, next to the code logic it contains, it stores and persists whatever data you define. This eliminates the need for a separate database. I will explain how we wrote our smart contract and what functionality it contains.

When writing a smart contract you have to use Solidity. Solidity is the coding language that Ethereum uses and is based on C++, JavaScript and Python. If you are familiar with coding in general, starting with Solidity shouldn’t be a problem for you. In this article I’m not going into the specifics of how Solidity works but at the bottom I will provide some links on how to get started with smart contracts and Solidity. Here you can find the smart contract we build for this project, which I’m going discuss in more detail.

In our smart contract we defined three main functions. The addPresent function, which allows us to register presents into the blockchain via a unique generated token. We send the token to the smart contract and hash it. Then we store the token in a list, waiting for it to be claimed. The unique tokens we generated and stored in the contract were sent to the receivers of the presents. The receivers can then claim their present via our Christmas website using their personal token.

The function handling the claiming is called claimPresent. The input parameters contain the unique token and the Ethereum address of the receiver of the present. The function loops over all the tokens stored in the contract and checks whether the submitted token has been claimed yet. If it’s still available it will set the token as claimed and add the Ethereum address of the claimer to that token.

Lastly we have the distribute function. This will distribute one third of the present to the receivers that claimed their present by sending a set amount of Ether to the address they provided. Once distributed we save the progression of the distribution of the present per receiver. Each receiver can receive Ether three times.

.NET Core
Next to the smart contract we also had to write some C# code to make the calls to the smart contract. For that we used a .NET Core library called Nethereum. This library contains all functionality needed to enable a C# application to communicate with the Ethereum blockchain. A while back, our colleague Casper wrote an article on how to get started with Nethereum. In his article he mentions what basic functionality you would need to start developing with Nethereum and also how to test your code without having to use actual Ether.

The testing part was crucial to this project because if we made a mistake during development actual money could get lost. Luckily, there are several test networks for Ethereum which enables developers to test and fool around with their projects without having to spend actual money on Ether. We used the Rinkeby test network during this project. One thing to watch out for when developing with Ethereum is that the test networks are much quicker than the actual Ethereum network (observation made during our development process). This sounds logical since there is much more traffic on the main network, but don’t let it take you by surprise and take for example larger timeouts into account.

If you have more questions on how we developed this project you can check out our Christmas website which contains more information about the project, or feel free to contact us!

References in this article:

  1. https://goo.gl/TRK8fe - VI Company Christmas website
  2. https://goo.gl/AmFkTa - Our smart contract
  3. https://goo.gl/gEsjwX - Nethereum .NET Core Library
  4. https://goo.gl/rBtyMQ - Casper’s article about Nethereum and Smart contract development
  5. https://goo.gl/GFaUe7 - Ethereum test network

For readers that are interested in learning more about Solidity and Smart Contracts, I recommend you check out these links:

  1. https://goo.gl/unD882 - Awesome web series about learning Solidity
  2. https://goo.gl/rNRSw8 - Solidity Docs

Casper, Vincent, Lucian, Olaf, Max, Nick and Kees: Many thanks for all your help on this exciting precent!

Back to top