-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathKohINoor.java
75 lines (66 loc) · 2.23 KB
/
KohINoor.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
package bt.sample;
import bt.*;
import bt.compiler.CompilerVersion;
import bt.compiler.TargetCompilerVersion;
/**
* The 'Koh-i-Noor' smart contract.
*
* This smart contract was designed to be single, in the sense that there is
* only one of his category in the entire world.
*
* When created, the contract is <i>owned</i> by the creator and a selling price
* of 5000 BURST is set. Anyone that transfers more than this amount is the new
* owner. When a new owner is set, the contract price is automatically increased by
* 10%.
*
* So, every new owner either have 10% return of investment (minus 1% fee) or
* keep the ownership of the Koh-i-Noor.
*
* @author jjos
*/
@TargetCompilerVersion(CompilerVersion.v0_0_0)
public class KohINoor extends Contract {
public static final long ACTIVATION_FEE = ONE_BURST * 30;
public static final long INITIAL_PRICE = ONE_BURST * 5000;
Address owner;
long balance;
long price, fee, activationFee;
/**
* Constructor, when in blockchain the constructor is called when the first TX
* reaches the contract.
*/
public KohINoor() {
owner = getCreator();
price = INITIAL_PRICE;
activationFee = ACTIVATION_FEE;
}
/**
* A buyer needs to transfer the current price to the contract.
*
* Current owner will receive this amount and the sender will become the new
* owner. On this event, the price is increased by 10% automatically.
*
*/
public void txReceived() {
if (getCurrentTxAmount() + activationFee >= price) {
// Conditions match, let's execute the sale
fee = price / 100; // 1% fee
sendAmount(price - fee, owner); // pay the current owner the price, minus fee
sendMessage("Koh-i-Noor has a new owner.", owner);
owner = getCurrentTxSender(); // new owner
sendMessage("You are the Koh-i-Noor owner!", owner);
price += 10 * price / 100; // new price is 10% more
return;
}
// send back funds of an invalid order
sendMessage("Amount sent was not enough.", getCurrentTxSender());
sendAmount(getCurrentTxAmount(), getCurrentTxSender());
}
@Override
protected void blockFinished() {
// round up with the creator if there is some balance left
balance = getCurrentBalance();
if(balance > activationFee)
sendAmount(balance - activationFee, getCreator());
}
}