Loading...
pragma solidity ^0.8.7;
contract crowdfund{
mapping(address=>uint) public contributors;
address public manager;
uint public minContribution; // Minimun contribution
uint public target; // Total amount required
uint public deadLine; // Deadline of the crowdfund
uint public raisedAmount; // Total amount raised
uint public noOfContributors; // Total number of contributors
struct Request{ //To request the amount
string description;
address payable recipient;
uint value;
bool completed;
}
mapping(uint=>Request) public requests;
uint public numRequests;
constructor (uint _target, uint _deadLine){
target = _target; // stores the target amount in target variable
deadLine = block.timestamp + _deadLine; // deploy time (e.g: 10sec) + _deadLine time (e.g: 60 * 60 sec);
minContribution = 99 wei; //Minimun contribution to be made
manager = msg.sender; //
}
function sent_Amount() public payable{
require(block.timestamp < deadLine, "Deadline has Passed."); //check the deadLine
require(msg.value >= minContribution, "Minimun contribution is not met the required contribution."); //check the minimun amount user wants to sent if it is greater than the "minContribution" then the amount will be accepeted by the contract else the message will displayed
if (contributors[msg.sender] == 0){
noOfContributors++;
}// these line codes check does the any user sending the amount again
contributors[msg.sender]+= msg.value;
raisedAmount+=msg.value;
}
function getContractBalance() public view returns (uint){
return address(this).balance;
}
function refund() public{ // this function sends refund to the contributors if the target amount not achieved within given time
require(block.timestamp>deadLine && raisedAmount<target, "You are not eligible for refund.");
require(contributors[msg.sender]>0);
address payable user = payable(msg.sender);
user.transfer(contributors[msg.sender]);
contributors[msg.sender] = 0;
}
modifier only_Manager(){
require(msg.sender == manager, "only Manager can all this function.");
_;
}
function create_Request (string memory _description, address payable _reciepient, uint _value) public only_Manager {
// This function allow manger to withdraw fund but before that he/she has to describe the reason
Request storage newRequest = requests[numRequests];
numRequests++;
newRequest.description = _description;
newRequest.recipient = _reciepient;
newRequest.value = _value;
newRequest.completed = false;
}
function makePayment (uint _requestNo) public only_Manager{
require (raisedAmount >= target);
Request storage thisRequest = requests[_requestNo];
require(thisRequest.completed == false , "This request has been completed");
thisRequest.recipient.transfer(thisRequest.value);
thisRequest.completed = true;
}
}