forked from Zilliqa/staking-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.scilla
390 lines (337 loc) · 15 KB
/
proxy.scilla
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
scilla_version 0
library SSNListProxy
let zero = Uint128 0
let one_msg =
fun (m: Message) =>
let e = Nil {Message} in
Cons {Message} m e
type SsnRewardShare =
| SsnRewardShare of ByStr20 Uint128
type SSNCycleInfo =
| SSNCycleInfo of Uint128 Uint128
(***************************************************)
(* The contract definition *)
(***************************************************)
contract SSNListProxy(
init_implementation: ByStr20,
init_admin: ByStr20
)
(* Mutable fields *)
field implementation: ByStr20 = init_implementation
field admin: ByStr20 = init_admin
field stagingadmin: Option ByStr20 = None {ByStr20}
(***************************************************)
(* Transition *)
(***************************************************)
(***************************************************)
(* Proxy Transition *)
(***************************************************)
transition UpgradeTo(newImplementation: ByStr20)
currentAdmin <- admin;
isAdmin = builtin eq currentAdmin _sender;
match isAdmin with
| True =>
implementation := newImplementation;
e = {_eventname: "Upgraded"; implementation_address: newImplementation};
event e
| False =>
e = {_eventname: "upgradeTo FailedNotAdmin"; newImplementation: newImplementation};
event e
end
end
transition ChangeProxyAdmin(newAdmin: ByStr20)
currentAdmin <- admin;
isAdmin = builtin eq currentAdmin _sender;
match isAdmin with
| True =>
new_staging_admin = Some {ByStr20} newAdmin;
stagingadmin := new_staging_admin;
e = {_eventname: "ChangeProxyAdmin"; oldAdmin: currentAdmin; newAdmin: newAdmin};
event e
| False =>
e = {_eventname: "ChangeProxyAdmin FailedNotAdmin"; newAdmin: newAdmin};
event e
end
end
transition ClaimProxyAdmin()
staging_admin_o <- stagingadmin;
match staging_admin_o with
| Some staging_admin =>
is_stagingadmin = builtin eq staging_admin _sender;
match is_stagingadmin with
| True =>
admin := _sender;
tmp_staging_admin = None {ByStr20};
stagingadmin := tmp_staging_admin;
e = {_eventname: "ClaimProxyAdmin"; newAdmin: _sender};
event e
| False =>
e = {_eventname: "ClaimProxyAdmin FailedNotStagingadmin"; newAdmin: _sender};
event e
end
| None =>
e = {_eventname: "ClaimProxyAdmin FailedNoStagingadmin"};
event e
end
end
(***************************************************)
(* House keeping transition *)
(***************************************************)
transition Pause()
current_impl <- implementation;
msg = {_tag: "Pause"; _recipient: current_impl; _amount: zero; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition UnPause()
current_impl <- implementation;
msg = {_tag: "UnPause"; _recipient: current_impl; _amount: zero; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition UpdateAdmin(new_admin: ByStr20)
current_impl <- implementation;
msg = {_tag: "UpdateAdmin"; _recipient: current_impl; _amount: zero; admin: new_admin; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition ClaimAdmin()
current_impl <- implementation;
msg = {_tag: "ClaimAdmin"; _recipient: current_impl; _amount: zero; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(* @dev: Set the verifier of contract. Used by admin only. *)
(* @param verif: New verifier value *)
transition UpdateVerifier(verif: ByStr20)
current_impl <- implementation;
msg = {_tag: "UpdateVerifier"; _recipient: current_impl; _amount: zero; verif: verif; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(* @dev: Set the verifier rewarding address of the contract. Used by admin only. *)
(* @param addr: New verifier receiving address *)
transition UpdateVerifierRewardAddr(addr: ByStr20)
current_impl <- implementation;
msg = {_tag: "UpdateVerifierRewardAddr"; _recipient: current_impl; _amount: zero; addr: addr; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(* @dev: Set the minstake, maxstake and contract maxstake of contract. Used by admin only. *)
(* @param min_stake: New min_stake value *)
(* @param min_deleg_stake: New mindelegstake value *)
transition UpdateStakingParameters(min_stake: Uint128, min_deleg_stake: Uint128, max_comm_change_rate: Uint128)
current_impl <- implementation;
msg = {_tag: "UpdateStakingParameters"; _recipient: current_impl; _amount: zero; min_stake: min_stake; min_deleg_stake: min_deleg_stake; max_comm_change_rate: max_comm_change_rate; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition ChangeBNumReq(input_bnum_req: Uint128)
current_impl <- implementation;
msg = {_tag: "ChangeBNumReq"; _recipient: current_impl; _amount: zero; input_bnum_req: input_bnum_req; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition UpdateGzilAddr(gzil_addr: ByStr20)
current_impl <- implementation;
msg = {_tag: "UpdateGzilAddr"; _recipient: current_impl; _amount: zero; gzil_addr: gzil_addr; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(* @dev: Adds new ssn to ssnlist. Used by admin only. *)
(* @param ssnaddr: Address of the ssn to be added *)
(* @param name: Name of the ssn to be added *)
(* @param urlraw: string representing "ip:port" of the ssn serving raw api request *)
(* @param urlapi: string representing url exposed by ssn serving public api request *)
(* @param buff_deposit: Any buffered stake deposit *)
(* @param comm: Percentage of incoming rewards that the SSN takes *)
transition AddSSN(ssnaddr: ByStr20, name: String, urlraw: String, urlapi: String, comm: Uint128)
current_impl <- implementation;
msg = {_tag: "AddSSN"; _recipient: current_impl; _amount: zero; ssnaddr: ssnaddr; name: name; urlraw: urlraw; urlapi: urlapi; comm: comm; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition UpdateSSN(ssnaddr: ByStr20, new_name: String, new_urlraw: String, new_urlapi: String)
current_impl <- implementation;
msg = {_tag: "UpdateSSN"; _recipient: current_impl; _amount: zero; ssnaddr: ssnaddr; new_name: new_name; new_urlraw: new_urlraw; new_urlapi: new_urlapi; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(***************************************************)
(* SSN operator transition *)
(***************************************************)
(* @dev : To update the commision rate for a given SSN *)
(* @param new_rate: The new commision rate *)
transition UpdateComm(new_rate: Uint128)
current_impl <- implementation;
msg = {_tag: "UpdateComm"; _recipient: current_impl; _amount: zero; new_rate: new_rate; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition WithdrawComm()
current_impl <- implementation;
msg = {_tag: "WithdrawComm"; _recipient: current_impl; _amount: zero; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(* @dev : To update the received address for a given SSN *)
(* @param new_address: The new received address *)
transition UpdateReceivingAddr(new_addr: ByStr20)
current_impl <- implementation;
msg = {_tag: "UpdateReceivingAddr"; _recipient: current_impl; _amount: zero; new_addr: new_addr; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(***************************************************)
(* Delegator transition *)
(***************************************************)
(* @dev : To delegate the stake to the contract. *)
(* @param ssnaddr: The address of the SSN to which the deleg wants to stake *)
transition DelegateStake(ssnaddr: ByStr20)
current_impl <- implementation;
accept;
msg = {_tag: "DelegateStake"; _recipient: current_impl; _amount: _amount; ssnaddr: ssnaddr; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(* @dev: Withdraw stake reward. Can be called by a deleg. *)
transition WithdrawStakeRewards(ssnaddr: ByStr20)
current_impl <- implementation;
msg = {_tag: "WithdrawStakeRewards"; _recipient: current_impl; _amount: zero; ssnaddr: ssnaddr; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition WithdrawStakeAmt(ssnaddr: ByStr20, amt: Uint128)
current_impl <- implementation;
msg = {_tag: "WithdrawStakeAmt"; _recipient: current_impl; _amount: zero; ssnaddr: ssnaddr; amt: amt; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition CompleteWithdrawal()
current_impl <- implementation;
msg = {_tag: "CompleteWithdrawal"; _recipient: current_impl; _amount: zero; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition ReDelegateStake(ssnaddr: ByStr20, to_ssn: ByStr20, amount: Uint128)
current_impl <- implementation;
msg = {_tag: "ReDelegateStake"; _recipient: current_impl; _amount: zero; ssnaddr: ssnaddr; to_ssn: to_ssn; amount: amount; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(***************************************************)
(* Verifier transition *)
(***************************************************)
(* @dev: Assign stake reward to all ssn from ssnlist. Used by verifier only. *)
(* @param ssnrewardlist: List of SsnRewardShare *)
transition AssignStakeReward(ssnreward_list: List SsnRewardShare)
current_impl <- implementation;
accept;
msg = {_tag: "AssignStakeReward"; _recipient: current_impl; _amount: _amount; ssnreward_list: ssnreward_list; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(***************************************************)
(* Generic transition *)
(***************************************************)
(* @dev: Move token amount from _sender to recipient i.e. contract address. *)
transition AddFunds()
current_impl <- implementation;
accept;
msg = {_tag: "AddFunds"; _recipient: current_impl; _amount: _amount; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition AddSSNAfterUpgrade(ssnaddr: ByStr20, stake_amt: Uint128, rewards: Uint128, name: String, urlraw: String, urlapi: String, buff_deposit: Uint128, comm: Uint128, comm_rewards: Uint128, rec_addr: ByStr20)
current_impl <- implementation;
msg = {_tag: "AddSSNAfterUpgrade"; _recipient: current_impl; _amount: zero; ssnaddr: ssnaddr; stake_amt: stake_amt; rewards: rewards; name: name; urlraw: urlraw; urlapi: urlapi; buff_deposit: buff_deposit; comm: comm; comm_rewards: comm_rewards; rec_addr: rec_addr; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(***************************************************)
(* Contract upgrade transition *)
(***************************************************)
(* @dev: Adds a new deleg. Used by admin only during contract upgrade. *)
(* @param ssnaddr: Address of the ssn *)
(* @param deleg: Address of the deleg *)
(* @param stake_amount: Stake amount *)
(* @param rewards: Rewards of the ssn to be added *)
transition UpdateDeleg(ssnaddr: ByStr20, deleg: ByStr20, stake_amt: Uint128)
current_impl <- implementation;
msg = {_tag: "UpdateDeleg"; _recipient: current_impl; _amount: zero; ssnaddr: ssnaddr; deleg: deleg; stake_amt: stake_amt; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateStakeSSNPerCycle(ssn_addr: ByStr20, cycle: Uint32, totalAmt: Uint128, rewards: Uint128)
current_impl <- implementation;
msg = {_tag: "PopulateStakeSSNPerCycle"; _recipient: current_impl; _amount: zero; ssn_addr: ssn_addr; cycle: cycle; totalAmt: totalAmt; rewards: rewards; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateLastWithdrawCycleForDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32)
current_impl <- implementation;
msg = {_tag: "PopulateLastWithdrawCycleForDeleg"; _recipient: current_impl; _amount: zero; deleg_addr: deleg_addr; ssn_addr: ssn_addr; cycle: cycle; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateLastBufDepositCycleDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32)
current_impl <- implementation;
msg = {_tag: "PopulateLastBufDepositCycleDeleg"; _recipient: current_impl; _amount: zero; deleg_addr: deleg_addr; ssn_addr: ssn_addr; cycle: cycle; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateBuffDeposit(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128)
current_impl <- implementation;
msg = {_tag: "PopulateBuffDeposit"; _recipient: current_impl; _amount: zero; deleg_addr: deleg_addr; ssn_addr: ssn_addr; cycle: cycle; amt: amt; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateDirectDeposit(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128)
current_impl <- implementation;
msg = {_tag: "PopulateDirectDeposit"; _recipient: current_impl; _amount: zero; deleg_addr: deleg_addr; ssn_addr: ssn_addr; cycle: cycle; amt: amt; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateDepositAmtDeleg(deleg_addr: ByStr20, ssn_addr: ByStr20, amt: Uint128)
current_impl <- implementation;
msg = {_tag: "PopulateDepositAmtDeleg"; _recipient: current_impl; _amount: zero; deleg_addr: deleg_addr; ssn_addr: ssn_addr; amt: amt; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateDelegStakePerCycle(deleg_addr: ByStr20, ssn_addr: ByStr20, cycle: Uint32, amt: Uint128)
current_impl <- implementation;
msg = {_tag: "PopulateDelegStakePerCycle"; _recipient: current_impl; _amount: zero; deleg_addr: deleg_addr; ssn_addr: ssn_addr; cycle: cycle; amt: amt; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateLastRewardCycle(cycle: Uint32)
current_impl <- implementation;
msg = {_tag: "PopulateLastRewardCycle"; _recipient: current_impl; _amount: zero; cycle: cycle; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateCommForSSN(ssn_addr: ByStr20, cycle: Uint32, comm: Uint128)
current_impl <- implementation;
msg = {_tag: "PopulateCommForSSN"; _recipient: current_impl; _amount: zero; ssn_addr: ssn_addr; cycle: cycle; comm: comm; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulateTotalStakeAmt(amt: Uint128)
current_impl <- implementation;
msg = {_tag: "PopulateTotalStakeAmt"; _recipient: current_impl; _amount: zero; amt: amt; initiator: _sender};
msgs = one_msg msg;
send msgs
end
transition PopulatePendingWithdrawal(ssn_addr: ByStr20, block_number: BNum, stake: Uint128)
current_impl <- implementation;
msg = {_tag: "PopulatePendingWithdrawal"; _recipient: current_impl; _amount: zero; ssn_addr: ssn_addr; block_number: block_number; stake: stake; initiator: _sender};
msgs = one_msg msg;
send msgs
end
(* @dev: Set the admin of contract. Used by current admin only. *)
transition DrainContractBalance(amt: Uint128)
current_impl <- implementation;
msg = {_tag: "DrainContractBalance"; _recipient: current_impl; _amount: zero; amt: amt; initiator: _sender};
msgs = one_msg msg;
send msgs
end