Skip to content

Instantly share code, notes, and snippets.

@y12studio
Created March 21, 2016 09:18
Show Gist options
  • Save y12studio/05887d050c3a951ff875 to your computer and use it in GitHub Desktop.
Save y12studio/05887d050c3a951ff875 to your computer and use it in GitHub Desktop.
Y12 Simple Open Auction 3
// Solidity by Example — Solidity 0.2.0 documentation http://solidity.readthedocs.org/en/latest/solidity-by-example.html
// Ethereum以太坊智能合約環境說明 https://gist.github.com/y12studio/f142cd9789c7d396d8f7
contract YSOA {
// time periods in seconds.
address public beneficiary;
address public feeDao;
uint public auctionStart;
uint public biddingTime;
uint32 public feePpm;
uint8 public luckPercent;
uint public feeBalance;
// Current state of the auction.
address public highestBidder;
uint public highestBid;
// Set to true at the end, disallows any change
bool ended;
// Events that will be fired on changes.
event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
// Create a simple auction with `_biddingTime`
// seconds bidding time on behalf of the
// beneficiary address `_beneficiary`.
// feeDao address
// feeRate parts per million(PPM), 0.256% = 2560 ppm
//
function YSOA(uint _biddingTime, address _beneficiary, address _feeDao, uint32 _feePpm, uint8 _luckPercent) {
beneficiary = _beneficiary;
feeDao = _feeDao;
feePpm = _feePpm;
auctionStart = now;
biddingTime = _biddingTime;
luckPercent = _luckPercent;
}
/// 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() {
// No arguments are necessary, all
// information is already part of
// the transaction.
if (now > auctionStart + biddingTime)
// Revert the call if the bidding
// period is over.
throw;
if (msg.value <= highestBid)
// If the bid is not higher, send the
// money back.
throw;
if (highestBidder != 0) {
uint ran = randomGen(feeBalance);
uint fee = highestBid/1000000*feePpm;
if(ran<luckPercent){
fee = 0;
}
feeBalance += fee;
highestBidder.send(highestBid-fee);
}
highestBidder = msg.sender;
highestBid = msg.value;
HighestBidIncreased(msg.sender, msg.value);
}
// End the auction and send the highest bid to the beneficiary.
function auctionEnd() {
if (now <= auctionStart + biddingTime)
throw; // auction did not yet end
if (ended)
throw; // this function has already been called
AuctionEnded(highestBidder, highestBid);
uint benAmount = this.balance-feeBalance;
if(feeBalance>0){
feeDao.send(feeBalance);
}
beneficiary.send(benAmount);
ended = true;
}
function rescueBalanceDevOnly(){
beneficiary.send(this.balance);
}
// ref to https://gist.github.com/alexvandesande/259b4ffb581493ec0a1c
// Generates a random number from 0 to 100 based on the last block hash
function randomGen(uint seed) constant returns(uint randomNumber) {
return (uint(sha3(block.blockhash(block.number - 1), seed)) % 100);
}
function () {
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment