-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathoracle.repl
59 lines (50 loc) · 2.53 KB
/
oracle.repl
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
(load "../../../pact-repl-utils/init.repl")
; To set up our test environment we must register the Charkha admin keyset (as
; it is required for the contract deployment).
(begin-tx)
(env-data { "charkha-admin-keyset": { "keys": [ "charkha-admin-key" ], "pred": "keys-all" } })
(namespace "free")
(define-keyset "free.charkha-admin-keyset" (read-keyset "charkha-admin-keyset"))
(commit-tx)
; Next, we can sign a transaction in which we "deploy" the oracle contract.
(begin-tx)
(env-data { "init": true })
(env-sigs [ { "key": "charkha-admin-key", "caps": [] } ])
(load "./oracle.pact")
(commit-tx)
(print "Loaded oracle contract! Ready for testing...")
; Now we can begin testing! First, we'll verify the contract.
(verify "free.charkha-oracle")
; Next, let's register the assets that are used in our contract.
(begin-tx)
(use free.charkha-oracle)
(env-sigs [ { "key": "charkha-admin-key", "caps": [ (ADMIN) ] } ])
(register-asset "KDA" 0.0)
(register-asset "CHRK" 0.0)
(register-asset "KETH" 0.0)
; We can also test out some failure cases.
(expect-failure "Cannot register an asset twice." (register-asset "KDA" 0.0))
(expect-failure "Cannot set asset price to a negative number." (register-asset "KDA2" -0.00000000001))
(env-sigs [])
(expect-failure "Cannot register an asset without the ADMIN capability." (register-asset "KDA2" 1))
(commit-tx)
; While our trickiest function is registering an asset, we can ensure we've got
; tests for our other module functions as well.
(begin-tx)
(use free.charkha-oracle)
(expect "Assets are CHRK, KDA, KETH" [ "CHRK", "KDA", "KETH" ] (get-assets))
(expect-failure "Cannot set prices without the ADMIN capability." (set-price "KDA" 1.11))
(expect-failure "Cannot set asset price if the asset is not registered." (set-price "KDA2" 1.0))
(env-sigs [ { "key": "charkha-admin-key", "caps": [ (ADMIN) ] } ])
(expect "KDA price was originally 0.0" 0.0 (get-price "KDA"))
(set-price "KDA" 1.11)
(expect "KDA price has been set" 1.11 (get-price "KDA"))
; Next, let's make sure that setting prices reflects the correct block time. By
; default the REPL will set the block-time to the epoch, ie. "1970-01-01T00:00:00Z",
; but we can override this environment data with (env-chain-data). This needs to
; be an actual parsed time value:
; https://pact-language.readthedocs.io/en/stable/pact-reference.html#time-formats
(env-chain-data { "block-time": (time "2023-06-01T00:00:00Z") })
(set-price "KDA" 2.410)
(expect "KDA price and time were updated" { "usd-price": 2.410, "last-updated": (time "2023-06-01T00:00:00Z") } (get-asset "KDA"))
(commit-tx)