/
msg_server_transfer_token.go
61 lines (48 loc) · 1.74 KB
/
msg_server_transfer_token.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
package keeper
import (
"context"
"fmt"
"strings"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/realiotech/realio-network/x/asset/types"
)
func (k msgServer) TransferToken(goCtx context.Context, msg *types.MsgTransferToken) (*types.MsgTransferTokenResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
var fromAddress, toAddress sdk.AccAddress
isAuthorizedFrom, isAuthorizedTo := true, true
fromAddress, _ = sdk.AccAddressFromBech32(msg.From)
toAddress, _ = sdk.AccAddressFromBech32(msg.To)
// Check if the value already exists
token, isFound := k.GetToken(
ctx,
msg.Symbol,
)
if !isFound {
return nil, errorsmod.Wrapf(sdkerrors.ErrKeyNotFound, "token %s not found", msg.Symbol)
}
if k.bankKeeper.BlockedAddr(toAddress) {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.To)
}
if token.AuthorizationRequired {
isAuthorizedFrom = k.IsAddressAuthorizedToSend(ctx, msg.Symbol, fromAddress)
isAuthorizedTo = k.IsAddressAuthorizedToSend(ctx, msg.Symbol, toAddress)
}
if isAuthorizedFrom && isAuthorizedTo {
totalInt, totalIsValid := math.NewIntFromString(msg.Amount)
if !totalIsValid {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, "invalid coin amount %s", msg.Amount)
}
baseDenom := fmt.Sprintf("a%s", strings.ToLower(msg.Symbol))
coin := sdk.Coins{{Denom: baseDenom, Amount: totalInt}}
err := k.bankKeeper.SendCoins(ctx, fromAddress, toAddress, coin)
if err != nil {
return nil, err
}
} else {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s transfer not authorized", msg.Symbol)
}
return &types.MsgTransferTokenResponse{}, nil
}