This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Defining a Struct for a Spending Request | |
struct Request { | |
string description; | |
uint value; | |
address recipient; | |
bool completed; | |
uint numberOfVoters; | |
mapping(address=>bool) voters; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constructor(uint _deadline,uint _goal,uint _minimumContribution) public{ | |
minimumContribution = _minimumContribution; | |
deadline=block.number + _deadline; | |
goal=_goal; | |
admin = msg.sender; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function contribute() public payable { | |
//CONDITIONS FOR EXECUTING THE REST OF THE FUNCTION | |
require(msg.value > minimumContribution); | |
require(block.number < deadline); | |
// CHECKING WHETHER THIS IS THE FIRST TIME THAT APERSON IS CONTRIBUTING TO THIS CAMPAIGN | |
if(contributions[msg.sender] == 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRefund() public { | |
// REQUIRED CONDITIONS FOR GETTING A REFUND | |
require(block.number > deadline); | |
require(raisedAmount < goal); | |
require(contributions[msg.sender] > 0); | |
msg.sender.transfer(contributions[msg.sender]); | |
contributions[msg.sender] = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
modifier onlyAdmin { | |
require(msg.sender == admin); | |
_; | |
} | |
modifier goalReached { | |
require(raisedAmount >= goal); | |
_; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function voteForRequest(uint index) public goalReached { | |
// Directly referencing the specific spending request inside the requests dynamic array | |
Request storage thisRequest = requests[index]; | |
// Checking for the conditions | |
require(contributions[msg.sender] > 0); | |
require(thisRequest.voters[msg.sender] == false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makePayment(uint index) public onlyAdmin goalReached{ | |
// Directly referencing the specific spending request inside the requests dynamic array | |
Request storage thisRequest = requests[index]; | |
// Checking the necessary conditions | |
require(thisRequest.completed == false); | |
require(thisRequest.numberOfVoters > totalContributors / 2);//more than 50% voted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createSpendingRequest(string _description, address _recipient, uint _value) public onlyAdmin goalReached{ | |
// INSTANTIATING A Request STRUCT IN THE MEMORY | |
Request memory newRequest = Request( | |
{ | |
description:_description, | |
value:_value, | |
recipient:_recipient, | |
numberOfVoters:0, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof window !== "undefined" && typeof window.web3 !== "undefined") { | |
// In the browser and metamask is running | |
web3 = new Web3(window.web3.currentProvider); | |
} else { | |
// We are on the server *OR* the user is not running metamask | |
const provider = new Web3.providers.HttpProvider( | |
"https://rinkeby.infura.io/v3/bb20540179824ccb94cf748904d43090" | |
); | |
web3 = new Web3(provider); | |
} |