/
msg_server_balance.go
43 lines (31 loc) · 1.22 KB
/
msg_server_balance.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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sge-network/sge/x/subaccount/types"
)
// TopUp increases the balance of sub account according to the input data.
func (k msgServer) TopUp(goCtx context.Context, msg *types.MsgTopUp) (*types.MsgTopUpResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
subAccAddr, err := k.keeper.TopUp(ctx, msg.Creator, msg.Address, msg.LockedBalances)
if err != nil {
return nil, err
}
msg.EmitEvent(&ctx, subAccAddr)
return &types.MsgTopUpResponse{}, nil
}
// WithdrawUnlockedBalances withdraws the unlocked balance of sub account according to the input account.
func (k msgServer) WithdrawUnlockedBalances(goCtx context.Context, msg *types.MsgWithdrawUnlockedBalances) (*types.MsgWithdrawUnlockedBalancesResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ownerAddr := sdk.MustAccAddressFromBech32(msg.Creator)
subAccAddr, exists := k.keeper.GetSubaccountByOwner(ctx, ownerAddr)
if !exists {
return nil, types.ErrSubaccountDoesNotExist
}
err := k.keeper.withdrawUnlocked(ctx, subAccAddr, ownerAddr)
if err != nil {
return nil, err
}
msg.EmitEvent(&ctx, subAccAddr.String())
return &types.MsgWithdrawUnlockedBalancesResponse{}, nil
}