Create a crowdfunding smart contract using solidity.

Create a crowdfunding smart contract using solidity.

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.

PREREQUISITE

  • Remix

  • Little or no knowledge of solidity

CASE STUDY

Kickstarter

ISSUES

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

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

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

How?

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

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 here to begin.

//SPDX-License-Identifier: MIT
pragma solidity ^0.4.17;

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.

In this article, we will be using version 0.4.17.

contract CrowdSource{
 address public manager;
 uint public MinimumContribution;
 address[] public approvals;

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;
  }

The constructor function

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

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

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

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; }

  • Description: string: the purpose of the request.

  • Value: Uint: the amount of money needed.

  • Recipient: address: the address of the vendor

Struct is not an instance but a definition of a type we can use multiple times across the project.

Testing the contract

  • Environment

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

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.

Number 3 tells us that a contributor joined the contract.

Conclusion

Resource

Solidity Zero to Hero CourseEthereum and Solidity: The Complete Developer's GuideLearnhub

Culled from my article written on Hackmamba