-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.pact
342 lines (276 loc) · 8.05 KB
/
tokens.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
(enforce-pact-version "3.7")
(namespace (read-msg 'ns))
(module tokens GOVERNANCE
@model
[
;; prop-supply-write-issuer-guard
(property
(forall (token:string)
(when (row-written supplies token)
(row-enforced issuers 'guard ISSUER_KEY)))
{ 'except:
[ transfer-crosschain ;; VACUOUS
debit ;; PRIVATE
credit ;; PRIVATE
update-supply ;; PRIVATE
] } )
;; prop-ledger-write-guard
(property
(forall (key:string)
(when (row-written ledger key)
(or
(row-enforced issuers 'guard ISSUER_KEY) ;; issuer write
(row-enforced ledger 'guard key)))) ;; owner write
{ 'except:
[ transfer-crosschain ;; VACUOUS
debit ;; PRIVATE
credit ;; PRIVATE
create-account ;; prop-ledger-conserves-mass, prop-supply-conserves-mass
transfer ;; prop-ledger-conserves-mass, prop-supply-conserves-mass
transfer-create ;; prop-ledger-conserves-mass, prop-supply-conserves-mass
] } )
;; prop-ledger-conserves-mass
(property
(= (column-delta ledger 'balance) 0.0)
{ 'except:
[ transfer-crosschain ;; VACUOUS
debit ;; PRIVATE
credit ;; PRIVATE
burn ;; prop-ledger-write-guard
mint ;; prop-ledger-write-guard
] } )
;; prop-supply-conserves-mass
(property
(= (column-delta supplies 'supply) 0.0)
{ 'except:
[ transfer-crosschain ;; VACUOUS
debit ;; PRIVATE
credit ;; PRIVATE
update-supply ;; PRIVATE
burn ;; prop-ledger-write-guard
mint ;; prop-ledger-write-guard
] } )
]
(defschema entry
token:string
account:string
balance:decimal
guard:guard
)
(deftable ledger:{entry})
(use fungible-util)
(defschema issuer
guard:guard
)
(deftable issuers:{issuer})
(defschema supply
supply:decimal
)
(deftable supplies:{supply})
(defconst ISSUER_KEY "I")
(defcap GOVERNANCE ()
true)
(defcap DEBIT (token:string sender:string)
(enforce-guard
(at 'guard
(read ledger (key token sender)))))
(defcap CREDIT (token:string receiver:string) true)
(defcap UPDATE_SUPPLY ()
"private cap for update-supply"
true)
(defcap ISSUE ()
(enforce-guard (at 'guard (read issuers ISSUER_KEY)))
)
(defcap MINT (token:string account:string amount:decimal)
@managed ;; one-shot for a given amount
(compose-capability (ISSUE))
)
(defcap BURN (token:string account:string amount:decimal)
@managed ;; one-shot for a given amount
(compose-capability (ISSUE))
)
(defun init-issuer (guard:guard)
(with-capability (GOVERNANCE)
(insert issuers ISSUER_KEY {'guard: guard}))
)
(defun key ( token:string account:string )
(format "{}:{}" [token account])
)
(defun total-supply:decimal (token:string)
(with-default-read supplies token
{ 'supply : 0.0 }
{ 'supply := s }
s)
)
(defcap TRANSFER:bool
( token:string
sender:string
receiver:string
amount:decimal
)
@managed amount TRANSFER-mgr
(enforce-unit token amount)
(enforce (> amount 0.0) "Positive amount")
(compose-capability (DEBIT token sender))
(compose-capability (CREDIT token receiver))
)
(defun TRANSFER-mgr:decimal
( managed:decimal
requested:decimal
)
(let ((newbal (- managed requested)))
(enforce (>= newbal 0.0)
(format "TRANSFER exceeded for balance {}" [managed]))
newbal)
)
(defconst MINIMUM_PRECISION 12)
(defun enforce-unit:bool (token:string amount:decimal)
(enforce
(= (floor amount (precision token))
amount)
"precision violation")
)
(defun truncate:decimal (token:string amount:decimal)
(floor amount (precision token))
)
(defun create-account:string
( token:string
account:string
guard:guard
)
(enforce-valid-account account)
(insert ledger (key token account)
{ "balance" : 0.0
, "guard" : guard
, "token" : token
, "account" : account
})
)
(defun get-balance:decimal (token:string account:string)
(at 'balance (read ledger (key token account)))
)
(defun details
( token:string account:string )
(read ledger (key token account))
)
(defun rotate:string (token:string account:string new-guard:guard)
(with-read ledger (key token account)
{ "guard" := old-guard }
(enforce-guard old-guard)
(update ledger (key token account)
{ "guard" : new-guard }))
)
(defun precision:integer (token:string)
MINIMUM_PRECISION)
(defun transfer:string
( token:string
sender:string
receiver:string
amount:decimal
)
(enforce (!= sender receiver)
"sender cannot be the receiver of a transfer")
(enforce-valid-transfer sender receiver (precision token) amount)
(with-capability (TRANSFER token sender receiver amount)
(debit token sender amount)
(with-read ledger (key token receiver)
{ "guard" := g }
(credit token receiver g amount))
)
)
(defun transfer-create:string
( token:string
sender:string
receiver:string
receiver-guard:guard
amount:decimal
)
(enforce (!= sender receiver)
"sender cannot be the receiver of a transfer")
(enforce-valid-transfer sender receiver (precision token) amount)
(with-capability (TRANSFER token sender receiver amount)
(debit token sender amount)
(credit token receiver receiver-guard amount))
)
(defun mint:string
( token:string
account:string
guard:guard
amount:decimal
)
(with-capability (MINT token account amount)
(with-capability (CREDIT token account)
(credit token account guard amount)))
)
(defun burn:string
( token:string
account:string
amount:decimal
)
(with-capability (BURN token account amount)
(with-capability (DEBIT token account)
(debit token account amount)))
)
(defun debit:string
( token:string
account:string
amount:decimal
)
(require-capability (DEBIT token account))
(enforce-unit token amount)
(with-read ledger (key token account)
{ "balance" := balance }
(enforce (<= amount balance) "Insufficient funds")
(update ledger (key token account)
{ "balance" : (- balance amount) }
))
(with-capability (UPDATE_SUPPLY)
(update-supply token (- amount)))
)
(defun credit:string
( token:string
account:string
guard:guard
amount:decimal
)
(require-capability (CREDIT token account))
(enforce-unit token amount)
(with-default-read ledger (key token account)
{ "balance" : 0.0, "guard" : guard }
{ "balance" := balance, "guard" := retg }
(enforce (= retg guard)
"account guards do not match")
(write ledger (key token account)
{ "balance" : (+ balance amount)
, "guard" : retg
, "token" : token
, "account" : account
})
(with-capability (UPDATE_SUPPLY)
(update-supply token amount))
))
(defun update-supply (token:string amount:decimal)
(require-capability (UPDATE_SUPPLY))
(with-default-read supplies token
{ 'supply: 0.0 }
{ 'supply := s }
(write supplies token {'supply: (+ s amount)}))
)
(defpact transfer-crosschain:string
( token:string
sender:string
receiver:string
receiver-guard:guard
target-chain:string
amount:decimal )
(step (format "{}" [(enforce false "cross chain not supported")]))
)
(defun get-tokens ()
"Get all token identifiers"
(keys supplies))
)
(if (read-msg 'upgrade)
["upgrade complete"]
[ (create-table ledger)
(create-table issuers)
(create-table supplies) ])