Skip to content

Instantly share code, notes, and snippets.

Avatar

Ankit Brahmbhatt ankitbrahmbhatt1997

View GitHub Profile
@ankitbrahmbhatt1997
ankitbrahmbhatt1997 / web3_setup
Created Mar 17, 2019
ethereum stack exchange question
View web3_setup
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);
}
@ankitbrahmbhatt1997
ankitbrahmbhatt1997 / FundRaising-8
Created Feb 21, 2019
making a payment to the third party
View FundRaising-8
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
View FundRaising-7
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);
View FundRaising-6
modifier onlyAdmin {
require(msg.sender == admin);
_;
}
modifier goalReached {
require(raisedAmount >= goal);
_;
}
View FundRaising-5
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,
View FundRaising-4
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;
@ankitbrahmbhatt1997
ankitbrahmbhatt1997 / FundRaising-3
Last active Feb 21, 2019
A payable function for contribution
View FundRaising-3
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)
@ankitbrahmbhatt1997
ankitbrahmbhatt1997 / FundRaising-2
Last active Feb 21, 2019
Constructor For the Fundraising contract
View FundRaising-2
constructor(uint _deadline,uint _goal,uint _minimumContribution) public{
minimumContribution = _minimumContribution;
deadline=block.number + _deadline;
goal=_goal;
admin = msg.sender;
}
View Fundraising-1
// Defining a Struct for a Spending Request
struct Request {
string description;
uint value;
address recipient;
bool completed;
uint numberOfVoters;
mapping(address=>bool) voters;
}