## Description Closes: #XXXX --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
198 lines
5.5 KiB
Go
198 lines
5.5 KiB
Go
package vesting
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
|
)
|
|
|
|
type msgServer struct {
|
|
keeper.AccountKeeper
|
|
types.BankKeeper
|
|
}
|
|
|
|
// NewMsgServerImpl returns an implementation of the vesting MsgServer interface,
|
|
// wrapping the corresponding AccountKeeper and BankKeeper.
|
|
func NewMsgServerImpl(k keeper.AccountKeeper, bk types.BankKeeper) types.MsgServer {
|
|
return &msgServer{AccountKeeper: k, BankKeeper: bk}
|
|
}
|
|
|
|
var _ types.MsgServer = msgServer{}
|
|
|
|
func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCreateVestingAccount) (*types.MsgCreateVestingAccountResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
ak := s.AccountKeeper
|
|
bk := s.BankKeeper
|
|
|
|
if err := bk.IsSendEnabledCoins(ctx, msg.Amount...); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
to, err := sdk.AccAddressFromBech32(msg.ToAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if bk.BlockedAddr(to) {
|
|
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress)
|
|
}
|
|
|
|
if acc := ak.GetAccount(ctx, to); acc != nil {
|
|
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "account %s already exists", msg.ToAddress)
|
|
}
|
|
|
|
baseAccount := authtypes.NewBaseAccountWithAddress(to)
|
|
baseAccount = ak.NewAccount(ctx, baseAccount).(*authtypes.BaseAccount)
|
|
baseVestingAccount := types.NewBaseVestingAccount(baseAccount, msg.Amount.Sort(), msg.EndTime)
|
|
|
|
var vestingAccount sdk.AccountI
|
|
if msg.Delayed {
|
|
vestingAccount = types.NewDelayedVestingAccountRaw(baseVestingAccount)
|
|
} else {
|
|
vestingAccount = types.NewContinuousVestingAccountRaw(baseVestingAccount, ctx.BlockTime().Unix())
|
|
}
|
|
|
|
ak.SetAccount(ctx, vestingAccount)
|
|
|
|
defer func() {
|
|
telemetry.IncrCounter(1, "new", "account")
|
|
|
|
for _, a := range msg.Amount {
|
|
if a.Amount.IsInt64() {
|
|
telemetry.SetGaugeWithLabels(
|
|
[]string{"tx", "msg", "create_vesting_account"},
|
|
float32(a.Amount.Int64()),
|
|
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
|
|
)
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err = bk.SendCoins(ctx, from, to, msg.Amount); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &types.MsgCreateVestingAccountResponse{}, nil
|
|
}
|
|
|
|
func (s msgServer) CreatePermanentLockedAccount(goCtx context.Context, msg *types.MsgCreatePermanentLockedAccount) (*types.MsgCreatePermanentLockedAccountResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
ak := s.AccountKeeper
|
|
bk := s.BankKeeper
|
|
|
|
if err := bk.IsSendEnabledCoins(ctx, msg.Amount...); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
to, err := sdk.AccAddressFromBech32(msg.ToAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if bk.BlockedAddr(to) {
|
|
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress)
|
|
}
|
|
|
|
if acc := ak.GetAccount(ctx, to); acc != nil {
|
|
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "account %s already exists", msg.ToAddress)
|
|
}
|
|
|
|
baseAccount := authtypes.NewBaseAccountWithAddress(to)
|
|
baseAccount = ak.NewAccount(ctx, baseAccount).(*authtypes.BaseAccount)
|
|
vestingAccount := types.NewPermanentLockedAccount(baseAccount, msg.Amount)
|
|
|
|
ak.SetAccount(ctx, vestingAccount)
|
|
|
|
defer func() {
|
|
telemetry.IncrCounter(1, "new", "account")
|
|
|
|
for _, a := range msg.Amount {
|
|
if a.Amount.IsInt64() {
|
|
telemetry.SetGaugeWithLabels(
|
|
[]string{"tx", "msg", "create_permanent_locked_account"},
|
|
float32(a.Amount.Int64()),
|
|
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
|
|
)
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err = bk.SendCoins(ctx, from, to, msg.Amount); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &types.MsgCreatePermanentLockedAccountResponse{}, nil
|
|
}
|
|
|
|
func (s msgServer) CreatePeriodicVestingAccount(goCtx context.Context, msg *types.MsgCreatePeriodicVestingAccount) (*types.MsgCreatePeriodicVestingAccountResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
ak := s.AccountKeeper
|
|
bk := s.BankKeeper
|
|
|
|
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
to, err := sdk.AccAddressFromBech32(msg.ToAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if acc := ak.GetAccount(ctx, to); acc != nil {
|
|
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "account %s already exists", msg.ToAddress)
|
|
}
|
|
|
|
var totalCoins sdk.Coins
|
|
for _, period := range msg.VestingPeriods {
|
|
totalCoins = totalCoins.Add(period.Amount...)
|
|
}
|
|
|
|
if err := bk.IsSendEnabledCoins(ctx, totalCoins...); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
baseAccount := authtypes.NewBaseAccountWithAddress(to)
|
|
baseAccount = ak.NewAccount(ctx, baseAccount).(*authtypes.BaseAccount)
|
|
vestingAccount := types.NewPeriodicVestingAccount(baseAccount, totalCoins.Sort(), msg.StartTime, msg.VestingPeriods)
|
|
|
|
ak.SetAccount(ctx, vestingAccount)
|
|
|
|
defer func() {
|
|
telemetry.IncrCounter(1, "new", "account")
|
|
|
|
for _, a := range totalCoins {
|
|
if a.Amount.IsInt64() {
|
|
telemetry.SetGaugeWithLabels(
|
|
[]string{"tx", "msg", "create_periodic_vesting_account"},
|
|
float32(a.Amount.Int64()),
|
|
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
|
|
)
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err = bk.SendCoins(ctx, from, to, totalCoins); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &types.MsgCreatePeriodicVestingAccountResponse{}, nil
|
|
}
|