forked from nstanford5/raffle-free
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.rsh
82 lines (78 loc) · 2.05 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
/**
* This DApp allows users to register their wallets for free
* for the raffle
*
* This uses a "first come, first serve" for the list
* right now, it could use the VRF once it is implemented
*
*/
'reach 0.1';
export const main = Reach.App(() => {
const A = Participant('Admin', {
// set params
params: Object({
max: UInt,
tok: Token,
}),
launched: Fun([Contract], Null),
getNum: Fun([UInt], UInt),
showNum: Fun([UInt], Null),
});
const B = API({
getTicket: Fun([Address], UInt),
checkTicket: Fun([Address], Bool),
});
const V = View({
howMany: Fun([], UInt),
});
init();
A.only(() => {
const p = declassify(interact.params);
const {max, tok} = p;
});
A.publish(max, tok);
commit();
A.pay([[1, tok]]);
A.interact.launched(getContract());
const pMap = new Map(Address, UInt);
const [count] = parallelReduce([1])
.define(() => {V.howMany.set(() => max - (count - 1));})
.invariant(balance() == 0, "network token balance wrong")
.invariant(balance(tok) == 1, "nft balance wrong")
.while(count < max + 1)
.api_(B.getTicket, (addr) => {
check(isNone(pMap[addr]), "sorry, you are already in the list");
return[ , (ret) => {
ret(count);
pMap[addr] = count;
return[count + 1];
}];
});
commit();
A.only(() => {
const num = declassify(interact.getNum(max));
});
A.publish(num);
A.interact.showNum(num);
const [tokFlag] = parallelReduce([0])
.invariant(balance() == 0, "network token balance wrong")
.invariant(balance(tok) == 1 - tokFlag, "nft balance wrong")
.while(tokFlag < 1)
.api_(B.checkTicket, (addr) => {
check(isSome(pMap[addr]), 'Sorry, you are not in the list');
return[ , (ret) => {
const n = fromSome(pMap[addr], 0);
if(n == num){
ret(true);
transfer(1, tok).to(addr);
return[tokFlag + 1];
} else {
ret(false);
delete pMap[addr];
return[tokFlag]
}
}];
});
commit();
exit();
})