Skip to content

Instantly share code, notes, and snippets.

@darrendbutler
Created November 14, 2018 01:59
Show Gist options
  • Select an option

  • Save darrendbutler/660c73a4e3c85500dcd76d54c6aab7dc to your computer and use it in GitHub Desktop.

Select an option

Save darrendbutler/660c73a4e3c85500dcd76d54c6aab7dc 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.4.25+commit.59dbf8f1.js&optimize=false&gist=
pragma solidity ^0.4.25;
contract Escrow {
enum State {AWAITING_PAYMENT, AWAITING_DELIVERY, COMPLETE, REFUNDED}
State public currentState;
modifier buyerOnly() { require(msg.sender == buyer || msg.sender == arbiter); _; }
modifier sellerOnly() { require(msg.sender == seller || msg.sender == arbiter); _; }
modifier inState(State expectedState) { require(currentState == expectedState); _; }
address public buyer;
address public seller;
address public arbiter;
constructor(address _buyer, address _seller, address _arbiter) public {
buyer = _buyer;
seller = _seller;
arbiter = _arbiter;
}
function sendPayment() buyerOnly inState(State.AWAITING_PAYMENT) public payable {
currentState = State.AWAITING_DELIVERY;
}
function confirmDelivery() buyerOnly inState(State.AWAITING_DELIVERY) public {
seller.transfer(address(this).balance);
currentState = State.COMPLETE;
}
function refundBuyer() sellerOnly inState(State.AWAITING_DELIVERY) public {
buyer.transfer(address(this).balance);
currentState = State.REFUNDED;
}
}
/*pragma solidity ^0.4.25;//Specifies which version of Soldity compiler to use.
//Contracts are similar to classes. They can include State Variables,
//Functions, Function Modifiers, Events, Struct Types and Enum Types.
//They can also inherit fromother contrcts.
contract Escrow {
//State variables are values permanently stored in contract storage.
//Function modifiers are used to change the Behavior of functions.
//Enums are custom data types.
enum State {AWAITING_PAYMENT, AWAITING_DELIVERY, COMPLETE, REFUNDED}
//Instance of enum type "State"
State public currentState;
//"adress" types hold an Ethereum address that cannot be sent ether
address public buyer;
address public seller;
address public arbiter;
//Initialize the state varibales buyer, seller, and arbiter in this
//constructor.
//The currentState varibale is not initialized here because it is
//given the firsst value in the enum by default, which is AWAITING_PAYMENT
constructor(address _buyer, address _seller, address _arbiter) public {
buyer = _buyer;
seller = _seller;
arbiter = _arbiter;
}
//Modifier: inState
//Only lets a function be called if the current state is the expected state
modifier inState(State expectedState){
require (currentState == expectedState);
_;
}
//Modifier requires that only the seller or arbiter be able to invoke a
//following fucntion.
modifier sellerOnly() {
require(msg.sender == seller || msg.sender == arbiter);
_;
}
//Modifier: buyerOnly
//We can reuse this modifier for functions only the buyer should be able to
//call. Modifiers can be used as pre or post condition checks
modifier buyerOnly() {
require(msg.sender == buyer || msg.sender == arbiter);
_; //Represents the code in functions that will use this modifer
}
//Function: sendPayment
//Allows buyer to send ether to the contract
//"payable" is used for function meant to receive either
function sendPayment() public payable buyerOnly inState(State.AWAITING_PAYMENT){
//"require" is used to see if the user account used to invoke the function
//is the buyer. If it's not the buyer invoking the function,
//an exeption is thrown (all changes in state are undone)
//require(msg.sender == buyer);// global varibale to all contracts
currentState = State.AWAITING_DELIVERY;
}
//Function: confirmDelivery
//When the buyer receives the goods the state changes to COMPLETE
//Only the buyer should be able to invoke this. Otherwise, the seller
//can claim the the order is complete and take the money wihtout sending
//the goods
function confirmDelivery() public buyerOnly inState(State.AWAITING_DELIVERY) {
//The transfer function is available to all address types.
//balance and transfer are memmbers of address types
seller.transfer(address(this).balance);//cast the current contract to an
//address. Access the balance of this current contract
currentState = State.COMPLETE;
}
//Transfer funds to buyer as refund
function refundBuyer() sellerOnly inState(State.AWAITING_DELIVERY) public {
buyer.transfer(address(this).balance);
currentState = State.REFUNDED;
}
}*/
pragma solidity ^0.4.7;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./ballot.sol";
contract test3 {
Ballot ballotToTest;
function beforeAll () {
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 constant returns (bool) {
return ballotToTest.winningProposal() == 1;
}
}
//This is an abstract base class because it is not implemented
// BaseAuction.sol
pragma solidity ^0.4.25;
contract BaseAuction {
address public owner;
modifier ownerOnly() {
require(msg.sender == owner);
_;
}
//Events log information and allow applications to listen
//to whats happening
event AuctionComplete(address winner, uint bid);
event BidAccepted(address bidder, uint bid);
constructor() public {
owner = msg.sender;
}
function bid() external payable;
function end() external;
}
pragma solidity ^0.4.25;
import "./BaseAuction.sol";
import "./Withdrawable.sol";
contract TimerAuction is BaseAuction, Withdrawable {
string public item;
uint public auctionEnd;
bool public ended;
address public maxBidder;
uint public maxBid;
constructor(string _item, uint _durationMinutes) public {
item = _item;
auctionEnd = now + (_durationMinutes * 1 minutes);
}
function bid() external payable {
require(now < auctionEnd);
require(msg.value > maxBid);
if (maxBidder != address(0)) {
pendingWithdrawals[maxBidder] += maxBid;
}
maxBidder = msg.sender;
maxBid = msg.value;
emit BidAccepted(maxBidder, maxBid);
}
function end() external ownerOnly {
require(!ended);
require(now >= auctionEnd);
ended = true;
owner.transfer(maxBid);
emit AuctionComplete(maxBidder, maxBid);
}
}
/*// TimerAuction.sol
//This auction contract has a time limit
pragma solidity ^0.4.25;
//BaseAuction.sol is imported
import "./BaseAuction.sol";
//Derive TimerAuction from BaseAuction. All public state varibles and functions
//from BaseAuction are extended to TimerAuction
contract TimerAuction is BaseAuction, Withdrawable {
//The state varibales keep track of the item being auctioned.
string public item;
uint public auctionEnd;
bool public ended;
uint public maxBid;
address public maxBidder;
//What does the _item mean?
constructor(string _item, uint _durationMinutes) public {
//This is the item description
item = _item;//item refers to the instance varibale
//The auction end time is set from the current block time
//now is the current block timestamp (other name for block.timestamp)
auctionEnd = now + (_durationMinutes * 1 minutes);
}
function bid() external payable {
//Check that the auction did not end
//The action is still ongoing if the timestamp of the block is
//less than the value in actionEnd
require (now < actionEnd);
//Check that the bid by the sender is greater than the current bid
//The msg.value (wei being sent with message) must be greater
//than than maxBid
require (msg.value > maxBid);
//Refund the previous bid to the previous max bidder if the adrress
// isn't the 0 (if this ins't the first bid'
if (maxBidder != address(0)) {
pendingWithdrawals[maxBidder] += maxBid;
}
//Update the maxBid and maxBidder
maxBid = msg.value;//Value is the amount of wei being sent
maxBidder = msg.sender; //.sender is the address that came with the
//message
emit BidAccepted(maxBidder, maxBid);
function end() external ownerOnly {
// Check that time has expired and can only end once
require(!ended);
require(now >= auctionEnd);
// Update internal states and transfer ether to owner
ended = true;
owner.transfer(maxBid);
emit AuctionComplete(maxBidder, maxBid); // Emit event
}
/*
We need to update the new max bid and the bidder
We need to emit the BidAccepted event so it can be logged.
*/
// Withdrawable.sol
pragma solidity ^0.4.25;
contract Withdrawable {
//the key is an address type and the value is an unsigned integer.
mapping(address => uint) internal pendingWithdrawals;
function withdraw() public {
require(pendingWithdrawals[msg.sender] > 0);
uint amount = pendingWithdrawals[msg.sender];
pendingWithdrawals[msg.sender] = 0;
msg.sender.transfer(amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment