-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_query.go
91 lines (76 loc) · 2.66 KB
/
grpc_query.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
package keeper
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/furynet/furymod/modules/coinswap/types"
)
var _ types.QueryServer = Keeper{}
// LiquidityPool returns the liquidity pool information of the denom
func (k Keeper) LiquidityPool(c context.Context, req *types.QueryLiquidityPoolRequest) (*types.QueryLiquidityPoolResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
pool, exists := k.GetPoolByLptDenom(ctx, req.LptDenom)
if !exists {
return nil, sdkerrors.Wrapf(types.ErrReservePoolNotExists, "liquidity pool token: %s", req.LptDenom)
}
balances, err := k.GetPoolBalancesByLptDenom(ctx, pool.LptDenom)
if err != nil {
return nil, err
}
standard := sdk.NewCoin(pool.StandardDenom, balances.AmountOf(pool.StandardDenom))
token := sdk.NewCoin(pool.CounterpartyDenom, balances.AmountOf(pool.CounterpartyDenom))
liquidity := k.bk.GetSupply(ctx, pool.LptDenom)
params := k.GetParams(ctx)
res := types.QueryLiquidityPoolResponse{
Pool: types.PoolInfo{
Id: pool.Id,
EscrowAddress: pool.EscrowAddress,
Standard: standard,
Token: token,
Lpt: liquidity,
Fee: params.Fee.String(),
},
}
return &res, nil
}
func (k Keeper) LiquidityPools(c context.Context, req *types.QueryLiquidityPoolsRequest) (*types.QueryLiquidityPoolsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
var pools []types.PoolInfo
store := ctx.KVStore(k.storeKey)
nftStore := prefix.NewStore(store, []byte(types.KeyPool))
pageRes, err := query.Paginate(nftStore, req.Pagination, func(key []byte, value []byte) error {
var pool types.Pool
k.cdc.MustUnmarshal(value, &pool)
balances, err := k.GetPoolBalancesByLptDenom(ctx, pool.LptDenom)
if err != nil {
return err
}
pools = append(pools, types.PoolInfo{
Id: pool.Id,
EscrowAddress: pool.EscrowAddress,
Standard: sdk.NewCoin(pool.StandardDenom, balances.AmountOf(pool.StandardDenom)),
Token: sdk.NewCoin(pool.CounterpartyDenom, balances.AmountOf(pool.CounterpartyDenom)),
Lpt: k.bk.GetSupply(ctx, pool.LptDenom),
Fee: params.Fee.String(),
})
return nil
})
if err != nil {
return nil, err
}
return &types.QueryLiquidityPoolsResponse{
Pagination: pageRes,
Pools: pools,
}, nil
}