-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_server.go
117 lines (93 loc) · 2.77 KB
/
msg_server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package keeper
import (
"context"
"encoding/hex"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rotosports/fury/x/bep3/types"
)
type msgServer struct {
keeper Keeper
}
// NewMsgServerImpl returns an implementation of the bep3 MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{keeper: keeper}
}
var _ types.MsgServer = msgServer{}
func (k msgServer) CreateAtomicSwap(goCtx context.Context, msg *types.MsgCreateAtomicSwap) (*types.MsgCreateAtomicSwapResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
to, err := sdk.AccAddressFromBech32(msg.To)
if err != nil {
return nil, err
}
randomNumberHash, err := hex.DecodeString(msg.RandomNumberHash)
if err != nil {
return nil, err
}
if err = k.keeper.CreateAtomicSwap(ctx, randomNumberHash, msg.Timestamp, msg.HeightSpan,
from, to, msg.SenderOtherChain, msg.RecipientOtherChain, msg.Amount, true); err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.From),
),
)
return &types.MsgCreateAtomicSwapResponse{}, nil
}
func (k msgServer) ClaimAtomicSwap(goCtx context.Context, msg *types.MsgClaimAtomicSwap) (*types.MsgClaimAtomicSwapResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
swapID, err := hex.DecodeString(msg.SwapID)
if err != nil {
return nil, err
}
randomNumber, err := hex.DecodeString(msg.RandomNumber)
if err != nil {
return nil, err
}
err = k.keeper.ClaimAtomicSwap(ctx, from, swapID, randomNumber)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.From),
),
)
return &types.MsgClaimAtomicSwapResponse{}, nil
}
func (k msgServer) RefundAtomicSwap(goCtx context.Context, msg *types.MsgRefundAtomicSwap) (*types.MsgRefundAtomicSwapResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
swapID, err := hex.DecodeString(msg.SwapID)
if err != nil {
return nil, err
}
err = k.keeper.RefundAtomicSwap(ctx, from, swapID)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.From),
),
)
return &types.MsgRefundAtomicSwapResponse{}, nil
}