-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin.pact
583 lines (440 loc) · 15.8 KB
/
coin.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
(module coin GOVERNANCE
@doc "'coin' represents the Kadena Coin Contract. This contract provides both the \
\buy/redeem gas support in the form of 'fund-tx', as well as transfer, \
\credit, debit, coinbase, account creation and query, as well as SPV burn \
\create. To access the coin contract, you may use its fully-qualified name, \
\or issue the '(use coin)' command in the body of a module declaration."
@model
[ (defproperty conserves-mass
(= (column-delta coin-table 'balance) 0.0))
(defproperty valid-account (account:string)
(and
(>= (length account) 3)
(<= (length account) 256)))
]
(implements fungible-v2)
; --------------------------------------------------------------------------
; Schemas and Tables
(defschema coin-schema
@doc "The coin contract token schema"
@model [ (invariant (>= balance 0.0)) ]
balance:decimal
guard:guard)
(deftable coin-table:{coin-schema})
; --------------------------------------------------------------------------
; Capabilities
(defcap GOVERNANCE ()
(enforce false "Enforce non-upgradeability"))
(defcap GAS ()
"Magic capability to protect gas buy and redeem"
true)
(defcap COINBASE ()
"Magic capability to protect miner reward"
true)
(defcap GENESIS ()
"Magic capability constraining genesis transactions"
true)
(defcap REMEDIATE ()
"Magic capability for remediation transactions"
true)
(defcap DEBIT (sender:string)
"Capability for managing debiting operations"
(enforce-guard (at 'guard (read coin-table sender)))
(enforce (!= sender "") "valid sender"))
(defcap CREDIT (receiver:string)
"Capability for managing crediting operations"
(enforce (!= receiver "") "valid receiver"))
(defcap ROTATE (account:string)
@doc "Autonomously managed capability for guard rotation"
@managed
true)
(defcap TRANSFER:bool
( sender:string
receiver:string
amount:decimal
)
@managed amount TRANSFER-mgr
(enforce (!= sender receiver) "same sender and receiver")
(enforce-unit amount)
(enforce (> amount 0.0) "Positive amount")
(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)
)
; --------------------------------------------------------------------------
; Constants
(defconst COIN_CHARSET CHARSET_LATIN1
"The default coin contract character set")
(defconst MINIMUM_PRECISION 12
"Minimum allowed precision for coin transactions")
(defconst MINIMUM_ACCOUNT_LENGTH 3
"Minimum account length admissible for coin accounts")
(defconst MAXIMUM_ACCOUNT_LENGTH 256
"Maximum account name length admissible for coin accounts")
; --------------------------------------------------------------------------
; Utilities
(defun enforce-unit:bool (amount:decimal)
@doc "Enforce minimum precision allowed for coin transactions"
(enforce
(= (floor amount MINIMUM_PRECISION)
amount)
(format "Amount violates minimum precision: {}" [amount]))
)
(defun validate-account (account:string)
@doc "Enforce that an account name conforms to the coin contract \
\minimum and maximum length requirements, as well as the \
\latin-1 character set."
(enforce
(is-charset COIN_CHARSET account)
(format
"Account does not conform to the coin contract charset: {}"
[account]))
(let ((account-length (length account)))
(enforce
(>= account-length MINIMUM_ACCOUNT_LENGTH)
(format
"Account name does not conform to the min length requirement: {}"
[account]))
(enforce
(<= account-length MAXIMUM_ACCOUNT_LENGTH)
(format
"Account name does not conform to the max length requirement: {}"
[account]))
)
)
; --------------------------------------------------------------------------
; Coin Contract
(defun gas-only ()
"Predicate for gas-only user guards."
(require-capability (GAS)))
(defun gas-guard (guard:guard)
"Predicate for gas + single key user guards"
(enforce-one
"Enforce either the presence of a GAS cap or keyset"
[ (gas-only)
(enforce-guard guard)
]))
(defun buy-gas:string (sender:string total:decimal)
@doc "This function describes the main 'gas buy' operation. At this point \
\MINER has been chosen from the pool, and will be validated. The SENDER \
\of this transaction has specified a gas limit LIMIT (maximum gas) for \
\the transaction, and the price is the spot price of gas at that time. \
\The gas buy will be executed prior to executing SENDER's code."
@model [ (property (> total 0.0))
(property (valid-account sender))
]
(validate-account sender)
(enforce-unit total)
(enforce (> total 0.0) "gas supply must be a positive quantity")
(require-capability (GAS))
(with-capability (DEBIT sender)
(debit sender total))
)
(defun redeem-gas:string (miner:string miner-guard:guard sender:string total:decimal)
@doc "This function describes the main 'redeem gas' operation. At this \
\point, the SENDER's transaction has been executed, and the gas that \
\was charged has been calculated. MINER will be credited the gas cost, \
\and SENDER will receive the remainder up to the limit"
@model [ (property (> total 0.0))
(property (valid-account sender))
(property (valid-account miner))
]
(validate-account sender)
(validate-account miner)
(enforce-unit total)
(require-capability (GAS))
(let*
((fee (read-decimal "fee"))
(refund (- total fee)))
(enforce-unit fee)
(enforce (>= fee 0.0)
"fee must be a non-negative quantity")
(enforce (>= refund 0.0)
"refund must be a non-negative quantity")
; directly update instead of credit
(with-capability (CREDIT sender)
(if (> refund 0.0)
(with-read coin-table sender
{ "balance" := balance }
(update coin-table sender
{ "balance": (+ balance refund) }))
"noop"))
(with-capability (CREDIT miner)
(if (> fee 0.0)
(credit miner miner-guard fee)
"noop"))
)
)
(defun create-account:string (account:string guard:guard)
@model [ (property (valid-account account)) ]
(validate-account account)
(insert coin-table account
{ "balance" : 0.0
, "guard" : guard
})
)
(defun get-balance:decimal (account:string)
(with-read coin-table account
{ "balance" := balance }
balance
)
)
(defun details:object{fungible-v2.account-details}
( account:string )
(with-read coin-table account
{ "balance" := bal
, "guard" := g }
{ "account" : account
, "balance" : bal
, "guard": g })
)
(defun rotate:string (account:string new-guard:guard)
(with-capability (ROTATE account)
(with-read coin-table account
{ "guard" := old-guard }
(enforce-guard old-guard)
(update coin-table account
{ "guard" : new-guard }
)))
)
(defun precision:integer
()
MINIMUM_PRECISION)
(defun transfer:string (sender:string receiver:string amount:decimal)
@model [ (property conserves-mass)
(property (> amount 0.0))
(property (valid-account sender))
(property (valid-account receiver))
(property (!= sender receiver)) ]
(enforce (!= sender receiver)
"sender cannot be the receiver of a transfer")
(validate-account sender)
(validate-account receiver)
(enforce (> amount 0.0)
"transfer amount must be positive")
(enforce-unit amount)
(with-capability (TRANSFER sender receiver amount)
(debit sender amount)
(with-read coin-table receiver
{ "guard" := g }
(credit receiver g amount))
)
)
(defun transfer-create:string
( sender:string
receiver:string
receiver-guard:guard
amount:decimal )
@model [ (property conserves-mass) ]
(enforce (!= sender receiver)
"sender cannot be the receiver of a transfer")
(validate-account sender)
(validate-account receiver)
(enforce (> amount 0.0)
"transfer amount must be positive")
(enforce-unit amount)
(with-capability (TRANSFER sender receiver amount)
(debit sender amount)
(credit receiver receiver-guard amount))
)
(defun coinbase:string (account:string account-guard:guard amount:decimal)
@doc "Internal function for the initial creation of coins. This function \
\cannot be used outside of the coin contract."
@model [ (property (valid-account account))
(property (> amount 0.0))
]
(validate-account account)
(enforce-unit amount)
(require-capability (COINBASE))
(with-capability (CREDIT account)
(credit account account-guard amount))
)
(defun remediate:string (account:string amount:decimal)
@doc "Allows for remediation transactions. This function \
\is protected by the REMEDIATE capability"
@model [ (property (valid-account account))
(property (> amount 0.0))
]
(validate-account account)
(enforce (> amount 0.0)
"Remediation amount must be positive")
(enforce-unit amount)
(require-capability (REMEDIATE))
(with-read coin-table account
{ "balance" := balance }
(enforce (<= amount balance) "Insufficient funds")
(update coin-table account
{ "balance" : (- balance amount) }
))
)
(defpact fund-tx (sender:string miner:string miner-guard:guard total:decimal)
@doc "'fund-tx' is a special pact to fund a transaction in two steps, \
\with the actual transaction transpiring in the middle: \
\ \
\ 1) A buying phase, debiting the sender for total gas and fee, yielding \
\ TX_MAX_CHARGE. \
\ 2) A settlement phase, resuming TX_MAX_CHARGE, and allocating to the \
\ coinbase account for used gas and fee, and sender account for bal- \
\ ance (unused gas, if any)."
@model [ (property (> total 0.0))
(property (valid-account sender))
(property (valid-account miner))
;(property conserves-mass) not supported yet
]
(step (buy-gas sender total))
(step (redeem-gas miner miner-guard sender total))
)
(defun debit:string (account:string amount:decimal)
@doc "Debit AMOUNT from ACCOUNT balance"
@model [ (property (> amount 0.0))
(property (valid-account account))
]
(validate-account account)
(enforce (> amount 0.0)
"debit amount must be positive")
(enforce-unit amount)
(require-capability (DEBIT account))
(with-read coin-table account
{ "balance" := balance }
(enforce (<= amount balance) "Insufficient funds")
(update coin-table account
{ "balance" : (- balance amount) }
))
)
(defun credit:string (account:string guard:guard amount:decimal)
@doc "Credit AMOUNT to ACCOUNT balance"
@model [ (property (> amount 0.0))
(property (valid-account account))
]
(validate-account account)
(enforce (> amount 0.0) "credit amount must be positive")
(enforce-unit amount)
(require-capability (CREDIT account))
(with-default-read coin-table account
{ "balance" : 0.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")
(write coin-table account
{ "balance" : (+ balance amount)
, "guard" : retg
})
))
(defschema crosschain-schema
@doc "Schema for yielded value in cross-chain transfers"
receiver:string
receiver-guard:guard
amount:decimal)
(defpact transfer-crosschain:string
( sender:string
receiver:string
receiver-guard:guard
target-chain:string
amount:decimal )
@model [ (property (> amount 0.0))
(property (valid-account sender))
(property (valid-account receiver))
]
(step
(with-capability (DEBIT sender)
(validate-account sender)
(validate-account 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 delete-account on current chain
(debit sender amount)
(let
((crosschain-details:object{crosschain-schema}
{ "receiver" : receiver
, "receiver-guard" : receiver-guard
, "amount" : amount
}))
(yield crosschain-details target-chain)
)))
(step
(resume
{ "receiver" := receiver
, "receiver-guard" := receiver-guard
, "amount" := amount
}
;; step 2 - credit create account on target chain
(with-capability (CREDIT receiver)
(credit receiver receiver-guard amount))
))
)
; --------------------------------------------------------------------------
; Coin allocations
(defschema allocation-schema
@doc "Genesis allocation registry"
;@model [ (invariant (>= balance 0.0)) ]
balance:decimal
date:time
guard:guard
redeemed:bool)
(deftable allocation-table:{allocation-schema})
(defun create-allocation-account
( account:string
date:time
keyset-ref:string
amount:decimal
)
@doc "Add an entry to the coin allocation table. This function \
\also creates a corresponding empty coin contract account \
\of the same name and guard. Requires GENESIS capability. "
@model [ (property (valid-account account)) ]
(require-capability (GENESIS))
(validate-account account)
(enforce (>= amount 0.0)
"allocation amount must be non-negative")
(enforce-unit amount)
(let
((guard:guard (keyset-ref-guard keyset-ref)))
(create-account account guard)
(insert allocation-table account
{ "balance" : amount
, "date" : date
, "guard" : guard
, "redeemed" : false
})))
(defun release-allocation
( account:string )
@doc "Release funds associated with allocation ACCOUNT into main ledger. \
\ACCOUNT must already exist in main ledger. Allocation is deactivated \
\after release."
@model [ (property (valid-account account)) ]
(validate-account account)
(with-read allocation-table account
{ "balance" := balance
, "date" := release-time
, "redeemed" := redeemed
, "guard" := guard
}
(let ((curr-time:time (at 'block-time (chain-data))))
(enforce (not redeemed)
"allocation funds have already been redeemed")
(enforce
(>= curr-time release-time)
(format "funds locked until {}. current time: {}" [release-time curr-time]))
(enforce-guard guard)
(with-capability (CREDIT account)
(credit account guard balance)
(update allocation-table account
{ "redeemed" : true
, "balance" : 0.0
})
"Allocation successfully released to main ledger")
)))
)