-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspot_prices.go
More file actions
193 lines (178 loc) · 7.28 KB
/
spot_prices.go
File metadata and controls
193 lines (178 loc) · 7.28 KB
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 client
import (
"fmt"
"math/big"
sdkmath "cosmossdk.io/math"
"github.com/ethereum/go-ethereum/common"
balancerpool "github.com/ojo-network/ethereum-api/abi/balancer/pool"
"github.com/ojo-network/ethereum-api/abi/camelot"
"github.com/ojo-network/ethereum-api/abi/curve"
"github.com/ojo-network/ethereum-api/abi/pancake"
"github.com/ojo-network/ethereum-api/abi/uniswap"
"github.com/ojo-network/ethereum-api/pool"
"github.com/ojo-network/indexer/indexer"
"github.com/ojo-network/indexer/utils"
)
// PollSpotPrices polls for the spot price of a pool
func (c *Client) PollSpotPrices(pools []pool.Pool) {
go func() {
for {
select {
case <-c.ctx.Done():
return
case blockNum := <-c.newBlock:
for _, p := range pools {
var spotPrice indexer.SpotPrice
switch c.poolContract {
case pool.PoolUniswap:
spotPrice = c.QueryUniswapSpotPrice(p, blockNum)
c.logger.Info().Interface("Uniswap spotPrice", spotPrice).Msg("spot price received")
c.indexer.AddPrice(spotPrice)
case pool.PoolAlgebra:
spotPrice = c.QueryAlgebraSpotPrice(p, blockNum)
c.logger.Info().Interface("Alegbra spotPrice", spotPrice).Msg("spot price received")
c.indexer.AddPrice(spotPrice)
case pool.PoolBalancer:
spotPrice = c.QueryBalancerSpotPrice(p, blockNum)
c.logger.Info().Interface("Balancer spotPrice", spotPrice).Msg("spot price received")
c.indexer.AddPrice(spotPrice)
case pool.PoolPancake:
spotPrice = c.QueryPancakeSpotPrice(p, blockNum)
c.logger.Info().Interface("Pancake spotPrice", spotPrice).Msg("spot price received")
c.indexer.AddPrice(spotPrice)
case pool.PoolCurve:
if p.PoolType == pool.StableSwapNG {
spotPrice = c.QueryCurveStableSwapNGSpotPrice(p, blockNum)
c.logger.Info().Interface("Curve stableswapng spotPrice", spotPrice).Msg("spot price received")
c.indexer.AddPrice(spotPrice)
} else if p.PoolType == pool.TwocryptoOptimized {
spotPrice = c.QueryCurveTwoCryptoOptimizedSpotPrice(p, blockNum)
c.logger.Info().Interface("Curve twocryptooptimized spotPrice", spotPrice).Msg("spot price received")
c.indexer.AddPrice(spotPrice)
}
}
}
}
}
}()
}
// QueryUniswapSpotPrice queries the spot price of a uniswap pool
func (c *Client) QueryUniswapSpotPrice(p pool.Pool, blockNum uint64) indexer.SpotPrice {
poolCaller, err := uniswap.NewPoolCaller(common.HexToAddress(p.Address), c.ethClient)
if err != nil {
c.reportError(fmt.Errorf("error initializing %s pool caller: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
slot0, err := poolCaller.Slot0(nil)
if err != nil {
c.reportError(fmt.Errorf("error getting %s pool balance: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
return indexer.SpotPrice{
BlockNum: indexer.BlockNum(blockNum),
Timestamp: utils.CurrentUnixTime(),
ExchangePair: p.ExchangePair(),
Price: p.SqrtPriceX96ToDec(slot0.SqrtPriceX96),
}
}
// QueryAlgebraSpotPrice queries the spot price of an alegbra pool
func (c *Client) QueryAlgebraSpotPrice(p pool.Pool, blockNum uint64) indexer.SpotPrice {
poolCaller, err := camelot.NewAlgebraPoolCaller(common.HexToAddress(p.Address), c.ethClient)
if err != nil {
c.reportError(fmt.Errorf("error initializing %s pool caller: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
globalState, err := poolCaller.GlobalState(nil)
if err != nil {
c.reportError(fmt.Errorf("error getting %s pool balance: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
return indexer.SpotPrice{
BlockNum: indexer.BlockNum(blockNum),
Timestamp: utils.CurrentUnixTime(),
ExchangePair: p.ExchangePair(),
Price: p.SqrtPriceX96ToDec(globalState.Price),
}
}
// QueryBalancerSpotPrice queries the spot price of a balancer pool
func (c *Client) QueryBalancerSpotPrice(p pool.Pool, blockNum uint64) indexer.SpotPrice {
poolCaller, err := balancerpool.NewPoolCaller(common.HexToAddress(p.Address), c.ethClient)
if err != nil {
c.reportError(fmt.Errorf("error initializing %s pool caller: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
poolRate, err := poolCaller.GetTokenRate(nil, common.HexToAddress(p.BaseAddress))
if err != nil {
c.reportError(fmt.Errorf("error getting %s token rate from pool: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
return indexer.SpotPrice{
BlockNum: indexer.BlockNum(blockNum),
Timestamp: utils.CurrentUnixTime(),
ExchangePair: p.ExchangePair(),
Price: sdkmath.LegacyNewDecFromBigIntWithPrec(poolRate, 18),
}
}
// QueryPancakeSpotPrice queries the spot price of a pancake pool
func (c *Client) QueryPancakeSpotPrice(p pool.Pool, blockNum uint64) indexer.SpotPrice {
pancakeCaller, err := pancake.NewPancakeCaller(common.HexToAddress(p.Address), c.ethClient)
if err != nil {
c.reportError(fmt.Errorf("error initializing %s pool caller: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
slot0, err := pancakeCaller.Slot0(nil)
if err != nil {
c.reportError(fmt.Errorf("error getting %s pool balance: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
return indexer.SpotPrice{
BlockNum: indexer.BlockNum(blockNum),
Timestamp: utils.CurrentUnixTime(),
ExchangePair: p.ExchangePair(),
Price: p.SqrtPriceX96ToDec(slot0.SqrtPriceX96),
}
}
// QueryCurveStableSwapNGSpotPrice queries the spot price of a curve stableswapng pool
func (c *Client) QueryCurveStableSwapNGSpotPrice(p pool.Pool, blockNum uint64) indexer.SpotPrice {
curveCaller, err := curve.NewStableSwapNGCaller(common.HexToAddress(p.Address), c.ethClient)
if err != nil {
c.reportError(fmt.Errorf("error initializing %s pool caller: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
// price comes inverted
poolPriceInverted, err := curveCaller.LastPrice(nil, big.NewInt(0))
if err != nil {
c.reportError(fmt.Errorf("error getting %s token last price from pool: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(36), nil)
poolPrice := new(big.Int).Quo(scale, poolPriceInverted)
return indexer.SpotPrice{
BlockNum: indexer.BlockNum(blockNum),
Timestamp: utils.CurrentUnixTime(),
ExchangePair: p.ExchangePair(),
Price: sdkmath.LegacyNewDecFromBigIntWithPrec(poolPrice, 18),
}
}
// QueryCurveTwoCryptoOptimizedSpotPrice queries the spot price of a curve twocryptooptimized pool
func (c *Client) QueryCurveTwoCryptoOptimizedSpotPrice(p pool.Pool, blockNum uint64) indexer.SpotPrice {
curveCaller, err := curve.NewTwocryptoOptimizedCaller(common.HexToAddress(p.Address), c.ethClient)
if err != nil {
c.reportError(fmt.Errorf("error initializing %s pool caller: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
// price comes inverted
poolPriceInverted, err := curveCaller.LastPrices(nil)
if err != nil {
c.reportError(fmt.Errorf("error getting %s token last price from pool: %w", p.ExchangePair(), err))
return indexer.SpotPrice{}
}
scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(36), nil)
poolPrice := new(big.Int).Quo(scale, poolPriceInverted)
return indexer.SpotPrice{
BlockNum: indexer.BlockNum(blockNum),
Timestamp: utils.CurrentUnixTime(),
ExchangePair: p.ExchangePair(),
Price: sdkmath.LegacyNewDecFromBigIntWithPrec(poolPrice, 18),
}
}