Create a crowdfunding smart contract using solidity
Tags: blockchain, javascript, cryptocurrency, tutorial
Published: 2023-03-07 12:38:04


Smart contracts are resilient and highly secured applications deployed on the blockchain.

We will build one of the smartest campaign contracts used to crowdsource projects and keep track of the spending habit of the crowdsourcer.
<h4>PREREQUISITE</h4>
<ul>
 	<li><a href="https://remix.ethereum.org/#optimize=false&amp;runs=200&amp;evmVersion=null&amp;version=soljson-v0.8.7+commit.e28d00a7.js" target="_blank" rel="noreferrer nofollow noopener" data-target-href="https://remix.ethereum.org/#optimize=false&amp;runs=200&amp;evmVersion=null&amp;version=soljson-v0.8.7+commit.e28d00a7.js">Remix </a></li>
 	<li>Little or no knowledge of solidity</li>
</ul>
<h4>CASE STUDY</h4>
Before we begin, let’s take a case study of <a href="https://www.kickstarter.com/" target="_blank" rel="noreferrer nofollow noopener" data-target-href="https://www.kickstarter.com/"><u>Kickstarter</u></a>. Kickstarter is a crowdsourcing website where anyone can drop their idea for people to crowdsource to enable them to finish their project.
While it is a beautiful idea, we would encounter issues on Web3 if we build this contract using blockchain technology.
<h4>ISSUES</h4>
<ul>
 	<li>Who sanctions the release of the funds inside the pool?</li>
 	<li>What if the owner swindles everyone and runs away with the funds after crowdsourcing?</li>
 	<li>How do we cross-check the project to see if the crowdsourcer is progressing?</li>
</ul>
While this might feel like a big task, we will create a smart contract that considers all of this and ensures the contributor’s funds are utilized based on the agreement.

How?

First, look at this diagram of what we intend to build.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648996527985_first+new.PNG" /><figcaption></figcaption></figure>
The manager calls a contract to request funds which the contributors all come together to crowdsource. After crowdsourcing, the manager calls a spending request that more than 50% of the contributor must approve, but what if the manager calls a request to his wallet.

How do we remedy such an instance?

Once approved, the vendor account is filled in the struct when requesting a spending request and sent to that vendor and not the manager.

The accountability and security of contributors’ funds are guaranteed.

We will use Remix IDE to write our smart contract, so click <a href="https://remix.ethereum.org/#optimize=false&amp;runs=200&amp;evmVersion=null&amp;version=soljson-v0.4.17+commit.bdeb9e52.js&amp;language=Solidity" target="_blank" rel="noreferrer nofollow noopener" data-target-href="https://remix.ethereum.org/#optimize=false&amp;runs=200&amp;evmVersion=null&amp;version=soljson-v0.4.17+commit.bdeb9e52.js&amp;language=Solidity"><u>here </u></a>to begin.
<pre><code spellcheck="false"><b><i>//SPDX-License-Identifier: MIT
</i></b></code><code spellcheck="false"><b><i>pragma solidity ^0.4.17;</i></b></code></pre>
<code spellcheck="false"><b><i>
</i></b></code>
The first line tells us the license we are working with, MIT free license. If we do not have this, our code will throw an error.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648994142272_next.png" /><figcaption></figcaption></figure>
Pragma solidity tells us the version we are working on; note this version must match our compiler, or we will get an error sign.

