DEV Community

Scofield Idehen for Hackmamba

Posted on • Edited on • Originally published at hackmamba.io

Create a Crowdfunding Smart Contract using Solidity

Image description

This post was originally posted on Hackmamba

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

We are going to build a smart campaign contracts used to crowdsource projects and keep track of the spending habit of the crowdsourcer.

PREREQUISITE

  • Remix
  • Little or no knowledge of solidity

CASE STUDY
Before we begin, let’s take a case study of Kickstarter. 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 kind of contract using blockchain technology.

ISSUES

  • Who sanctions the release of the funds inside the pool?

  • What if the owner swindles everyone and runs away with the whole funds after crowdsourcing?

  • How do we cross-check the project to see if the crowdsourcer is making progress?

While this might feel like a big task, we will create a smart contract that takes all of this into account and ensures the contributor’s funds are utilized based on the agreement.

How?

First, take a look at this diagram of what we intend to build.

Image description

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?

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

The accountability and security of contributors’ funds are guaranteed.

We will be using Remix IDE to write our smart contract, so click here to begin.

//SPDX-License-Identifier: MIT
    pragma solidity ^0.4.17;
Enter fullscreen mode Exit fullscreen mode

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.

Image description

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.

contract CrowdSource{
     address public manager;
     uint public MinimumContribution;
     address[] public approvals;
Enter fullscreen mode Exit fullscreen mode

We can create the contract by calling the function contract and giving **CrowdSource**. 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.

function campaign(uint minimum)public{
        manager = msg.sender;
        MinimumContribution = minimum;
      }
Enter fullscreen mode Exit fullscreen mode

The constructor function
The Constructor or function campaign is the first function to run as we want to make sure 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 uint public MinimumContribution;

The approval process
The approvals function is called when a contributor pays payable a minimum amount to the contract.

function contribute() public payable{
        require(msg.value > MinimumContribution);
        approvals.push(msg.sender);
      }
Enter fullscreen mode Exit fullscreen mode

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.

Requesting funds
Contributors can send funds into our contract, but we can not pull money out of the request function, leading us to create a struct. Struct is a collection of key-value pairs that have different types.

contract CrowdSource{
      struct request{
        string description;
        uint value;
        address recipiant;
        bool complete;
    }

Enter fullscreen mode Exit fullscreen mode
  • Description: string: the purpose of the request.

  • Value: Uint: the amount of money needed.

  • Recipient: address: the address of the vendor
    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.

Testing the contract
We’ll test the contract right in the IDE.

Image description

Running a test on Remix is straightforward. We can see the DEPLOY AND RUN TRANSACTIONS on the left-hand side.

Image description

Environment
There are about four different environments, but most times will use just two javascript VM (London) and Injected Web3.

  • Account
    Remix gives 15 free accounts that we can use to deploy contracts and test our deployment.

  • 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.

  • 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.

  • Contract The name of the contract, which in this case is new.sol. .sol is a file format for solidity contracts.

``Deploy Deploys the contract into the blockchain network so we can interact with the network.

Compile the contract and click on deploy, a contract will be created below.

Image description

Click on the drop-down button by the left, and see the full deployment.

Image description

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.

Click on the campaign, and notice a panel on the right that turns green, it means funds has been sent to the contract.

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.

Conclusion
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.

Resource
Solidity Zero to Hero Course
Ethereum and Solidity: The Complete Developer's Guide

Top comments (0)