0g-chain/x/liquid/keeper/msg_server.go
2024-07-30 21:46:39 +08:00

85 lines
2.0 KiB
Go

package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/0glabs/0g-chain/x/liquid/types"
)
type msgServer struct {
keeper Keeper
}
// NewMsgServerImpl returns an implementation of the liquid MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{keeper: keeper}
}
var _ types.MsgServer = msgServer{}
// MintDerivative handles MintDerivative msgs.
func (k msgServer) MintDerivative(goCtx context.Context, msg *types.MsgMintDerivative) (*types.MsgMintDerivativeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
validator, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}
mintedDerivative, err := k.keeper.MintDerivative(ctx, sender, validator, msg.Amount)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
)
return &types.MsgMintDerivativeResponse{
Received: mintedDerivative,
}, nil
}
// BurnDerivative handles BurnDerivative msgs.
func (k msgServer) BurnDerivative(goCtx context.Context, msg *types.MsgBurnDerivative) (*types.MsgBurnDerivativeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}
validator, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
sharesReceived, err := k.keeper.BurnDerivative(ctx, sender, validator, msg.Amount)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
)
return &types.MsgBurnDerivativeResponse{
Received: sharesReceived,
}, nil
}