-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwpsti.tyron.scilla
564 lines (508 loc) · 20.4 KB
/
wpsti.tyron.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
(* v0.4.0
Wrapped profit-sharing token <> Implementation smart contract
Self-Sovereign Identity Protocol
Copyright Tyron Mapu Community Interest Company 2022. All rights reserved.
You acknowledge and agree that Tyron Mapu Community Interest Company (Tyron) own all legal right, title and interest in and to the work, software, application, source code, documentation and any other documents in this repository (collectively, the Program), including any intellectual property rights which subsist in the Program (whether those rights happen to be registered or not, and wherever in the world those rights may exist), whether in source code or any other form.
Subject to the limited license below, you may not (and you may not permit anyone else to) distribute, publish, copy, modify, merge, combine with another program, create derivative works of, reverse engineer, decompile or otherwise attempt to extract the source code of, the Program or any part thereof, except that you may contribute to this repository.
You are granted a non-exclusive, non-transferable, non-sublicensable license to distribute, publish, copy, modify, merge, combine with another program or create derivative works of the Program (such resulting program, collectively, the Resulting Program) solely for Non-Commercial Use as long as you:
1. give prominent notice (Notice) with each copy of the Resulting Program that the Program is used in the Resulting Program and that the Program is the copyright of Tyron; and
2. subject the Resulting Program and any distribution, publication, copy, modification, merger therewith, combination with another program or derivative works thereof to the same Notice requirement and Non-Commercial Use restriction set forth herein.
Non-Commercial Use means each use as described in clauses (1)-(3) below, as reasonably determined by Tyron in its sole discretion:
1. personal use for research, personal study, private entertainment, hobby projects or amateur pursuits, in each case without any anticipated commercial application;
2. use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization or government institution; or
3. the number of monthly active users of the Resulting Program across all versions thereof and platforms globally do not exceed 10,000 at any time.
You will not use any trade mark, service mark, trade name, logo of Tyron or any other company or organization in a way that is likely or intended to cause confusion about the owner or authorized user of such marks, names or logos.
If you have any questions, comments or interest in pursuing any other use cases, please reach out to us at [email protected].*)
scilla_version 0
import BoolUtils IntUtils ListUtils PairUtils
library WPSTi
type Account =
| Account of Uint128 Uint128 Bool
let one_msg = fun( msg: Message ) =>
let nil_msg = Nil{ Message } in Cons{ Message } msg nil_msg
let two_msgs = fun( msg1: Message ) => fun( msg2: Message ) =>
let msgs_tmp = one_msg msg2 in Cons{ Message } msg1 msgs_tmp
let three_msgs = fun( msg1: Message ) => fun( msg2: Message ) => fun( msg3: Message ) =>
let msgs_tmp = two_msgs msg2 msg3 in Cons{ Message } msg1 msgs_tmp
let four_msgs = fun( msg1: Message ) => fun( msg2: Message ) => fun( msg3: Message ) => fun( msg4: Message ) =>
let msgs_tmp = three_msgs msg2 msg3 msg4 in Cons{ Message } msg1 msgs_tmp
type Error =
| CodeNotProxy
| CodeNotAdmin
| CodeNotPauser
| CodeNotLister
| CodeIsPaused
| CodeNotPaused
| CodeIsBlocked
| CodeNotBlocked
| CodeSameAddress
| CodeIsNull
| CodeInsufficientFunds
| CodeInsufficientAllowance
| CodeNotValid
let make_error = fun( error: Error ) =>
let result = match error with
| CodeNotProxy => Int32 -1
| CodeNotAdmin => Int32 -2
| CodeNotPauser => Int32 -3
| CodeNotLister => Int32 -4
| CodeIsPaused => Int32 -5
| CodeNotPaused => Int32 -6
| CodeIsBlocked => Int32 -7
| CodeNotBlocked => Int32 -8
| CodeSameAddress => Int32 -9
| CodeIsNull => Int32 -10
| CodeInsufficientFunds => Int32 -11
| CodeInsufficientAllowance => Int32 -12
| CodeNotValid => Int32 -13
end in { _exception: "Error"; code: result }
let zero = Uint128 0
let one = Uint128 1
let hundred = Uint128 100
let yes = True
let no = False
let none_uint128 = None{ Uint128 }
let null_address = 0x0000000000000000000000000000000000000000
let option_value =
tfun 'A => fun( default: 'A ) => fun( opt_val: Option 'A ) => match opt_val with
| Some v => v
| None => default end
let option_account =
let f = @option_value Account in let noAccount = Account zero zero no in f noAccount
let account_balance =
fun( getAccount: Option Account ) =>
let account = option_account getAccount in match account with
| Account balance share vote => balance end
let option_uint128_value =
let f = @option_value Uint128 in f zero
let better_subtract =
fun( a: Uint128 ) => fun( b: Uint128 ) =>
let a_ge_b = uint128_ge a b in match a_ge_b with
| True => builtin sub a b
| False => zero end
contract WPSTi(
init_admin: ByStr20,
proxy_addr: ByStr20 with contract
field total_supply: Uint128,
field accounts: Map ByStr20 Account,
field allowances: Map ByStr20 ( Map ByStr20 Uint128 ) end,
zilswap_addr: ByStr20,
token_addr: ByStr20
)
field admin: ByStr20 = init_admin
field pauser: ByStr20 = init_admin
field lister: ByStr20 = init_admin
field wrapped_token: ByStr20 = token_addr
field zilswap: ByStr20 = zilswap_addr
field demanded_token: ByStr20 = token_addr
field paused: Bool = False
field blocked: Map ByStr20 Uint128 = Emp ByStr20 Uint128
field deadline_block: BNum = BNum 0
procedure ThrowError( err: Error )
e = make_error err; throw e end
procedure ThrowIfNotProxy()
verified = builtin eq proxy_addr _sender;
match verified with
| True =>
| False => err = CodeNotProxy; ThrowError err end end
procedure IsAdmin()
current_admin <- admin;
is_admin = builtin eq current_admin _origin;
match is_admin with
| True =>
| False => err = CodeNotAdmin; ThrowError err end end
procedure IsPauser()
current_pauser <- pauser;
is_pauser = builtin eq current_pauser _origin;
match is_pauser with
| True =>
| False => err = CodeNotPauser; ThrowError err end end
procedure IsPaused()
is_paused <- paused;
match is_paused with
| True =>
| False => err = CodeNotPaused; ThrowError err end end
procedure IsNotPaused()
is_paused <- paused;
match is_paused with
| True => err = CodeIsPaused; ThrowError err
| False => end end
procedure IsLister()
current_lister <- lister;
is_lister = builtin eq current_lister _origin;
match is_lister with
| True =>
| False => err = CodeNotLister; ThrowError err end end
procedure IsBlocked( addr: ByStr20 )
is_blocked <- exists blocked[addr];
match is_blocked with
| True =>
| False => err = CodeNotBlocked; ThrowError err end end
procedure IsNotBlocked( addr: ByStr20 )
is_blocked <- exists blocked[addr];
match is_blocked with
| True => err = CodeIsBlocked; ThrowError err
| False => end end
procedure IsNotNull( addr: ByStr20 )
is_null = builtin eq null_address addr;
match is_null with
| True => err = CodeIsNull; ThrowError err
| False => end end
procedure ThowIfSameAddr(
a: ByStr20,
b: ByStr20
)
is_self = builtin eq a b;
match is_self with
| True => err = CodeSameAddress; ThrowError err
| False => end end
procedure IsSufficientFunds(
funds: Uint128,
amount: Uint128
)
sufficient_funds = uint128_ge funds amount;
match sufficient_funds with
| True =>
| False => err = CodeInsufficientFunds; ThrowError err end end
procedure IsSufficientAllowance(
allowance: Uint128,
amount: Uint128
)
sufficient_allowance = uint128_ge allowance amount;
match sufficient_allowance with
| True =>
| False => err = CodeInsufficientAllowance; ThrowError err end end
transition RecipientAcceptTransfer(
sender: ByStr20,
recipient: ByStr20,
amount: Uint128
) end
transition TransferSuccessCallBack(
sender: ByStr20,
recipient: ByStr20,
amount : Uint128
) end
transition RecipientAcceptTransferFrom(
initiator: ByStr20,
sender: ByStr20,
recipient: ByStr20,
amount: Uint128
)
is_valid_transfer_to_self =
let self_triggered = builtin eq initiator _this_address
in let is_transfer_to_self = builtin eq recipient _this_address
in andb self_triggered is_transfer_to_self;
match is_valid_transfer_to_self with
| False => err = CodeNotValid; ThrowError err
| True => end end
transition TransferFromSuccessCallBack(
initiator: ByStr20,
sender: ByStr20,
recipient: ByStr20,
amount: Uint128
) end
transition UpdateAdmin( new_admin: ByStr20 )
IsAdmin; IsNotPaused; IsNotNull new_admin;
current_admin <- admin;
ThowIfSameAddr current_admin new_admin; admin := new_admin;
e = { _eventname: "AdminUpdated";
admin_updated: new_admin
}; event e end
transition UpdatePauser( new_pauser: ByStr20 )
IsAdmin; IsNotPaused; IsNotNull new_pauser;
current_pauser <- pauser;
ThowIfSameAddr current_pauser new_pauser; pauser := new_pauser;
e = { _eventname: "PauserUpdated";
pauser_updated: new_pauser
}; event e end
transition Pause()
ThrowIfNotProxy; IsPauser;
IsNotPaused; paused := yes;
e = { _eventname: "SmartContractPaused";
pauser: _origin
}; event e end
transition Unpause()
ThrowIfNotProxy; IsPauser;
IsPaused; paused := no;
e = { _eventname: "SmartContractUnpaused";
pauser: _origin
}; event e end
transition UpdateLister( new_lister: ByStr20 )
IsAdmin; IsNotPaused; IsNotNull new_lister;
current_lister <- lister;
ThowIfSameAddr current_lister new_lister; lister:= new_lister;
e = { _eventname: "ListerUpdated";
lister_updated: new_lister
}; event e end
transition Block( addr: ByStr20 )
ThrowIfNotProxy; IsLister;
IsNotPaused; IsNotNull addr; IsNotBlocked addr; blocked[addr] := one;
e = { _eventname: "AddressBlocked";
address: addr;
lister: _origin
}; event e end
transition Unblock( addr: ByStr20 )
ThrowIfNotProxy; IsLister;
IsNotPaused; IsNotNull addr; IsBlocked addr; delete blocked[addr];
e = { _eventname: "AddressUnblocked";
address: addr;
lister: _origin
}; event e end
transition LawEnforcementWipingBurn( addr: ByStr20 )
ThrowIfNotProxy; IsLister;
IsNotPaused; IsBlocked addr; IsNotBlocked _origin;
get_account <-& proxy_addr.accounts[addr]; bal = account_balance get_account;
current_supply <-& proxy_addr.total_supply;
new_supply = builtin sub current_supply bal;
e = { _eventname: "LawEnforcementWipingBurnt";
lister: _origin;
address: addr;
amount: bal
}; event e;
msg_to_proxy = { _tag: "LawEnforcementWipingBurnCallBack";
_recipient: _sender;
_amount: zero;
addr: addr;
new_supply : new_supply
};
msg_to_lister = { _tag: "LawEnforcementWipingBurnSuccessCallBack";
_recipient: _origin;
_amount: zero;
addr: addr;
amount: bal
}; msgs = two_msgs msg_to_proxy msg_to_lister; send msgs end
transition Mint(
originator: ByStr20,
beneficiary: ByStr20, (* self-triggered, i.e. the originator is the beneficiary *)
amount: Uint128
)
ThrowIfNotProxy; IsNotPaused;
IsNotBlocked originator;
current_supply <-& proxy_addr.total_supply;
new_supply = builtin add current_supply amount;
get_account <-& proxy_addr.accounts[originator]; bal = account_balance get_account;
new_bal = builtin add bal amount;
token_address <- wrapped_token;
msg_to_token = { _tag: "TransferFrom"; _recipient: token_address; _amount: zero;
from: originator;
to: _this_address;
amount: amount
};
msg_to_proxy = { _tag: "MintCallBack"; _recipient: _sender; _amount: zero;
beneficiary: originator;
new_balance: new_bal;
new_supply: new_supply
};
msg_to_originator = { _tag: "RecipientAcceptMint"; _recipient: originator; _amount: zero;
minter: originator;
amount: amount
};
msgs = three_msgs msg_to_token msg_to_proxy msg_to_originator; send msgs end
transition Burn(
originator: ByStr20,
amount: Uint128
)
ThrowIfNotProxy; IsNotPaused;
IsNotBlocked originator;
get_account <-& proxy_addr.accounts[originator]; bal = account_balance get_account;
IsSufficientFunds bal amount;
new_balance = builtin sub bal amount;
current_supply <-& proxy_addr.total_supply;
new_supply = builtin sub current_supply amount;
e = { _eventname: "XBurnt";
originator: originator;
amount: amount
}; event e;
token_address <- wrapped_token;
msg_to_token = { _tag: "Transfer"; _recipient: token_address; _amount: zero;
to: originator;
amount: amount
};
msg_to_proxy = { _tag: "BurnCallBack"; _recipient: _sender; _amount: zero;
originator: originator;
new_balance: new_balance;
new_supply: new_supply
};
msg_to_originator = { _tag: "BurnSuccessCallBack"; _recipient: originator; _amount: zero;
amount: amount
};
msgs = three_msgs msg_to_token msg_to_proxy msg_to_originator; send msgs end
transition Transfer(
originator: ByStr20,
beneficiary: ByStr20,
amount: Uint128
)
ThrowIfNotProxy;
IsNotPaused; IsNotBlocked originator; IsNotNull beneficiary; IsNotBlocked beneficiary; ThowIfSameAddr originator beneficiary;
get_originator_account <-& proxy_addr.accounts[originator]; originator_bal = account_balance get_originator_account;
IsSufficientFunds originator_bal amount;
new_originator_bal = builtin sub originator_bal amount;
get_beneficiary_account <-& proxy_addr.accounts[beneficiary]; beneficiary_bal = account_balance get_beneficiary_account;
new_beneficiary_bal = builtin add beneficiary_bal amount;
e = { _eventname: "TransferSuccess";
originator: originator;
beneficiary: beneficiary;
amount: amount
}; event e;
msg_to_proxy = { _tag: "TransferCallBack"; _recipient: _sender; _amount: zero;
originator: originator;
beneficiary: beneficiary;
new_originator_bal: new_originator_bal;
new_beneficiary_bal: new_beneficiary_bal
};
msg_to_originator = { _tag: "TransferSuccessCallBack"; _recipient: originator; _amount: zero;
sender: originator;
recipient: beneficiary;
amount: amount
};
msg_to_beneficiary = { _tag: "RecipientAcceptTransfer"; _recipient: beneficiary; _amount: zero;
sender: originator;
recipient: beneficiary;
amount: amount
}; msgs = three_msgs msg_to_proxy msg_to_originator msg_to_beneficiary; send msgs end
transition IncreaseAllowance(
originator: ByStr20,
spender: ByStr20,
amount: Uint128
)
ThrowIfNotProxy; IsNotPaused;
IsNotBlocked originator; IsNotBlocked spender; ThowIfSameAddr originator spender;
get_allowance <-& proxy_addr.allowances[originator][spender]; allowance = option_uint128_value get_allowance;
new_allowance = builtin add allowance amount;
e = { _eventname: "AllowanceIncreased";
originator: originator;
spender: spender;
allowance_updated: new_allowance
}; event e;
msg = let m = { _tag: "AllowanceCallBack"; _recipient: _sender; _amount: zero;
originator: originator;
spender: spender;
new_allowance: new_allowance
} in one_msg m; send msg end
transition DecreaseAllowance(
originator: ByStr20,
spender: ByStr20,
amount: Uint128
)
ThrowIfNotProxy; IsNotPaused;
IsNotBlocked originator; IsNotBlocked spender; ThowIfSameAddr originator spender;
get_allowance <-& proxy_addr.allowances[originator][spender]; allowance = option_uint128_value get_allowance;
new_allowance = better_subtract allowance amount;
e = { _eventname: "AllowanceDecreased";
originator: originator;
spender: spender;
allowance_updated: new_allowance
}; event e;
msg = let m = { _tag: "AllowanceCallBack"; _recipient: _sender; _amount: zero;
originator: originator;
spender: spender;
new_allowance: new_allowance
} in one_msg m; send msg end
transition TransferFrom(
originator: ByStr20,
spender: ByStr20,
beneficiary: ByStr20,
amount: Uint128
)
ThrowIfNotProxy; IsNotPaused;
IsNotBlocked originator; IsNotBlocked spender; IsNotBlocked beneficiary; IsNotNull beneficiary; ThowIfSameAddr originator beneficiary;
get_originator_account <-& proxy_addr.accounts[originator]; originator_bal = account_balance get_originator_account;
IsSufficientFunds originator_bal amount;
get_allowance <-& proxy_addr.allowances[originator][spender]; allowance = option_uint128_value get_allowance;
IsSufficientAllowance allowance amount;
new_originator_bal = builtin sub originator_bal amount;
new_allowance = builtin sub allowance amount;
get_beneficiary_account <-& proxy_addr.accounts[beneficiary]; beneficiary_bal = account_balance get_beneficiary_account;
new_beneficiary_bal = builtin add beneficiary_bal amount;
e = { _eventname: "TransferFromSuccess";
originator: originator;
spender: spender;
beneficiary: beneficiary;
amount: amount
}; event e;
msg_to_proxy_balances = { _tag: "TransferFromCallBack"; _recipient: _sender; _amount: zero;
originator: originator;
beneficiary: beneficiary;
new_originator_bal: new_originator_bal;
new_beneficiary_bal: new_beneficiary_bal
};
msg_to_proxy_allowance = { _tag: "AllowanceCallBack"; _recipient: _sender; _amount: zero;
originator: originator;
spender: spender;
new_allowance: new_allowance
};
msg_to_spender = { _tag: "TransferFromSuccessCallBack"; _recipient: spender; _amount: zero;
initiator: spender;
sender: originator;
recipient: beneficiary;
amount: amount
};
msg_to_beneficiary = { _tag: "RecipientAcceptTransferFrom"; _recipient: beneficiary; _amount: zero;
initiator: spender;
sender: originator;
recipient: beneficiary;
amount: amount
}; msgs = four_msgs msg_to_proxy_balances msg_to_proxy_allowance msg_to_spender msg_to_beneficiary; send msgs end
transition UpdateZilswap( new_addr: ByStr20 )
IsNotPaused; IsAdmin; zilswap := new_addr end
transition UpgradeImplementation( new_implementation: ByStr20 )
IsNotPaused; IsAdmin;
token_address <- wrapped_token;
current_supply <-& proxy_addr.total_supply;
msg_to_token = { _tag: "Transfer"; _recipient: token_address; _amount: zero;
to: new_implementation;
amount: current_supply
};
e = { _eventname: "ImplementationUpgraded";
implementation_upgraded: new_implementation
}; event e end
transition UpdateTokenAddr( new_addr: ByStr20 )
IsNotPaused; IsAdmin; wrapped_token := new_addr end
transition UpdateDemandedToken( pst: Bool )
IsNotPaused; IsAdmin;
token_address <- wrapped_token;
match pst with
| True => demanded_token := proxy_addr
| False => demanded_token := token_address end end
procedure UpdateDeadline( deadline: Uint128 )
current_block <- &BLOCKNUMBER;
new_deadline = builtin badd current_block deadline;
deadline_block := new_deadline end
transition SwapZILForPSTAndDistribute(
amount: Uint128,
originator: ByStr20,
beneficiary: Option( List ByStr20 ),
deadline: Uint128
)
ThrowIfNotProxy;
UpdateDeadline deadline; get_deadline <- deadline_block;
zilswap_address <- zilswap; token_address <- demanded_token;
is_pst = builtin eq token_address proxy_addr;
match is_pst with
| True =>
accept; msg = let m = { _tag: "SwapExactZILForTokens"; _recipient: zilswap_address; _amount: _amount;
token_address: proxy_addr;
min_token_amount: amount;
deadline_block: get_deadline;
recipient_address: originator
} in one_msg m; send msg
| False =>
accept; msg = let m = { _tag: "SwapZILForExactTokens"; _recipient: zilswap_address; _amount: _amount;
token_address: token_address;
token_amount: amount;
deadline_block: get_deadline;
recipient_address: _this_address
} in one_msg m; send msg;
get_originator_account <-& proxy_addr.accounts[originator]; originator_bal = account_balance get_originator_account;
new_originator_bal = builtin add originator_bal amount;
current_supply <-& proxy_addr.total_supply;
new_supply = builtin add amount current_supply;
msg = let m = { _tag: "MintCallBack"; _recipient: proxy_addr; _amount: zero;
beneficiary: originator;
new_beneficiary_bal: new_originator_bal;
new_supply: new_supply
} in one_msg m; send msg end;
msg = let m = { _tag: "SwapZILForPSTAndDistributeCallBack"; _recipient: proxy_addr; _amount: zero;
originator: originator;
beneficiary: beneficiary
} in one_msg m; send msg end