assert.gno
6.47 Kb · 172 lines
1package impl
2
3import (
4 "gno.land/p/gnoswap/order/access/manager/v0"
5 pdca "gno.land/p/gnoswap/order/dca/v0"
6 prbac "gno.land/p/gnoswap/rbac/v1"
7 bptree "gno.land/p/nt/bptree/v0"
8 ufmt "gno.land/p/nt/ufmt/v0"
9
10 "gno.land/r/gnoswap/access/v1"
11 "gno.land/r/gnoswap/common"
12 "gno.land/r/gnoswap/order/halt"
13 "gno.land/r/gnoswap/order/keeper"
14 "gno.land/r/gnoswap/order/whitelist"
15)
16
17// assertIsAdminCall runs the checks shared by admin entry points
18// and returns the calling realm.
19func (d *dcaOrderImpl) assertIsAdminCall(_ int, rlm realm) realm {
20 access.AssertIsRlmCurrent(0, rlm)
21 common.AssertIsNotHandleNativeCoin()
22 d.assertIsNotExecuting()
23
24 prev := rlm.Previous()
25 access.AssertIsAdminOrGovernance(prev.Address())
26
27 return prev
28}
29
30// assertIsNotExecuting rejects re-entry from the swap
31// into any state-changing entry point.
32func (d *dcaOrderImpl) assertIsNotExecuting() {
33 if d.store.IsExecuting() {
34 panic(makeErrorWithDetails(errReentrant, "group execution in progress").Error())
35 }
36}
37
38// assertIsNotHalted rejects the call while any of operations is halted
39// for DCA orders in gno.land/r/gnoswap/order/halt.
40func assertIsNotHalted(operations ...string) {
41 if operation, halted := haltedOperation(operations...); halted {
42 panic(makeErrorWithDetails(errHalted, ufmt.Sprintf("operation(%s)", operation)).Error())
43 }
44}
45
46// haltedOperation returns the first of operations halted for DCA orders.
47func haltedOperation(operations ...string) (string, bool) {
48 for _, operation := range operations {
49 if halt.IsHalted(halt.TargetDCA, operation) {
50 return operation, true
51 }
52 }
53
54 return "", false
55}
56
57// assertIsAuthorizedKeeper rejects callers that gno.land/r/gnoswap/order/keeper
58// does not allow to run rounds on target, the DCA proxy package path.
59// ExecuteGroup is assigned to DCAKeeperRole at deployment; admin or
60// governance grants it to keepers or reassigns the selector.
61func assertIsAuthorizedKeeper(target string, caller address) {
62 if !keeper.IsAuthorized(target, keeper.SelectorExecuteGroup, caller) {
63 panic(makeErrorWithDetails(errNotKeeper, ufmt.Sprintf("caller(%s)", caller.String())).Error())
64 }
65}
66
67// assertIsManagerCall runs the checks shared by the manager entry points
68// (token minimums and pair price limits) and returns the calling realm.
69// Admin and governance may always call them; other callers need the
70// PoolManagerRole that gno.land/r/gnoswap/order/keeper assigns on the DCA
71// proxy package path.
72func (d *dcaOrderImpl) assertIsManagerCall(_ int, rlm realm, selector manager.Selector) realm {
73 access.AssertIsRlmCurrent(0, rlm)
74 common.AssertIsNotHandleNativeCoin()
75 d.assertIsNotExecuting()
76
77 prev := rlm.Previous()
78 caller := prev.Address()
79
80 if isAdminOrGovernance(caller) || keeper.IsAuthorized(rlm.PkgPath(), selector, caller) {
81 return prev
82 }
83
84 panic(makeErrorWithDetails(errNotKeeper, ufmt.Sprintf("caller(%s) selector(%s)", caller.String(), string(selector))).Error())
85}
86
87// isAdminOrGovernance reports whether caller is the GnoSwap admin or governance.
88func isAdminOrGovernance(caller address) bool {
89 return access.IsAuthorized(prbac.ROLE_ADMIN.String(), caller) || access.IsAuthorized(prbac.ROLE_GOVERNANCE.String(), caller)
90}
91
92// assertIsValidPair checks that inputToken and outputToken differ
93// and that at least one whitelisted direct pool connects them.
94func assertIsValidPair(inputToken string, outputToken string) {
95 if inputToken == outputToken {
96 panic(makeErrorWithDetails(errInvalidToken, ufmt.Sprintf("inputToken and outputToken are both %s", inputToken)).Error())
97 }
98
99 if !whitelist.HasWhitelistedPool(whitelist.TargetDCA, inputToken, outputToken) {
100 panic(makeErrorWithDetails(errInvalidPool, ufmt.Sprintf("no whitelisted pool for %s -> %s", inputToken, outputToken)).Error())
101 }
102}
103
104// assertHasPairPriceLimit checks that the pair direction has a price limit,
105// without which its rounds cannot trade.
106func assertHasPairPriceLimit(pairPriceLimits *bptree.BPTree, inputToken string, outputToken string) {
107 if getPairPriceLimit(pairPriceLimits, inputToken, outputToken) == nil {
108 panic(makeErrorWithDetails(errMissingPriceLimit, ufmt.Sprintf("no price limit for %s -> %s", inputToken, outputToken)).Error())
109 }
110}
111
112// assertIsAllowedInterval checks that new orders may use interval.
113func assertIsAllowedInterval(config *pdca.Config, interval int64) {
114 if !config.IsAllowedInterval(interval) {
115 panic(makeErrorWithDetails(errInvalidInterval, ufmt.Sprintf("interval(%d)", interval)).Error())
116 }
117}
118
119// assertIsAllowedSlot checks that new orders may use slotOffset at interval.
120func assertIsAllowedSlot(config *pdca.Config, interval int64, slotOffset int64) {
121 if !config.IsAllowedSlot(interval, slotOffset) {
122 panic(makeErrorWithDetails(
123 errInvalidSlotOffset,
124 ufmt.Sprintf("slotOffset(%d) must be a multiple of %d in [0, %d)", slotOffset, config.SlotStep(interval), interval),
125 ).Error())
126 }
127}
128
129// assertIsValidOrderSize checks the order against the configured limits;
130// minAmountPerExecution is the effective minimum of the input token
131// (see minAmountPerExecution).
132func assertIsValidOrderSize(config *pdca.Config, minAmountPerExecution int64, amountPerExecution int64, executionCount int64) {
133 if amountPerExecution < minAmountPerExecution {
134 panic(makeErrorWithDetails(
135 errInvalidAmount,
136 ufmt.Sprintf("amountPerExecution(%d) must be >= %d", amountPerExecution, minAmountPerExecution),
137 ).Error())
138 }
139
140 if executionCount <= 0 || executionCount > config.MaxExecutionCount() {
141 panic(makeErrorWithDetails(
142 errInvalidExecutionCount,
143 ufmt.Sprintf("executionCount(%d) must be in [1, %d]", executionCount, config.MaxExecutionCount()),
144 ).Error())
145 }
146}
147
148// assertIsOrderOwner checks that caller owns the order.
149func assertIsOrderOwner(order *pdca.Order, caller address) {
150 if order.Owner() != caller {
151 panic(makeErrorWithDetails(errNotOrderOwner, ufmt.Sprintf("orderId(%d)", order.ID())).Error())
152 }
153}
154
155// assertIsQuotedAmountIn checks that the round sells the input
156// amountOutMin was quoted for.
157// A floor quoted for a larger input would keep that trade's price impact,
158// which is looser than the smaller round needs, so a changed orderAmount
159// needs a new quote instead of a rescaled floor.
160// Passing 0 for both skips the check.
161func assertIsQuotedAmountIn(group *pdca.Group, expectedAmountIn int64, amountOutMin int64) {
162 if expectedAmountIn == 0 && amountOutMin == 0 {
163 return
164 }
165
166 if expectedAmountIn != group.OrderAmount() {
167 panic(makeErrorWithDetails(
168 errAmountInMismatch,
169 ufmt.Sprintf("groupId(%s) expectedAmountIn(%d) orderAmount(%d)", group.ID(), expectedAmountIn, group.OrderAmount()),
170 ).Error())
171 }
172}