-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap_querier.go
451 lines (398 loc) · 14.9 KB
/
swap_querier.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package keeper
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"github.com/fastock/fastock-chain/x/ammswap"
swaptypes "github.com/fastock/fastock-chain/x/ammswap/types"
"github.com/fastock/fastock-chain/x/common"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fastock/fastock-chain/x/backend/types"
abci "github.com/tendermint/tendermint/abci/types"
)
// querySwapWatchlist returns watchlist of swap
func querySwapWatchlist(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var queryParams types.QuerySwapWatchlistParams
err := keeper.cdc.UnmarshalJSON(req.Data, &queryParams)
if err != nil {
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
}
offset, limit := common.GetPage(queryParams.Page, queryParams.PerPage)
if offset < 0 || limit < 0 {
return nil, sdk.ErrUnknownRequest(fmt.Sprintf("invalid params: page=%d or per_page=%d", queryParams.Page, queryParams.PerPage))
}
//check sort column param
switch queryParams.SortColumn {
case "":
case types.SwapWatchlistLiquidity:
case types.SwapWatchlistVolume24h:
case types.SwapWatchlistFeeApy:
case types.SwapWatchlistLastPrice:
case types.SwapWatchlistChange24h:
default:
return nil, sdk.ErrUnknownRequest(fmt.Sprintf("invalid sort_column: %s", queryParams.SortColumn))
}
// whitelist map
whitelistMap := getSwapWhitelistMap(keeper)
// all swap token pairs
swapTokenPairs := keeper.swapKeeper.GetSwapTokenPairs(ctx)
startTime := ctx.BlockTime().Add(-24 * time.Hour).Unix()
// query last 24 hours swap infos in orm db
swapInfos := keeper.Orm.GetSwapInfo(startTime)
swapVolumePriceMap := make(map[string]types.SwapVolumePriceInfo, len(swapTokenPairs))
for _, swapInfo := range swapInfos {
var err error
price, err := sdk.NewDecFromStr(swapInfo.Price)
if err != nil {
continue
}
// check if in whitelist
if _, found := whitelistMap[swapInfo.TokenPairName]; !found {
continue
}
// calculate volume in dollar
sellAmount, err := sdk.ParseDecCoin(swapInfo.SellAmount)
if err != nil {
continue
}
buyAmount, err := sdk.ParseDecCoin(swapInfo.BuysAmount)
if err != nil {
continue
}
volume := calculateDollarAmount(ctx, keeper, sellAmount, buyAmount)
volumePriceInfo, ok := swapVolumePriceMap[swapInfo.TokenPairName]
// not exist
if !ok {
swapVolumePriceMap[swapInfo.TokenPairName] = types.SwapVolumePriceInfo{
Volume: volume,
Price24h: price,
Timestamp: swapInfo.Timestamp,
}
continue
}
// update swapVolumePriceMap
if volumePriceInfo.Timestamp > swapInfo.Timestamp {
volumePriceInfo.Price24h = price
}
volumePriceInfo.Volume = volumePriceInfo.Volume.Add(volume)
swapVolumePriceMap[swapInfo.TokenPairName] = volumePriceInfo
}
// total watchlist
var totalWatchlist []types.SwapWatchlist
swapParams := keeper.swapKeeper.GetParams(ctx)
for _, swapTokenPair := range swapTokenPairs {
tokenPairName := swapTokenPair.TokenPairName()
// check if in whitelist
if _, found := whitelistMap[tokenPairName]; !found {
continue
}
// calculate liquidity in dollar
liquidity := calculateDollarAmount(ctx, keeper, swapTokenPair.BasePooledCoin, swapTokenPair.QuotePooledCoin)
// calculate last price
lastPrice := sdk.ZeroDec()
if swapTokenPair.QuotePooledCoin.Amount.IsPositive() {
lastPrice = swapTokenPair.BasePooledCoin.Amount.Quo(swapTokenPair.QuotePooledCoin.Amount)
}
// 24h volume and price
volume24h := sdk.ZeroDec()
price24h := sdk.ZeroDec()
if volumePriceInfo, ok := swapVolumePriceMap[tokenPairName]; ok {
volume24h = volumePriceInfo.Volume
price24h = volumePriceInfo.Price24h
}
// calculate fee apy
feeApy := sdk.ZeroDec()
if liquidity.IsPositive() && liquidity.IsPositive() {
feeApy = volume24h.Mul(swapParams.FeeRate).Quo(liquidity).Mul(sdk.NewDec(365))
}
// calculate price change
change24h := sdk.ZeroDec()
if price24h.IsPositive() {
change24h = lastPrice.Sub(price24h).Quo(price24h)
}
totalWatchlist = append(totalWatchlist, types.SwapWatchlist{
SwapPair: tokenPairName,
Liquidity: liquidity,
Volume24h: volume24h,
FeeApy: feeApy,
LastPrice: lastPrice,
Change24h: change24h,
})
}
// sort watchlist
if queryParams.SortColumn != "" && len(totalWatchlist) != 0 {
watchlistSorter := &types.SwapWatchlistSorter{
Watchlist: totalWatchlist,
SortField: queryParams.SortColumn,
SortDirectory: queryParams.SortDirection,
}
sort.Sort(watchlistSorter)
totalWatchlist = watchlistSorter.Watchlist
}
total := len(totalWatchlist)
switch {
case total < offset:
totalWatchlist = totalWatchlist[0:0]
case total < offset+limit:
totalWatchlist = totalWatchlist[offset:]
default:
totalWatchlist = totalWatchlist[offset : offset+limit]
}
var response *common.ListResponse
if len(totalWatchlist) > 0 {
response = common.GetListResponse(total, queryParams.Page, queryParams.PerPage, totalWatchlist)
} else {
response = common.GetEmptyListResponse(total, queryParams.Page, queryParams.PerPage)
}
bz, err := json.Marshal(response)
if err != nil {
return nil, common.ErrMarshalJSONFailed(err.Error())
}
return bz, nil
}
// calculate baseAmount and quoteAmount in dollar by usdk
func calculateDollarAmount(ctx sdk.Context, keeper Keeper, baseAmount sdk.SysCoin, quoteAmount sdk.SysCoin) sdk.Dec {
dollarAmount := sdk.ZeroDec()
baseTokenDollar := sdk.ZeroDec()
quoteTokenDollar := sdk.ZeroDec()
dollarQuoteToken := keeper.farmKeeper.GetParams(ctx).QuoteSymbol
if baseAmount.Denom == dollarQuoteToken {
baseTokenDollar = baseAmount.Amount
} else {
baseTokenPairName := ammswap.GetSwapTokenPairName(baseAmount.Denom, dollarQuoteToken)
if baseTokenPair, err := keeper.swapKeeper.GetSwapTokenPair(ctx, baseTokenPairName); err == nil {
if baseTokenPair.BasePooledCoin.Denom == dollarQuoteToken && baseTokenPair.QuotePooledCoin.Amount.IsPositive() {
baseTokenDollar = common.MulAndQuo(baseTokenPair.BasePooledCoin.Amount, baseAmount.Amount, baseTokenPair.QuotePooledCoin.Amount)
} else if baseTokenPair.BasePooledCoin.Amount.IsPositive() {
baseTokenDollar = common.MulAndQuo(baseTokenPair.QuotePooledCoin.Amount, baseAmount.Amount, baseTokenPair.BasePooledCoin.Amount)
}
}
}
if quoteAmount.Denom == dollarQuoteToken {
quoteTokenDollar = quoteAmount.Amount
} else {
quoteTokenPairName := ammswap.GetSwapTokenPairName(quoteAmount.Denom, dollarQuoteToken)
if quoteTokenPair, err := keeper.swapKeeper.GetSwapTokenPair(ctx, quoteTokenPairName); err == nil {
if quoteTokenPair.BasePooledCoin.Denom == dollarQuoteToken && quoteTokenPair.QuotePooledCoin.Amount.IsPositive() {
quoteTokenDollar = common.MulAndQuo(quoteTokenPair.BasePooledCoin.Amount, quoteAmount.Amount, quoteTokenPair.QuotePooledCoin.Amount)
} else if quoteTokenPair.BasePooledCoin.Amount.IsPositive() {
quoteTokenDollar = common.MulAndQuo(quoteTokenPair.QuotePooledCoin.Amount, quoteAmount.Amount, quoteTokenPair.BasePooledCoin.Amount)
}
}
}
if baseTokenDollar.IsZero() && quoteTokenDollar.IsPositive() && baseAmount.Amount.IsPositive() {
baseTokenDollar = quoteTokenDollar
}
if quoteTokenDollar.IsZero() && baseTokenDollar.IsPositive() && quoteAmount.Amount.IsPositive() {
quoteTokenDollar = baseTokenDollar
}
dollarAmount = baseTokenDollar.Add(quoteTokenDollar)
return dollarAmount
}
// querySwapTokens returns tokens which are supported to swap in ammswap module
func querySwapTokens(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var queryParams types.QuerySwapTokensParams
err := keeper.cdc.UnmarshalJSON(req.Data, &queryParams)
if err != nil {
return nil, common.ErrUnMarshalJSONFailed(err.Error())
}
if queryParams.BusinessType == "" {
return nil, swaptypes.ErrIsZeroValue("input business type param")
}
// coins in account
var accountCoins sdk.SysCoins
if queryParams.Address != "" {
addr, err := sdk.AccAddressFromBech32(queryParams.Address)
if err != nil {
return nil, common.ErrCreateAddrFromBech32Failed(queryParams.Address, err.Error())
}
accountCoins = keeper.TokenKeeper.GetCoins(ctx, addr)
}
var tokens []string
switch queryParams.BusinessType {
case types.SwapBusinessTypeCreate:
tokens = getSwapCreateLiquidityTokens(ctx, keeper)
case types.SwapBusinessTypeAdd:
tokens = getSwapAddLiquidityTokens(ctx, keeper, queryParams.BaseTokenName)
case types.SwapBusinessTypeSwap:
tokens = getSwapTokens(ctx, keeper, queryParams.BaseTokenName)
}
swapTokensMap := make(map[string]sdk.Dec, len(tokens))
for _, token := range tokens {
swapTokensMap[token] = sdk.ZeroDec()
}
// update amount by coins in account
for _, coin := range accountCoins {
if _, ok := swapTokensMap[coin.Denom]; ok {
swapTokensMap[coin.Denom] = coin.Amount
}
}
// sort token list by account balance
var swapTokens types.SwapTokens
for symbol, available := range swapTokensMap {
swapTokens = append(swapTokens, types.NewSwapToken(symbol, available))
}
sort.Sort(swapTokens)
swapTokensResp := types.SwapTokensResponse{
NativeToken: common.NativeToken,
Tokens: swapTokens,
}
response := common.GetBaseResponse(swapTokensResp)
bz, err := json.Marshal(response)
if err != nil {
return nil, common.ErrMarshalJSONFailed(err.Error())
}
return bz, nil
}
func getSwapCreateLiquidityTokens(ctx sdk.Context, keeper Keeper) []string {
var tokens []string
allTokens := keeper.TokenKeeper.GetTokensInfo(ctx)
for _, token := range allTokens {
if !strings.HasPrefix(token.Symbol, swaptypes.PoolTokenPrefix) {
tokens = append(tokens, token.Symbol)
}
}
return tokens
}
func getSwapAddLiquidityTokens(ctx sdk.Context, keeper Keeper, baseTokenName string) []string {
var tokens []string
// all swap token pairs
swapTokenPairs := keeper.swapKeeper.GetSwapTokenPairs(ctx)
for _, swapTokenPair := range swapTokenPairs {
if baseTokenName == "" {
tokens = append(tokens, swapTokenPair.BasePooledCoin.Denom)
tokens = append(tokens, swapTokenPair.QuotePooledCoin.Denom)
} else if baseTokenName == swapTokenPair.BasePooledCoin.Denom {
tokens = append(tokens, swapTokenPair.QuotePooledCoin.Denom)
} else if baseTokenName == swapTokenPair.QuotePooledCoin.Denom {
tokens = append(tokens, swapTokenPair.BasePooledCoin.Denom)
}
}
return tokens
}
func getSwapTokens(ctx sdk.Context, keeper Keeper, baseTokenName string) []string {
var tokens []string
// whitelist map
whitelistMap := getSwapWhitelistMap(keeper)
// swap by route
hasSwapRoute := false
if baseTokenName != "" {
nativePairName := swaptypes.GetSwapTokenPairName(baseTokenName, common.NativeToken)
// check if in whitelist
if _, found := whitelistMap[nativePairName]; found {
if _, err := keeper.swapKeeper.GetSwapTokenPair(ctx, nativePairName); err == nil {
hasSwapRoute = true
}
}
}
// all swap token pairs
swapTokenPairs := keeper.swapKeeper.GetSwapTokenPairs(ctx)
for _, swapTokenPair := range swapTokenPairs {
// check if in whitelist
if _, found := whitelistMap[swapTokenPair.TokenPairName()]; !found {
continue
}
if baseTokenName == "" {
tokens = append(tokens, swapTokenPair.BasePooledCoin.Denom)
tokens = append(tokens, swapTokenPair.QuotePooledCoin.Denom)
} else if baseTokenName == swapTokenPair.BasePooledCoin.Denom {
tokens = append(tokens, swapTokenPair.QuotePooledCoin.Denom)
} else if baseTokenName == swapTokenPair.QuotePooledCoin.Denom {
tokens = append(tokens, swapTokenPair.BasePooledCoin.Denom)
}
// swap by route
if hasSwapRoute {
if swapTokenPair.BasePooledCoin.Denom == common.NativeToken && swapTokenPair.QuotePooledCoin.Denom != baseTokenName {
tokens = append(tokens, swapTokenPair.QuotePooledCoin.Denom)
} else if swapTokenPair.QuotePooledCoin.Denom == common.NativeToken && swapTokenPair.BasePooledCoin.Denom != baseTokenName {
tokens = append(tokens, swapTokenPair.BasePooledCoin.Denom)
}
}
}
return tokens
}
// nolint
func querySwapTokenPairs(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte,
err sdk.Error) {
// whitelist map
whitelistMap := getSwapWhitelistMap(keeper)
swapTokenPairs := keeper.swapKeeper.GetSwapTokenPairs(ctx)
var whitelistTokenPair []swaptypes.SwapTokenPair
for _, swapTokenPair := range swapTokenPairs {
// check if in whitelist
if _, found := whitelistMap[swapTokenPair.TokenPairName()]; !found {
continue
}
whitelistTokenPair = append(whitelistTokenPair, swapTokenPair)
}
response := common.GetBaseResponse(whitelistTokenPair)
bz, err := json.Marshal(response)
if err != nil {
return nil, common.ErrMarshalJSONFailed(err.Error())
}
return bz, nil
}
// querySwapLiquidityHistories returns liquidity info of the address
func querySwapLiquidityHistories(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var queryParams types.QuerySwapLiquidityInfoParams
err := keeper.cdc.UnmarshalJSON(req.Data, &queryParams)
if err != nil {
return nil, common.ErrUnMarshalJSONFailed(err.Error())
}
// check params
if queryParams.Address == "" {
return nil, swaptypes.ErrQueryParamsAddressIsEmpty()
}
// coins in account
addr, err := sdk.AccAddressFromBech32(queryParams.Address)
if err != nil {
return nil, common.ErrCreateAddrFromBech32Failed(queryParams.Address, err.Error())
}
var liquidityInfoList []types.SwapLiquidityInfo
// coins in account
accountCoins := keeper.TokenKeeper.GetCoins(ctx, addr)
for _, coin := range accountCoins {
// check if the token is pool token
if !strings.HasPrefix(coin.Denom, swaptypes.PoolTokenPrefix) {
continue
}
// check token pair name
tokenPairName := coin.Denom[len(swaptypes.PoolTokenPrefix):]
if queryParams.TokenPairName != "" && queryParams.TokenPairName != tokenPairName {
continue
}
// get swap token pair
swapTokenPair, err := keeper.swapKeeper.GetSwapTokenPair(ctx, tokenPairName)
if err != nil {
continue
}
poolTokenAmount := keeper.swapKeeper.GetPoolTokenAmount(ctx, swapTokenPair.PoolTokenName)
baseDec := common.MulAndQuo(swapTokenPair.BasePooledCoin.Amount, coin.Amount, poolTokenAmount)
quoteDec := common.MulAndQuo(swapTokenPair.QuotePooledCoin.Amount, coin.Amount, poolTokenAmount)
baseAmount := sdk.NewDecCoinFromDec(swapTokenPair.BasePooledCoin.Denom, baseDec)
quoteAmount := sdk.NewDecCoinFromDec(swapTokenPair.QuotePooledCoin.Denom, quoteDec)
liquidityInfo := types.SwapLiquidityInfo{
BasePooledCoin: baseAmount,
QuotePooledCoin: quoteAmount,
PoolTokenCoin: coin,
PoolTokenRatio: coin.Amount.Quo(poolTokenAmount),
}
liquidityInfoList = append(liquidityInfoList, liquidityInfo)
}
response := common.GetBaseResponse(liquidityInfoList)
bz, err := json.Marshal(response)
if err != nil {
return nil, common.ErrMarshalJSONFailed(err.Error())
}
return bz, nil
}
func getSwapWhitelistMap(keeper Keeper) map[string]struct{} {
swapWhitelist := keeper.Orm.GetSwapWhitelist()
whitelistMap := make(map[string]struct{}, len(swapWhitelist))
for _, whitelist := range swapWhitelist {
whitelistMap[whitelist.TokenPairName] = struct{}{}
}
return whitelistMap
}