Commit 09c1f8f7 authored by BHAVYA CHOUDHARY's avatar BHAVYA CHOUDHARY

add all the auctions

parent b0963cbd
pragma solidity ^0.4.4;
import "DAuction.sol";
import "DVickreyAuction.sol";
import "DDutchAuction.sol";
import "DEnglishAuction.sol";
//This code is provided for you. You can modify if you want to for debugging, but you shouldn't need to put any logic here.
contract DAuctions {
mapping(uint256 => DAuction) auctions;
uint256 numAuctions;
// This function is used for testing
function testAuction(uint256 id) returns (uint256 aid){
return numAuctions;
}
function beginDutchAuction(uint256 reservePrice, address judgeAddress, uint256 biddingTimePeriod, uint256 offerPriceDecrement) returns(uint256 auctionID) {
auctionID = numAuctions++;
auctions[auctionID] = new DDutchAuction(reservePrice, judgeAddress, biddingTimePeriod, offerPriceDecrement);
return auctionID;
}
function beginEnglishAuction(uint256 reservePrice, address judgeAddress, uint256 biddingTimePeriod, uint256 minBidIncrement) returns(uint256 auctionID) {
auctionID = numAuctions++;
auctions[auctionID] = new DEnglishAuction(reservePrice, judgeAddress, biddingTimePeriod, minBidIncrement);
return auctionID;
}
function beginVickreyAuction(uint256 reservePrice, address judgeAddress, uint256 commitTimePeriod, uint256 revealTimePeriod, uint256 bidDepositAmount) returns(uint256 auctionID) {
auctionID = numAuctions++;
auctions[auctionID] = new DVickreyAuction(reservePrice, judgeAddress, commitTimePeriod, revealTimePeriod, bidDepositAmount);
return auctionID;
}
function bid(uint256 id) payable returns(address) {
return auctions[id].bid.value(msg.value)();
}
function finalize(uint256 id) {
auctions[id].finalize();
}
function refund(uint256 id, uint256 amount) {
auctions[id].refund(amount);
}
function revealBid(uint256 id, uint256 nonce) payable returns(address) {
return auctions[id].revealBid.value(msg.value)(nonce);
}
function commitBid(uint256 id, bytes32 bidCommitment) payable returns(bool) {
return auctions[id].commitBid.value(msg.value)(bidCommitment);
}
}
pragma solidity ^0.4.4;
import "DAuction.sol";
contract DDutchAuction is DAuction {
uint256 decrementPerBlock;
bool bidover;
uint256 startPrice;
bool refundCalled;
bool finalizeCalled;
// constructor
function DDutchAuction(uint256 reservePrice, address judgeAddress, uint256 biddingPeriod, uint256 offerPriceDecrement) DAuction(reservePrice, biddingPeriod, judgeAddress) {
decrementPerBlock = offerPriceDecrement;
startPrice = reservePrice+biddingPeriod*offerPriceDecrement;
}
function bid() biddingOpen payable returns(address highestBidderAddr){
uint256 currentPrice = startPrice-(block.number-startBlockNum)*decrementPerBlock;
require(msg.value >= currentPrice);
highestBidder = msg.sender;
highestBid = msg.value;
bidover = true;
return highestBidder;
}
function finalize(){
require(msg.sender==highestBidder || msg.sender = judgeAddress);
require(!refundCalled && !finalizeCalled);
finalizeCalled=truel
creator.transfer(highestBid);
}
function refund(uint256 refundAmount) auctionOver judgeOnly {
require(highestBidder!=0);
require(refundAmount<= this.balance);
require(!finalizeCalled && !refundCalled);
refundCalled=true;
highestBidder.transfer(refundAmount);
creator.transfer(this.balance);
}
modifier biddingOpen {
require(block.number<=startBlockNum+biddingBlocksNum);
require(!bidover);
_;
}
modifier auctionOver {
require(block.number>startBlockNum+biddingBlocksNum || bidover);
_;
}
}
pragma solidity ^0.4.4;
import "DAuction.sol";
contract DEnglishAuction is DAuction {
uint256 minBidIncrement;
uint numUnchallengedBlocks;
uint lastBidBlock;
mapping (address => uint) PendingWithdrawals;
bool refundCalled;
bool finalizeCalle
// constructor
function DEnglishAuction(uint256 reservePrice, address judgeAddress, uint256 biddingTimePeriod, uint256 minBidIncrement) DAuction(reservePrice, biddingTimePeriod, judgeAddress) {
this.minBidIncrement = minBidIncrement;
numUnchallengedBlocks = 3;
}
function bid() biddingOpen payable returns(address highestBidderAddr) {
require(msg.value >= minimumPrice);
require(msg.value >= highestBid + minBidIncrement);
if(highestBidder!=0){
if(!highestBidder.send(highestBid)){
PendingWithdrawals[highestBidder]+=highestBid;
}
}
highestBidder = msg.sender;
highestBid = msg.value;
lastBidBlock = block.number;
return highestBidder;
}
function finalize() auctionOver {
require(msg.sender==highestBidder || msg.sender = judgeAddress);
require(!refundCalled && !finalizeCalled);
finalizeCalled=true;
creator.transfer(highestBid);
}
function refund(uint256 refundAmount) auctionOver judgeOnly {
require(highestBidder!=0);
require(refundAmount<= this.balance);
require(!finalizeCalled && !refundCalled);
refundCalled = true;
highestBidder.transfer(refundAmount);
creator.transfer(highestBid-refundAmount);
}
function withdraw(){
int amount = PendingWithdrawals[msg.sender];
PendingWithdrawals[msg.sender]=0;
if(amount>0){
msg.sender.transfer(amount);
}
}
modifier auctionOver {
require(block.number > startBlockNum+biddingBlocksNum);
_;
}
modifier biddingOpen {
require(lastBidBlock+numUnchallengedBlocks>=block.number);
require(block.number<=startBlockNum+biddingBlocksNum);
_;
}
}
...@@ -3,62 +3,103 @@ import "DAuction.sol"; ...@@ -3,62 +3,103 @@ import "DAuction.sol";
contract DVickreyAuction is DAuction { contract DVickreyAuction is DAuction {
uint256 public bidDepositAmount; uint256 public bidDepositAmount;
uint256 public revealBlocksNum; uint256 public revealBlocksNum;
uint256 secondHighestBid; uint256 secondHighestBid;
bool refundCalled; bool refundCalled;
bool finalizeCalled; bool finalizeCalled;
mapping ( address => mapping ( bytes32 => uint32) ) deposits; // maps address,bid => count
// constructor
function DVickreyAuction(uint256 reservePrice, address judgeAddress, uint256 commitTimePeriod, uint256 revealTimePeriod, uint256 _bidDepositAmount) DAuction(reservePrice, commitTimePeriod, judgeAddress) { mapping ( address => uint ) bidAmount;
bidDepositAmount = _bidDepositAmount;
revealBlockNum = revealTimePeriod; // constructor
} function DVickreyAuction(uint256 reservePrice, address judgeAddress, uint256 commitTimePeriod, uint256 revealTimePeriod, uint256 _bidDepositAmount) DAuction(reservePrice, commitTimePeriod, judgeAddress) {
bidDepositAmount = _bidDepositAmount;
function commitBid(bytes32 bidCommitment) biddingOpen payable returns(bool) { revealBlockNum = revealTimePeriod;
throw; }
}
function commitBid(bytes32 bidCommitment) biddingOpen payable returns(bool) {
function revealBid(uint256 nonce) revealOpen payable returns(address highestBidder) { require(msg.value == bidDepositAmount);
throw; deposits[msg.sender][bidCommitment]+=msg.value;
} return true;
}
function finalize() auctionover {
require(msg.sender == highestBidder);
require(!refundCalled && !finalizeCalled); function revealBid(uint256 nonce) revealOpen payable returns(address highestBidder) {
finalizeCalled=true; bytes32 commitval = sha3(nonce,msg.value);
creator.transfer(secondHighestBid); require(deposits[msg.sender][commitval]>0);
} deposits[msg.sender][commitval]--;
bidAmount[msg.sender]+=msg.value;
function refund(uint256 refundAmount) auctionOver judgeOnly { if(msg.value>secondHighestBid && msg.value>=minimumPrice){
require(highestBidder!=0); if(msg.value>highestBid){
require(refundAmount<= this.balance); secondHighestBid = highestBid;
require(!finalizeCalled && !refundCalled); highestBid = msg.value;
refundCalled=true; highestBidder = msg.sender;
highestBidder.transfer(refundAmount); }else{
creator.transfer(secondHighestBid-refundAmount); secondHighestBid = msg.value;
} }
}
function withdraw() auctionover returns (uint){ msg.sender.transfer(bidDepositAmount);
throw; returns highestBidder;
} }
modifier auctionover { function finalize() auctionover {
require(block.blockNum > startBlockNum+biddingBlocksNum+revealBlocksNum); require(msg.sender == highestBidder);
_; require(!refundCalled && !finalizeCalled);
} finalizeCalled=true;
if(secondHighestBid==0 && highestBid!=0){
modifier biddingOpen { creator.transfer(minimumPrice);
require(block.blockNum <= startBlockNum+biddingBlocksNum); }else if(secondHighestBid>0){
_; creator.transfer(secondHighestBid);
} }
}
modifier revealOpen {
require(block.blockNum > startBlockNum+biddingBlocksNum); function refund(uint256 refundAmount) auctionOver judgeOnly {
require(block.blockNum <= startBlockNum+biddingBlocksNum+revealBlocksNum); require(highestBidder!=0);
_; require(refundAmount<=this.balance);
} require(!finalizeCalled && !refundCalled);
refundCalled=true;
highestBidder.transfer(refundAmount);
if(secondHighestBid==0 && highestBid>0){
if(minimumPrice>refundAmount)
creator.transfer(minimumPrice-refundAmount)
}else if (secondHighestBid>refundAmount)
creator.transfer(secondHighestBid-refundAmount);
}
function withdraw() auctionover returns (uint){
uint amount;
if(msg.sender==highestBidder){
uint payAmount = secondHighestBid;
if(secondHighestBid == 0)
payAmount = minimumPrice;
amount = bidAmount[msg.sender]-payAmount;
bidAmount[msg.sender]=payAmount;
msg.sender.transfer(amount);
return amount;
}
amount = bidAmount[msg.sender];
bidAmount[msg.sender] = 0;
msg.sender.transfer(amount);
return amount;
}
modifier auctionover {
require(block.blockNum > startBlockNum+biddingBlocksNum+revealBlocksNum);
_;
}
modifier biddingOpen {
require(block.blockNum <= startBlockNum+biddingBlocksNum);
_;
}
modifier revealOpen {
require(block.blockNum > startBlockNum+biddingBlocksNum);
require(block.blockNum <= startBlockNum+biddingBlocksNum+revealBlocksNum);
_;
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment