Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

config.gno

7.24 Kb · 223 lines
  1package dca
  2
  3// BasisPointDenominator is 100% in basis points, the denominator of every
  4// rate in basis points: executionFeeRate, minExecutionGapRate, and
  5// maxPoolPriceDeviationRate.
  6const BasisPointDenominator int64 = 10_000
  7
  8// MaxExecutionFeeRate is the largest execution fee rate: 1000 basis points
  9// (10%).
 10const MaxExecutionFeeRate int64 = 1_000
 11
 12// Config holds governance-controlled DCA parameters.
 13type Config struct {
 14	minAmountPerExecution int64 // minimum input per execution
 15	maxExecutionCount     int64 // maximum executions per order
 16
 17	// allowedIntervals lists the intervals new orders may use, ascending.
 18	// Removing one only blocks new orders; existing groups keep running.
 19	allowedIntervals []int64
 20
 21	// Execution guard.
 22	minExecutionGapRate int64 // min gap between a group's executions, bps of interval
 23	// slotSteps[i] is the spacing of the slot offsets
 24	// allowed for allowedIntervals[i]; it divides the interval.
 25	// A step equal to the interval allows offset 0 only.
 26	slotSteps []int64
 27
 28	maxClaimBatchSize int64 // most orders one ClaimGroupOrders call examines
 29
 30	// maxPoolPriceDeviationRate is how far, in basis points of price, a pool's
 31	// price may sit on the favorable side of its swap price limit and still be
 32	// quoted; 0 disables the check. It bounds the ticks a quote or swap walks
 33	// when a pool's price is far from the oracle-derived limit.
 34	maxPoolPriceDeviationRate int64
 35
 36	// maxPairPriceLimitAge is how long, in seconds, a pair price limit stays
 37	// usable after it was set; a round or quote under an older limit finds
 38	// no eligible pool. 0 is invalid and leaves no pool eligible.
 39	maxPairPriceLimitAge int64
 40
 41	// executionFeeRate is the share of an order's gross output,
 42	// in basis points, sent to the protocol fee realm.
 43	// Each new order snapshots it; changing it leaves existing orders as is.
 44	executionFeeRate int64
 45}
 46
 47// NewConfig returns a config that allows allowedIntervals, which must be
 48// ascending. The slice is copied. Fees and the execution gap start disabled.
 49// Each interval starts with a single slot, offset 0; see SetSlotStep.
 50func NewConfig(minAmountPerExecution, maxExecutionCount int64, allowedIntervals []int64) *Config {
 51	return &Config{
 52		minAmountPerExecution: minAmountPerExecution,
 53		maxExecutionCount:     maxExecutionCount,
 54		allowedIntervals:      cloneInt64s(allowedIntervals),
 55		minExecutionGapRate:   0,
 56		slotSteps:             cloneInt64s(allowedIntervals),
 57		maxClaimBatchSize:     0,
 58		executionFeeRate:      0,
 59
 60		maxPoolPriceDeviationRate: 0,
 61		maxPairPriceLimitAge:      0,
 62	}
 63}
 64
 65// Clone returns a deep copy of the config.
 66func (c *Config) Clone() *Config {
 67	return &Config{
 68		minAmountPerExecution: c.minAmountPerExecution,
 69		maxExecutionCount:     c.maxExecutionCount,
 70		allowedIntervals:      cloneInt64s(c.allowedIntervals),
 71		minExecutionGapRate:   c.minExecutionGapRate,
 72		slotSteps:             cloneInt64s(c.slotSteps),
 73		maxClaimBatchSize:     c.maxClaimBatchSize,
 74		executionFeeRate:      c.executionFeeRate,
 75
 76		maxPoolPriceDeviationRate: c.maxPoolPriceDeviationRate,
 77		maxPairPriceLimitAge:      c.maxPairPriceLimitAge,
 78	}
 79}
 80
 81func (c *Config) MinAmountPerExecution() int64     { return c.minAmountPerExecution }
 82func (c *Config) SetMinAmountPerExecution(v int64) { c.minAmountPerExecution = v }
 83
 84func (c *Config) MaxExecutionCount() int64     { return c.maxExecutionCount }
 85func (c *Config) SetMaxExecutionCount(v int64) { c.maxExecutionCount = v }
 86
 87func (c *Config) MinExecutionGapRate() int64     { return c.minExecutionGapRate }
 88func (c *Config) SetMinExecutionGapRate(v int64) { c.minExecutionGapRate = v }
 89
 90func (c *Config) MaxClaimBatchSize() int64     { return c.maxClaimBatchSize }
 91func (c *Config) SetMaxClaimBatchSize(v int64) { c.maxClaimBatchSize = v }
 92
 93func (c *Config) MaxPoolPriceDeviationRate() int64     { return c.maxPoolPriceDeviationRate }
 94func (c *Config) SetMaxPoolPriceDeviationRate(v int64) { c.maxPoolPriceDeviationRate = v }
 95
 96func (c *Config) MaxPairPriceLimitAge() int64     { return c.maxPairPriceLimitAge }
 97func (c *Config) SetMaxPairPriceLimitAge(v int64) { c.maxPairPriceLimitAge = v }
 98
 99func (c *Config) ExecutionFeeRate() int64 { return c.executionFeeRate }
