CryptoFlight
This tutorial is part of a course project for Advanced Topics in Blockchain (CSCI5590) given at The Chinese University of Hong Kong, spring 2019.
Last updated
This tutorial is part of a course project for Advanced Topics in Blockchain (CSCI5590) given at The Chinese University of Hong Kong, spring 2019.
Last updated
Live at: cryptoflight.andyafk.com (access it from a browser with MetaMask installed and make sure it's set to use the Ropsten testnet)
Video walkthrough of the dapp: http://youtu.be/UvvvXHdT3hE
Screenshots:
The author is a Computer Science MSc student at Chalmers University of Technology, Sweden, currently doing an exchange at the Chinese University of Hong Kong. The tutorial will go through everything expecting that you have no previous experience in building blockchain-based applications. However, you are expected to have basic knowledge about programming and know your way around the terminal. I will expect that the reader has experience similar to what can be expected from a third-year undergrad student. If anything is unclear, feel free to drop me an email at hello@andyafk.com.
The tutorial is done on Mac but you should be able to do it on any Unix-based system. If you prefer Windows the easiest way to get going is to either spin up a virtual machine or just use Docker-containers. The scope of that is outside of this tutorial but there are great tutorials on how to get started with for example Ubuntu on Windows or Docker all over the web.
In this tutorial we are going to build a dapp called CryptoFlight. It's a distributed application that solves a real problem for both airlines and travellers. Imagine you run an airline company that end up with empty seats every now and then. Every seat that is empty is a loss in terms of missing out on ticket sales.For the airline, selling those seats for highly discounted prices will definitely be more profitable than not selling them at all. However, if those tickets are being sold via the usual channels the customers would soon notice and wait until the last minute to buy tickets. This would be less than optimal since it decreases your ability to plan future flights.
As a traveller with a flexible schedule you often want to go somewhere but it doesn't really matter if it's tomorrow, in two days or in the next three days, as long as you can get a good price on a ticket. For example, let's say you live in Stockholm, Sweden and your girlfriend is doing an exchange in Berlin, Germany. There are six direct flights between Stockholm and Berlin every day with prices ranging between $350 and $780 HKD. Since you're a student you only can afford tickets for at most $300, on the other hand you're highly flexible. You could sit and wait until a last-minute ticket appears and try to grab it. However, those tickets are offered as first come - first served, and they have a fixed price.
CryptoFlight solves both of these problems by using blockchain technology, also avoiding the need of having a third trusted party involved. Airlines can create smart contracts with flights details and a minimum acceptable ticket price (since they need to cover for taxes and fees). Travellers can then browse available flights and bid for seats. The bid is done using our own currency, CFToken, and has to reach up to at least the minimum acceptable ticket price.
When the airline sees that they have a few seats over on a flight they simply go and "activate" that contract with the number of seats they want to sell. If the number of travellers in the contract exceeds the number of seats that are made available by the airline the travellers with the highest bid will go first. The airline can also choose whether or not the contract should live on. If they choose to have the contract active, the travellers that didn't get a ticket will still be waiting in the contract for future flights. If the airline instead chooses to close the contract the unused ether will be sent back to the travellers that didn't get a ticket.
Users will be able to both buy CFTokens using Ether as well as transferring CFTokens so other users. Since we are conforming to the official ERC20-protocol, our ERC20 token will also be tradable on blockchain exchange services.
There are several reasons why blockchain is suitable for this type of application. First off, it eliminates the need for a trusted third party. Secondly, whenever an airline chooses to activate a deal they will get immediate payout. The CFTokens are stored in the contract and are sent to the airline as soon as a deal is activated.
However, there still are some issues with this approach. Since we don't have any trusted third party we need to trust that the airlines posting the deals are legit companies and that they will fulfill their part of the deal once they get the ether from all the customers. One way to solve this is to have the airlines post their public key to their websites, similar to how software developers post the hash of their programs online to make sure no-one tampered with the source code. The travellers could then verify that it's a legit offer before placing their bids. Another option would be to have some kind of a reputation scheme, letting travellers post reviews for airlines. After you finished this tutorial you are hopefully comfortable enough with dapp-development to implement these kinds of structures yourself.