-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuctionHouse.sol
94 lines (74 loc) · 2.9 KB
/
AuctionHouse.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
pragma solidity 0.4.9;
contract AuctionHouse {
//Used for debugging purposes only.
event Print(uint256 output);
event PrintAddr(address addr);
//Easily can be handled by even listener on the client and implication is basically
// Listening for new auctions, notifying an user and placing bids as fast as possible.
event NewBid(uint256 amount,string item);
event AuctionOpened(address seller, string item);
event AuctionClosed(address seller, string item);
enum AuctionState {Inactive,Active} //We want to track all new and previous auctions.
modifier ifThereIsNoAuctionForTheUser() {
var hasAlreadyAuction = auctions[msg.sender].isInitialized;
if(hasAlreadyAuction) throw;
_;
}
modifier andBidderIsNotTheAuctionOwner(address seller){
PrintAddr(seller);
PrintAddr(msg.sender);
if(seller == msg.sender) throw;
_;
}
modifier ifAuctionExists (address seller){
var hasAlreadyAuction = auctions[seller].isInitialized;
if(!hasAlreadyAuction) throw;
_;
}
struct Auction {
string item;
Bid[] bids;
AuctionState state;
bool isInitialized; //used only because key in mapping always maps toa value.
//so by default boolean are false so what ever this default value it is not initialized
}
struct Bid {
address issuer;
uint256 amount;
}
mapping (address => Auction) public auctions;
function startAuctionOf(string item) ifThereIsNoAuctionForTheUser {
var seller = msg.sender;
Auction newAuction;
newAuction.item = item;
newAuction.isInitialized = true;
auctions[seller] = newAuction;
AuctionOpened(seller,item);
}
function placeBid(address seller, uint256 amount) ifAuctionExists(seller) andBidderIsNotTheAuctionOwner(seller) returns (bool bidPlacedSuccessfully){
var auction = auctions[seller];
var bid = Bid(msg.sender,amount);
if(auction.bids.length > 0) {
var highestBid = auction.bids[auction.bids.length-1];
Print(highestBid.amount);
if(amount > highestBid.amount) {
auction.bids.push(bid);
NewBid(bid.amount, auction.item);
return true;
}
return false;
} else {
auction.bids.push(bid);
Print(bid.amount);
NewBid(bid.amount, auction.item);
return true;
}
}
function closeAuction() ifAuctionExists(msg.sender) returns (bool auctionClosedSuccessfully) {
var auction = auctions[msg.sender];
auction.isInitialized = false;
auction.state = AuctionState.Inactive;
AuctionClosed(msg.sender,auction.item);
return true;
}
}