/
positions.go
149 lines (115 loc) · 3.67 KB
/
positions.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
package views
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"sort"
"github.com/cnf/structhash"
"github.com/CrocSwap/graphcache-go/model"
"github.com/CrocSwap/graphcache-go/types"
)
type UserPosition struct {
types.PositionLocation
model.PositionTracker
model.APRCalcResult
PositionId string `json:"positionId"`
}
func (v *Views) QueryUserPositions(chainId types.ChainId, user types.EthAddress) []UserPosition {
positions := v.Cache.RetrieveUserPositions(chainId, user)
results := make([]UserPosition, 0)
for key, val := range positions {
element := UserPosition{key, *val, val.CalcAPR(key), formPositionId(key)}
results = append(results, element)
}
sort.Sort(byTime(results))
return results
}
func (v *Views) QueryPoolPositions(chainId types.ChainId,
base types.EthAddress, quote types.EthAddress, poolIdx int, nResults int,
omitEmpty bool) []UserPosition {
loc := types.PoolLocation{
ChainId: chainId,
PoolIdx: poolIdx,
Base: base,
Quote: quote,
}
positions := v.Cache.RetrievePoolPositions(loc)
results := make([]UserPosition, 0)
for key, val := range positions {
element := UserPosition{key, *val, val.CalcAPR(key), formPositionId(key)}
if !omitEmpty || !val.PositionLiquidity.IsEmpty() {
results = append(results, element)
}
}
sort.Sort(byTime(results))
if len(results) < nResults {
return results
} else {
return results[0:nResults]
}
}
func (v *Views) QueryPoolApyLeaders(chainId types.ChainId,
base types.EthAddress, quote types.EthAddress, poolIdx int, nResults int,
omitEmpty bool) []UserPosition {
results := v.QueryPoolPositions(chainId, base, quote, poolIdx, 1000000, true)
sort.Sort(byApr(results))
if len(results) < nResults {
return results
} else {
return results[0:nResults]
}
}
func (v *Views) QueryUserPoolPositions(chainId types.ChainId, user types.EthAddress,
base types.EthAddress, quote types.EthAddress, poolIdx int) []UserPosition {
loc := types.PoolLocation{
ChainId: chainId,
PoolIdx: poolIdx,
Base: base,
Quote: quote,
}
positions := v.Cache.RetrieveUserPoolPositions(user, loc)
results := make([]UserPosition, 0)
for key, val := range positions {
element := UserPosition{key, *val, val.CalcAPR(key), formPositionId(key)}
results = append(results, element)
}
sort.Sort(byTime(results))
return results
}
func (v *Views) QuerySinglePosition(chainId types.ChainId, user types.EthAddress,
base types.EthAddress, quote types.EthAddress, poolIdx int, bidTick int, askTick int) *UserPosition {
entries := v.QueryUserPoolPositions(chainId, user, base, quote, poolIdx)
for _, pos := range entries {
if pos.BidTick == bidTick && pos.AskTick == askTick {
return &pos
}
}
return nil
}
func formPositionId(loc types.PositionLocation) string {
hash := sha256.Sum256(structhash.Dump(loc, 1))
return fmt.Sprintf("pos_%s", hex.EncodeToString(hash[:]))
}
type byTime []UserPosition
func (a byTime) Len() int { return len(a) }
func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byTime) Less(i, j int) bool {
// Break ties by unique hash
if a[i].LatestUpdateTime == a[j].LatestUpdateTime {
return a[i].FirstMintTx > a[j].FirstMintTx
}
return a[i].LatestUpdateTime > a[j].LatestUpdateTime
}
type byApr []UserPosition
func (a byApr) Len() int { return len(a) }
func (a byApr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byApr) Less(i, j int) bool {
// Break ties by time, the hash
if a[i].APRCalcResult.Apr != a[j].APRCalcResult.Apr {
return a[i].APRCalcResult.Apr > a[j].APRCalcResult.Apr
} else if a[i].LatestUpdateTime != a[j].LatestUpdateTime {
return a[i].LatestUpdateTime > a[j].LatestUpdateTime
} else {
return a[i].FirstMintTx > a[j].FirstMintTx
}
}