@@ -2,6 +2,7 @@ package views
2
2
3
3
import (
4
4
"encoding/hex"
5
+ "math/big"
5
6
"sort"
6
7
7
8
"github.com/CrocSwap/graphcache-go/model"
@@ -15,6 +16,19 @@ type UserPosition struct {
15
16
PositionId string `json:"positionId"`
16
17
}
17
18
19
+ type HistoricUserPosition struct {
20
+ types.PositionLocation
21
+ PositionType string `json:"positionType"`
22
+ TimeFirstMint int `json:"timeFirstMint"`
23
+ FirstMintTx string `json:"firstMintTx"`
24
+ PositionId string `json:"positionId"`
25
+ Liq * big.Int `json:"liq"`
26
+ BaseTokens * big.Int `json:"baseTokens"`
27
+ QuoteTokens * big.Int `json:"quoteTokens"`
28
+ PoolPrice float64 `json:"poolPrice"`
29
+ LiqHist model.LiquidityDeltaHist `json:"liqHist"`
30
+ }
31
+
18
32
func (v * Views ) QueryUserPositions (chainId types.ChainId , user types.EthAddress ) []UserPosition {
19
33
positions := v .Cache .RetrieveUserPositions (chainId , user )
20
34
@@ -122,6 +136,68 @@ func (v *Views) QuerySinglePosition(chainId types.ChainId, user types.EthAddress
122
136
return nil
123
137
}
124
138
139
+ func (v * Views ) QueryHistoricPositions (chainId types.ChainId , base types.EthAddress , quote types.EthAddress ,
140
+ poolIdx int , time int , user types.EthAddress , omitEmpty bool ) []HistoricUserPosition {
141
+ livePositions := make ([]HistoricUserPosition , 0 )
142
+
143
+ pools := v .Cache .RetrievePoolSet ()
144
+ for _ , loc := range pools {
145
+ if (chainId != "" && loc .ChainId != chainId ) || (base != "" && loc .Base != base ) || (quote != "" && loc .Quote != quote ) || (poolIdx != 0 && loc .PoolIdx != poolIdx ) {
146
+ continue
147
+ }
148
+ positions := v .Cache .RetrievePoolPositions (loc )
149
+
150
+ poolHistPos := make ([]HistoricUserPosition , 0 )
151
+
152
+ for key , val := range positions {
153
+ if user != "" && user != key .User {
154
+ continue
155
+ }
156
+ histPos := HistoricUserPosition {
157
+ PositionLocation : key ,
158
+ PositionId : formPositionId (key ),
159
+ TimeFirstMint : val .TimeFirstMint ,
160
+ FirstMintTx : val .FirstMintTx ,
161
+ PositionType : val .PositionType ,
162
+ LiqHist : val .LiqHist ,
163
+ }
164
+ poolHistPos = append (poolHistPos , histPos )
165
+ }
166
+
167
+ lastTrade := v .Cache .RetrievePoolAccumBefore (loc , time )
168
+ poolPrice := lastTrade .LastPriceIndic
169
+ for _ , position := range poolHistPos {
170
+ var liqSum float64
171
+ for _ , liqChange := range position .LiqHist .Hist {
172
+ if liqChange .Time <= time {
173
+ liqSum += liqChange .LiqChange
174
+ }
175
+ }
176
+ if liqSum <= 0 {
177
+ liqSum = 0
178
+ if omitEmpty {
179
+ continue
180
+ }
181
+ }
182
+ if position .TimeFirstMint > time {
183
+ continue
184
+ }
185
+ if position .PositionType == "concentrated" {
186
+ liqSumBig , _ := big .NewFloat (liqSum ).Int (nil )
187
+ position .Liq = liqSumBig
188
+ position .BaseTokens , position .QuoteTokens = model .DeriveTokensFromConcLiquidity (liqSum , position .BidTick , position .AskTick , poolPrice )
189
+ } else if position .PositionType == "ambient" {
190
+ liqSumBig , _ := big .NewFloat (liqSum ).Int (nil )
191
+ position .Liq = liqSumBig
192
+ position .BaseTokens , position .QuoteTokens = model .DeriveTokensFromAmbLiquidity (liqSum , poolPrice )
193
+ }
194
+ position .PoolPrice = poolPrice
195
+ livePositions = append (livePositions , position )
196
+ }
197
+ }
198
+ return livePositions
199
+ }
200
+
125
201
func formPositionId (loc types.PositionLocation ) string {
126
202
hash := loc .Hash ()
127
203
return "pos_" + hex .EncodeToString (hash [:])
0 commit comments