-
Notifications
You must be signed in to change notification settings - Fork 0
/
stake.pact
517 lines (401 loc) · 13.1 KB
/
stake.pact
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
;(namespace 'free)
(namespace (read-msg 'ns))
(module stake-token GOVERNANCE
;\|/ (__) STEAK - Stake Factory Token
; `\------(oo)
; || (__)
; ||w--|| \|/
@doc " STEAK - Stake Factory Token"
@model
[ (defproperty conserves-mass (amount:decimal)
(= (column-delta token-table 'balance) 0.0))
(defproperty valid-account-id (accountId:string)
(and
(>= (length accountId) 3)
(<= (length accountId) 256)))
]
(implements fungible-v2)
(implements fungible-xchain-v1)
; --------------------------------------------------------------------------
; Schemas and Tables
(defschema token-schema
@doc " An account, holding a token balance. \
\ \
\ ROW KEY: accountId. "
balance:decimal
guard:guard
)
(deftable token-table:{token-schema})
; --------------------------------------------------------------------------
; Capatilibites
(defcap GOVERNANCE
()
@doc " Give the admin full access to call and upgrade the module. "
(enforce-keyset "test.admin-kadena-stake")
)
(defcap POOL_GUARD
(id:string)
true)
(defun enforce-pool-guard:bool
(id:string)
(require-capability (POOL_GUARD id)))
(defun create-pool-guard:guard
(account:string)
(create-user-guard (enforce-pool-guard account)))
(defcap INTERNAL ()
@doc "only for internal use"
true
)
(defcap ACCOUNT_GUARD
( accountId:string )
@doc " Look up the guard for an account, required to debit from that account. "
(enforce-guard (at 'guard (read token-table accountId ['guard])))
)
(defcap DEBIT
( sender:string )
@doc " Capability to perform debiting operations. "
(enforce-guard (at 'guard (read token-table sender ['guard])))
(enforce (!= sender "") "Invalid sender.")
)
(defcap CREDIT
( receiver:string )
@doc " Capability to perform crediting operations. "
(enforce (!= receiver "") "Invalid receiver.")
)
(defcap TRANSFER:bool
( sender:string
receiver:string
amount:decimal )
@doc " Capability to perform transfer between two accounts. "
@managed amount TRANSFER-mgr
(enforce (!= sender receiver) "Sender cannot be the receiver.")
(enforce-unit amount)
(enforce (> amount 0.0) "Transfer amount must be positive.")
(compose-capability (DEBIT sender))
(compose-capability (CREDIT receiver))
)
(defun TRANSFER-mgr:decimal
( managed:decimal
requested:decimal )
(let ((newbal (- managed requested)))
(enforce (>= newbal 0.0)
(format "TRANSFER exceeded for balance {}" [managed]))
newbal
)
)
(defcap TRANSFER_XCHAIN:bool
( sender:string
receiver:string
amount:decimal
target-chain:string
)
@managed amount TRANSFER_XCHAIN-mgr
(enforce-unit amount)
(enforce (> amount 0.0) "Cross-chain transfers require a positive amount")
(compose-capability (DEBIT sender))
)
(defun TRANSFER_XCHAIN-mgr:decimal
( managed:decimal
requested:decimal
)
(enforce (>= managed requested)
(format "TRANSFER_XCHAIN exceeded for balance {}" [managed]))
0.0
)
(defcap TRANSFER_XCHAIN_RECD:bool
( sender:string
receiver:string
amount:decimal
source-chain:string
)
@event true
)
; --------------------------------------------------------------------------
; Constants
(defconst ROOT_ACCOUNT_ID:string 'STEAKFACTORY
" ID for the account which initially owns all the tokens. ")
(defconst INITIAL_SUPPLY:decimal 100000000.0
" Initial supply of 100 million tokens.")
(defconst DECIMALS 12
" Specifies the minimum denomination for token transactions. ")
(defconst ACCOUNT_ID_CHARSET CHARSET_LATIN1
" Allowed character set for account IDs. ")
(defconst ACCOUNT_ID_PROHIBITED_CHARACTER "$")
(defconst ACCOUNT_ID_MIN_LENGTH 3
" Minimum character length for account IDs. ")
(defconst ACCOUNT_ID_MAX_LENGTH 256
" Maximum character length for account IDs. ")
; --------------------------------------------------------------------------
; Burn
(defun burn (accountId:string amount:decimal)
@doc " Burns STEAK token "
(with-capability (ACCOUNT_GUARD accountId)
(with-read token-table accountId
{ "balance" := balance }
(enforce (<= amount balance) "Insufficient funds.")
(update token-table accountId
{ "balance" : (- balance amount) }
)
)
)
)
; --------------------------------------------------------------------------
; Utilities
(defun validate-account-id
( accountId:string )
@doc " Enforce that an account ID meets charset and length requirements. "
(enforce
(is-charset ACCOUNT_ID_CHARSET accountId)
(format
"Account ID does not conform to the required charset: {}"
[accountId]))
(enforce
(not (contains accountId ACCOUNT_ID_PROHIBITED_CHARACTER))
(format "Account ID contained a prohibited character: {}" [accountId]))
(let ((accountLength (length accountId)))
(enforce
(>= accountLength ACCOUNT_ID_MIN_LENGTH)
(format
"Account ID does not conform to the min length requirement: {}"
[accountId]))
(enforce
(<= accountLength ACCOUNT_ID_MAX_LENGTH)
(format
"Account ID does not conform to the max length requirement: {}"
[accountId]))
)
)
;; ; --------------------------------------------------------------------------
;; ; Fungible-v2 Implementation
(defun transfer-create:string
( sender:string
receiver:string
receiver-guard:guard
amount:decimal )
@doc " Transfer to an account, creating it if it does not exist. "
@model [ (property (conserves-mass amount))
(property (> amount 0.0))
(property (valid-account-id sender))
(property (valid-account-id receiver))
(property (!= sender receiver)) ]
(with-capability (TRANSFER sender receiver amount)
(debit sender amount)
(credit receiver receiver-guard amount)
)
)
(defun transfer:string
( sender:string
receiver:string
amount:decimal )
@doc " Transfer to an account, failing if the account does not exist. "
@model [ (property (conserves-mass amount))
(property (> amount 0.0))
(property (valid-account-id sender))
(property (valid-account-id receiver))
(property (!= sender receiver)) ]
(with-read token-table receiver
{ "guard" := guard }
(transfer-create sender receiver guard amount)
)
)
(defun debit
( accountId:string
amount:decimal )
@doc " Decrease an account balance. Internal use only. "
@model [ (property (> amount 0.0))
(property (valid-account-id accountId))
]
(validate-account-id accountId)
;(if (= accountId ROOT_ACCOUNT_ID) (require-capability (INTERNAL)) true)
(enforce (> amount 0.0) "Debit amount must be positive.")
(enforce-unit amount)
(require-capability (DEBIT accountId))
(with-read token-table accountId
{ "balance" := balance }
(enforce (<= amount balance) "Insufficient funds.")
(update token-table accountId
{ "balance" : (- balance amount) }
)
)
)
(defun credit
( accountId:string
guard:guard
amount:decimal )
@doc " Increase an account balance. Internal use only. "
@model [ (property (> amount 0.0))
(property (valid-account-id accountId))
]
(validate-account-id accountId)
(enforce (> amount 0.0) "Credit amount must be positive.")
(enforce-unit amount)
(require-capability (CREDIT accountId))
(with-default-read token-table accountId
{ "balance" : -1.0, "guard" : guard }
{ "balance" := balance, "guard" := retg }
; we don't want to overwrite an existing guard with the user-supplied one
(enforce (= retg guard)
"account guards do not match")
(let ((is-new
(if (= balance -1.0)
(enforce-reserved accountId guard)
false)))
(write token-table accountId
{ "balance" : (if is-new amount (+ balance amount))
, "guard" : retg
}))
))
(defun check-reserved:string (accountId:string)
" Checks ACCOUNT for reserved name and returns type if \
\ found or empty string. Reserved names start with a \
\ single char and colon, e.g. 'c:foo', which would return 'c' as type."
(let ((pfx (take 2 accountId)))
(if (= ":" (take -1 pfx)) (take 1 pfx) "")))
(defun enforce-reserved:bool (accountId:string guard:guard)
@doc "Enforce reserved account name protocols."
(let ((r (check-reserved accountId)))
(if (= "" r) true
(if (= "k" r)
(enforce
(= (format "{}" [guard])
(format "KeySet {keys: [{}],pred: keys-all}"
[(drop 2 accountId)]))
"Single-key account protocol violation")
(enforce false
(format "Unrecognized reserved protocol: {}" [r]))))))
(defschema crosschain-schema
@doc " Schema for yielded value in cross-chain transfers "
receiver:string
receiver-guard:guard
amount:decimal
source-chain:string
)
(defpact transfer-crosschain:string
( sender:string
receiver:string
receiver-guard:guard
target-chain:string
amount:decimal )
@model [ (property (> amount 0.0))
(property (!= receiver ""))
(property (valid-account-id sender))
(property (valid-account-id receiver))
]
(step
(with-capability (TRANSFER_XCHAIN sender receiver amount target-chain)
(validate-account-id sender)
(validate-account-id receiver)
(enforce (!= "" target-chain) "empty target-chain")
(enforce (!= (at 'chain-id (chain-data)) target-chain)
"cannot run cross-chain transfers to the same chain")
(enforce (> amount 0.0)
"transfer quantity must be positive")
(enforce-unit amount)
;; Step 1 - debit sender account on current chain
(debit sender amount)
(emit-event (TRANSFER sender "" amount))
(let
((crosschain-details:object{crosschain-schema}
{ "receiver" : receiver
, "receiver-guard" : receiver-guard
, "amount" : amount
, "source-chain" : (at 'chain-id (chain-data))
}
))
(yield crosschain-details target-chain)
)
)
)
(step
(resume
{ "receiver" := receiver
, "receiver-guard" := receiver-guard
, "amount" := amount
}
;; Step 2 - credit receiver account on target chain
(with-capability (CREDIT receiver)
(credit receiver receiver-guard amount)
)
)
)
)
(defun get-balance:decimal
( account:string )
(at 'balance (read token-table account ['balance]))
)
(defun details:object{fungible-v2.account-details}
( account:string )
(with-read token-table account
{ "balance" := balance
, "guard" := guard
}
{ "account" : account
, "balance" : balance
, "guard" : guard
}
)
)
(defun precision:integer
()
DECIMALS
)
(defun enforce-unit:bool
( amount:decimal )
@doc " Enforce the minimum denomination for token transactions. "
(enforce
(= (floor amount DECIMALS) amount)
(format "Amount violates minimum denomination: {}" [amount])
)
)
(defun create-account:string
( account:string
guard:guard )
@doc " Create a new account. "
@model [ (property (valid-account-id account)) ]
(validate-account-id account)
(enforce-reserved account guard)
(insert token-table account
{ "balance" : 0.0
, "guard" : guard
}
)
)
(defun rotate:string
( account:string
new-guard:guard )
(with-read token-table account
{ "guard" := oldGuard }
;(if (= account ROOT_ACCOUNT_ID) (require-capability (INTERNAL)) true)
(enforce-guard oldGuard)
(enforce-guard new-guard)
(update token-table account
{ "guard" : new-guard }
)
)
)
;; ; --------------------------------------------------------------------------
;; ; Custom Functions
(defun initialize:string
()
@doc " Initialize the contract. \
\ Admin-only. Should fail if it has been called before. "
(with-capability (GOVERNANCE)
(create-account ROOT_ACCOUNT_ID (create-pool-guard ROOT_ACCOUNT_ID))
(update token-table ROOT_ACCOUNT_ID { "balance" : INITIAL_SUPPLY })
)
)
(defun move-premine:string
( receiver:string
rguard:guard
amount:decimal )
@doc " Admin-only. Move the premine. "
(with-capability (GOVERNANCE)
(with-capability (POOL_GUARD ROOT_ACCOUNT_ID)
(install-capability (TRANSFER ROOT_ACCOUNT_ID receiver amount))
(transfer-create ROOT_ACCOUNT_ID receiver rguard amount)
)
)
)
)
;(create-table token-table)
;(free.stake-token.initialize)