/
query_server.go
54 lines (42 loc) · 1.74 KB
/
query_server.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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sge-network/sge/app/params"
"github.com/sge-network/sge/x/subaccount/types"
)
func NewQueryServer(keeper Keeper) types.QueryServer {
return queryServer{keeper: keeper}
}
type queryServer struct {
keeper Keeper
}
func (q queryServer) Subaccount(goCtx context.Context, request *types.QuerySubaccountRequest) (*types.QuerySubaccountResponse, error) {
addr, err := sdk.AccAddressFromBech32(request.Address)
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(goCtx)
subaccountAddr, exists := q.keeper.GetSubaccountByOwner(ctx, addr)
if !exists {
return nil, types.ErrSubaccountDoesNotExist
}
accSummary, exists := q.keeper.GetAccountSummary(ctx, subaccountAddr)
if !exists {
panic("subaccount exists but balance not found")
}
lockedBalances, _ := q.keeper.GetBalances(ctx, subaccountAddr, types.LockedBalanceStatus_LOCKED_BALANCE_STATUS_LOCKED)
unlockedBalances, unlockedAmount := q.keeper.GetBalances(ctx, subaccountAddr, types.LockedBalanceStatus_LOCKED_BALANCE_STATUS_UNLOCKED)
bankBalance := q.keeper.bankKeeper.GetBalance(ctx, subaccountAddr, params.DefaultBondDenom)
return &types.QuerySubaccountResponse{
Address: subaccountAddr.String(),
Balance: accSummary,
LockedBalance: lockedBalances,
UnlockedBalance: unlockedBalances,
WithdrawableUnlockedBalance: accSummary.WithdrawableUnlockedBalance(unlockedAmount, bankBalance.Amount),
}, nil
}
func (q queryServer) Params(ctx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
params := q.keeper.GetParams(sdk.UnwrapSDKContext(ctx))
return &types.QueryParamsResponse{Params: params}, nil
}