Skip to content

Instantly share code, notes, and snippets.

@V1rtuousCycle
Created June 13, 2018 06:28
Show Gist options
  • Save V1rtuousCycle/71d881b41155a82b35d5521a5b82fd19 to your computer and use it in GitHub Desktop.
Save V1rtuousCycle/71d881b41155a82b35d5521a5b82fd19 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.20+commit.3155dd80.js&optimize=false&gist=
pragma solidity ^0.4.11;
contract SimpleAuction {
// Parameters of the auction. Times are either
// absolute unix timestamps (seconds since 1970-01-01)
// or time periods in seconds.
// final state of the auction
address public beneficiary;
uint public auctionEnd;
bool ended;
// current state of the auction
address public highestBidder;
uint public highestBid;
// Allowed withdrawls of previous bids
mapping(address => uint) pendingReturns;
event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
/// Three slash comments will be shown when a user is asked to confirm a transaction.
/// These are known as natspec comments
/// Create a simple auction with `_biddingTime`
/// seconds bidding time on behalf of the
/// beneficiary address `_beneficiary`.
function SimpleAuction(uint _biddingTime, address _beneficiary) public {
beneficiary = _beneficiary;
auctionEnd = now + _biddingTime;
}
/// Bid on the auction with the value sent
/// together with this transaction.
/// The value will only be refunded if the
/// auction is not won.
function bid() public payable {
require(now <= auctionEnd);
require(msg.value > highestBid);
if (highestBidder != 0) {
pendingReturns[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
HighestBidIncreased(msg.sender, msg.value);
}
function withdraw() public returns (bool) {
uint amount = pendingReturns[msg.sender];
if (amount > 0) {
// its important to set to 0 first because the recipient can call this function again as part of the receiving call
pendingReturns[msg.sender] = 0;
if (!msg.sender.send(amount)) {
// no need to call throw here,just reset the amount owing
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
/// End the auction and send the highest bid to the beneficiary
function auctionEnd() public {
// It is a good guideline to structure functions that interact
// with other contracts (i.e. they call functions or send Ether)
// into three phases:
// 1. checking conditions
// 2. performing actions (potentially changing conditions)
// 3. interacting with other contracts
// If these phases are mixed up, the other contract could call
// back into the current contract and modify the state or cause
// effects (ether payout) to be performed multiple times.
// If functions called internally include interaction with external
// contracts, they also have to be considered interaction with
// external contracts.
// 1. Conditions
require(now >= auctionEnd);
require(!ended);
// 2. Effects
ended = true;
AuctionEnded(highestBidder, highestBid);
// 3. Interactions
beneficiary.transfer(highestBid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment