Exploring Sharing Economy in Blockchain with Lisk.Bike

Today, we will throw in a short piece that explores the community project Lisk.Bike. The technical side of things will be covered, explaining the developed custom transactions. First, let’s learn how Lisk.Bike aims to enhance the sharing economy using blockchain.

By Michiel Mulders

24 Apr 2020

Lisk Bike_JAN 17 - BLOG.png

What is Lisk.Bike?

A big thank you to Caspar Roelofs (Gimly) for writing an extended, original version of the learnings of creating Lisk.Bike. You can read the full blog post on Caspar’s blog.

Let me give you a gentle introduction to Lisk.Bike. The project, or also proof of concept, has been created by Blockchain030 which is located at Lisk Center Utrecht.

Lisk.Bike has been developed from the viewpoint of becoming a decentralized bike sharing platform, as described more extensively in this introductory blog by Gimly. The project wants to spark an active sharing economy but also grow the blockchain industry.

What is the definition of sharing economy?

An economic system in which assets or services are shared between private individuals, either free or for a fee, typically by means of the Internet.

The idea is to build one tool that allows anyone to participate in a bike sharing economy. When visiting Berlin, you’ll notice you have more than 15 bike sharing apps to choose from. To avoid installing multiple bike sharing apps that are controlled by different organisations, Lisk.Bike aims to open up this industry to regular users like us. Anyone can add their bike to the bike sharing economy while also reducing possible waste. With the benefit of Lisk.Bike explained, the technical side of things remains.

Introduction to Lisk.Bike

Lisk.Bike allows anyone to register their bike with the application. This allows any user of the application to reserve a bike to take it for a ride.

Furthermore, the user has to deposit a fixed cost to the bike’s address. If the ride turns out to be shorter, the difference between the actual riding cost and the deposit will be returned to the user’s wallet.

Lastly, when users arrive at their destination, they have to lock the bike which is a custom transaction. Therefore, let’s explore the locking transaction on a technical level.

Technical exploration custom transaction Lisk.Bike

For the technical side of things, we’ll explore how the end rental custom transaction works. If you don’t have a lot of knowledge about custom transactions, read the following introductory post on the Lisk blog. You might want to check out the deep dive into custom transactions article as well.

Imagine Jan wants to get home from work and he locates a bike. He has sufficient funds in his account and he starts the rental of a bike close to his location. After riding home, Jan wants to lock the bike. Let’s see what happens in the applyAsset function of the custom transaction.

First of all, we need to retrieve some data from the ‘accounts’ entity. We are retrieving the bike account, renter account, and bike object.

File name
1
2Next, the code makes room for dynamic validation checks:
3
41. First of all, the bike object should exist.
52. Secondly, the “rentedBy” property of the bike should hold an address as an “undefined” value. This would indicate nobody is renting the bike.
63. Lastly, we need to check for the address that is sending the end rental transaction as the bike can only be returned from its own account or by the owner of the bike.
html if (object === undefined) { errors.push(new TransactionError("Object not found", this.id, "this.asset.id", this.asset.id, "An existing object ID on recipient account")); } if (object.asset.rentedBy === undefined) { errors.push(new TransactionError("Object not currently rented", this.id, "this.asset.id", this.asset.id, "The ID of a currently rented object")); } if (object.address !== this.senderId) { errors.push(new TransactionError(\`Bike can only be returned from its own account or by its owner\`, this.id, "this.asset.address", this.senderId, "The address of the bike")); }
File name
1
2Next, the code calculates the total rental time. Using the property “billedHours”, we can determine how much the user has to pay considering the price per hour the bike owner has defined.
html const rentStartTimestamp = object.asset.rentalStartDatetime; // this.timestamp - 15 \* 60; // 15 minutes const rentalDuration = this.timestamp - rentStartTimestamp; const billedHours = Math.ceil(rentalDuration / 3600); const billedAmount = Number(object.asset.pricePerHour) \* Number(billedHours); const paidAmount = object.asset.deposit;
File name
1
2The next step tries to calculate how much of the initial deposit Jan will get back. When renting a bike, the user has to make an estimated deposit. For the end rental transaction, we calculate the difference between the actual amount Jan has to pay and his deposit. The difference will be returned to his account. Besides, the bike’s balance also gets updated to its new balance.
html // Calculate unused deposit const netDepositReturn = Number(paidAmount) - Number(billedAmount);//19 // Set new bikes balance const newBikeBalance = (Number(sender.balance) - Number(netDepositReturn)).toString();//120-19 = 101 // Set new renter's balance const newRenterBalance = (Number(recipient.balance) + Number(netDepositReturn)).toString();// 80 + 19 = 99
File name
1
2Lastly, the code sets the “rentedBy” property to an empty string so the bike can be rented out again.
html object.asset.rentalEndDatetime = this.timestamp; object.asset.rentedBy = "";
File name
1
2Of course, we have to save the changes for the user and the bike. Therefore, we update both objects in the [StateStore](https://lisk.io/blog/tutorial/answering-all-your-questions-about-custom-transactions).
html // Update users balance and location store.account.set(this.recipientId, {...recipient, balance: newRenterBalance}); // Store bike object object.balance = newBikeBalance; store.account.set(this.asset.id, object); ```

Final Words

The full code for Lisk.Bike can be explored in Lisk Center Utrecht’s GitHub repository. Let us know if you like this new type of short-form content exploring community projects or code snippets.

Lastly, we want to give Lisk Center Utrecht the opportunity to promote their blockchain + JavaScript conference.