-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguards1.pact
96 lines (76 loc) · 3.46 KB
/
guards1.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
;; guards1.pact
(namespace 'util)
(module guards1 'util-ns-admin
"************************WARNING************************\
\ This module is currently governed by 'util-ns-admin \
\ and should not be in use until the governance is \
\ replaced with AUTONOMOUS, meaning that the module \
\ will be non-upgradable. \
\ ******************************************************\
\ Functions for implementing gas guards."
; (defcap AUTONOMOUS ()
; (enforce false "Non-upgradeable"))
(defun guard-all:guard (guards:[guard])
"Create a guard that only succeeds if every guard in GUARDS is successfully enforced."
(enforce (< 0 (length guards)) "Guard list cannot be empty")
(create-user-guard (enforce-guard-all guards)))
(defun enforce-guard-all:bool (guards:[guard])
"Enforces all guards in GUARDS"
(map (enforce-guard) guards)
)
(defun guard-any:guard (guards:[guard])
"Create a guard that succeeds if at least one guard in GUARDS is successfully enforced."
(enforce (< 0 (length guards)) "Guard list cannot be empty")
(create-user-guard (enforce-guard-any guards)))
(defun enforce-guard-any:bool (guards:[guard])
"Will succeed if at least one guard in GUARDS is successfully enforced."
(enforce (< 0
(length
(filter
(= true)
(map (try-enforce-guard) guards))))
"None of the guards passed")
)
(defun try-enforce-guard (g:guard)
(try false (enforce-guard g))
)
(defun max-gas-notional:guard (gasNotional:decimal)
"Guard to enforce gas price * gas limit is smaller than or equal to GAS"
(create-user-guard
(enforce-below-or-at-gas-notional gasNotional)))
(defun enforce-below-gas-notional (gasNotional:decimal)
(enforce (< (chain-gas-notional) gasNotional)
(format "Gas Limit * Gas Price must be smaller than {}" [gasNotional])))
(defun enforce-below-or-at-gas-notional (gasNotional:decimal)
(enforce (<= (chain-gas-notional) gasNotional)
(format "Gas Limit * Gas Price must be smaller than or equal to {}" [gasNotional])))
(defun max-gas-price:guard (gasPrice:decimal)
"Guard to enforce gas price is smaller than or equal to GAS PRICE"
(create-user-guard
(enforce-below-or-at-gas-price gasPrice)))
(defun enforce-below-gas-price:bool (gasPrice:decimal)
(enforce (< (chain-gas-price) gasPrice)
(format "Gas Price must be smaller than {}" [gasPrice])))
(defun enforce-below-or-at-gas-price:bool (gasPrice:decimal)
(enforce (<= (chain-gas-price) gasPrice)
(format "Gas Price must be smaller than or equal to {}" [gasPrice])))
(defun max-gas-limit:guard (gasLimit:integer)
"Guard to enforce gas limit is smaller than or equal to GAS LIMIT"
(create-user-guard
(enforce-below-or-at-gas-limit gasLimit)))
(defun enforce-below-gas-limit:bool (gasLimit:integer)
(enforce (< (chain-gas-limit) gasLimit)
(format "Gas Limit must be smaller than {}" [gasLimit])))
(defun enforce-below-or-at-gas-limit:bool (gasLimit:integer)
(enforce (<= (chain-gas-limit) gasLimit)
(format "Gas Limit must be smaller than or equal to {}" [gasLimit])))
(defun chain-gas-price ()
"Return gas price from chain-data"
(at 'gas-price (chain-data)))
(defun chain-gas-limit ()
"Return gas limit from chain-data"
(at 'gas-limit (chain-data)))
(defun chain-gas-notional ()
"Return gas limit * gas price from chain-data"
(* (chain-gas-price) (chain-gas-limit)))
)