Loading...
// SPDX-License-Identifier: GPL-3.0
// This is required when we need to creat a nested array
pragma experimental ABIEncoderV2;
pragma solidity ^0.6.0;
// Declaring contract named CrowdFunding
contract CrowdFunding{
//Declaring state varibles
uint amount;
uint goal;
// Defining a structure
struct Project{
string ProjectName;
string StartingDate;
uint finishingDate;
uint expectedCost;
}
// Defining a modifier to check the required condition
modifier validate(){
require(amount >= goal);
_;
}
// Declaring an array
Project[] public project;
function getProjectInfo(string memory title, string memory startingdate, uint finishingdate, uint expectedcost) external {
amount = expectedcost;
// Creating a nested array
Project memory newProject = Project( title, startingdate, finishingdate, expectedcost);
project.push(newProject);
}
// Defining a function to return details of project
function displayProject() internal view returns(Project[] memory, address){
// Generatin different address for every project
address projectId = address(uint(keccak256(abi.encodePacked(now))));
return (project, projectId);
}
// Defining functions defined to deposite and get balnce of smart contract
function depositeToSmartContract() payable external validate{
require(msg.value == amount);
}
// Defining a function that returns the balance of smart contract
function getBalanceofSmartContract() public view returns(uint){
return address(this).balance;
}
// Defining a function to withdraw funds to owners account
function withdrawFromContract() public validate{
msg.sender.transfer(amount);
}
}
1.While declaring a nested array we need to include pragma experimnetal ABIEncoderV2; without including this your compiler will throw an error.
pragma experimental ABIEncoderV2;
2.Now that we have made arrangements for declaring a nested array we can continue further. We have declared a contract named CrowFunding.
3.In the next part you can see the declaration of state variables. Later on a structure is defined it is taking some paramerters like projectName, StartingDate of project which is declared string since we need to give date in day, month, and year form. fininshingDate is an uint because it it is count of days after the stating date. And finally we have expectedCost which asks for how much money is needed to be raised.
struct Project{
string ProjectName;
string StartingDate;
uint finishingDate;
uint expectedCost;
}
4.Modifier is used so that if the required conditon is not fulfilled then the ether is returned back.
modifier validate(){
require(amount >= goal);
_;
}
5.Project[] array is declared to store different project into it then we have defined function getProjectInfo which allows user to enter the required information about their project. And the push method is used to add new project to array Project[].
Project[] public project;
function getProjectInfo(string memory title, string memory startingdate, uint finishingdate, uint expectedcost) external {
amount = expectedcost;
// Creating a nested array
Project memory newProject = Project( title, startingdate, finishingdate, expectedcost);
project.push(newProject);
}
6.Another function displayProject is defined to return the project details and it also returns the particluar projects address.
// Defining a function to return details of project
function displayProject() internal view returns(Project[] memory, address){
// Generatin different address for every project
address projectId = address(uint(keccak256(abi.encodePacked(now))));
return (project, projectId);
}
7.Now a function depositeToSmartContract is defined to deposite ether to smart contract. This function is suppose to be payble since it is gonna cost ether. We also have used a fallback function require to ensure the person who is depositing the ether should be greater than or equal to expectedcost mentioned in respected project.
// Defining functions defined to deposite and get balnce of smart contract
function depositeToSmartContract() payable external validate{
require(msg.value == amount);
}
8.Function getBalanceofSmartContract is defined which returns the balance of smart contract.
// Defining a function that returns the balance of smart contract
function getBalanceofSmartContract() public view returns(uint){
return address(this).balance;
}
9.And finally function withdrawFromContract is defined to withdraw the amount from the smart contract.
// Defining a function to withdraw funds to owners account
function withdrawFromContract() public validate{
msg.sender.transfer(amount);
}
1. Red button will help you deposite eth to smart contract.
2. getProjectInfo which is a orange button will help add project details to like title, starting date(in this format 12/11/2021), finishing date(has to be like 30 days after starting date) and expectedcost
3. withdrawFromContract which is orange button withdraws funds.
4. getBalanceofSmartContract returns balance of contract
5. project in blue button helps you fetch details of projects by index(the first project would be at indes 0 so to get its details put 0 in project box and hit call).