2024-02-01 04:48:40 +00:00
|
|
|
package keeper
|
|
|
|
|
2024-02-02 09:39:10 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-06-25 06:33:41 +00:00
|
|
|
"git.vdb.to/cerc-io/laconicd/utils"
|
2024-04-01 09:57:26 +00:00
|
|
|
"git.vdb.to/cerc-io/laconicd/x/bond"
|
2024-02-02 09:39:10 +00:00
|
|
|
)
|
|
|
|
|
2024-02-12 08:34:40 +00:00
|
|
|
var _ bond.MsgServer = msgServer{}
|
|
|
|
|
2024-02-01 04:48:40 +00:00
|
|
|
type msgServer struct {
|
2024-02-28 04:34:58 +00:00
|
|
|
k *Keeper
|
2024-02-01 04:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgServerImpl returns an implementation of the module MsgServer interface.
|
2024-02-28 04:34:58 +00:00
|
|
|
func NewMsgServerImpl(keeper *Keeper) bond.MsgServer {
|
2024-02-02 09:39:10 +00:00
|
|
|
return &msgServer{k: keeper}
|
|
|
|
}
|
2024-02-01 04:48:40 +00:00
|
|
|
|
2024-02-02 09:39:10 +00:00
|
|
|
func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bond.MsgCreateBondResponse, error) {
|
2024-03-06 09:24:15 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-02 09:39:10 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-06-25 06:33:41 +00:00
|
|
|
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
|
2024-02-08 13:23:20 +00:00
|
|
|
|
2024-02-02 09:39:10 +00:00
|
|
|
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-04 11:16:09 +00:00
|
|
|
|
|
|
|
resp, err := ms.k.CreateBond(ctx, signerAddress, msg.Coins)
|
2024-02-02 09:39:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
bond.EventTypeCreateBond,
|
|
|
|
sdk.NewAttribute(bond.AttributeKeySigner, msg.Signer),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Coins.String()),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, bond.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(bond.AttributeKeySigner, msg.Signer),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2024-06-25 06:33:41 +00:00
|
|
|
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "CreateBond")
|
|
|
|
|
2024-03-04 11:16:09 +00:00
|
|
|
return &bond.MsgCreateBondResponse{Id: resp.Id}, nil
|
2024-02-02 09:39:10 +00:00
|
|
|
}
|
2024-02-08 13:23:20 +00:00
|
|
|
|
|
|
|
// RefillBond implements bond.MsgServer.
|
|
|
|
func (ms msgServer) RefillBond(c context.Context, msg *bond.MsgRefillBond) (*bond.MsgRefillBondResponse, error) {
|
2024-03-06 09:24:15 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 13:23:20 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-06-25 06:33:41 +00:00
|
|
|
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
|
2024-02-08 13:23:20 +00:00
|
|
|
|
|
|
|
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ms.k.RefillBond(ctx, msg.Id, signerAddress, msg.Coins)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
bond.EventTypeRefillBond,
|
|
|
|
sdk.NewAttribute(bond.AttributeKeySigner, msg.Signer),
|
|
|
|
sdk.NewAttribute(bond.AttributeKeyBondId, msg.Id),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Coins.String()),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, bond.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(bond.AttributeKeySigner, msg.Signer),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2024-06-25 06:33:41 +00:00
|
|
|
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "RefillBond")
|
|
|
|
|
2024-02-08 13:23:20 +00:00
|
|
|
return &bond.MsgRefillBondResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithdrawBond implements bond.MsgServer.
|
|
|
|
func (ms msgServer) WithdrawBond(c context.Context, msg *bond.MsgWithdrawBond) (*bond.MsgWithdrawBondResponse, error) {
|
2024-03-06 09:24:15 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 13:23:20 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-06-25 06:33:41 +00:00
|
|
|
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
|
2024-02-08 13:23:20 +00:00
|
|
|
|
|
|
|
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ms.k.WithdrawBond(ctx, msg.Id, signerAddress, msg.Coins)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
bond.EventTypeWithdrawBond,
|
|
|
|
sdk.NewAttribute(bond.AttributeKeySigner, msg.Signer),
|
|
|
|
sdk.NewAttribute(bond.AttributeKeyBondId, msg.Id),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Coins.String()),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, bond.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(bond.AttributeKeySigner, msg.Signer),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2024-06-25 06:33:41 +00:00
|
|
|
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "WithdrawBond")
|
|
|
|
|
2024-02-08 13:23:20 +00:00
|
|
|
return &bond.MsgWithdrawBondResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelBond implements bond.MsgServer.
|
|
|
|
func (ms msgServer) CancelBond(c context.Context, msg *bond.MsgCancelBond) (*bond.MsgCancelBondResponse, error) {
|
2024-03-06 09:24:15 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 13:23:20 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-06-25 06:33:41 +00:00
|
|
|
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
|
2024-03-06 09:24:15 +00:00
|
|
|
|
2024-02-08 13:23:20 +00:00
|
|
|
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-06 09:24:15 +00:00
|
|
|
|
2024-02-08 13:23:20 +00:00
|
|
|
_, err = ms.k.CancelBond(ctx, msg.Id, signerAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
|
|
sdk.NewEvent(
|
|
|
|
bond.EventTypeCancelBond,
|
|
|
|
sdk.NewAttribute(bond.AttributeKeySigner, msg.Signer),
|
|
|
|
sdk.NewAttribute(bond.AttributeKeyBondId, msg.Id),
|
|
|
|
),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, bond.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(bond.AttributeKeySigner, msg.Signer),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2024-06-25 06:33:41 +00:00
|
|
|
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "CancelBond")
|
|
|
|
|
2024-02-08 13:23:20 +00:00
|
|
|
return &bond.MsgCancelBondResponse{}, nil
|
|
|
|
}
|