Loading...
Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. They typically are used to automate the execution of an agreement so that all participants can be immediately certain of the outcome, without any intermediary’s involvement or time loss. They can also automate a workflow, triggering the next action when conditions are met.
Crowdfunding is the use of small amounts of capital from a large number of individuals to finance a new business venture.
Suppose I want to start a small tea shop and I do not have enough funds/money to do it. So, I what can I do?
Either I can borrow some money from friend/family/bank, or I can ask someone to give me money and become my partner in business. If the project is too big for any single investor to invest money into it, they I will go for crowdfunding. I will ask for money from any individual in exchange for my company shares and every investor will become stakeholder in the company.
Anyone will be able to list his/her project for crowdfunding in smart contract. Create a function where each project will have unique Id. You can ask for some details like project name, expected cost, starting date, finishing date, etc while registering for particular project for crowdfunding. After registering for crowdfunding the contract owner will verify the details and validate it. Once owner has validated for the project, it is available for public to fund the project. Anyone can see the project details and ID accordingly they will transfer their funds from own wallet to the smart contract.
After the funding process is completed the particular project owner can withdraw his/her collected funds from smart contract.
pragma solidity ^0.7.0;
contract crowdFunding{
mapping (address => uint) public fundContributors;
address public manager;
uint public deadline;
uint public targetAmount;
uint public raisedAmount;
struct Request {
string description;
address payable recipient;
uint value;
bool completed;
}
mapping (uint => Request) public requests;
uint public numRequests;
constructor (uint _targetAmount, uint _deadline) {
targetAmount = _targetAmount;
deadline = block.timestamp + _deadline;
manager = msg.sender;
}
function sendFunds() public payable {
require(block.timestamp < deadline, "crowdFunding is over");
fundContributors[msg.sender] += msg.value;
raisedAmount += msg.value;
}
function getBalance() public view returns(uint){
return address(this).balance;
}
function refund() public {
require(block.timestamp > deadline && raisedAmount < targetAmount, "You are not eligible for refund");
require(fundContributors[msg.sender] > 0);
address payable user = payable(msg.sender);
user.transfer(fundContributors[msg.sender]);
fundContributors[msg.sender] = 0;
}
modifier onlyManager() {
require(msg.sender == manager, "Only manager can call this function");
_;
}
function createRequest (string memory _description, address payable _recipient, uint _value) public onlyManager {
Request storage newRequest = requests[numRequests];
numRequests++;
newRequest.description = _description;
newRequest.recipient = _recipient;
newRequest.value = _value;
newRequest.completed = false;
}
function makePayment (uint _requestNo) public onlyManager{
require (raisedAmount >= targetAmount);
Request storage thisRequest = requests[_requestNo];
require(thisRequest.completed == false , "This request has been completed");
thisRequest.recipient.transfer(thisRequest.value);
thisRequest.completed = true;
}
}