-
Notifications
You must be signed in to change notification settings - Fork 9
/
query_interchain_liquidity_pool.go
115 lines (91 loc) · 3.92 KB
/
query_interchain_liquidity_pool.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
package keeper
import (
"context"
"encoding/binary"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/sideprotocol/ibcswap/v6/modules/apps/101-interchain-swap/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) InterchainLiquidityPoolAll(goCtx context.Context, req *types.QueryAllInterchainLiquidityPoolRequest) (*types.QueryAllInterchainLiquidityPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
var interchainLiquidityPools []types.InterchainLiquidityPool
ctx := sdk.UnwrapSDKContext(goCtx)
// Use the store with the mapping of poolId to its count
store := ctx.KVStore(k.storeKey)
poolIdToCountStore := prefix.NewStore(store, types.PoolIdToCountKeyPrefix)
// A counter for pagination
var counter int
pageRes, err := query.Paginate(poolIdToCountStore, req.Pagination, func(key []byte, value []byte) error {
counter++
// Decode the count for the given poolId
count := binary.BigEndian.Uint64(value)
// Get the actual pool using the count
poolStore := prefix.NewStore(store, types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
poolBytes := poolStore.Get(GetInterchainLiquidityPoolKey(count))
if poolBytes == nil {
return nil
}
var interchainLiquidityPool types.InterchainLiquidityPool
if err := k.cdc.Unmarshal(poolBytes, &interchainLiquidityPool); err != nil {
return err
}
interchainLiquidityPools = append(interchainLiquidityPools, interchainLiquidityPool)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryAllInterchainLiquidityPoolResponse{InterchainLiquidityPool: interchainLiquidityPools, Pagination: pageRes}, nil
}
func (k Keeper) InterchainLiquidityMyPoolAll(goCtx context.Context, req *types.QueryAllInterchainLiquidityMyPoolRequest) (*types.QueryAllInterchainLiquidityPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
var interchainLiquidityPools []types.InterchainLiquidityPool
ctx := sdk.UnwrapSDKContext(goCtx)
// Use the store with the mapping of poolId to its count
store := ctx.KVStore(k.storeKey)
poolIdToCountStore := prefix.NewStore(store, types.PoolIdToCountKeyPrefix)
pageRes, err := query.Paginate(poolIdToCountStore, req.Pagination, func(key []byte, value []byte) error {
// Decode the count for the given poolId
count := binary.BigEndian.Uint64(value)
// Get the actual pool using the count
poolStore := prefix.NewStore(store, types.KeyPrefix(types.InterchainLiquidityPoolKeyPrefix))
poolBytes := poolStore.Get(GetInterchainLiquidityPoolKey(count))
if poolBytes == nil {
return nil
}
var interchainLiquidityPool types.InterchainLiquidityPool
if err := k.cdc.Unmarshal(poolBytes, &interchainLiquidityPool); err != nil {
return err
}
// Check if the creator is either SourceCreator or DestinationCreator
if interchainLiquidityPool.SourceCreator == req.Creator || interchainLiquidityPool.DestinationCreator == req.Creator {
interchainLiquidityPools = append(interchainLiquidityPools, interchainLiquidityPool)
}
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryAllInterchainLiquidityPoolResponse{InterchainLiquidityPool: interchainLiquidityPools, Pagination: pageRes}, nil
}
func (k Keeper) InterchainLiquidityPool(goCtx context.Context, req *types.QueryGetInterchainLiquidityPoolRequest) (*types.QueryGetInterchainLiquidityPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
val, found := k.GetInterchainLiquidityPool(
ctx,
req.PoolId,
)
if !found {
return nil, status.Error(codes.NotFound, "not found")
}
return &types.QueryGetInterchainLiquidityPoolResponse{InterchainLiquidityPool: val}, nil
}