group.gno
7.36 Kb · 196 lines
1package dca
2
3import (
4 u256 "gno.land/p/gnoswap/uint256/v1"
5 rotree "gno.land/p/nt/bptree/rotree/v0"
6 bptree "gno.land/p/nt/bptree/v0"
7)
8
9// roundMarkBPTreeFanout is the fanout of a group's round mark B+tree.
10const roundMarkBPTreeFanout = 16
11
12// Group aggregates the orders selling inputToken for outputToken
13// at the same interval and slot offset.
14// Its rounds run at the unix times t with t % interval == slotOffset.
15// A round sells the sum of their amountPerExecution (orderAmount)
16// in one pool swap, on the pool chosen at execution time,
17// and adds the gross output per unit of input to
18// accumulatedOutputPerAmountX128; each order's execution fee is deducted
19// when the order is paid out.
20type Group struct {
21 id string
22 inputToken string
23 outputToken string
24 interval int64
25 slotOffset int64 // seconds after each interval boundary of the unix epoch
26
27 round uint64 // number of executed rounds
28 orderAmount int64 // input sold in the next round, sum of active amountPerExecution
29 accumulatedOutputPerAmountX128 *u256.Uint // cumulative output per unit of input, Q128
30 nextExecutionTimestamp int64 // earliest unix time of the next execution
31
32 activeOrderCount int64 // number of unfinalized orders
33
34 roundMarks *bptree.BPTree // EncodeUint64(round) -> *RoundMark
35}
36
37// NewGroup creates an empty group whose first round is due at nextExecutionTimestamp.
38func NewGroup(id string, inputToken string, outputToken string, interval int64, slotOffset int64, nextExecutionTimestamp int64) *Group {
39 return &Group{
40 id: id,
41 inputToken: inputToken,
42 outputToken: outputToken,
43 interval: interval,
44 slotOffset: slotOffset,
45 round: 0,
46 orderAmount: 0,
47 accumulatedOutputPerAmountX128: u256.Zero(),
48 nextExecutionTimestamp: nextExecutionTimestamp,
49 activeOrderCount: 0,
50 roundMarks: bptree.NewBPTreeN(roundMarkBPTreeFanout),
51 }
52}
53
54// Clone returns a deep copy of the group, including its round marks,
55// so callers can read it without aliasing stored state.
56// Its cost grows with the number of round marks; getters use
57// CloneWithoutRoundMarks instead.
58func (g *Group) Clone() *Group {
59 clone := g.CloneWithoutRoundMarks()
60 g.roundMarks.Iterate("", "", func(key string, value any) bool {
61 clone.roundMarks.Set(key, value.(*RoundMark).Clone())
62 return false
63 })
64
65 return clone
66}
67
68// CloneWithoutRoundMarks returns a copy of the group's fields with an empty
69// round mark tree, at a cost independent of the number of round marks.
70// Like Clone, it shares no state with g; read the marks through RoundMarks.
71func (g *Group) CloneWithoutRoundMarks() *Group {
72 return &Group{
73 id: g.id,
74 inputToken: g.inputToken,
75 outputToken: g.outputToken,
76 interval: g.interval,
77 slotOffset: g.slotOffset,
78 round: g.round,
79 orderAmount: g.orderAmount,
80 accumulatedOutputPerAmountX128: g.accumulatedOutputPerAmountX128.Clone(),
81 nextExecutionTimestamp: g.nextExecutionTimestamp,
82 activeOrderCount: g.activeOrderCount,
83 roundMarks: bptree.NewBPTreeN(roundMarkBPTreeFanout),
84 }
85}
86
87func (g *Group) ID() string { return g.id }
88func (g *Group) InputToken() string { return g.inputToken }
89func (g *Group) OutputToken() string { return g.outputToken }
90func (g *Group) Interval() int64 { return g.interval }
91func (g *Group) SlotOffset() int64 { return g.slotOffset }
92
93func (g *Group) Round() uint64 { return g.round }
94func (g *Group) SetRound(v uint64) { g.round = v }
95
96func (g *Group) OrderAmount() int64 { return g.orderAmount }
97func (g *Group) SetOrderAmount(v int64) { g.orderAmount = v }
98
99func (g *Group) AccumulatedOutputPerAmountX128() *u256.Uint {
100 return g.accumulatedOutputPerAmountX128.Clone()
101}
102func (g *Group) SetAccumulatedOutputPerAmountX128(v *u256.Uint) {
103 g.accumulatedOutputPerAmountX128.Set(v)
104}
105
106func (g *Group) NextExecutionTimestamp() int64 { return g.nextExecutionTimestamp }
107func (g *Group) SetNextExecutionTimestamp(v int64) { g.nextExecutionTimestamp = v }
108
109func (g *Group) ActiveOrderCount() int64 { return g.activeOrderCount }
110func (g *Group) SetActiveOrderCount(v int64) { g.activeOrderCount = v }
111
112func (g *Group) RoundMarkCount() int { return g.roundMarks.Size() }
113
114// GetRoundMark returns the mark of a round, or nil when no order ends there.
115func (g *Group) GetRoundMark(round uint64) *RoundMark {
116 value := g.roundMarks.Get(RoundKey(round))
117 if value == nil {
118 return nil
119 }
120
121 return value.(*RoundMark)
122}
123
124// RoundMarks returns a read-only view of the round marks keyed by RoundKey.
125// Each mark read from the view is a copy, so the view cannot reach stored state.
126func (g *Group) RoundMarks() *rotree.ReadOnlyTree {
127 return rotree.Wrap(g.roundMarks, cloneRoundMarkEntry)
128}
129
130// GetOrCreateRoundMark returns the mark of a round, creating it if missing.
131func (g *Group) GetOrCreateRoundMark(round uint64) *RoundMark {
132 mark := g.GetRoundMark(round)
133 if mark != nil {
134 return mark
135 }
136
137 // Allocate the snapshot slot now, while creating the first order
138 // ending here, so that order's creator pays its storage deposit
139 // instead of the keeper who later executes the round.
140 mark = &RoundMark{orderAmount: 0, orderCount: 0, accumulatedOutputPerAmountX128: u256.Zero()}
141 g.roundMarks.Set(RoundKey(round), mark)
142
143 return mark
144}
145
146func (g *Group) RemoveRoundMark(round uint64) {
147 g.roundMarks.Remove(RoundKey(round))
148}
149
150// RoundMark tracks the orders whose last round is the mark's round.
151//
152// When the round executes, orderAmount leaves the group orderAmount,
153// and accumulatedOutputPerAmountX128 snapshots the group accumulator,
154// so finished orders are paid without per-round history.
155// The mark is removed once orderCount, the number of unfinalized orders
156// ending at the round, reaches zero.
157type RoundMark struct {
158 orderAmount int64 // sum of amountPerExecution of the orders ending at the round
159 orderCount int64 // number of unfinalized orders ending at the round
160 accumulatedOutputPerAmountX128 *u256.Uint // valid once the group round reaches the mark
161}
162
163// Clone returns a deep copy of the mark.
164func (m *RoundMark) Clone() *RoundMark {
165 return &RoundMark{
166 orderAmount: m.orderAmount,
167 orderCount: m.orderCount,
168 accumulatedOutputPerAmountX128: m.accumulatedOutputPerAmountX128.Clone(),
169 }
170}
171
172func cloneRoundMarkEntry(value any) any {
173 mark, ok := value.(*RoundMark)
174 if !ok {
175 return nil
176 }
177
178 return mark.Clone()
179}
180
181func (m *RoundMark) OrderAmount() int64 { return m.orderAmount }
182func (m *RoundMark) SetOrderAmount(v int64) { m.orderAmount = v }
183
184func (m *RoundMark) OrderCount() int64 { return m.orderCount }
185func (m *RoundMark) SetOrderCount(v int64) { m.orderCount = v }
186
187func (m *RoundMark) AccumulatedOutputPerAmountX128() *u256.Uint {
188 return m.accumulatedOutputPerAmountX128.Clone()
189}
190
191// SnapshotAccumulatedOutputPerAmountX128 records the group accumulator
192// right after the round executes.
193// It writes into the existing slot, so no new object is stored.
194func (m *RoundMark) SnapshotAccumulatedOutputPerAmountX128(v *u256.Uint) {
195 m.accumulatedOutputPerAmountX128.Set(v)
196}