How to build smart contracts for Ethereum Blockchain using Solidity

Blockchain has made a revolution these days and left everyone with thousands of questions. This challenging technology moved the world and promised a new wave, not only in the tech world, but also in any area that needs third party authorization.

We were not left behind this euphoria and we’re hands on these amazing technologies. Recently, our team experimented with Ethereum and managed to design private Ethereum blockchain. Now, we will explain how we can build an interesting application that we cannot build so easily using traditional technologies.

As mentioned before, Ethereum can take place wherever we want to have control and make a fair choice, but also eliminate the third parties. We decided to make a simple application that can utilize these benefits. We got the idea from one common problem that fund-raising websites have.

Let us say you are a young enthusiast who wants to develop some kind of product. You do not have budget and want to raise money online. You elaborate the idea on a web site for donations and wait for donations from people on the web. This fund-raise is considered successful only if certain minimum amount of money is collected. After reaching the threshold, money is transferred to you, as a product owner, and you should send prototype of the product as a reward to all donоrs.

Not everyone that presents itself as an inventor comes with good intentions. Some malicious users manage to raise funds, but do not intend to create the product and run away with the money.

So how to prevent this behavior? We should give power to the contributors, i.e. the people that have donated the money to approve the spending that the product owner wants to make. Whenever the inventor says, I need money to buy some material for my product the contributor can vote on that request if he approves it or not. This way they would have control where their money is going.  The product owner can only make the spending if he has met the required minimum with positive votes. If the condition is met, the money will be directly transferred to the outside vendor that sells the material that the product owner needs.

How our application handles this?

For creating this app, we used the free online editor Remix. It helps us write smart contracts with the Solidity language. It also enables testing, debugging and deploying. If you are familiar with JavaScript you can handle Solidity with ease.

We choose for environment JavaScript VM, the account that we want to be the product owner, define the minimum amount for donation and click on deploy. It is important to be careful with which address we deploy the contract and with which we execute the functions. Usually we want to grant different privileges to different users (addresses).

How to build smart contracts for Ethereum Blockchain using Solidity

First, with the support function people donate money to the project. This function is called whenever someone wants to donate to the project, so that is why it is marked as payable. The amount they pay must be higher than a previously defined minimum. Those that would pay that amount would be considered for the list of approvers of the spending request.

How to build smart contracts for Ethereum Blockchain using Solidity

I will create three supporters of this function, by calling the support function with three different addresses.

To create the spending request, I have to call the createRequest function with the owner’s address (we restrict this with a modifier). He can create many different requests, for example for buying different parts of the product. That is why we have an array of requests. The contributors can index the request they want to support. Each request has parameters that we have to define before calling the request. After the request is created, approvers are asked to vote on it with the approveRequest function.

We call the approveRequest function at least two times, with addresses of two different donors, so we can make this request successful. As a function argument we put 0, since we have created only one request, therefore it has the index 0.

How to build smart contracts for Ethereum Blockchain using Solidity

We count the positive votes and add them to the approvals list.

We set that the request is successful if at least 50% of all the eligible approvers give a positive vote. These people are added to the approvals list. The product owner has the exclusive right to finalize the request when he is ready. So if we want to mark this request as complete, we choose the product owner’s address and call the finalizeRequest function with index 0 (the index of the request).

 

Conclusion

This is just one example how we can make fair deals without the control of third parties. With unbiased control that we put with the smart contracts, we can reduce the frauds and danger of malicious users. Moreover, considering the number of the transactions that take place daily, we can significantly increase the level of security. Although still young and with a many places for improvements, Ethereum is a technology that seems to have bright and promising future.