In this article, we will be using version 0.4.17.
<pre><code spellcheck="false"><b><i>contract CrowdSource{
</i></b></code><code spellcheck="false"> <b><i>address public manager;
</i></b></code><code spellcheck="false"> <b><i>uint public MinimumContribution;
</i></b></code><code spellcheck="false"> <b><i>address[] public approvals;</i></b></code></pre>
<code spellcheck="false"><b><i>
</i></b></code>
We can create the contract by calling the function contract and giving <b>CrowdSource</b>. Use the curly brace to open it.

We call the variable called manager as this is the crowdsourcer and the contract caller.

Uint is an unsigned variable similar to uint256; we make this function public as we want anyone to call it when contributing the MinimumContribution.

We created another function called address approvals, but with [ ], everyone who made the MinimumContribution will be saved in the approvals list. We would remove this soon as it is expensive and gas-intensive to run.
<pre><code spellcheck="false">function campaign(uint minimum)public{
</code><code spellcheck="false">    manager = msg.sender;
</code><code spellcheck="false">    MinimumContribution = minimum;
</code><code spellcheck="false">  }</code></pre>
<h4>The constructor function</h4>
The Constructor or function campaign is the first function to run, as we want to ensure only the manager can call the contract.

Note that we made it public to see the caller of the contract.

msg.sender is a universal variable that we assign to the manager, manager = msg.sender; whenever the contract is called so, whoever calls the contract is the manager.

We called (uint minimum)public{ which is an unsigned integer for the amount of money required to become a contributor, we can have a different minimum or create a static amount.

Then we can call in the function MinimumContribution = minimum; so whoever contributes to the contract is added here <b><i>uint public MinimumContribution;</i></b>
<b><i>
</i></b>
<h4>The approval process</h4>
The approvals function is called when a contributor pays payable a minimum amount to the contract.
<h4><code spellcheck="false">function contribute() public payable{
</code><code spellcheck="false">    require(msg.value &gt; MinimumContribution);
</code><code spellcheck="false">    approvals.push(msg.sender);
</code><code spellcheck="false">  }</code></h4>
The require function kicks out anyone that has not added value to the minimumContribution and pushes a contributor to the approvals list.

Next, we introduce the payable function and the approval variable, and we will see why they play a significant role in the smart contract.
<h4>Requesting funds</h4>
Contributors can send funds into our contract, but we can not pull money out of the request function, leading us to create a <b>struct</b><b>. Struct is a collection of key-value pairs that have different types. </b>
<b>
</b>
<h4><code spellcheck="false">contract CrowdSource{
</code><code spellcheck="false">  struct request{
</code><code spellcheck="false">    string description;
</code><code spellcheck="false">    uint value;
</code><code spellcheck="false">    address recipiant;
</code><code spellcheck="false">    bool complete;
</code><code spellcheck="false">}</code></h4>
<code spellcheck="false"> </code>
<ul>
 	<li><b>Description</b>: string: the purpose of the request.</li>
 	<li><b>Value</b>: Uint: the amount of money needed.</li>
 	<li><b>Recipient</b>: address: the address of the vendor</li>
</ul>
We create a struct as a request that the contributors approve before sending it to the vendor, which is also a struct type.

Struct is not an instance but a definition of a type we can use multiple times across the project.
<h4>Testing the contract</h4>
We’ll test the contract right in the IDE.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648994838376_1.png" /><figcaption></figcaption></figure>
Running a test on Remix is straightforward. We can see the DEPLOY AND RUN TRANSACTIONS on the left-hand side.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648994867564_log1.png" /><figcaption></figcaption></figure>
<ul>
 	<li>Environment</li>
</ul>
There are about four different environments, but most times will use just two javascript VM (London) and Injected Web3.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648995005179_account.png" /><figcaption></figcaption></figure>
<ul>
 	<li>Account</li>
 	<li>Remix gives 15 free accounts that we can use to deploy contracts and test our deployment.</li>
</ul>
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648995044109_final+part.PNG" /><figcaption></figcaption></figure>
<ul>
 	<li>Gas Limit - Gas limit replicates the actual gas fees when deploying a contract. Each deployment or change in the contract will cost us a gas fee, and we will review the gas fee later on.</li>
</ul>
<ul>
 	<li>Value - The value is the fund we put into the contract when deploying it. Wei is the smallest unit, while ether is the highest unit of the currency used on Ethereum.</li>
</ul>
<ul>
 	<li>Contract  - The name of the contract, which in this case is new.sol. .sol is a file format for solidity contracts.</li>
 	<li>Deploy - Deploys the contract into the blockchain network so we can interact with the network.</li>
</ul>
Compile the contract and click on deploy, a contract will be created below.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648995155726_contract.PNG" /><figcaption></figcaption></figure>
Click on the drop-down button by the left, and see the full deployment.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648995181995_deployed+contract.PNG" /><figcaption></figcaption></figure>
In our contract, we have the campaign which reads uint256, which is an unsigned integer; this is the part we input our contribution into the contract, so whoever puts any funds is added to the contract.

Let’s test it.

We will put a 100wei into the campaign input space but remember to change the account above, so we do not make the contract manager a contributor.

Use another account and input 100wei.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648995246474_campddd.PNG" /><figcaption></figcaption></figure>
Click on the campaign, and notice a panel on the right that turns green, it means funds has been sent to the contract.
<figure><img src="https://paper-attachments.dropboxusercontent.com/s_4A9BDF78EEFF4803CB8911A5A67E7C2DCAE879D67F7C24F4451F0BDB1145EAED_1648995285378_new2.PNG" /><figcaption></figcaption></figure>
Number 1 is the deployed contract by the manager; notice the address begins with 0X746.. while number 2 is the new contributor to the contract.

Number 3 tells us that a contributor joined the contract.
<h4>Conclusion</h4>
We have created a crowd-funding smart contract that allows users to crowd-fund together, and we can deploy this contract on the blockchain network.
<h4>Resource</h4>
<a href="https://www.codiesalert.com/courses/free-solidity-course/" target="_blank" rel="noreferrer nofollow noopener" data-target-href="https://www.codiesalert.com/courses/free-solidity-course/">Solidity Zero to Hero Course</a>
<a href="https://www.udemy.com/course/ethereum-and-solidity-the-complete-developers-guide/learn/lecture/9020750?start=75#overview" target="_blank" rel="noreferrer nofollow noopener" data-target-href="https://www.udemy.com/course/ethereum-and-solidity-the-complete-developers-guide/learn/lecture/9020750?start=75#overview">Ethereum and Solidity: The Complete Developer's Guide</a>
<a href="https://blog.learnhub.africa/category/blockchain/">Learnhub</a>

Culled from my article written on <a href="http://hacmamba.io">Hackmamba</a>