-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.rsh
155 lines (125 loc) · 4.49 KB
/
index.rsh
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'reach 0.1';
export const main = Reach.App(() => {
setOptions({ untrustworthyMaps: true });
const [isOutcome, WINNER, STALEMATE, CONTINUE, TICKETS_FINISHED] = makeEnum(4);
const Alice = Participant('Alice', {
supplyRaffle: Fun([], Tuple(Token, UInt)),
provideWinningNumber: Fun([UInt], UInt),
displayWinner: Fun([Address], Null),
informOfNoWinner: Fun([], Null),
check: Fun([], Null),
displayWinnerBalance: Fun([Address], Null),
getRegInfo: Fun([Address, UInt], Null),
displayHash: Fun([Digest], Null),
...hasRandom,
});
const Bob = API('Bob', {
subscribeToNFT: Fun([], Token),
drawRaffleTicket: Fun([UInt], Null),
getNumberOfTicketsAvailable: Fun([], UInt),
checkStatus: Fun([], Tuple(Bool, Bool))
});
init();
// The first one to publish deploys the contract
Alice.only(() => {
const [NFT, numberOfTickets] = declassify(interact.supplyRaffle());
const _winningNumber = interact.provideWinningNumber(numberOfTickets);
const [_commitAlice, _saltAlice ] = makeCommitment(interact, _winningNumber);
const commitAlice = declassify(_commitAlice);
interact.displayHash(commitAlice);
})
Alice.publish(NFT, numberOfTickets, commitAlice);
commit();
Alice.pay([[1, NFT]]);
commit();
Alice.publish();
const submissions = new Map(Address, UInt);
const contestants = new Set();
commit();
Alice.only(() => {
const winningNumber = declassify(_winningNumber);
const saltAlice = declassify(_saltAlice);
});
Alice.publish(saltAlice, winningNumber);
checkCommitment(commitAlice, saltAlice, winningNumber);
const [ numOfDraws, outcome, addressToPay, numOfChecks ] =
parallelReduce([ 0, CONTINUE, Alice, 0 ])
.invariant( balance(NFT) == 1)
.while((numOfDraws < numberOfTickets) || (numOfChecks < numberOfTickets) || (outcome == CONTINUE))
.api_(Bob.subscribeToNFT, () => {
check(this != Alice, "Not deployer");
return [0, (resolve) => {
resolve(NFT)
return [numOfDraws, CONTINUE, Alice, numOfChecks]
}]
})
.api_(Bob.getNumberOfTicketsAvailable, () => {
check(this != Alice, "Not deployer");
return [0, (resolve) => {
resolve(numberOfTickets)
return [numOfDraws, CONTINUE, Alice, numOfChecks]
}]
})
.api_(Bob.drawRaffleTicket, (draw) => {
check(this != Alice, "Not Deployer");
check(isNone(submissions[this]), "Already made a draw")
return [ 0, (resolve) => {
resolve(null);
submissions[this] = draw;
Alice.interact.getRegInfo(this, draw);
if ((numOfDraws + 1) == numberOfTickets) {
return [numOfDraws + 1, TICKETS_FINISHED, Alice, numOfChecks];
}
else {
return [ numOfDraws + 1, CONTINUE, Alice, numOfChecks];
}
}]
})
.api_(Bob.checkStatus, () => {
check(this != Alice, "Not Deployer");
check(isSome(submissions[this]), "No draw has been made by you yet !!!");
check(!contestants.member(this), "Your status has been checked by you already !!!");
return [0, (resolve) => {
if (outcome == CONTINUE) {
resolve([false, false]);
return [numOfDraws, outcome, addressToPay, numOfChecks]
}
else {
contestants.insert(this);
return submissions[this].match({
Some: (value) => {
if (outcome == WINNER) {
resolve([true, false]);
return [numOfDraws, WINNER, addressToPay, numOfChecks + 1];
}
else if (value == winningNumber) {
resolve([true, true]);
return [numOfDraws, WINNER, this, numOfChecks + 1];
}
else {
resolve([true, false]);
return [numOfDraws, TICKETS_FINISHED, addressToPay, numOfChecks + 1]
}
},
None: () => {
resolve([false, false]);
return [numOfDraws, TICKETS_FINISHED, addressToPay, numOfChecks]
}
})
}
}]
})
commit();
if (addressToPay == Alice) {
Alice.interact.informOfNoWinner();
}
else {
Alice.interact.displayWinner(addressToPay);
}
Alice.publish();
transfer(balance(NFT), NFT).to(addressToPay);
transfer(balance()).to(Alice);
Alice.interact.displayWinnerBalance(addressToPay);
commit();
exit();
});