From ac9156a595076e50208c0cfd6c663d591e8c7506 Mon Sep 17 00:00:00 2001 From: pr0n00gler Date: Tue, 7 Nov 2023 19:25:14 +0200 Subject: [PATCH 1/3] small improvements --- x/dex/keeper/liquidity.go | 11 ++++++----- x/dex/keeper/liquidity_test.go | 7 +++---- x/dex/keeper/multihop_swap.go | 6 ++---- x/gmp/types.go | 2 -- x/incentives/types/stake.go | 9 +-------- 5 files changed, 12 insertions(+), 23 deletions(-) diff --git a/x/dex/keeper/liquidity.go b/x/dex/keeper/liquidity.go index a24ec9bfb..6bae05491 100644 --- a/x/dex/keeper/liquidity.go +++ b/x/dex/keeper/liquidity.go @@ -3,6 +3,7 @@ package keeper import ( "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" + math_utils "github.com/neutron-org/neutron/utils/math" "github.com/neutron-org/neutron/x/dex/types" ) @@ -13,7 +14,7 @@ func (k Keeper) Swap( maxAmountTakerDenom math.Int, maxAmountMakerDenom *math.Int, limitPrice *math_utils.PrecDec, -) (totalTakerCoin, totalMakerCoin sdk.Coin, orderFilled bool, err error) { +) (totalTakerCoin, totalMakerCoin sdk.Coin, orderFilled bool) { useMaxOut := maxAmountMakerDenom != nil var remainingMakerDenom *math.Int if useMaxOut { @@ -72,7 +73,7 @@ func (k Keeper) Swap( ), sdk.NewCoin( tradePairID.MakerDenom, totalMakerDenom, - ), orderFilled, nil + ), orderFilled } func (k Keeper) SwapWithCache( @@ -81,9 +82,9 @@ func (k Keeper) SwapWithCache( maxAmountIn math.Int, maxAmountOut *math.Int, limitPrice *math_utils.PrecDec, -) (totalIn, totalOut sdk.Coin, orderFilled bool, err error) { +) (totalIn, totalOut sdk.Coin, orderFilled bool) { cacheCtx, writeCache := ctx.CacheContext() - totalIn, totalOut, orderFilled, err = k.Swap( + totalIn, totalOut, orderFilled = k.Swap( cacheCtx, tradePairID, maxAmountIn, @@ -93,7 +94,7 @@ func (k Keeper) SwapWithCache( writeCache() - return totalIn, totalOut, orderFilled, err + return totalIn, totalOut, orderFilled } func (k Keeper) SaveLiquidity(sdkCtx sdk.Context, liquidityI types.Liquidity) { diff --git a/x/dex/keeper/liquidity_test.go b/x/dex/keeper/liquidity_test.go index 0cfdad2b7..1896dc28d 100644 --- a/x/dex/keeper/liquidity_test.go +++ b/x/dex/keeper/liquidity_test.go @@ -5,6 +5,7 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/neutron-org/neutron/x/dex/types" ) @@ -590,14 +591,13 @@ func (s *DexTestSuite) swap( ) (coinIn, coinOut sdk.Coin) { tradePairID, err := types.NewTradePairID(tokenIn, tokenOut) s.Assert().NoError(err) - coinIn, coinOut, _, err = s.App.DexKeeper.Swap( + coinIn, coinOut, _ = s.App.DexKeeper.Swap( s.Ctx, tradePairID, sdkmath.NewInt(maxAmountIn).Mul(denomMultiple), nil, nil, ) - s.Assert().NoError(err) return coinIn, coinOut } @@ -609,14 +609,13 @@ func (s *DexTestSuite) swapWithMaxOut( ) (coinIn, coinOut sdk.Coin) { tradePairID := types.MustNewTradePairID(tokenIn, tokenOut) maxAmountOutInt := sdkmath.NewInt(maxAmountOut).Mul(denomMultiple) - coinIn, coinOut, _, err := s.App.DexKeeper.Swap( + coinIn, coinOut, _ = s.App.DexKeeper.Swap( s.Ctx, tradePairID, sdkmath.NewInt(maxAmountIn).Mul(denomMultiple), &maxAmountOutInt, nil, ) - s.Assert().NoError(err) return coinIn, coinOut } diff --git a/x/dex/keeper/multihop_swap.go b/x/dex/keeper/multihop_swap.go index 656a76732..0704e83b5 100644 --- a/x/dex/keeper/multihop_swap.go +++ b/x/dex/keeper/multihop_swap.go @@ -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" "github.com/neutron-org/neutron/x/dex/types" ) @@ -143,16 +144,13 @@ func (k Keeper) SwapExactAmountIn(ctx sdk.Context, tradePairID *types.TradePairID, amountIn math.Int, ) (totalOut sdk.Coin, err error) { - _, swapAmountMakerDenom, orderFilled, err := k.Swap( + _, swapAmountMakerDenom, orderFilled := k.Swap( ctx, tradePairID, amountIn, nil, nil, ) - if err != nil { - return sdk.Coin{}, err - } if !orderFilled { return sdk.Coin{}, types.ErrInsufficientLiquidity } diff --git a/x/gmp/types.go b/x/gmp/types.go index ed497174e..63c371ff6 100644 --- a/x/gmp/types.go +++ b/x/gmp/types.go @@ -1,7 +1,5 @@ package gmp -const AxelarGMPAcc = "axelar1dv4u5k73pzqrxlzujxg3qp8kvc3pje7jtdvu72npnt5zhq05ejcsn5qme5" - // Message is attached in ICS20 packet memo field type Message struct { SourceChain string `json:"source_chain"` diff --git a/x/incentives/types/stake.go b/x/incentives/types/stake.go index 7d2507cbb..a830e885c 100644 --- a/x/incentives/types/stake.go +++ b/x/incentives/types/stake.go @@ -1,10 +1,10 @@ package types import ( - "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" + dextypes "github.com/neutron-org/neutron/x/dex/types" ) @@ -35,13 +35,6 @@ func (p Stake) OwnerAddress() sdk.AccAddress { return addr } -func (p Stake) SingleCoin() (sdk.Coin, error) { - if len(p.Coins) != 1 { - return sdk.Coin{}, fmt.Errorf("Stake %d has no single coin: %s", p.ID, p.Coins) - } - return p.Coins[0], nil -} - func (p Stake) ValidateBasic() error { for _, coin := range p.Coins { err := dextypes.ValidatePoolDenom(coin.Denom) From c6469edc863dcb4c3f1fcbc947c97c3563426526 Mon Sep 17 00:00:00 2001 From: pr0n00gler Date: Tue, 7 Nov 2023 19:38:36 +0200 Subject: [PATCH 2/3] fix core.go --- x/dex/keeper/core.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/x/dex/keeper/core.go b/x/dex/keeper/core.go index ebe804de0..cab8d8e05 100644 --- a/x/dex/keeper/core.go +++ b/x/dex/keeper/core.go @@ -7,6 +7,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" "github.com/neutron-org/neutron/x/dex/types" "github.com/neutron-org/neutron/x/dex/utils" @@ -330,16 +331,13 @@ func (k Keeper) PlaceLimitOrderCore( } var orderFilled bool - swapInCoin, swapOutCoin, orderFilled, err = k.SwapWithCache( + swapInCoin, swapOutCoin, orderFilled = k.SwapWithCache( ctx, takerTradePairID, amountIn, maxAmountOut, &limitPrice, ) - if err != nil { - return - } if orderType.IsFoK() && !orderFilled { err = types.ErrFoKLimitOrderNotFilled From 8afbca93a8b0a2759ea11ee11c75ed39b368d1bb Mon Sep 17 00:00:00 2001 From: pr0n00gler Date: Sat, 11 Nov 2023 13:18:56 +0200 Subject: [PATCH 3/3] revert returning an error in Swap() --- x/dex/keeper/core.go | 5 ++++- x/dex/keeper/liquidity.go | 10 +++++----- x/dex/keeper/liquidity_test.go | 6 ++++-- x/dex/keeper/multihop_swap.go | 5 ++++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/x/dex/keeper/core.go b/x/dex/keeper/core.go index cab8d8e05..ec7122a68 100644 --- a/x/dex/keeper/core.go +++ b/x/dex/keeper/core.go @@ -331,13 +331,16 @@ func (k Keeper) PlaceLimitOrderCore( } var orderFilled bool - swapInCoin, swapOutCoin, orderFilled = k.SwapWithCache( + swapInCoin, swapOutCoin, orderFilled, err = k.SwapWithCache( ctx, takerTradePairID, amountIn, maxAmountOut, &limitPrice, ) + if err != nil { + return + } if orderType.IsFoK() && !orderFilled { err = types.ErrFoKLimitOrderNotFilled diff --git a/x/dex/keeper/liquidity.go b/x/dex/keeper/liquidity.go index 6bae05491..1b86c504b 100644 --- a/x/dex/keeper/liquidity.go +++ b/x/dex/keeper/liquidity.go @@ -14,7 +14,7 @@ func (k Keeper) Swap( maxAmountTakerDenom math.Int, maxAmountMakerDenom *math.Int, limitPrice *math_utils.PrecDec, -) (totalTakerCoin, totalMakerCoin sdk.Coin, orderFilled bool) { +) (totalTakerCoin, totalMakerCoin sdk.Coin, orderFilled bool, err error) { useMaxOut := maxAmountMakerDenom != nil var remainingMakerDenom *math.Int if useMaxOut { @@ -73,7 +73,7 @@ func (k Keeper) Swap( ), sdk.NewCoin( tradePairID.MakerDenom, totalMakerDenom, - ), orderFilled + ), orderFilled, nil } func (k Keeper) SwapWithCache( @@ -82,9 +82,9 @@ func (k Keeper) SwapWithCache( maxAmountIn math.Int, maxAmountOut *math.Int, limitPrice *math_utils.PrecDec, -) (totalIn, totalOut sdk.Coin, orderFilled bool) { +) (totalIn, totalOut sdk.Coin, orderFilled bool, err error) { cacheCtx, writeCache := ctx.CacheContext() - totalIn, totalOut, orderFilled = k.Swap( + totalIn, totalOut, orderFilled, err = k.Swap( cacheCtx, tradePairID, maxAmountIn, @@ -94,7 +94,7 @@ func (k Keeper) SwapWithCache( writeCache() - return totalIn, totalOut, orderFilled + return totalIn, totalOut, orderFilled, err } func (k Keeper) SaveLiquidity(sdkCtx sdk.Context, liquidityI types.Liquidity) { diff --git a/x/dex/keeper/liquidity_test.go b/x/dex/keeper/liquidity_test.go index 1896dc28d..94d42ef68 100644 --- a/x/dex/keeper/liquidity_test.go +++ b/x/dex/keeper/liquidity_test.go @@ -591,13 +591,14 @@ func (s *DexTestSuite) swap( ) (coinIn, coinOut sdk.Coin) { tradePairID, err := types.NewTradePairID(tokenIn, tokenOut) s.Assert().NoError(err) - coinIn, coinOut, _ = s.App.DexKeeper.Swap( + coinIn, coinOut, _, err = s.App.DexKeeper.Swap( s.Ctx, tradePairID, sdkmath.NewInt(maxAmountIn).Mul(denomMultiple), nil, nil, ) + s.Assert().NoError(err) return coinIn, coinOut } @@ -609,13 +610,14 @@ func (s *DexTestSuite) swapWithMaxOut( ) (coinIn, coinOut sdk.Coin) { tradePairID := types.MustNewTradePairID(tokenIn, tokenOut) maxAmountOutInt := sdkmath.NewInt(maxAmountOut).Mul(denomMultiple) - coinIn, coinOut, _ = s.App.DexKeeper.Swap( + coinIn, coinOut, _, err := s.App.DexKeeper.Swap( s.Ctx, tradePairID, sdkmath.NewInt(maxAmountIn).Mul(denomMultiple), &maxAmountOutInt, nil, ) + s.Assert().NoError(err) return coinIn, coinOut } diff --git a/x/dex/keeper/multihop_swap.go b/x/dex/keeper/multihop_swap.go index 0704e83b5..a9acc89e4 100644 --- a/x/dex/keeper/multihop_swap.go +++ b/x/dex/keeper/multihop_swap.go @@ -144,13 +144,16 @@ func (k Keeper) SwapExactAmountIn(ctx sdk.Context, tradePairID *types.TradePairID, amountIn math.Int, ) (totalOut sdk.Coin, err error) { - _, swapAmountMakerDenom, orderFilled := k.Swap( + _, swapAmountMakerDenom, orderFilled, err := k.Swap( ctx, tradePairID, amountIn, nil, nil, ) + if err != nil { + return sdk.Coin{}, err + } if !orderFilled { return sdk.Coin{}, types.ErrInsufficientLiquidity }