100
101// SetExecutionFeeRate sets the execution fee rate in basis points.
102// It returns ErrInvalidInput outside [0, MaxExecutionFeeRate].
103func (c *Config) SetExecutionFeeRate(v int64) error {
104	if !isValidExecutionFeeRate(v) {
105		return ErrInvalidInput
106	}
107
108	c.executionFeeRate = v
109
110	return nil
111}
112
113// AllowedIntervals returns a copy of the allowed intervals, ascending.
114func (c *Config) AllowedIntervals() []int64 {
115	return cloneInt64s(c.allowedIntervals)
116}
117
118// IsAllowedInterval reports whether new orders may use interval.
119func (c *Config) IsAllowedInterval(interval int64) bool {
120	return c.intervalIndex(interval) >= 0
121}
122
123// AddAllowedInterval allows interval, keeping the list ascending.
124// The interval starts with a single slot, offset 0; see SetSlotStep.
125func (c *Config) AddAllowedInterval(interval int64) error {
126	if c.IsAllowedInterval(interval) {
127		return ErrIntervalExists
128	}
129
130	i := 0
131	for i < len(c.allowedIntervals) && c.allowedIntervals[i] < interval {
132		i++
133	}
134
135	c.allowedIntervals = insertInt64(c.allowedIntervals, i, interval)
136	c.slotSteps = insertInt64(c.slotSteps, i, interval)
137
138	return nil
139}
140
141// RemoveAllowedInterval disallows interval for new orders,
142// dropping its slot step with it.
143func (c *Config) RemoveAllowedInterval(interval int64) error {
144	i := c.intervalIndex(interval)
145	if i < 0 {
146		return ErrIntervalNotFound
147	}
148
149	c.allowedIntervals = removeInt64(c.allowedIntervals, i)
150	c.slotSteps = removeInt64(c.slotSteps, i)
151
152	return nil
153}
154
155// SlotStep returns the spacing of the slot offsets allowed for interval,
156// or 0 if the interval is not allowed.
157func (c *Config) SlotStep(interval int64) int64 {
158	i := c.intervalIndex(interval)
159	if i < 0 {
160		return 0
161	}
162
163	return c.slotSteps[i]
164}
165
166// SetSlotStep sets the spacing of the slot offsets allowed for interval.
167// It returns ErrInvalidInput if the interval is not allowed,
168// or if step is not a positive divisor of the interval.
169func (c *Config) SetSlotStep(interval int64, step int64) error {
170	i := c.intervalIndex(interval)
171	if i < 0 || step <= 0 || interval%step != 0 {
172		return ErrInvalidInput
173	}
174
175	c.slotSteps[i] = step
176
177	return nil
178}
179
180// IsAllowedSlot reports whether new orders may use slotOffset at interval:
181// a multiple of the interval's slot step in [0, interval).
182func (c *Config) IsAllowedSlot(interval int64, slotOffset int64) bool {
183	step := c.SlotStep(interval)
184	if step <= 0 {
185		return false
186	}
187
188	return slotOffset >= 0 && slotOffset < interval && slotOffset%step == 0
189}
190
191func (c *Config) intervalIndex(interval int64) int {
192	for i, v := range c.allowedIntervals {
193		if v == interval {
194			return i
195		}
196	}
197
198	return -1
199}
200
201// insertInt64 returns a copy of values with v inserted at index i.
202func insertInt64(values []int64, i int, v int64) []int64 {
203	out := make([]int64, 0, len(values)+1)
204	out = append(out, values[:i]...)
205	out = append(out, v)
206
207	return append(out, values[i:]...)
208}
209
210// removeInt64 returns a copy of values without the element at index i.
211func removeInt64(values []int64, i int) []int64 {
212	out := make([]int64, 0, len(values)-1)
213	out = append(out, values[:i]...)
214
215	return append(out, values[i+1:]...)
216}
217
218func cloneInt64s(values []int64) []int64 {
219	out := make([]int64, len(values))
220	copy(out, values)
221
222	return out
223}