-
Notifications
You must be signed in to change notification settings - Fork 44
/
query_earn_mining_pool_all.go
193 lines (161 loc) · 5.16 KB
/
query_earn_mining_pool_all.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 wasm
import (
"encoding/json"
"fmt"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
)
// Check pool type
func (oq *Querier) checkFilterType(ctx sdk.Context, ammPool *types.Pool, filterType types.FilterType) bool {
switch filterType {
case types.FilterType_FilterAll:
return true
case types.FilterType_FilterPerpetual:
_, found := oq.perpetualKeeper.GetPool(ctx, ammPool.PoolId)
return found
case types.FilterType_FilterFixedWeight:
return ammPool.PoolParams.UseOracle
case types.FilterType_FilterDynamicWeight:
return !ammPool.PoolParams.UseOracle
case types.FilterType_FilterLeverage:
_, found := oq.leveragelpKeeper.GetPool(ctx, ammPool.PoolId)
return found
}
return false
}
// Calculate the amm pool ratio
func CalculatePoolRatio(ctx sdk.Context, pool *types.Pool) string {
weightString := ""
totalWeight := sdk.ZeroInt()
for _, asset := range pool.PoolAssets {
totalWeight = totalWeight.Add(asset.Weight)
}
if totalWeight.IsZero() {
return weightString
}
for _, asset := range pool.PoolAssets {
weight := sdk.NewDecFromInt(asset.Weight).QuoInt(totalWeight).MulInt(sdk.NewInt(100)).TruncateInt64()
weightString = fmt.Sprintf("%s : %d", weightString, weight)
}
// remove prefix " : " 3 characters
if len(weightString) > 1 {
weightString = weightString[3:]
}
// returns pool weight string
return weightString
}
// Generate earn pool struct
func (oq *Querier) generateEarnPool(ctx sdk.Context, ammPool *types.Pool, filterType types.FilterType) types.EarnPool {
rewardsApr := sdk.ZeroDec()
borrowApr := sdk.ZeroDec()
leverageLpPercent := sdk.ZeroDec()
perpetualPercent := sdk.ZeroDec()
poolInfo, found := oq.masterchefKeeper.GetPool(ctx, ammPool.PoolId)
if found {
rewardsApr = poolInfo.DexApr.Add(poolInfo.EdenApr)
}
if filterType == types.FilterType_FilterLeverage {
prams := oq.stablestakeKeeper.GetParams(ctx)
borrowApr = prams.InterestRate
}
tvl, _ := ammPool.TVL(ctx, oq.oraclekeeper)
lpTokenPrice, _ := ammPool.LpTokenPrice(ctx, oq.oraclekeeper)
// Get rewards amount
rewardsUsd, rewardCoins := oq.incentiveKeeper.GetDailyRewardsAmountForPool(ctx, ammPool.PoolId)
// Get pool ratio
poolRatio := CalculatePoolRatio(ctx, ammPool)
leverageLpPool, found := oq.leveragelpKeeper.GetPool(ctx, ammPool.PoolId)
if found {
leverageLpPercent = leverageLpPool.Health
}
perpetualPool, found := oq.perpetualKeeper.GetPool(ctx, ammPool.PoolId)
if found {
perpetualPercent = perpetualPool.Health
}
return types.EarnPool{
Assets: ammPool.PoolAssets,
PoolRatio: poolRatio,
RewardsApr: rewardsApr,
BorrowApr: borrowApr,
LeverageLp: leverageLpPercent,
Perpetual: perpetualPercent,
Tvl: tvl,
LpTokenPrice: lpTokenPrice,
RewardsUsd: rewardsUsd,
RewardCoins: rewardCoins,
PoolId: ammPool.PoolId,
TotalShares: ammPool.TotalShares,
SwapFee: ammPool.PoolParams.SwapFee,
FeeDenom: ammPool.PoolParams.FeeDenom,
UseOracle: ammPool.PoolParams.UseOracle,
}
}
// Reverse pools
func (oq *Querier) reversePools(earnPools []types.EarnPool) []types.EarnPool {
for i, j := 0, len(earnPools)-1; i < j; i, j = i+1, j-1 {
earnPools[i], earnPools[j] = earnPools[j], earnPools[i]
}
return earnPools
}
func (oq *Querier) queryEarnMiningPoolAll(ctx sdk.Context, poolRequest *types.QueryEarnPoolRequest) ([]byte, error) {
pools := make([]types.EarnPool, 0)
skipCount := uint64(0)
// If we have pool Ids specified,
if len(poolRequest.PoolIds) > 0 {
for _, pId := range poolRequest.PoolIds {
pool, found := oq.keeper.GetPool(ctx, pId)
if !found {
continue
}
// apply filter type
if !oq.checkFilterType(ctx, &pool, poolRequest.FilterType) {
continue
}
// check offset if pagination available
if poolRequest.Pagination != nil && skipCount < poolRequest.Pagination.Offset {
skipCount++
continue
}
// check maximum limit if pagination available
if poolRequest.Pagination != nil && len(pools) >= int(poolRequest.Pagination.Limit) {
break
}
// Construct earn pool
earnPool := oq.generateEarnPool(ctx, &pool, poolRequest.FilterType)
pools = append(pools, earnPool)
}
} else {
oq.keeper.IterateLiquidityPools(ctx, func(p types.Pool) bool {
// apply filter type
if !oq.checkFilterType(ctx, &p, poolRequest.FilterType) {
return false
}
// check offset if pagination available
if poolRequest.Pagination != nil && skipCount < poolRequest.Pagination.Offset {
skipCount++
return false
}
// check maximum limit if pagination available
if poolRequest.Pagination != nil && len(pools) >= int(poolRequest.Pagination.Limit) {
return true
}
// Construct earn pool
earnPool := oq.generateEarnPool(ctx, &p, poolRequest.FilterType)
pools = append(pools, earnPool)
return false
})
}
// If reverse is set true.
if poolRequest.Pagination != nil && poolRequest.Pagination.Reverse {
pools = oq.reversePools(pools)
}
res := types.QueryEarnPoolResponse{
Pools: pools,
}
responseBytes, err := json.Marshal(res)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to serialize pool all response")
}
return responseBytes, nil
}