/
controller.go
193 lines (164 loc) · 5.3 KB
/
controller.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package controller
import (
"log"
"time"
"github.com/CrocSwap/graphcache-go/cache"
"github.com/CrocSwap/graphcache-go/loader"
"github.com/CrocSwap/graphcache-go/model"
"github.com/CrocSwap/graphcache-go/tables"
"github.com/CrocSwap/graphcache-go/types"
)
type Controller struct {
netCfg loader.NetworkConfig
cache *cache.MemoryCache
history *model.HistoryWriter
workers *workers
}
func New(netCfg loader.NetworkConfig, cache *cache.MemoryCache) *Controller {
history := model.NewHistoryWriter(netCfg, cache.AddPoolEvent)
ctrl := &Controller{
netCfg: netCfg,
cache: cache,
workers: initWorkers(netCfg),
history: history,
}
go ctrl.runPeriodicRefresh()
return ctrl
}
type ControllerOverNetwork struct {
chainId types.ChainId
chainCfg loader.ChainConfig
ctrl *Controller
}
func (c *Controller) OnNetwork(network types.NetworkName) *ControllerOverNetwork {
chainId, okay := c.netCfg.ChainIDForNetwork(network)
chainCfg, okay2 := c.netCfg.ChainConfig(chainId)
if !okay || !okay2 {
log.Fatal("No network config for " + network)
}
return &ControllerOverNetwork{
ctrl: c,
chainId: chainId,
chainCfg: chainCfg,
}
}
func (c *ControllerOverNetwork) IngestBalance(b tables.Balance) {
token := types.RequireEthAddr(b.Token)
user := types.RequireEthAddr(b.User)
c.ctrl.cache.AddUserBalance(c.chainId, user, token)
}
func (c *ControllerOverNetwork) IngestLiqChange(l tables.LiqChange) {
c.applyToPosition(l)
c.applyToLiqCurve(l)
c.ctrl.history.CommitLiqChange(l)
}
func (c *ControllerOverNetwork) applyToPosition(l tables.LiqChange) {
liq := formLiqLoc(l)
pool := types.PoolLocation{
ChainId: c.chainId,
PoolIdx: l.PoolIdx,
Base: types.RequireEthAddr(l.Base),
Quote: types.RequireEthAddr(l.Quote),
}
loc := types.PositionLocation{
PoolLocation: pool,
LiquidityLocation: liq,
User: types.RequireEthAddr(l.User),
}
if l.PositionType == "knockout" {
c.applyToKnockout(l, loc)
} else {
c.applyToPassiveLiq(l, loc)
}
}
func (c *ControllerOverNetwork) applyToLiqCurve(l tables.LiqChange) {
pool := types.PoolLocation{
ChainId: c.chainId,
PoolIdx: l.PoolIdx,
Base: types.RequireEthAddr(l.Base),
Quote: types.RequireEthAddr(l.Quote),
}
curve := c.ctrl.cache.MaterializePoolLiqCurve(pool)
curve.UpdateLiqChange(l)
}
func (c *ControllerOverNetwork) applyToKnockout(l tables.LiqChange, loc types.PositionLocation) {
if l.ChangeType == "cross" {
return // Cross events are handled KnockoutCross table
}
pos := c.ctrl.cache.MaterializeKnockoutPos(loc)
c.ctrl.workers.omniUpdates <- &koPosUpdateMsg{liq: l, pos: pos, loc: loc}
}
func (c *ControllerOverNetwork) applyToPassiveLiq(l tables.LiqChange, loc types.PositionLocation) {
pos := c.ctrl.cache.MaterializePosition(loc)
c.ctrl.workers.omniUpdates <- &posUpdateMsg{liq: l, pos: pos, loc: loc}
}
func (c *ControllerOverNetwork) IngestSwap(l tables.Swap) {
c.ctrl.history.CommitSwap(l)
updates := c.resyncPoolOnSwap(l)
// Use array entry, instead of element loop, because otherwise same pointer
// is passed multiple times to channel and may overwritten
for i := range updates {
c.ctrl.workers.omniUpdates <- &updates[i]
}
}
func (c *ControllerOverNetwork) IngestFee(l tables.FeeChange) {
}
func (c *ControllerOverNetwork) IngestAggEvent(r tables.AggEvent) {
pool := types.PoolLocation{
ChainId: c.chainId,
PoolIdx: r.PoolIdx,
Base: types.RequireEthAddr(r.Base),
Quote: types.RequireEthAddr(r.Quote),
}
hist := c.ctrl.cache.MaterializePoolTradingHist(pool)
hist.NextEvent(r)
}
func (c *ControllerOverNetwork) IngestKnockout(r tables.KnockoutCross) {
liq := types.KnockoutTickLocation(r.Tick, r.IsBid > 0, c.chainCfg.KnockoutTickWidth)
pool := types.PoolLocation{
ChainId: c.chainId,
PoolIdx: r.PoolIdx,
Base: types.RequireEthAddr(r.Base),
Quote: types.RequireEthAddr(r.Quote),
}
loc := types.BookLocation{
PoolLocation: pool,
LiquidityLocation: liq,
}
pos := c.ctrl.cache.MaterializeKnockoutBook(loc)
c.ctrl.workers.omniUpdates <- &koCrossUpdateMsg{loc: loc, pos: pos, cross: r}
}
/* Called to indicate that all tables have completed the most recent sync cycle up
* to the checkpointed time. */
func (c *ControllerOverNetwork) FlushSyncCycle(time int) {
}
/* Currently this uses a preset value from the network config. Long-term we should be querying
* the knockout position from CrocQuery::queryKnockoutPivot() to confirm tick widthm, as it
* can change over time, and different tick widths could even co-exist in the same pool. */
func (c *ControllerOverNetwork) knockoutTickWidth() int {
return c.chainCfg.KnockoutTickWidth
}
func formLiqLoc(l tables.LiqChange) types.LiquidityLocation {
if l.PositionType == "ambient" {
return types.AmbientLiquidityLocation()
} else if l.PositionType == "knockout" {
return types.KnockoutRangeLocation(l.BidTick, l.AskTick, l.IsBid > 0)
} else {
return types.RangeLiquidityLocation(l.BidTick, l.AskTick)
}
}
func (c *Controller) resyncFullCycle(time int) {
for poolLoc, poolPos := range c.cache.RetrieveAllPositions() {
if !poolPos.IsEmpty() {
c.workers.omniUpdates <- &posImpactMsg{poolLoc, poolPos, time}
}
}
}
const REFRESH_CYCLE_TIME = 10 * 60
func (c *Controller) runPeriodicRefresh() {
for true {
time.Sleep(time.Second * REFRESH_CYCLE_TIME)
refreshTime := time.Now().Unix()
c.resyncFullCycle(int(refreshTime))
}
}