Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
azuredeviil053 committed Feb 22, 2018
0 parents commit 74a6713
Show file tree
Hide file tree
Showing 26 changed files with 629 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Reentrancy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pragma solidity ^0.4.17;
contract SimpleDeposit {
mapping (address => uint) balances;

event LogDepositMade(address from, uint amount);

modifier minAmount(uint amount) {
require(msg.value >= amount);
_;
}

function SimpleDeposit() public payable {
balances[msg.sender] = msg.value;
}

function deposit() public payable minAmount(1 ether) {
balances[msg.sender] += msg.value;
LogDepositMade(msg.sender, msg.value);
}

function getBalance() public view returns (uint balance) {
return balances[msg.sender];
}

function withdrawBalance() public {
uint amount = balances[msg.sender];
require(msg.sender.call.value(amount)()); // caller's code is executed and can re-enter withdrawBalance again
balances[msg.sender] = 0; // INSECURE - user's balance must be reset before the external call
}
}
24 changes: 24 additions & 0 deletions SimpleDeposit.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pragma solidity ^0.4.17;
contract SimpleDeposit {
mapping (address => uint) balances;

event LogDepositMade(address from, uint amount);

modifier minAmount(uint amount) {
require(msg.value >= amount);
_;
}

function SimpleDeposit() public payable {
balances[msg.sender] = msg.value;
}

function deposit() public payable minAmount(1 ether) {
balances[msg.sender] += msg.value;
LogDepositMade(msg.sender, msg.value);
}

function getBalance() public view returns (uint balance) {
return balances[msg.sender];
}
}
44 changes: 44 additions & 0 deletions action_and_control/CommitReveal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pragma solidity ^0.4.17;
contract CommitReveal {
struct Commit {string choice; string secret; string status;}
mapping(address => mapping(bytes32 => Commit)) public userCommits;

event LogCommit(bytes32, address);
event LogReveal(bytes32, address, string, string);

function CommitReveal() public {}

function commit(bytes32 _commit) public returns (bool success) {
var userCommit = userCommits[msg.sender][_commit];
if(bytes(userCommit.status).length != 0) {
return false; // commit has been used before
}
userCommit.status = "c"; // comitted
LogCommit(_commit, msg.sender);
return true;
}

function reveal(string _choice, string _secret, bytes32 _commit) public returns (bool success) {
var userCommit = userCommits[msg.sender][_commit];
bytes memory bytesStatus = bytes(userCommit.status);
if(bytesStatus.length == 0) {
return false; // choice not committed before
} else if (bytesStatus[0] == "r") {
return false; // choice already revealed
}
if (_commit != keccak256(_choice, _secret)) {
return false; // hash does not match commit
}
userCommit.choice = _choice;
userCommit.secret = _secret;
userCommit.status = "r"; // revealed
LogReveal(_commit, msg.sender, _choice, _secret);
return true;
}

function traceCommit(address _address, bytes32 _commit) public view returns (string choice, string secret, string status) {
var userCommit = userCommits[_address][_commit];
require(bytes(userCommit.status)[0] == "r");
return (userCommit.choice, userCommit.secret, userCommit.status);
}
}
22 changes: 22 additions & 0 deletions action_and_control/SendingFunds.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma solidity ^0.4.17;
contract Auction {
address public highestBidder;
uint highestBid;
mapping(address => uint) refunds;

function bid() public payable {
require(msg.value >= highestBid);
if (highestBidder != 0) {
// record the underlying bid to be refund
refunds[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
}

function withdrawRefund() public {
uint refund = refunds[msg.sender];
refunds[msg.sender] = 0;
msg.sender.transfer(refund);
}
}
16 changes: 16 additions & 0 deletions action_and_control/SendingFundsAntipattern.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pragma solidity ^0.4.17;
contract Auction {
address public highestBidder;
uint highestBid;

function bid() public payable {
require(msg.value >= highestBid);
if (highestBidder != 0) {
// if call fails causing a rollback,
// no one else can bid
highestBidder.transfer(highestBid);
}
highestBidder = msg.sender;
highestBid = msg.value;
}
}
38 changes: 38 additions & 0 deletions action_and_control/StateMachine.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pragma solidity ^0.4.17;
contract DepositLock {
enum Stages {
AcceptingDeposits,
FreezingDeposits,
ReleasingDeposits
}
Stages public stage = Stages.AcceptingDeposits;
uint public creationTime = now;
mapping (address => uint) balances;

modifier atStage(Stages _stage) {
require(stage == _stage);
_;
}

modifier timedTransitions() {
if (stage == Stages.AcceptingDeposits && now >= creationTime + 1 days)
nextStage();
if (stage == Stages.FreezingDeposits && now >= creationTime + 8 days)
nextStage();
_;
}

function nextStage() internal {
stage = Stages(uint(stage) + 1);
}

function deposit() public payable timedTransitions atStage(Stages.AcceptingDeposits) {
balances[msg.sender] += msg.value;
}

function withdraw() public timedTransitions atStage(Stages.ReleasingDeposits) {
uint amount = balances[msg.sender];
balances[msg.sender] = 0;
msg.sender.transfer(amount);
}
}
25 changes: 25 additions & 0 deletions action_and_control/oracle/Oracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pragma solidity ^0.4.17;
contract Oracle {
address knownSource = 0x123...; // known source
struct Request {
bytes data;
function(bytes memory) external callback;
}
Request[] requests;

event NewRequest(uint);

modifier onlyBy(address account) {
require(msg.sender == account); _;
}

function query(bytes data, function(bytes memory) external callback) public {
requests.push(Request(data, callback));
NewRequest(requests.length - 1);
}

// invoked by outside world
function reply(uint requestID, bytes response) public onlyBy(knownSource) {
requests[requestID].callback(response);
}
}
17 changes: 17 additions & 0 deletions action_and_control/oracle/OracleConsumer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.4.17;
import "./Oracle.sol";
contract OracleConsumer {
Oracle oracle = Oracle(0x123...); // known contract

modifier onlyBy(address account) {
require(msg.sender == account); _;
}

function updateExchangeRate() {
oracle.query("USD", this.oracleResponse);
}

function oracleResponse(bytes response) onlyBy(oracle) {
// use the data
}
}
29 changes: 29 additions & 0 deletions authorization/AccessRestriction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pragma solidity ^0.4.17;
import "./Ownership.sol";
contract AccessRestriction is Owned {
uint public creationTime = now;

modifier onlyBefore(uint _time) {
require(now < _time); _;
}

modifier onlyAfter(uint _time) {
require(now > _time); _;
}

modifier onlyBy(address account) {
require(msg.sender == account); _;
}

modifier condition(bool _condition) {
require(_condition); _;
}

modifier minAmount(uint _amount) {
require(msg.value >= _amount); _;
}

function f() payable onlyAfter(creationTime + 1 minutes) onlyBy(owner) minAmount(2 ether) condition(msg.sender.balance >= 50 ether) {
// some code
}
}
21 changes: 21 additions & 0 deletions authorization/Ownership.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pragma solidity ^0.4.17;
contract Owned {
address public owner;

event LogOwnershipTransferred(address indexed previousOwner, address indexed newOwner);

modifier onlyOwner() {
require(msg.sender == owner);
_;
}

function Owned() public {
owner = msg.sender;
}

function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
LogOwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
30 changes: 30 additions & 0 deletions lifecycle/AutomaticDeprecation.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pragma solidity ^0.4.17;
contract AutoDeprecate {
uint expires;

function AutoDeprecate(uint _days) public {
expires = now + _days * 1 days;
}

function expired() internal view returns (bool) {
return now > expires;
}

modifier willDeprecate() {
require(!expired());
_;
}

modifier whenDeprecated() {
require(expired());
_;
}

function deposit() public payable willDeprecate {
// some code
}

function withdraw() public view whenDeprecated {
// some code
}
}
17 changes: 17 additions & 0 deletions lifecycle/AutomaticDeprecation2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.4.17;
contract AutoDeprecate {
uint public constant BLOCK_NUMBER = 4400000;

modifier isActive() {
require(block.number <= BLOCK_NUMBER);
_;
}

function deposit() public isActive() {
// some code
}

function withdraw() public {
// some code
}
}
11 changes: 11 additions & 0 deletions lifecycle/Mortal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pragma solidity ^0.4.17;
import "../authorization/Ownership.sol";
contract Mortal is Owned {
function destroy() public onlyOwner {
selfdestruct(owner);
}

function destroyAndSend(address recipient) public onlyOwner {
selfdestruct(recipient);
}
}
19 changes: 19 additions & 0 deletions maintenance/Register.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity ^0.4.17;
import "../authorization/Ownership.sol";
contract Register is Owned {
address backendContract;
address[] previousBackends;

function Register() public {
owner = msg.sender;
}

function changeBackend(address newBackend) public onlyOwner() returns (bool) {
if(newBackend != backendContract) {
previousBackends.push(backendContract);
backendContract = newBackend;
return true;
}
return false;
}
}
19 changes: 19 additions & 0 deletions maintenance/Relay.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity ^0.4.17;
import "../authorization/Ownership.sol";
contract Relay is Owned {
address public currentVersion;

function Relay(address initAddr) public {
currentVersion = initAddr;
owner = msg.sender;
}

function changeContract(address newVersion) public onlyOwner() {
currentVersion = newVersion;
}

// fallback function
function() public {
require(currentVersion.delegatecall(msg.data));
}
}
Loading

0 comments on commit 74a6713

Please sign in to comment.