-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.repl
More file actions
441 lines (411 loc) · 12.7 KB
/
token.repl
File metadata and controls
441 lines (411 loc) · 12.7 KB
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
; Setup KeySets to define a namespace
(env-data {
"namespace-keyset": {
"keys": [],
"pred": "keys-all"
},
"ac-keyset": {
"keys": [],
"pred": "keys-all"
}
})
; Setup namespace
(begin-tx)
(define-namespace 'free (read-keyset "namespace-keyset") (read-keyset "ac-keyset"))
(commit-tx)
(env-data {
"academy-token-keyset": {
"keys": [
"academy-token-public-key"
],
"pred": "keys-all"
}
})
(env-sigs [{
"key": "academy-token-public-key",
"caps": []
}])
; ; Load the pact contracts from root in oder to be able to make transactions
(begin-tx)
(load "./fungible-v2.pact")
(load "./fungible-xchain-v1.pact")
(load "token.pact")
(commit-tx)
(env-sigs [])
(env-data {})
; Disabled due to lambda's not being verified: https://github.com/kadena-io/pact/issues/1065
; (verify "free.academy-token")
; (typecheck "free.academy-token")
; Init module
(env-sigs [{
"key": "academy-token-public-key",
"caps": []
}])
(begin-tx)
(free.academy-token.init (describe-keyset "free.academy-token-keyset"))
(commit-tx)
(env-data {})
(env-sigs [])
; Grand a new user some tokens
(env-data { "new-user-keyset": { "keys": [ "user-public-key" ], "pred": "keys-all" } })
(begin-tx)
(namespace "free")
(define-keyset "free.new-user-keyset" (read-keyset "new-user-keyset"))
(commit-tx)
(env-data {})
(env-sigs [{
"key": "academy-token-public-key",
"caps": []
}])
(begin-tx)
(expect
"Expect the new user to have 42.0 tokens"
42.0
(at "balance" (free.academy-token.grant-tokens "new-user" (describe-keyset "free.new-user-keyset") 42.0)))
(expect
"The remaining tokens should be 99958.0"
99958.0
(at "balance" (free.academy-token.check-balance)))
(commit-tx)
; Test granting another user some tokens
(env-data { "other-user-keyset": { "keys": [ "other-user-public-key" ], "pred": "keys-all" } })
(begin-tx)
(namespace "free")
(define-keyset "free.other-user-keyset" (read-keyset "other-user-keyset"))
(commit-tx)
(env-data {})
(env-sigs [{
"key": "academy-token-public-key",
"caps": []
}])
(begin-tx)
(expect
"Expect the other user to have 42.0 tokens"
42.0
(at "balance" (free.academy-token.grant-tokens "other-user" (describe-keyset "free.other-user-keyset") 42.0)))
(expect
"Expect the remaining balance to be 99916.0"
99916.0
(at "balance" (free.academy-token.check-balance)))
(commit-tx)
(env-data {})
(env-sigs [])
; ; Only admin can grant tokens
; (env-sigs [{
; "key": "user-public-key",
; "caps": []
; }])
; (begin-tx)
; (expect-failure
; "Expect that only the admin can transfer tokens"
; "enforce-keyset"
; (free.academy-token.grant-tokens "other-user" (describe-keyset "free.other-user-keyset") 100.0))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test that the v2 fungible interface is implemented
; (env-sigs [])
; (begin-tx)
; (expect
; "Expect the user to be able to retrieve their balance"
; 100.0
; (free.academy-token.get-balance "new-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; (env-sigs [])
; (begin-tx)
; (expect
; "Expect the user details to be retrievable"
; { "account": "new-user", "balance": 100.0, "guard": (describe-keyset "free.new-user-keyset") }
; (free.academy-token.details "new-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; (env-sigs [])
; (begin-tx)
; ; Test that invalid accounts can't be created
; (env-sigs [])
; (begin-tx)
; (expect-failure "Expect an account that violates the single key protocol to be blocked"
; "Should either contain no protocol or adhere to the single key account protocol"
; (free.academy-token.create-account "k:new-user-created" (describe-keyset "free.new-user-keyset")) )
; (commit-tx)
; ; Test that a valid account can be created
; (env-sigs [])
; (begin-tx)
; (free.academy-token.create-account "new-user-created" (describe-keyset "free.new-user-keyset"))
; (expect "Expect that a account can be created"
; { "account": "new-user-created", "balance": 0.0, "guard": (describe-keyset "free.new-user-keyset") }
; (free.academy-token.details "new-user-created"))
; (commit-tx)
; ; Test that a account guard can be rotated
; (env-sigs [{
; "key": "other-user-public-key",
; "caps": []
; } {
; "key": "user-public-key",
; "caps": []
; }])
; (begin-tx)
; (free.academy-token.rotate "new-user-created" (describe-keyset "free.other-user-keyset"))
; (expect "Expect that a account guard can be rotated"
; { "account": "new-user-created", "balance": 0.0, "guard": (describe-keyset "free.other-user-keyset") }
; (free.academy-token.details "new-user-created"))
; (commit-tx)
; ; Test a transfer
; (env-sigs [{
; "key": "user-public-key",
; "caps": [(free.academy-token.TRANSFER "new-user" "other-user" 25.0)]
; }])
; (begin-tx)
; (free.academy-token.transfer "new-user" "other-user" 25.0)
; (expect "Expect the new user to have 75 tokens left"
; 75.0
; (free.academy-token.get-balance "new-user"))
; (expect "Expect that the other user to have 125 tokens"
; 125.0
; (free.academy-token.get-balance "other-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test a transfer to non existing account
; (env-sigs [{
; "key": "user-public-key",
; "caps": [(free.academy-token.TRANSFER "new-user" "non-existing-user" 25.0)]
; }])
; (begin-tx)
; (expect-failure
; "with-read: row not found"
; (free.academy-token.transfer "new-user" "non-existing-user" 25.0))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test a cross chain transfer
; (env-keys ["user-public-key"])
; (begin-tx)
; (free.academy-token.transfer-crosschain "new-user" "other-user" (describe-keyset "free.other-user-keyset") "TestNet00/3" 25.0)
; (expect "Expect the new user to have 50 tokens left"
; 50.0
; (free.academy-token.get-balance "new-user"))
; (env-chain-data
; { "chain-id": "TestNet00/3"
; , "block-height": 21
; , "block-time": (time "2024-01-01T00:00:00Z")
; })
; (continue-pact 1)
; (expect "Expect that the other user to have 150 tokens"
; 150.0
; (free.academy-token.get-balance "other-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test a cross chain transfer to non existing user
; (env-chain-data
; { "chain-id": "TestNet00/2"
; , "block-height": 20
; , "block-time": (time "2024-01-01T00:00:00Z")
; })
; (env-keys ["user-public-key"])
; (begin-tx)
; (expect-failure
; "with-read: row not found"
; (free.academy-token.transfer-crosschain "new-user" "non-existing-user" (describe-keyset "free.non-existing-user-keyset") "TestNet00/3" 25.0))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test a safe transfer: storing funds
; (env-sigs [{
; "key": "user-public-key",
; "caps": [(free.academy-token.TRANSFER "new-user" "other-user" 25.0)]
; }])
; (env-hash (hash "paying transaction"))
; (begin-tx)
; (let ((other-user-balance (free.academy-token.get-balance "other-user")))
; (free.academy-token.safe-transfer "new-user" "other-user" 25.0 600)
; (expect "Expect the new user to have 50 tokens left"
; 25.0
; (free.academy-token.get-balance "new-user"))
; (expect "Expect that the other user to have not have any additional tokens"
; other-user-balance
; (free.academy-token.get-balance "other-user")))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test a safe transfer: retrieval of funds
; (env-sigs [{
; "key": "other-user-public-key",
; "caps": []
; }])
; ; Set a different hash to simulate a different transaction
; (env-hash (hash "retrieving transaction"))
; (begin-tx)
; (free.academy-token.safe-transfer-retrieve (hash "paying transaction"))
; (expect "Expect that the other user to have additional tokens"
; 175.0
; (free.academy-token.get-balance "other-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test that a acquisition can only be retrieved once
; (env-sigs [{
; "key": "other-user-public-key",
; "caps": []
; }])
; (begin-tx)
; (expect-failure
; "Expect the safe transfer to be already retrieved"
; "There is nothing to retrieve"
; (free.academy-token.safe-transfer-retrieve (hash "paying transaction")))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test expired safe transfer
; ; Set a different hash to simulate a different transaction
; (env-chain-data
; { "chain-id": "TestNet00/2"
; , "block-height": 20
; , "block-time": (time "2024-01-01T00:00:00Z")
; })
; (env-sigs [{
; "key": "other-user-public-key",
; "caps": [(free.academy-token.TRANSFER "other-user" "new-user" 25.0)]
; }])
; (env-hash (hash "expired safe transfer"))
; (begin-tx)
; (free.academy-token.safe-transfer "other-user" "new-user" 10.0 600)
; (commit-tx)
; (env-hash (hash "expired safe transfer 2"))
; (begin-tx)
; (free.academy-token.safe-transfer "other-user" "new-user" 15.0 600)
; (expect "Expect the other user to have 150 tokens left"
; 150.0
; (free.academy-token.get-balance "other-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test that the safe transfer is funded back to the other-user
; (env-chain-data
; { "chain-id": "TestNet00/2"
; , "block-height": 20
; , "block-time": (time "2024-01-01T00:11:00Z")
; })
; (begin-tx)
; (expect "Expect the other user to have 175 tokens left after refunding"
; 175.0
; (free.academy-token.get-balance "other-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test safe cross chain transfer
; (env-chain-data
; { "chain-id": "TestNet00/2"
; , "block-height": 20
; , "block-time": (time "2024-01-01T00:00:00Z")
; })
; (env-sigs [{
; "key": "other-user-public-key",
; "caps": [
; (free.academy-token.TRANSFER "other-user" "new-user" 25.0)
; (free.academy-token.DEBIT "other-user")
; ]
; }])
; (env-hash (hash "cross chain safe transfer"))
; (begin-tx)
; (free.academy-token.safe-transfer-crosschain "other-user" "new-user" "TestNet00/3" 25.0 600)
; (expect "Expect the other user to have 150 tokens left"
; 150.0
; (free.academy-token.get-balance "other-user"))
; (env-chain-data
; { "chain-id": "TestNet00/3"
; , "block-height": 21
; , "block-time": (time "2024-01-01T00:01:00Z")
; })
; (expect "Expect the new user to have 25 tokens left"
; 25.0
; (free.academy-token.get-balance "new-user"))
; (continue-pact 1)
; (expect "Expect the new user to have 25 tokens left"
; 25.0
; (free.academy-token.get-balance "new-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test the retrieval cross chain safe transfer
; (env-keys ["user-public-key"])
; (env-hash (hash "cross chain safe transfer retrieval"))
; (begin-tx)
; (free.academy-token.safe-transfer-retrieve (hash "cross chain safe transfer"))
; (expect "Expect the new user to have 50 tokens left"
; 50.0
; (free.academy-token.get-balance "new-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ;Test expiration of cross chain safe transfer
; (env-chain-data
; { "chain-id": "TestNet00/2"
; , "block-height": 20
; , "block-time": (time "2024-01-01T00:00:00Z")
; })
; (env-sigs [{
; "key": "other-user-public-key",
; "caps": [
; (free.academy-token.TRANSFER "other-user" "new-user" 25.0)
; (free.academy-token.DEBIT "other-user")
; ]
; }])
; (env-hash (hash "expired cross chain safe transfer"))
; (begin-tx)
; (free.academy-token.safe-transfer-crosschain "other-user" "new-user" "TestNet00/3" 25.0 600)
; (expect "Expect the other user to have 125 tokens left"
; 125.0
; (free.academy-token.get-balance "other-user"))
; (env-chain-data
; { "chain-id": "TestNet00/3"
; , "block-height": 21
; , "block-time": (time "2024-01-01T00:01:00Z")
; })
; (expect "Expect the new user to have 50 tokens left"
; 50.0
; (free.academy-token.get-balance "new-user"))
; (continue-pact 1)
; (expect "Expect the new user to have 50 tokens left"
; 50.0
; (free.academy-token.get-balance "new-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; ; Test the retrieval of expired safe crosschain transaction
; (env-chain-data
; { "chain-id": "TestNet00/3"
; , "block-height": 21
; , "block-time": (time "2024-02-01T00:00:00Z")
; })
; (env-hash (hash "expired cross chain safe transfer retrieval"))
; (begin-tx)
; (expect "Expect the new user to have 50 tokens left"
; 50.0
; (free.academy-token.get-balance "new-user"))
; (expect "Expect the other use to have 150 tokens"
; 150.0
; (free.academy-token.get-balance "other-user"))
; (commit-tx)
; (env-data {})
; (env-sigs [])
; (env-sigs [{
; "key": "user-public-key",
; "caps": [
; (free.academy-token.TRANSFER "new-user" "other-user" 25.0)
; (free.academy-token.DEBIT "new-user")
; ]
; }
; {
; "key": "other-user-public-key",
; "caps": []
; }])
; (begin-tx)
; (free.academy-token.direct-safe-transfer "new-user" "other-user" 10.0)
; (commit-tx)
; (env-data {})
; (env-sigs [])