-
Notifications
You must be signed in to change notification settings - Fork 80
/
message_multi_hop_swap.go
105 lines (86 loc) · 2.27 KB
/
message_multi_hop_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
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
package types
import (
sdkerrors "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
math_utils "github.com/neutron-org/neutron/v2/utils/math"
)
const TypeMsgMultiHopSwap = "multi_hop_swap"
var _ sdk.Msg = &MsgMultiHopSwap{}
func NewMsgMultiHopSwap(
creator string,
receiver string,
routesArr [][]string,
amountIn math.Int,
exitLimitPrice math_utils.PrecDec,
pickBestRoute bool,
) *MsgMultiHopSwap {
routes := make([]*MultiHopRoute, len(routesArr))
for i, hops := range routesArr {
routes[i] = &MultiHopRoute{Hops: hops}
}
return &MsgMultiHopSwap{
Creator: creator,
Receiver: receiver,
Routes: routes,
AmountIn: amountIn,
ExitLimitPrice: exitLimitPrice,
PickBestRoute: pickBestRoute,
}
}
func (msg *MsgMultiHopSwap) Route() string {
return RouterKey
}
func (msg *MsgMultiHopSwap) Type() string {
return TypeMsgMultiHopSwap
}
func (msg *MsgMultiHopSwap) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgMultiHopSwap) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgMultiHopSwap) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(ErrInvalidAddress, "invalid creator address (%s)", err)
}
_, err = sdk.AccAddressFromBech32(msg.Receiver)
if err != nil {
return sdkerrors.Wrapf(ErrInvalidAddress, "invalid receiver address (%s)", err)
}
if len(msg.Routes) == 0 {
return ErrMissingMultihopRoute
}
for _, route := range msg.Routes {
if len(route.Hops) < 2 {
return ErrRouteWithoutExitToken
}
existingHops := make(map[string]bool, len(route.Hops))
for _, hop := range route.Hops {
if _, ok := existingHops[hop]; ok {
return ErrCycleInHops
}
existingHops[hop] = true
}
}
expectedExitToken := msg.Routes[0].Hops[len(msg.Routes[0].Hops)-1]
for _, route := range msg.Routes[1:] {
hops := route.Hops
if expectedExitToken != hops[len(hops)-1] {
return ErrMultihopExitTokensMismatch
}
}
if msg.AmountIn.LTE(math.ZeroInt()) {
return ErrZeroSwap
}
if !msg.ExitLimitPrice.IsPositive() {
return ErrZeroExitPrice
}
return nil
}