forked from xBacked-DAO/reach-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.rsh
169 lines (144 loc) · 4.07 KB
/
base.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'reach 0.1';
/**
* All functions used to configure the contract upon deployment
*/
const AdminInteract = {
...hasConsoleLogger,
setFee: Fun([], UInt),
// Indicates to the frontend the contract has been deployed.
isInitialized: Fun([Contract], Null),
// A Fun that that throws an exception allowing the
// frontend to exit the thread.
ready: Fun([], Null)
};
const userState = Struct([['userId', UInt]]);
export const main = Reach.App(() => {
// Compilation options for Reach
setOptions({
untrustworthyMaps: true,
connectors: [ALGO]
});
const Admin = Participant('Admin', {
...AdminInteract
});
const AdminAPI = API('AdminAPI', {
deprecate: Fun([Bool], Bool),
updateParams: Fun([Tuple(UInt)], Bool)
});
const Any = API('Any', {
halt: Fun([], Null)
});
const ContractUser = API('ContractUser', {
helloWorld: Fun([UInt], Bool)
});
const [isTxnType, DEPRECATE, UPDATE_PARAMS, HELLO] = makeEnum(3);
const Announcer = Events('Announcer', {
transaction: [Address, UInt]
});
const ContractState = Struct([
['adminParams', Tuple(UInt)],
['deprecated', Bool]
]);
const State = View('State', {
read: ContractState,
readUser: Fun([Address], Data({ None: Null, Some: userState }))
});
init();
Admin.publish();
commit();
Admin.only(() => {
const adminAddress = this;
const initialFee = declassify(interact.setFee());
});
// These are the variables we can change per contract deployment
Admin.publish(adminAddress, initialFee);
const contractUsers = new Map(userState);
// use this when doing fromSome(contractUsers)
const emptyUser = userState.fromObject({ userId: 0 });
// Define the function to return a users state
State.readUser.set((addr) => contractUsers[addr]);
commit();
const getCollateralBalance = () => balance();
const createPaymentExpression = (collateralAmt, tokenAmt) => [
collateralAmt
// [tokenAmt, tok]
];
assert(getCollateralBalance() == 0);
Admin.publish();
Admin.interact.isInitialized(getContract());
Admin.interact.ready();
const [deprecated, adminParams] = parallelReduce([false, [5]])
.define(() => {
const [fee] = adminParams;
State.read.set(
ContractState.fromObject({
adminParams,
deprecated
})
);
const isAdmin = (who) =>
check(who == adminAddress, 'You are not the admin');
const isNotDeprecated = () => check(!deprecated, 'This is deprecated');
})
// .paySpec([tok])
.invariant(balance() >= 0)
// vault only ends when deprecated, or when collat is zero (all withdrawn)
.while(!deprecated || balance() != 0)
.api(
AdminAPI.deprecate,
(_) => {
isAdmin(this);
},
(_) => {
return createPaymentExpression(0, 0);
},
(deprecateVal, apiReturn) => {
isAdmin(this);
Announcer.transaction(this, DEPRECATE);
apiReturn(true);
return [deprecateVal, adminParams];
}
)
.api(
AdminAPI.updateParams,
(_) => {
isAdmin(this);
},
(_) => {
return createPaymentExpression(0, 0);
},
// frontend must format the array of Maybe(Address)
(updatedParams, apiReturn) => {
isAdmin(this);
Announcer.transaction(this, UPDATE_PARAMS);
apiReturn(true);
return [deprecated, updatedParams];
}
)
.api(
ContractUser.helloWorld,
(id) => {
check(id > 0, 'id must be greater than 0');
},
(_) => {
return createPaymentExpression(0, 0);
},
// frontend must format the array of Maybe(Address)
(id, apiReturn) => {
check(id > 0, 'id must be greater than 0');
contractUsers[this] = userState.fromObject({
userId: id
});
Announcer.transaction(this, HELLO);
apiReturn(true);
return [deprecated, adminParams];
}
);
assert(deprecated == true && balance() == 0);
commit();
const [[], haltK] = call(Any.halt);
haltK(null);
transfer(balance()).to(Admin);
commit();
exit();
});