forked from signum-network/signum-smartj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Will.java
102 lines (89 loc) · 2.06 KB
/
Will.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
package bt.sample;
import bt.Contract;
import bt.EmulatorWindow;
import bt.Address;
import bt.Timestamp;
/**
* A "will" contract that transfers its balance to another account
* if a given timeout is reached.
*
* Inspired by http://ciyam.org/at/at_dormant.html
*
* @author jjos
*/
class Will extends Contract {
Address payoutAddress;
Timestamp timeout;
boolean finished;
/**
* Constructor, when in blockchain the constructor is called when the first TX
* reaches the contract.
*/
public Will(){
payoutAddress = parseAddress("BURST-TSLQ-QLR9-6HRD-HCY22");
timeout = getBlockTimestamp().addMinutes(30000);
finished = false;
}
/**
* Sets a new timeout in minutes from now (if not expired)
*
* @param minutes
*/
public void setTimeout(int minutes){
if(!getCurrentTx().getSenderAddress().equals(getCreator()))
// only creator is allowed
return;
if(expired()){
txReceived();
return;
}
timeout = getBlockTimestamp().addMinutes(minutes);
}
/**
* Private function for checking if the timeout expired
*
* @return true if this contract expired
*/
private boolean expired(){
return finished || getBlockTimestamp().ge(timeout);
}
/**
* Private function that pays this contract.
*/
private void pay(){
sendBalance(payoutAddress);
finished = true;
}
/**
* Sets a new payout address (if not expired)
*
* @param newPayout
*/
public void setPayoutAddress(Address newPayout){
if(!getCurrentTx().getSenderAddress().equals(this.getCreator()))
// only creator is allowed
return;
if(expired()){
txReceived();
return;
}
payoutAddress = newPayout;
}
/**
* Any call not recognized will be handled by the this function
*/
@Override
public void txReceived(){
if(finished && getCurrentTx().getAmount()>0){
// Send back funds or they will be lost
sendAmount(getCurrentTx().getAmount(), getCurrentTx().getSenderAddress());
}
else if(expired()){
pay();
}
// Otherwise do nothing, wait for the timeout
}
public static void main(String[] args) {
new EmulatorWindow(Will.class);
}
}