-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsimpleOpenAuction.iele
134 lines (108 loc) · 4.04 KB
/
simpleOpenAuction.iele
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2017 Runtime Verification, Inc. All Rights Reserved.
//
// This contract implements a simple open auction. When this contract is
// deployed with an account, the account is hosting an auction and can
// manage incoming bids, as well as close the auction and settle the transfer
// of funds from the final highest bidder to the auction's beneficiary.
contract SimpleOpenAuction {
// variable locations - the following account storage locations hold the
// described information:
// account address of the beneficiary of the auction
@beneficiary = 0
// timestamp of the start of the auction, bids received before that time
// will not be accepted
@auction.start = 1
// timestamp of the end of the auction, bids received after that time
// will not be accepted
@auction.end = 2
// account address of the current highest bidder
@highest.bidder = 3
// value of the current highest bid
@highest.bid = 4
// set to 1 after the auction has been settled (the auction end time has passed and
// the beneficiary has received the final highest bid, set to 0 otherwise
@settled = 5
// initializes an auction with the given beneficiary and bidding time duration
define @init(%beneficiary, %bidding.time) {
// init beneficiary
sstore %beneficiary, @beneficiary
// init auction start timestamp
%auction.start = call @iele.timestamp()
sstore %auction.start, @auction.start
// init auction end timestamp
%auction.end = add %auction.start, %bidding.time
sstore %auction.end, @auction.end
// init highest bidder, highest bid, and settled
sstore 0, @highest.bidder
sstore 0, @highest.bid
sstore 0, @settled
}
// sends a bid with value equal to the value sent with the corresponding
// account call
// if the bid becomes the new highest bid returns with exit status 0, else
// reverts with exit status -1
define public @bid() {
// check if auction is active
%timestamp = call @iele.timestamp()
%auction.end = sload @auction.end
%auction.closed = cmp ge %timestamp, %auction.end
br %auction.closed, revert.bid
// check if incoming bid is higher than current highest bid
%bid.value = call @iele.callvalue()
%highest.bid = sload @highest.bid
%low.bid = cmp le %bid.value, %highest.bid
br %low.bid, revert.bid
// check if we need to refund current highest bidder
%no.refund.needed = iszero %highest.bid
br %no.refund.needed, update.bid
refund.bid:
// refund current highest bidder
%highest.bidder = sload @highest.bidder
%gas = call @iele.gas()
%status = call @deposit at %highest.bidder() send %highest.bid, gaslimit %gas
br %status, throw
update.bid:
// store the new highest bidder and bid
%new.highest.bidder = call @iele.caller()
sstore %new.highest.bidder, @highest.bidder
sstore %bid.value, @highest.bid
// return with exit status 0
ret void
revert.bid:
// the auction was closed or incoming bid was not high enough, revert so
// that the bidder gets their bid back
revert -1
throw:
call @iele.invalid()
}
// settles the auction by sending the final highest bid value to the
// beneficiary and returns the address of the final highest bidder's
// account
// if the auction is still active, reverts with exit status -1
define public @settle.auction() {
// check if auction has been closed
%timestamp = call @iele.timestamp()
%auction.end = sload @auction.end
%auction.active = cmp lt %timestamp, %auction.end
br %auction.active, auction.active
// check if auction has been already settled
%already.settled = sload @settled
br %already.settled, exit
// send highest bid to beneficiary and settle the auction
%highest.bid = sload @highest.bid
%beneficiary = sload @beneficiary
%gas = call @iele.gas()
%status = call @deposit at %beneficiary() send %highest.bid, gaslimit %gas
br %status, throw
sstore 1, @settled
exit:
// return the address of the final highest bidder's account
%highest.bidder = sload @highest.bidder
ret %highest.bidder
auction.active:
// auction is still active, revert with exit status -1
revert -1
throw:
call @iele.invalid()
}
}