2022-08-23 17:04:40 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/evmutil/types"
|
2022-08-23 17:04:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type msgServer struct {
|
|
|
|
keeper Keeper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgServerImpl returns an implementation of the evmutil MsgServer interface
|
|
|
|
// for the provided Keeper.
|
|
|
|
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
|
|
|
|
return &msgServer{keeper: keeper}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.MsgServer = msgServer{}
|
|
|
|
|
2023-05-23 18:01:45 +00:00
|
|
|
////////////////////////////
|
|
|
|
// EVM-native assets -> Cosmos SDK
|
|
|
|
////////////////////////////
|
|
|
|
|
2022-08-23 17:04:40 +00:00
|
|
|
// ConvertCoinToERC20 handles a MsgConvertCoinToERC20 message to convert
|
|
|
|
// sdk.Coin to Kava EVM tokens.
|
|
|
|
func (s msgServer) ConvertCoinToERC20(
|
|
|
|
goCtx context.Context,
|
|
|
|
msg *types.MsgConvertCoinToERC20,
|
|
|
|
) (*types.MsgConvertCoinToERC20Response, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
|
|
|
initiator, err := sdk.AccAddressFromBech32(msg.Initiator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid Initiator address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver, err := types.NewInternalEVMAddressFromString(msg.Receiver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid Receiver address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.keeper.ConvertCoinToERC20(
|
|
|
|
ctx,
|
|
|
|
initiator,
|
|
|
|
receiver,
|
|
|
|
*msg.Amount,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Initiator),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return &types.MsgConvertCoinToERC20Response{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertERC20ToCoin handles a MsgConvertERC20ToCoin message to convert
|
|
|
|
// sdk.Coin to Kava EVM tokens.
|
|
|
|
func (s msgServer) ConvertERC20ToCoin(
|
|
|
|
goCtx context.Context,
|
|
|
|
msg *types.MsgConvertERC20ToCoin,
|
|
|
|
) (*types.MsgConvertERC20ToCoinResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
|
|
|
initiator, err := types.NewInternalEVMAddressFromString(msg.Initiator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid initiator address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver, err := sdk.AccAddressFromBech32(msg.Receiver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid receiver address: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-08-03 11:04:48 +00:00
|
|
|
contractAddr, err := types.NewInternalEVMAddressFromString(msg.ZgChainERC20Address)
|
2022-08-23 17:04:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid contract address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.keeper.ConvertERC20ToCoin(
|
|
|
|
ctx,
|
|
|
|
initiator,
|
|
|
|
receiver,
|
|
|
|
contractAddr,
|
|
|
|
msg.Amount,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Initiator),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return &types.MsgConvertERC20ToCoinResponse{}, nil
|
|
|
|
}
|
2023-05-23 18:01:45 +00:00
|
|
|
|
|
|
|
////////////////////////////
|
|
|
|
// Cosmos SDK-native assets -> EVM
|
|
|
|
////////////////////////////
|
|
|
|
|
2023-05-23 19:32:27 +00:00
|
|
|
// ConvertCosmosCoinToERC20 converts a native sdk.Coin to an ERC20.
|
2023-05-23 18:01:45 +00:00
|
|
|
// If no ERC20 contract has been deployed for the given denom, a new
|
|
|
|
// contract will be deployed and registered to the module.
|
2023-05-24 23:23:33 +00:00
|
|
|
func (s msgServer) ConvertCosmosCoinToERC20(
|
|
|
|
goCtx context.Context,
|
2023-05-23 19:32:27 +00:00
|
|
|
msg *types.MsgConvertCosmosCoinToERC20,
|
|
|
|
) (*types.MsgConvertCosmosCoinToERC20Response, error) {
|
2023-05-30 19:00:01 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
|
|
|
initiator, err := sdk.AccAddressFromBech32(msg.Initiator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid initiator address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver, err := types.NewInternalEVMAddressFromString(msg.Receiver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid receiver address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.keeper.ConvertCosmosCoinToERC20(
|
|
|
|
ctx,
|
|
|
|
initiator,
|
|
|
|
receiver,
|
|
|
|
*msg.Amount,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-30 20:06:46 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Initiator),
|
|
|
|
),
|
|
|
|
)
|
2023-05-30 19:00:01 +00:00
|
|
|
|
|
|
|
return &types.MsgConvertCosmosCoinToERC20Response{}, nil
|
2023-05-23 18:01:45 +00:00
|
|
|
}
|
2023-06-02 19:36:59 +00:00
|
|
|
|
|
|
|
// ConvertCosmosCoinFromERC20 converts an ERC20 representation of a cosmos-native asset
|
|
|
|
// back into an sdk.Coin.
|
|
|
|
func (s msgServer) ConvertCosmosCoinFromERC20(
|
|
|
|
goCtx context.Context,
|
2023-06-02 21:26:51 +00:00
|
|
|
msg *types.MsgConvertCosmosCoinFromERC20,
|
2023-06-02 19:36:59 +00:00
|
|
|
) (*types.MsgConvertCosmosCoinFromERC20Response, error) {
|
2023-06-02 21:26:51 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
|
|
|
initiator, err := types.NewInternalEVMAddressFromString(msg.Initiator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid initiator address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver, err := sdk.AccAddressFromBech32(msg.Receiver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid receiver address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.keeper.ConvertCosmosCoinFromERC20(
|
|
|
|
ctx,
|
|
|
|
initiator,
|
|
|
|
receiver,
|
|
|
|
*msg.Amount,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Initiator),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return &types.MsgConvertCosmosCoinFromERC20Response{}, nil
|
2023-06-02 19:36:59 +00:00
|
|
|
}
|