-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathCrowdfundPlatform.java
72 lines (61 loc) · 1.71 KB
/
CrowdfundPlatform.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
package bt.sample;
import bt.*;
import bt.compiler.CompilerVersion;
import bt.compiler.TargetCompilerVersion;
import bt.ui.EmulatorWindow;
/**
* Crowd-funding smart contract to be used in a crowd-funding platform.
*
* If a target amount is achieved before the given number of blocks, then the entire
* balance will be sent to the beneficiary account (minus a platform fee).
* If target is not achieved, then all transactions are refunded (minus the gas fee).
*
* Inspired by http://ciyam.org/at/at_crowdfund.html
*
* @author jjos
*/
@TargetCompilerVersion(CompilerVersion.v0_0_0)
public class CrowdfundPlatform extends Contract {
Address beneficiary;
long targetAmount;
long nBlocksSleep;
Address platform;
long platformFeePerThousand;
boolean successful;
long fee;
/**
* Constructor, when in blockchain the constructor is called when the first TX
* reaches the contract.
*/
public CrowdfundPlatform(){
// We immediately put the contract to sleep by the specified number of blocks
sleep(nBlocksSleep);
// After the sleep, we check if it is successful or not
successful = getCurrentBalance() >= targetAmount;
}
/**
* Iterates over every transaction received
*/
@Override
public void txReceived(){
if(!successful){
// Send back funds since it failed
sendAmount(getCurrentTxAmount(), getCurrentTxSender());
}
}
@Override
protected void blockFinished() {
if(successful) {
fee = getCurrentBalance()*platformFeePerThousand/1000;
sendAmount(fee, platform);
sendBalance(beneficiary);
}
else {
// Send the remaining gas fee to the platform
sendBalance(platform);
}
}
public static void main(String[] args) {
new EmulatorWindow(CrowdfundPlatform.class);
}
}