Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: msg multihop validation #ntrn-157 #357

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion proto/neutron/dex/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ message QueryEstimateMultiHopSwapRequest {
string exitLimitPrice = 5 [(gogoproto.moretags) = "yaml:\"exitLimitPrice\"", (gogoproto.customtype) = "github.com/neutron-org/neutron/utils/math.PrecDec", (gogoproto.nullable) = false, (gogoproto.jsontag) = "exitLimitPrice"];

// If pickBestRoute == true then all routes are run and the route with the best price is chosen
// otherwise, the first succesful route is used.
// otherwise, the first successful route is used.
bool pickBestRoute = 6;
}

Expand Down
2 changes: 1 addition & 1 deletion proto/neutron/dex/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ message MsgMultiHopSwap {
(gogoproto.jsontag) = "exitLimitPrice"
];
// If pickBestRoute == true then all routes are run and the route with the best price is chosen
// otherwise, the first succesful route is used.
// otherwise, the first successful route is used.
bool pickBestRoute = 6;
}

Expand Down
17 changes: 16 additions & 1 deletion x/dex/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,24 @@ var (
1149,
"Invalid Address",
)
ErrDuplicatePoolDeposit = sdkerrors.Register(
ErrRouteWithoutExitToken = sdkerrors.Register(
ModuleName,
1150,
"Each route should specify at least two hops - input and output tokens",
)
ErrCycleInHops = sdkerrors.Register(
ModuleName,
1151,
"Hops cannot have cycles",
)
ErrZeroExitPrice = sdkerrors.Register(
ModuleName,
1152,
"Cannot have negative or zero exit price",
)
ErrDuplicatePoolDeposit = sdkerrors.Register(
ModuleName,
1153,
"Can only provide a single deposit amount for each tick, fee pair",
)
)
23 changes: 21 additions & 2 deletions x/dex/types/message_multi_hop_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdkerrors "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

math_utils "github.com/neutron-org/neutron/utils/math"
)

Expand All @@ -13,7 +14,7 @@ var _ sdk.Msg = &MsgMultiHopSwap{}

func NewMsgMultiHopSwap(
creator string,
receiever string,
receiver string,
routesArr [][]string,
amountIn math.Int,
exitLimitPrice math_utils.PrecDec,
Expand All @@ -26,7 +27,7 @@ func NewMsgMultiHopSwap(

return &MsgMultiHopSwap{
Creator: creator,
Receiver: receiever,
Receiver: receiver,
Routes: routes,
AmountIn: amountIn,
ExitLimitPrice: exitLimitPrice,
Expand Down Expand Up @@ -70,6 +71,20 @@ func (msg *MsgMultiHopSwap) ValidateBasic() error {
return ErrMissingMultihopRoute
}

for _, route := range msg.Routes {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of a nitpick, but we're already iterating through the routes below, would be a bit cleaner to combine all checks into a single loop.

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
Expand All @@ -82,5 +97,9 @@ func (msg *MsgMultiHopSwap) ValidateBasic() error {
return ErrZeroSwap
}

if !msg.ExitLimitPrice.IsPositive() {
return ErrZeroExitPrice
}

return nil
}
68 changes: 59 additions & 9 deletions x/dex/types/message_multi_hop_swap_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package types_test

import (

Check failure on line 3 in x/dex/types/message_multi_hop_swap_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
math_utils "github.com/neutron-org/neutron/utils/math"
"testing"

Check failure on line 5 in x/dex/types/message_multi_hop_swap_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)

"cosmossdk.io/math"
"github.com/neutron-org/neutron/testutil/common/sample"
Expand All @@ -20,6 +21,10 @@
msg: MsgMultiHopSwap{
Creator: "invalid_address",
Receiver: sample.AccAddress(),
Routes: []*MultiHopRoute{
{Hops: []string{"A", "B", "C"}},
},
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("0.9"),
},
err: ErrInvalidAddress,
},
Expand All @@ -28,14 +33,20 @@
msg: MsgMultiHopSwap{
Creator: sample.AccAddress(),
Receiver: "invalid_address",
Routes: []*MultiHopRoute{
{Hops: []string{"A", "B", "C"}},
},
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("0.9"),
},
err: ErrInvalidAddress,
},
{
name: "missing route",
msg: MsgMultiHopSwap{
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
Routes: []*MultiHopRoute{},
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("0.9"),
},
err: ErrMissingMultihopRoute,
},
Expand All @@ -48,26 +59,65 @@
{Hops: []string{"A", "B", "C"}},
{Hops: []string{"A", "B", "Z"}},
},
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("0.9"),
},
err: ErrMultihopExitTokensMismatch,
},
{
name: "invalid amountIn",
msg: MsgMultiHopSwap{
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
Routes: []*MultiHopRoute{{Hops: []string{"A", "B", "C"}}},
AmountIn: math.NewInt(-1),
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
Routes: []*MultiHopRoute{{Hops: []string{"A", "B", "C"}}},
AmountIn: math.NewInt(-1),
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("0.9"),
},
err: ErrZeroSwap,
},
{
name: "valid",
name: "cycles in hops",
msg: MsgMultiHopSwap{
Routes: []*MultiHopRoute{{Hops: []string{"A", "B", "C"}}},
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
AmountIn: math.OneInt(),
Routes: []*MultiHopRoute{
{Hops: []string{"A", "B", "C"}}, // normal
{Hops: []string{"A", "B", "D", "E", "B", "C"}}, // has cycle
},
AmountIn: math.OneInt(),
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("0.9"),
},
err: ErrCycleInHops,
},
{
name: "zero exit limit price",
msg: MsgMultiHopSwap{
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
Routes: []*MultiHopRoute{{Hops: []string{"A", "B", "C"}}},
AmountIn: math.OneInt(),
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("0"),
},
err: ErrZeroExitPrice,
},
{
name: "negative exit limit price",
msg: MsgMultiHopSwap{
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
Routes: []*MultiHopRoute{{Hops: []string{"A", "B", "C"}}},
AmountIn: math.OneInt(),
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("-0.5"),
},
err: ErrZeroExitPrice,
},
{
name: "valid",
msg: MsgMultiHopSwap{
Creator: sample.AccAddress(),
Receiver: sample.AccAddress(),
Routes: []*MultiHopRoute{{Hops: []string{"A", "B", "C"}}},
AmountIn: math.OneInt(),
ExitLimitPrice: math_utils.MustNewPrecDecFromStr("0.9"),
},
},
}
Expand Down
Loading