/
msg_server_cancel_swap.go
67 lines (57 loc) · 1.89 KB
/
msg_server_cancel_swap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sideprotocol/ibcswap/v6/modules/apps/100-atomic-swap/types"
)
// CancelSwap is the step 10 (Cancel Request) of the atomic swap: https://github.com/cosmos/ibc/tree/main/spec/app/ics-100-atomic-swap.
// It is executed on the Maker chain. Only the maker of the order can cancel the order.
func (k Keeper) CancelSwap(goCtx context.Context, msg *types.CancelSwapMsg) (*types.MsgCancelSwapResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := msg.ValidateBasic(); err != nil {
return nil, err
}
msgbyte, err := types.ModuleCdc.MarshalJSON(msg)
if err != nil {
return nil, err
}
order, ok := k.GetAtomicOrder(ctx, msg.OrderId)
if !ok {
return &types.MsgCancelSwapResponse{}, types.ErrOrderDoesNotExists
}
// Make sure the sender is the maker of the order.
if order.Maker.MakerAddress != msg.MakerAddress {
return &types.MsgCancelSwapResponse{}, fmt.Errorf("sender is not the maker of the order")
}
// Make sure the order is in a valid state for cancellation
if order.Status != types.Status_SYNC && order.Status != types.Status_INITIAL {
return &types.MsgCancelSwapResponse{}, fmt.Errorf("order is not in a valid state for cancellation")
}
packet := types.AtomicSwapPacketData{
Type: types.CANCEL_SWAP,
Data: msgbyte,
Memo: "",
}
if _, err := k.SendSwapPacket(ctx, order.Maker.SourcePort, order.Maker.SourceChannel, msg.TimeoutHeight, msg.TimeoutTimestamp, packet); err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.ModuleName,
sdk.Attribute{
Key: types.AttributeAction,
Value: types.EventValueActionCancelOrder,
},
sdk.Attribute{
Key: types.AttributeOrderId,
Value: order.Id,
},
sdk.Attribute{
Key: types.AttributeName,
Value: types.EventOwner,
},
),
)
return &types.MsgCancelSwapResponse{}, nil
}