mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 16:25:21 +00:00
a573625df8
* bump SDK version to v0.38.3 Co-authored-by: Denali Marsh <denali@kava.io> Co-authored-by: Kevin Davis <kjydavis3@gmail.com> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> Co-authored-by: denalimarsh <denalimarsh@gmail.com> Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com>
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
package bep3
|
|
|
|
import (
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
)
|
|
|
|
// NewHandler creates an sdk.Handler for all the bep3 type messages
|
|
func NewHandler(k Keeper) sdk.Handler {
|
|
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
|
switch msg := msg.(type) {
|
|
case MsgCreateAtomicSwap:
|
|
return handleMsgCreateAtomicSwap(ctx, k, msg)
|
|
case MsgClaimAtomicSwap:
|
|
return handleMsgClaimAtomicSwap(ctx, k, msg)
|
|
case MsgRefundAtomicSwap:
|
|
return handleMsgRefundAtomicSwap(ctx, k, msg)
|
|
default:
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
// handleMsgCreateAtomicSwap handles requests to create a new AtomicSwap
|
|
func handleMsgCreateAtomicSwap(ctx sdk.Context, k Keeper, msg MsgCreateAtomicSwap) (*sdk.Result, error) {
|
|
err := k.CreateAtomicSwap(ctx, msg.RandomNumberHash, msg.Timestamp, msg.HeightSpan, msg.From, msg.To,
|
|
msg.SenderOtherChain, msg.RecipientOtherChain, msg.Amount, msg.ExpectedIncome, msg.CrossChain)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
sdk.EventTypeMessage,
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.From.String()),
|
|
),
|
|
)
|
|
|
|
return &sdk.Result{
|
|
Events: ctx.EventManager().Events(),
|
|
}, nil
|
|
}
|
|
|
|
// handleMsgClaimAtomicSwap handles requests to claim funds in an active AtomicSwap
|
|
func handleMsgClaimAtomicSwap(ctx sdk.Context, k Keeper, msg MsgClaimAtomicSwap) (*sdk.Result, error) {
|
|
|
|
err := k.ClaimAtomicSwap(ctx, msg.From, msg.SwapID, msg.RandomNumber)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
sdk.EventTypeMessage,
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.From.String()),
|
|
),
|
|
)
|
|
|
|
return &sdk.Result{
|
|
Events: ctx.EventManager().Events(),
|
|
}, nil
|
|
}
|
|
|
|
// handleMsgRefundAtomicSwap handles requests to refund an active AtomicSwap
|
|
func handleMsgRefundAtomicSwap(ctx sdk.Context, k Keeper, msg MsgRefundAtomicSwap) (*sdk.Result, error) {
|
|
|
|
err := k.RefundAtomicSwap(ctx, msg.From, msg.SwapID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
sdk.EventTypeMessage,
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.From.String()),
|
|
),
|
|
)
|
|
|
|
return &sdk.Result{
|
|
Events: ctx.EventManager().Events(),
|
|
}, nil
|
|
}
|