-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.rsh
56 lines (40 loc) · 1.39 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
'reach 0.1';
'use strict';
export const main = Reach.App(() => {
const A = Participant('Alice', {
ready: Fun([], Null),
...hasConsoleLogger,
});
const Bob = API('Bob',{
attach: Fun([], Bool),
});
init();
A.only(() => {
interact.ready();
})
A.publish();
commit();
A.publish();
const [count, keepgoing] =
parallelReduce([0, true])
.invariant(balance() == balance()) // invariant: the condition inside must be true for the all time that the while goes on
.while(keepgoing)
.api(Bob.attach, // the name of the api that is called
(y) => { // the code to execute and the returning variable of the api (y)
if(count == 5){ //this is the 6 element, we start counting from 0
A.interact.log("You cannot attach. There maximun number has been reached.")
y(false)
return [count, true]; // NOTE: You can set keepgoing to True if you want to terminate the smart contract. It was not required for this level
}else{
const counter = count + 1;
A.interact.log("The counter has been increased");
y(true);
return [counter, true]; // the returning of the API for the parallel reduce necessary to update the initial variable
}
}
)
// avoid the error "balance zero at application exit"
transfer(balance()).to(A)
commit();
exit();
});