/
liquidity_iterator.go
95 lines (81 loc) · 2.17 KB
/
liquidity_iterator.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/neutron-org/neutron/v3/x/dex/types"
)
type LiquidityIterator struct {
keeper *Keeper
tradePairID *types.TradePairID
ctx sdk.Context
iter TickIterator
}
func (k Keeper) NewLiquidityIterator(
ctx sdk.Context,
tradePairID *types.TradePairID,
) *LiquidityIterator {
return &LiquidityIterator{
iter: k.NewTickIterator(ctx, tradePairID),
keeper: &k,
ctx: ctx,
tradePairID: tradePairID,
}
}
func (s *LiquidityIterator) Next() types.Liquidity {
// Move iterator to the next tick after each call
// iter must be in valid state to call next
defer func() {
if s.iter.Valid() {
s.iter.Next()
}
}()
for ; s.iter.Valid(); s.iter.Next() {
tick := s.iter.Value()
// Don't bother to look up pool counter-liquidities if there's no liquidity here
if !tick.HasToken() {
continue
}
liq := s.WrapTickLiquidity(tick)
if liq != nil {
return liq
}
}
return nil
}
func (s *LiquidityIterator) Close() {
s.iter.Close()
}
func (s *LiquidityIterator) WrapTickLiquidity(tick types.TickLiquidity) types.Liquidity {
switch liquidity := tick.Liquidity.(type) {
case *types.TickLiquidity_PoolReserves:
poolReserves := liquidity.PoolReserves
counterpartKey := poolReserves.Key.Counterpart()
counterpartReserves, counterpartReservesFound := s.keeper.GetPoolReserves(s.ctx, counterpartKey)
if !counterpartReservesFound {
counterpartReserves = types.NewPoolReservesFromCounterpart(poolReserves)
}
var lowerTick0, upperTick1 *types.PoolReserves
if s.tradePairID.IsTakerDenomToken0() {
lowerTick0 = counterpartReserves
upperTick1 = poolReserves
} else {
lowerTick0 = poolReserves
upperTick1 = counterpartReserves
}
return &types.PoolLiquidity{
TradePairID: s.tradePairID,
Pool: &types.Pool{
LowerTick0: lowerTick0,
UpperTick1: upperTick1,
},
}
case *types.TickLiquidity_LimitOrderTranche:
tranche := liquidity.LimitOrderTranche
// If we hit a tranche with an expired goodTil date keep iterating
if tranche.IsExpired(s.ctx) {
return nil
}
return tranche
default:
panic("Tick does not have liquidity")
}
}