Smart Contract Blockchain Splitwise DApp

0
33

probably, You have heard about smart contracts and you became curious about how do they work. If you are curious about this new technology you are in a good place, because in this article we are going to find out how interesting these contracts are and how they are useful.

For our purpose, we are going to explain the subject with a project. So by understanding and doing this project step by step you achieve the answer to these questions:

What is a smart contract?

How we can use this technology for different use cases?

What is solidity language and how we can write a smart contract?

What is a decentralized application and how they are developed?

So there is a lot to say, let's go dig to it. We want to create a decentralized system to track debit and credit - a blockchain version of Splitwise. If you haven’t heard of the app, it’s a simple way to keep track of who owes who money within a group of people (maybe after splitting lunch, groceries, or bills). To illustrate the application, consider the following scenario:

Alice, Bob, and Carol are all friends who like to go out to eat together. Bob paid for lunch the last time he and Alice went out to eat, so Alice owes Bob $10. Similarly, Carol paid when she and Bob went out to eat, and so Bob owes Carol $10.

Now, imagine Carol runs short on cash and borrows $10 from Alice. Notice that at this point, instead of each person paying back their ‘loan’ at some point, they could just all agree that nobody owes anyone. In other words, whenever there is a cycle of debt, we can just remove it from our bookkeeping, making everything simpler and reducing the number of times cash needs to change hands.

We will build a decentralized way to track who owes what to who, so that no trusted third party has to be relied upon. So with this introduction, you may guess that, this process is going to be handled by a smart contract.

a smart contract is a piece of code that manages the states of a process. This self-executing protocol could make different types of agreements using blockchain if the needed conditions become satisfied. The key to these contracts is the decentralized network known as the blockchain.

Smart contracts use blockchain technology to verify, validate, capture and enforce agreed-upon terms between multiple parties. I will explain more about blockchain in another article.

So back to our project, our DApp will be written with the help of a smart contract and blockchain technology. Because it’s on the blockchain, when Carol picks up the check for her and Bob’s meal, she can ask Bob to submit an IOU (which he can do using our DApp), and she can verify that he indeed has. The public on-chain storage will serve as a single source of truth for who owes who. Later, when the cycle illustrated above gets resolved, Carol will see that Bob no longer owes her money.

So let’s go for implementing our smart contract which you can add an IOU (I owe you), so we need the IOU functionality, we need a function for getting all debts and we need the add and get functions for users.

As you can see from the code above, splitwise contract contains the functions we need for our purpose. This is a code written with solidity language. Solidity is the language we use for implementing smart contracts. It is developed on the Ethereum platform and allows programmers to write blockchain DApps.

Here let’s see what do these functions do:

lookup(address debtor, address creditor) public view returns (uint32 ret): Returns the amount that the debtor owes the creditor.

add_IOU(address creditor, uint32 amount, …): Informs the contract that msg.sender now owes amount more dollars to the creditor. It is additive: if you already owed money, this will add to that.

addDebt(address debtor, address creditor, int32 amount): actually, add IOU function uses this function and it gives the msg.sender address instead of debtor, then, It increases the debts between debtor and creditor by the amount given to it. It also adds the creditor and debtor addresses to users.

Now it’s time for deploying and running our smart contract to see how actually it works. for this, we could use the remix website.

after creating our contract file with .sol extension, we could compile it from SOLIDITY COMPILER section by choosing right compiler version.

In this state, we need to install and run ganache-cli which is a blockchain emulator. It is used to simulate a real Ethereum node on our local machines. It allows you to make calls to the blockchain without the overheads of running an actual Ethereum node. for installing this emulator first you need to have Node.js and npm installed on your system. then you could easily install ganache by below command:

npm install -g ganache-cli

by running ganache-cli, it will set up 10 default Etheruem addresses, complete with private keys and all, and pre-loads them with 100 simulated Ether each.

Now we could easily deploy our contract to blockchain from the DEPLOY & RUN TRABSACTIONS section of the remix website. we set the environment to Web3 Provider and then set the Endpoint to http://localhost:8545 — this should be the default. Now we have also our deployed contract’s address.

Once the contract is deployed you are able to use the functions of your smart contract and make transactions.

First, let’s make an IOU transaction. By default, our current address is the first address ganache made for us. For instance, we choose the second address of ganache addresses to indicate we owe 5 (ether or $) to it. after making a transaction, we could also see the transaction details:

using lookup function will tell us how much the debtor owes to the creditor:

you can test other functions by yourself and enjoy your experience of deploying a smart contract.

also, we have a user interface that computes useful information for the user and allows non-programmers to use the DApp. It is implemented in index.html and script.js files. all the source codes are available in my GitHub repository.

For using the client user interface, just you should replace your deployed smart contract’s address with var contractAddress in the script.js file, and you are done.

also, more details are handled in the client source code including Resolving Loops of Debt like below which I will explain later about them or you could find out how it works by your curiosity. I hope you enjoyed it.

Join Coinmonks Telegram Channel and learn about crypto trading and investing

Smart Contract Blockchain Splitwise DApp was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.