Created
April 30, 2020 09:00
-
-
Save beginnerJq/f8cbfc1daea2ea94bb3cb72ee87e2c2b to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.17+commit.d19bba13.js&optimize=false&gist=
This file contains hidden or 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
pragma solidity ^0.6.6; | |
contract Car { | |
string public brand; | |
constructor(string memory initialBrand) public { | |
brand = initialBrand; | |
} | |
function setBrand(string memory newBrand) public { | |
brand = newBrand; | |
} | |
} |
This file contains hidden or 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
pragma solidity >=0.4.22 <0.6.0; | |
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () public { | |
ballotToTest = new Ballot(2); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(1); | |
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
This file contains hidden or 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
pragma solidity ^0.4.17; | |
/** | |
* @title SafeMath | |
* @dev Math operations with safety checks that throw on error | |
*/ | |
library SafeMath { | |
function mul(uint a, uint b) internal pure returns (uint) { | |
uint c = a * b; | |
assert(a == 0 || c / a == b); | |
return c; | |
} | |
function div(uint a, uint b) internal pure returns (uint) { | |
// assert(b > 0); // Solidity automatically throws when dividing by 0 | |
uint c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
function sub(uint a, uint b) internal pure returns (uint) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint a, uint b) internal pure returns (uint) { | |
uint c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} | |
contract Project { | |
using SafeMath for uint; | |
struct Payment { | |
string description; | |
uint256 amount; | |
address receiver; | |
bool completed; | |
mapping(address => bool) voters; | |
uint voterCount; | |
} | |
address public owner; | |
string public description; | |
uint256 public minInvest; | |
uint256 public maxInvest; | |
uint256 public goal; | |
uint public investorCount; | |
mapping(address => uint) public investors; | |
Payment[] public payments; | |
modifier ownerOnly() { | |
require(msg.sender == owner); | |
_; | |
} | |
constructor( | |
string _description, | |
uint256 _minInvest, | |
uint256 _maxInvest, | |
uint256 _goal | |
) public { | |
owner = msg.sender; | |
description = _description; | |
minInvest = _minInvest; | |
maxInvest = _maxInvest; | |
goal = _goal; | |
} | |
function contribute() public payable { | |
require(msg.value >= minInvest); | |
require(msg.value <= maxInvest); | |
uint newBalance = 0; | |
newBalance = address(this).balance.add(msg.value); | |
require(newBalance <= goal); | |
investors[msg.sender] = msg.value; | |
investorCount += 1; | |
} | |
function createPayment( | |
string _description, | |
uint256 _amount, | |
address _receiver | |
) ownerOnly public { | |
Payment memory newPayment = Payment({ | |
description: _description, | |
amount: _amount, | |
receiver: _receiver, | |
completed: false, | |
voterCount: 0 | |
}); | |
payments.push(newPayment); | |
} | |
function approvePayment(uint256 index) public { | |
Payment storage payment = payments[index]; | |
// must be investor to vote | |
require(investors[msg.sender] > 0); | |
// can not vote twice | |
require(!payment.voters[msg.sender]); | |
payment.voters[msg.sender] = true; | |
payment.voterCount += 1; | |
} | |
function doPayment(uint256 index) ownerOnly public { | |
Payment storage payment = payments[index]; | |
require(!payment.completed); | |
require(address(this).balance >= payment.amount); | |
// require(payment.voters.length > (investors.length / 2)); | |
require(payment.voterCount > (investorCount / 2)); | |
payment.receiver.transfer(payment.amount); | |
payment.completed = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment