-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAuction.java
121 lines (103 loc) · 3.2 KB
/
Auction.java
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
package bt.sample;
import bt.*;
import bt.compiler.CompilerVersion;
import bt.compiler.TargetCompilerVersion;
import bt.ui.EmulatorWindow;
/**
* Auction smart contract.
*
* An Auction smart contract that will allow people to send funds which
* will be refunded back to them (minus the activation fee) if someone
* else sends more. Minimum bid should be higher than the INITIAL_PRICE.
*
* After the auction timed-out a final transaction (likely to come from
* the beneficiary) is needed to trigger the payment and close the auction.
*
* This auction contract cannot be reused, once closed or timedout the
* auction is never open again.
*
* Inspired by http://ciyam.org/at/at_auction.html
*
* @author jjos
*/
@TargetCompilerVersion(CompilerVersion.v0_0_0)
public class Auction extends Contract {
public static final String BENEFICIARY = "BURST-TSLQ-QLR9-6HRD-HCY22";
public static final long INITIAL_PRICE = 1000*ONE_BURST;
public static final int TIMEOUT_MIN = 40; // 40 minutes == 10 blocks
public static final int ACTIVATION_FEE = 22; // expected activation fee in BURST
boolean isOpen;
Address beneficiary;
long highestBid;
Address highestBidder;
Timestamp timeout;
long newBid, fee;
/**
* Constructor, when in blockchain the constructor is called when the first TX
* reaches the contract.
*/
public Auction(){
beneficiary = parseAddress(BENEFICIARY);
timeout = getBlockTimestamp().addMinutes(TIMEOUT_MIN);
isOpen = true;
highestBid = INITIAL_PRICE; // Start value, we will not accept less than this
highestBidder = null;
}
/**
* Private function for checking if the timeout expired.
*
* @return true if this contract expired
*/
private boolean expired(){
if(!isOpen)
return true;
if(getBlockTimestamp().ge(timeout)) {
isOpen = false;
fee = getCurrentBalance()/100;
sendAmount(fee, getCreator());
// send the funds (best auction) to the beneficiary, minus the current TX
// amount that will be refunded
sendAmount(getCurrentBalance() - getCurrentTxAmount(), beneficiary);
}
return !isOpen;
}
/**
* Any new transaction received will be handled by this function.
*/
public void txReceived(){
if(expired()){
// Send back this last funds or they will be lost
refund();
return;
}
newBid = getCurrentTxAmount() + ACTIVATION_FEE;
if(newBid > highestBid){
// we have a new higher bid, return the previous one
if(highestBidder != null) {
sendAmount(highestBid - ACTIVATION_FEE, highestBidder);
}
highestBidder = getCurrentTxSender();
highestBid = newBid;
}
else {
// just send back
refund();
}
}
/**
* Refunds the last received transaction
*/
private void refund() {
sendAmount(getCurrentTxAmount(), getCurrentTxSender());
}
public static void main(String[] args) throws Exception {
// some initialization code to make things easier to debug
Emulator emu = Emulator.getInstance();
Address creator = Emulator.getInstance().getAddress(BENEFICIARY);
emu.airDrop(creator, 1000*Contract.ONE_BURST);
Address auction = Emulator.getInstance().getAddress("AUCTION");
emu.createConctract(creator, auction, Auction.class, Contract.ONE_BURST);
emu.forgeBlock();
new EmulatorWindow(Auction.class);
}
}