manager.gno
4.19 Kb · 128 lines
1package impl
2
3import (
4 "time"
5
6 pdca "gno.land/p/gnoswap/order/dca/v0"
7 u256 "gno.land/p/gnoswap/uint256/v1"
8 ufmt "gno.land/p/nt/ufmt/v0"
9
10 "gno.land/r/gnoswap/common"
11 "gno.land/r/gnoswap/order/keeper"
12)
13
14// Manager settings.
15//
16// The pool manager (PoolManagerRole at deployment, admin and governance too)
17// sets, from an off-chain oracle price:
18// - the minimum input per execution of each token, so a new order is worth
19// at least a set value per round whatever the token's decimals; it applies
20// on top of the global minimum (Config.MinAmountPerExecution) and only to
21// new orders
22// - the price limit of each pair direction (see price_limit.gno)
23
24// SetTokenMinAmountPerExecution sets the minimum input per execution of new
25// orders selling tokenPath; 0 removes it, leaving the global minimum.
26func (d *dcaOrderImpl) SetTokenMinAmountPerExecution(_ int, rlm realm, tokenPath string, minAmountPerExecution int64) {
27 prev := d.assertIsManagerCall(0, rlm, keeper.SelectorSetTokenMinAmountPerExecution)
28
29 if minAmountPerExecution < 0 {
30 panic(makeErrorWithDetails(
31 errInvalidConfig,
32 ufmt.Sprintf("minAmountPerExecution(%d) must not be negative", minAmountPerExecution),
33 ).Error())
34 }
35
36 tokenMinAmounts := d.store.GetTokenMinAmountsPerExecution()
37 previousMinAmountPerExecution := getTokenMinAmountPerExecution(d.store, tokenPath)
38
39 if minAmountPerExecution == 0 {
40 tokenMinAmounts.Remove(tokenPath)
41 } else {
42 if err := common.IsRegistered(tokenPath); err != nil {
43 panic(makeErrorWithDetails(errInvalidToken, ufmt.Sprintf("tokenPath(%s): %s", tokenPath, err.Error())).Error())
44 }
45
46 tokenMinAmounts.Set(tokenPath, minAmountPerExecution)
47 }
48
49 emitTokenMinAmountPerExecutionUpdated(prev.Address(), prev.PkgPath(), tokenPath, previousMinAmountPerExecution, minAmountPerExecution)
50}
51
52// SetPairPriceLimit sets the minimum pool output per unit of input, in Q128,
53// of rounds selling inputToken for outputToken; "0" removes it, which stops
54// the pair direction from trading and from receiving new orders.
55// It takes effect from the next quote or round, for existing orders too.
56func (d *dcaOrderImpl) SetPairPriceLimit(_ int, rlm realm, inputToken string, outputToken string, minOutputPerInputX128 string) {
57 prev := d.assertIsManagerCall(0, rlm, keeper.SelectorSetPairPriceLimit)
58
59 if inputToken == outputToken {
60 panic(makeErrorWithDetails(errInvalidToken, ufmt.Sprintf("inputToken and outputToken are both %s", inputToken)).Error())
61 }
62
63 limitX128, err := parseMinOutputPerInputX128(minOutputPerInputX128)
64 if err != nil {
65 panic(err.Error())
66 }
67
68 pairPriceLimits := d.store.GetPairPriceLimits()
69 pairKey := pdca.PairKey(inputToken, outputToken)
70
71 previous := getPairPriceLimit(pairPriceLimits, inputToken, outputToken)
72
73 previousLimitX128 := u256.Zero()
74 if previous != nil {
75 previousLimitX128 = previous.MinOutputPerInputX128()
76 }
77
78 updatedAt := time.Now().Unix()
79
80 switch {
81 case limitX128.IsZero():
82 pairPriceLimits.Remove(pairKey)
83
84 updatedAt = 0
85 case previous != nil:
86 // Stored limits are updated in place, like other stored entries.
87 previous.Update(limitX128, updatedAt)
88 default:
89 if err := common.ValidateRegistered(inputToken, outputToken); err != nil {
90 panic(makeErrorWithDetails(errInvalidToken, err.Error()).Error())
91 }
92
93 pairPriceLimits.Set(pairKey, pdca.NewPairPriceLimit(limitX128, updatedAt))
94 }
95
96 emitPairPriceLimitUpdated(
97 prev.Address(),
98 prev.PkgPath(),
99 inputToken,
100 outputToken,
101 previousLimitX128.ToString(),
102 limitX128.ToString(),
103 updatedAt,
104 )
105}
106
107// getTokenMinAmountPerExecution returns the minimum input per execution set
108// for tokenPath, or 0 if none is set.
109func getTokenMinAmountPerExecution(s pdca.IDCAOrderStore, tokenPath string) int64 {
110 value := s.GetTokenMinAmountsPerExecution().Get(tokenPath)
111 if value == nil {
112 return 0
113 }
114
115 return value.(int64)
116}
117
118// minAmountPerExecution returns the minimum input per execution of a new order
119// selling tokenPath: the larger of the global and the token minimum.
120func (d *dcaOrderImpl) minAmountPerExecution(tokenPath string) int64 {
121 minAmount := d.store.GetConfig().MinAmountPerExecution()
122
123 if tokenMinAmount := getTokenMinAmountPerExecution(d.store, tokenPath); tokenMinAmount > minAmount {
124 return tokenMinAmount
125 }
126
127 return minAmount
128}