From 064f0477c23d77490d8e48a5c5fe07e794d2a886 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Thu, 21 Dec 2023 09:42:32 +0100 Subject: [PATCH] fix: price impact calculation (#316) * fix: price impact calculation * test: fix * refactor: calc price impact only if decimals value provided --- x/amm/keeper/calc_swap_estimation_by_denom.go | 94 +++++++++++-------- .../calc_swap_estimation_by_denom_test.go | 12 ++- x/amm/keeper/msg_server_swap_by_denom.go | 3 +- x/amm/keeper/msg_server_swap_by_denom_test.go | 8 ++ .../keeper/query_swap_estimation_by_denom.go | 10 +- x/amm/types/expected_keepers.go | 2 + x/assetprofile/keeper/entry.go | 18 ++++ x/margin/keeper/query_open_estimation.go | 3 +- x/margin/types/expected_keepers.go | 3 + x/margin/types/mocks/amm_keeper.go | 61 ++++++------ x/margin/types/mocks/asset_profile_keeper.go | 53 +++++++++++ 11 files changed, 191 insertions(+), 76 deletions(-) diff --git a/x/amm/keeper/calc_swap_estimation_by_denom.go b/x/amm/keeper/calc_swap_estimation_by_denom.go index f0210db30..5810de7a5 100644 --- a/x/amm/keeper/calc_swap_estimation_by_denom.go +++ b/x/amm/keeper/calc_swap_estimation_by_denom.go @@ -1,14 +1,12 @@ package keeper import ( + "math" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/elys-network/elys/x/amm/types" ) -const ( - lowestAmountForInitialSpotPriceCalc = 10 // lowest amount to use for initial spot price calculation -) - // CalcSwapEstimationByDenom calculates the swap estimation by denom func (k Keeper) CalcSwapEstimationByDenom( ctx sdk.Context, @@ -18,6 +16,7 @@ func (k Keeper) CalcSwapEstimationByDenom( baseCurrency string, discount sdk.Dec, overrideSwapFee sdk.Dec, + decimals uint64, ) ( inRoute []*types.SwapAmountInRoute, outRoute []*types.SwapAmountOutRoute, @@ -30,50 +29,65 @@ func (k Keeper) CalcSwapEstimationByDenom( priceImpact sdk.Dec, err error, ) { - // if amount denom is equal to denomIn, calculate swap estimation by denomIn + var ( + initialSpotPrice sdk.Dec + ) + + // Initialize return variables + inRoute, outRoute = nil, nil + outAmount, availableLiquidity = sdk.Coin{}, sdk.Coin{} + spotPrice, swapFeeOut, discountOut, weightBonus, priceImpact = sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec() + + // Determine the correct route based on the amount's denom if amount.Denom == denomIn { - inRoute, err := k.CalcInRouteByDenom(ctx, denomIn, denomOut, baseCurrency) - if err != nil { - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), err - } - initialSpotPrice, _, _, _, _, _, err := k.CalcInRouteSpotPrice(ctx, sdk.NewInt64Coin(amount.Denom, lowestAmountForInitialSpotPriceCalc), inRoute, discount, overrideSwapFee) - if err != nil { - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), err - } - // check if initialSpotPrice is zero to avoid division by zero - if initialSpotPrice.IsZero() { - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), types.ErrInitialSpotPriceIsZero - } - spotPrice, tokenOut, swapFeeOut, _, availableLiquidity, weightBonus, err := k.CalcInRouteSpotPrice(ctx, amount, inRoute, discount, overrideSwapFee) - if err != nil { - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), err - } - priceImpact := initialSpotPrice.Sub(spotPrice).Quo(initialSpotPrice) - return inRoute, nil, tokenOut, spotPrice, swapFeeOut, discount, availableLiquidity, weightBonus, priceImpact, nil + inRoute, err = k.CalcInRouteByDenom(ctx, denomIn, denomOut, baseCurrency) + } else if amount.Denom == denomOut { + outRoute, err = k.CalcOutRouteByDenom(ctx, denomOut, denomIn, baseCurrency) + } else { + err = types.ErrInvalidDenom + return } - // if amount denom is equal to denomOut, calculate swap estimation by denomOut - if amount.Denom == denomOut { - outRoute, err := k.CalcOutRouteByDenom(ctx, denomOut, denomIn, baseCurrency) - if err != nil { - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), err + if err != nil { + return + } + + // Calculate initial spot price and price impact if decimals is not zero + if decimals != 0 { + lowestAmountForInitialSpotPriceCalc := int64(math.Pow10(int(decimals))) + initialCoin := sdk.NewInt64Coin(amount.Denom, lowestAmountForInitialSpotPriceCalc) + + if amount.Denom == denomIn { + initialSpotPrice, _, _, _, _, _, err = k.CalcInRouteSpotPrice(ctx, initialCoin, inRoute, discount, overrideSwapFee) + } else { + initialSpotPrice, _, _, _, _, _, err = k.CalcOutRouteSpotPrice(ctx, initialCoin, outRoute, discount, overrideSwapFee) } - initialSpotPrice, _, _, _, _, _, err := k.CalcOutRouteSpotPrice(ctx, sdk.NewInt64Coin(amount.Denom, lowestAmountForInitialSpotPriceCalc), outRoute, discount, overrideSwapFee) + if err != nil { - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), err + return } - // check if initialSpotPrice is zero to avoid division by zero if initialSpotPrice.IsZero() { - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), types.ErrInitialSpotPriceIsZero + err = types.ErrInitialSpotPriceIsZero + return } - spotPrice, tokenIn, swapFeeOut, _, availableLiquidity, weightBonus, err := k.CalcOutRouteSpotPrice(ctx, amount, outRoute, discount, overrideSwapFee) - if err != nil { - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), err - } - priceImpact := initialSpotPrice.Sub(spotPrice).Quo(initialSpotPrice) - return nil, outRoute, tokenIn, spotPrice, swapFeeOut, discount, availableLiquidity, weightBonus, priceImpact, nil } - // if amount denom is neither equal to denomIn nor denomOut, return error - return nil, nil, sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), types.ErrInvalidDenom + // Calculate final spot price and other outputs + if amount.Denom == denomIn { + spotPrice, outAmount, swapFeeOut, _, availableLiquidity, weightBonus, err = k.CalcInRouteSpotPrice(ctx, amount, inRoute, discount, overrideSwapFee) + } else { + spotPrice, outAmount, swapFeeOut, _, availableLiquidity, weightBonus, err = k.CalcOutRouteSpotPrice(ctx, amount, outRoute, discount, overrideSwapFee) + } + + if err != nil { + return + } + + // Calculate price impact if decimals is not zero + if decimals != 0 { + priceImpact = initialSpotPrice.Sub(spotPrice).Quo(initialSpotPrice) + } + + // Return the calculated values + return } diff --git a/x/amm/keeper/calc_swap_estimation_by_denom_test.go b/x/amm/keeper/calc_swap_estimation_by_denom_test.go index 33b92e4fc..33cc5e7a7 100644 --- a/x/amm/keeper/calc_swap_estimation_by_denom_test.go +++ b/x/amm/keeper/calc_swap_estimation_by_denom_test.go @@ -97,7 +97,9 @@ func (suite *KeeperTestSuite) TestCalcSwapEstimationByDenom() { amount, ptypes.Elys, "uusda", ptypes.BaseCurrency, sdk.ZeroDec(), - sdk.ZeroDec()) + sdk.ZeroDec(), + 1, + ) suite.Require().NoError(err) suite.Require().NotNil(inRoute) suite.Require().Nil(outRoute) @@ -110,7 +112,9 @@ func (suite *KeeperTestSuite) TestCalcSwapEstimationByDenom() { amount, ptypes.Elys, "uusda", ptypes.BaseCurrency, sdk.ZeroDec(), - sdk.ZeroDec()) + sdk.ZeroDec(), + 1, + ) suite.Require().NoError(err) suite.Require().Nil(inRoute) suite.Require().NotNil(outRoute) @@ -123,6 +127,8 @@ func (suite *KeeperTestSuite) TestCalcSwapEstimationByDenom() { suite.ctx, amount, ptypes.Elys, "uusda", ptypes.BaseCurrency, sdk.ZeroDec(), - sdk.ZeroDec()) + sdk.ZeroDec(), + 1, + ) suite.Require().Error(err) } diff --git a/x/amm/keeper/msg_server_swap_by_denom.go b/x/amm/keeper/msg_server_swap_by_denom.go index f57f870a9..0b6abb2bc 100644 --- a/x/amm/keeper/msg_server_swap_by_denom.go +++ b/x/amm/keeper/msg_server_swap_by_denom.go @@ -18,13 +18,14 @@ func (k msgServer) SwapByDenom(goCtx context.Context, msg *types.MsgSwapByDenom) return nil, err } + // retrieve base currency denom entry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency) if !found { return nil, errorsmod.Wrapf(assetprofiletypes.ErrAssetProfileNotFound, "asset %s not found", ptypes.BaseCurrency) } baseCurrency := entry.Denom - inRoute, outRoute, _, spotPrice, _, _, _, _, _, err := k.CalcSwapEstimationByDenom(ctx, msg.Amount, msg.DenomIn, msg.DenomOut, baseCurrency, msg.Discount, sdk.ZeroDec()) + inRoute, outRoute, _, spotPrice, _, _, _, _, _, err := k.CalcSwapEstimationByDenom(ctx, msg.Amount, msg.DenomIn, msg.DenomOut, baseCurrency, msg.Discount, sdk.ZeroDec(), 0) if err != nil { return nil, err } diff --git a/x/amm/keeper/msg_server_swap_by_denom_test.go b/x/amm/keeper/msg_server_swap_by_denom_test.go index 9db124646..cbd08ad9e 100644 --- a/x/amm/keeper/msg_server_swap_by_denom_test.go +++ b/x/amm/keeper/msg_server_swap_by_denom_test.go @@ -6,6 +6,7 @@ import ( minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/elys-network/elys/x/amm/keeper" "github.com/elys-network/elys/x/amm/types" + assetprofiletypes "github.com/elys-network/elys/x/assetprofile/types" ptypes "github.com/elys-network/elys/x/parameter/types" ) @@ -54,6 +55,13 @@ func (suite *KeeperTestSuite) TestMsgServerSwapByDenom() { suite.Run(tc.desc, func() { suite.SetupTest() + // set asset profile + suite.app.AssetprofileKeeper.SetEntry(suite.ctx, assetprofiletypes.Entry{ + BaseDenom: ptypes.Elys, + Denom: ptypes.Elys, + Decimals: 6, + }) + // bootstrap accounts sender := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) poolAddr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) diff --git a/x/amm/keeper/query_swap_estimation_by_denom.go b/x/amm/keeper/query_swap_estimation_by_denom.go index dad788624..3c21012c4 100644 --- a/x/amm/keeper/query_swap_estimation_by_denom.go +++ b/x/amm/keeper/query_swap_estimation_by_denom.go @@ -19,15 +19,23 @@ func (k Keeper) SwapEstimationByDenom(goCtx context.Context, req *types.QuerySwa ctx := sdk.UnwrapSDKContext(goCtx) + // retrieve base currency denom entry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency) if !found { return nil, errorsmod.Wrapf(assetprofiletypes.ErrAssetProfileNotFound, "asset %s not found", ptypes.BaseCurrency) } baseCurrency := entry.Denom + // retrieve denom in decimals + entry, found = k.assetProfileKeeper.GetEntryByDenom(ctx, req.DenomIn) + if !found { + return nil, errorsmod.Wrapf(assetprofiletypes.ErrAssetProfileNotFound, "asset %s not found", req.DenomIn) + } + decimals := entry.Decimals + _ = baseCurrency - inRoute, outRoute, amount, spotPrice, swapFee, discount, availableLiquidity, weightBonus, priceImpact, err := k.CalcSwapEstimationByDenom(ctx, req.Amount, req.DenomIn, req.DenomOut, baseCurrency, req.Discount, sdk.ZeroDec()) + inRoute, outRoute, amount, spotPrice, swapFee, discount, availableLiquidity, weightBonus, priceImpact, err := k.CalcSwapEstimationByDenom(ctx, req.Amount, req.DenomIn, req.DenomOut, baseCurrency, req.Discount, sdk.ZeroDec(), decimals) if err != nil { return nil, err } diff --git a/x/amm/types/expected_keepers.go b/x/amm/types/expected_keepers.go index f3517c796..63917c2c1 100644 --- a/x/amm/types/expected_keepers.go +++ b/x/amm/types/expected_keepers.go @@ -44,6 +44,8 @@ type AssetProfileKeeper interface { SetEntry(ctx sdk.Context, entry atypes.Entry) // GetEntry returns a entry from its index GetEntry(ctx sdk.Context, baseDenom string) (val atypes.Entry, found bool) + // GetEntryByDenom returns a entry from its denom value + GetEntryByDenom(ctx sdk.Context, denom string) (val atypes.Entry, found bool) } // AccountedPoolKeeper defines the expected interfaces diff --git a/x/assetprofile/keeper/entry.go b/x/assetprofile/keeper/entry.go index 6aeb35d9e..beabd8f5a 100644 --- a/x/assetprofile/keeper/entry.go +++ b/x/assetprofile/keeper/entry.go @@ -25,6 +25,24 @@ func (k Keeper) GetEntry(ctx sdk.Context, baseDenom string) (val types.Entry, fo return val, true } +// GetEntryByDenom returns a entry from its denom value +func (k Keeper) GetEntryByDenom(ctx sdk.Context, denom string) (val types.Entry, found bool) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.EntryKeyPrefix)) + iterator := sdk.KVStorePrefixIterator(store, []byte{}) + + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + var val types.Entry + k.cdc.MustUnmarshal(iterator.Value(), &val) + if val.Denom == denom { + return val, true + } + } + + return types.Entry{}, false +} + // RemoveEntry removes a entry from the store func (k Keeper) RemoveEntry(ctx sdk.Context, baseDenom string) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.EntryKeyPrefix)) diff --git a/x/margin/keeper/query_open_estimation.go b/x/margin/keeper/query_open_estimation.go index a3cda88f5..37f67672c 100644 --- a/x/margin/keeper/query_open_estimation.go +++ b/x/margin/keeper/query_open_estimation.go @@ -28,6 +28,7 @@ func (k Keeper) OpenEstimation(goCtx context.Context, req *types.QueryOpenEstima // get swap fee param swapFee := k.GetSwapFee(ctx) + // retrieve base currency denom entry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency) if !found { return nil, errorsmod.Wrapf(assetprofiletypes.ErrAssetProfileNotFound, "asset %s not found", ptypes.BaseCurrency) @@ -37,7 +38,7 @@ func (k Keeper) OpenEstimation(goCtx context.Context, req *types.QueryOpenEstima leveragedAmount := sdk.NewDecFromBigInt(req.Collateral.Amount.BigInt()).Mul(req.Leverage).TruncateInt() leveragedCoin := sdk.NewCoin(req.Collateral.Denom, leveragedAmount) - _, _, positionSize, openPrice, swapFee, discount, availableLiquidity, _, _, err := k.amm.CalcSwapEstimationByDenom(ctx, leveragedCoin, req.Collateral.Denom, req.TradingAsset, baseCurrency, req.Discount, swapFee) + _, _, positionSize, openPrice, swapFee, discount, availableLiquidity, _, _, err := k.amm.CalcSwapEstimationByDenom(ctx, leveragedCoin, req.Collateral.Denom, req.TradingAsset, baseCurrency, req.Discount, swapFee, 0) if err != nil { return nil, err } diff --git a/x/margin/types/expected_keepers.go b/x/margin/types/expected_keepers.go index 4399addb4..ab44b99eb 100644 --- a/x/margin/types/expected_keepers.go +++ b/x/margin/types/expected_keepers.go @@ -144,6 +144,7 @@ type AmmKeeper interface { baseCurrency string, discount sdk.Dec, overrideSwapFee sdk.Dec, + decimals uint64, ) ( inRoute []*ammtypes.SwapAmountInRoute, outRoute []*ammtypes.SwapAmountOutRoute, @@ -182,4 +183,6 @@ type BankKeeper interface { type AssetProfileKeeper interface { // GetEntry returns a entry from its index GetEntry(ctx sdk.Context, baseDenom string) (val atypes.Entry, found bool) + // GetEntryByDenom returns a entry from its denom value + GetEntryByDenom(ctx sdk.Context, denom string) (val atypes.Entry, found bool) } diff --git a/x/margin/types/mocks/amm_keeper.go b/x/margin/types/mocks/amm_keeper.go index 4c8e85b07..e7437adad 100644 --- a/x/margin/types/mocks/amm_keeper.go +++ b/x/margin/types/mocks/amm_keeper.go @@ -141,9 +141,9 @@ func (_c *AmmKeeper_CalcOutAmtGivenIn_Call) RunAndReturn(run func(types.Context, return _c } -// CalcSwapEstimationByDenom provides a mock function with given fields: ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee -func (_m *AmmKeeper) CalcSwapEstimationByDenom(ctx types.Context, amount types.Coin, denomIn string, denomOut string, baseCurrency string, discount math.LegacyDec, overrideSwapFee math.LegacyDec) ([]*ammtypes.SwapAmountInRoute, []*ammtypes.SwapAmountOutRoute, types.Coin, math.LegacyDec, math.LegacyDec, math.LegacyDec, types.Coin, math.LegacyDec, math.LegacyDec, error) { - ret := _m.Called(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) +// CalcSwapEstimationByDenom provides a mock function with given fields: ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals +func (_m *AmmKeeper) CalcSwapEstimationByDenom(ctx types.Context, amount types.Coin, denomIn string, denomOut string, baseCurrency string, discount math.LegacyDec, overrideSwapFee math.LegacyDec, decimals uint64) ([]*ammtypes.SwapAmountInRoute, []*ammtypes.SwapAmountOutRoute, types.Coin, math.LegacyDec, math.LegacyDec, math.LegacyDec, types.Coin, math.LegacyDec, math.LegacyDec, error) { + ret := _m.Called(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) var r0 []*ammtypes.SwapAmountInRoute var r1 []*ammtypes.SwapAmountOutRoute @@ -155,69 +155,69 @@ func (_m *AmmKeeper) CalcSwapEstimationByDenom(ctx types.Context, amount types.C var r7 math.LegacyDec var r8 math.LegacyDec var r9 error - if rf, ok := ret.Get(0).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) ([]*ammtypes.SwapAmountInRoute, []*ammtypes.SwapAmountOutRoute, types.Coin, math.LegacyDec, math.LegacyDec, math.LegacyDec, types.Coin, math.LegacyDec, math.LegacyDec, error)); ok { - return rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(0).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) ([]*ammtypes.SwapAmountInRoute, []*ammtypes.SwapAmountOutRoute, types.Coin, math.LegacyDec, math.LegacyDec, math.LegacyDec, types.Coin, math.LegacyDec, math.LegacyDec, error)); ok { + return rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } - if rf, ok := ret.Get(0).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) []*ammtypes.SwapAmountInRoute); ok { - r0 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(0).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) []*ammtypes.SwapAmountInRoute); ok { + r0 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]*ammtypes.SwapAmountInRoute) } } - if rf, ok := ret.Get(1).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) []*ammtypes.SwapAmountOutRoute); ok { - r1 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(1).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) []*ammtypes.SwapAmountOutRoute); ok { + r1 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { if ret.Get(1) != nil { r1 = ret.Get(1).([]*ammtypes.SwapAmountOutRoute) } } - if rf, ok := ret.Get(2).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) types.Coin); ok { - r2 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(2).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) types.Coin); ok { + r2 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { r2 = ret.Get(2).(types.Coin) } - if rf, ok := ret.Get(3).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) math.LegacyDec); ok { - r3 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(3).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) math.LegacyDec); ok { + r3 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { r3 = ret.Get(3).(math.LegacyDec) } - if rf, ok := ret.Get(4).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) math.LegacyDec); ok { - r4 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(4).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) math.LegacyDec); ok { + r4 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { r4 = ret.Get(4).(math.LegacyDec) } - if rf, ok := ret.Get(5).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) math.LegacyDec); ok { - r5 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(5).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) math.LegacyDec); ok { + r5 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { r5 = ret.Get(5).(math.LegacyDec) } - if rf, ok := ret.Get(6).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) types.Coin); ok { - r6 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(6).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) types.Coin); ok { + r6 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { r6 = ret.Get(6).(types.Coin) } - if rf, ok := ret.Get(7).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) math.LegacyDec); ok { - r7 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(7).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) math.LegacyDec); ok { + r7 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { r7 = ret.Get(7).(math.LegacyDec) } - if rf, ok := ret.Get(8).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) math.LegacyDec); ok { - r8 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(8).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) math.LegacyDec); ok { + r8 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { r8 = ret.Get(8).(math.LegacyDec) } - if rf, ok := ret.Get(9).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) error); ok { - r9 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee) + if rf, ok := ret.Get(9).(func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) error); ok { + r9 = rf(ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals) } else { r9 = ret.Error(9) } @@ -238,13 +238,14 @@ type AmmKeeper_CalcSwapEstimationByDenom_Call struct { // - baseCurrency string // - discount math.LegacyDec // - overrideSwapFee math.LegacyDec -func (_e *AmmKeeper_Expecter) CalcSwapEstimationByDenom(ctx interface{}, amount interface{}, denomIn interface{}, denomOut interface{}, baseCurrency interface{}, discount interface{}, overrideSwapFee interface{}) *AmmKeeper_CalcSwapEstimationByDenom_Call { - return &AmmKeeper_CalcSwapEstimationByDenom_Call{Call: _e.mock.On("CalcSwapEstimationByDenom", ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee)} +// - decimals uint64 +func (_e *AmmKeeper_Expecter) CalcSwapEstimationByDenom(ctx interface{}, amount interface{}, denomIn interface{}, denomOut interface{}, baseCurrency interface{}, discount interface{}, overrideSwapFee interface{}, decimals interface{}) *AmmKeeper_CalcSwapEstimationByDenom_Call { + return &AmmKeeper_CalcSwapEstimationByDenom_Call{Call: _e.mock.On("CalcSwapEstimationByDenom", ctx, amount, denomIn, denomOut, baseCurrency, discount, overrideSwapFee, decimals)} } -func (_c *AmmKeeper_CalcSwapEstimationByDenom_Call) Run(run func(ctx types.Context, amount types.Coin, denomIn string, denomOut string, baseCurrency string, discount math.LegacyDec, overrideSwapFee math.LegacyDec)) *AmmKeeper_CalcSwapEstimationByDenom_Call { +func (_c *AmmKeeper_CalcSwapEstimationByDenom_Call) Run(run func(ctx types.Context, amount types.Coin, denomIn string, denomOut string, baseCurrency string, discount math.LegacyDec, overrideSwapFee math.LegacyDec, decimals uint64)) *AmmKeeper_CalcSwapEstimationByDenom_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.Context), args[1].(types.Coin), args[2].(string), args[3].(string), args[4].(string), args[5].(math.LegacyDec), args[6].(math.LegacyDec)) + run(args[0].(types.Context), args[1].(types.Coin), args[2].(string), args[3].(string), args[4].(string), args[5].(math.LegacyDec), args[6].(math.LegacyDec), args[7].(uint64)) }) return _c } @@ -254,7 +255,7 @@ func (_c *AmmKeeper_CalcSwapEstimationByDenom_Call) Return(inRoute []*ammtypes.S return _c } -func (_c *AmmKeeper_CalcSwapEstimationByDenom_Call) RunAndReturn(run func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec) ([]*ammtypes.SwapAmountInRoute, []*ammtypes.SwapAmountOutRoute, types.Coin, math.LegacyDec, math.LegacyDec, math.LegacyDec, types.Coin, math.LegacyDec, math.LegacyDec, error)) *AmmKeeper_CalcSwapEstimationByDenom_Call { +func (_c *AmmKeeper_CalcSwapEstimationByDenom_Call) RunAndReturn(run func(types.Context, types.Coin, string, string, string, math.LegacyDec, math.LegacyDec, uint64) ([]*ammtypes.SwapAmountInRoute, []*ammtypes.SwapAmountOutRoute, types.Coin, math.LegacyDec, math.LegacyDec, math.LegacyDec, types.Coin, math.LegacyDec, math.LegacyDec, error)) *AmmKeeper_CalcSwapEstimationByDenom_Call { _c.Call.Return(run) return _c } diff --git a/x/margin/types/mocks/asset_profile_keeper.go b/x/margin/types/mocks/asset_profile_keeper.go index 3a9a34852..45841b368 100644 --- a/x/margin/types/mocks/asset_profile_keeper.go +++ b/x/margin/types/mocks/asset_profile_keeper.go @@ -76,6 +76,59 @@ func (_c *AssetProfileKeeper_GetEntry_Call) RunAndReturn(run func(types.Context, return _c } +// GetEntryByDenom provides a mock function with given fields: ctx, denom +func (_m *AssetProfileKeeper) GetEntryByDenom(ctx types.Context, denom string) (assetprofiletypes.Entry, bool) { + ret := _m.Called(ctx, denom) + + var r0 assetprofiletypes.Entry + var r1 bool + if rf, ok := ret.Get(0).(func(types.Context, string) (assetprofiletypes.Entry, bool)); ok { + return rf(ctx, denom) + } + if rf, ok := ret.Get(0).(func(types.Context, string) assetprofiletypes.Entry); ok { + r0 = rf(ctx, denom) + } else { + r0 = ret.Get(0).(assetprofiletypes.Entry) + } + + if rf, ok := ret.Get(1).(func(types.Context, string) bool); ok { + r1 = rf(ctx, denom) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// AssetProfileKeeper_GetEntryByDenom_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetEntryByDenom' +type AssetProfileKeeper_GetEntryByDenom_Call struct { + *mock.Call +} + +// GetEntryByDenom is a helper method to define mock.On call +// - ctx types.Context +// - denom string +func (_e *AssetProfileKeeper_Expecter) GetEntryByDenom(ctx interface{}, denom interface{}) *AssetProfileKeeper_GetEntryByDenom_Call { + return &AssetProfileKeeper_GetEntryByDenom_Call{Call: _e.mock.On("GetEntryByDenom", ctx, denom)} +} + +func (_c *AssetProfileKeeper_GetEntryByDenom_Call) Run(run func(ctx types.Context, denom string)) *AssetProfileKeeper_GetEntryByDenom_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(string)) + }) + return _c +} + +func (_c *AssetProfileKeeper_GetEntryByDenom_Call) Return(val assetprofiletypes.Entry, found bool) *AssetProfileKeeper_GetEntryByDenom_Call { + _c.Call.Return(val, found) + return _c +} + +func (_c *AssetProfileKeeper_GetEntryByDenom_Call) RunAndReturn(run func(types.Context, string) (assetprofiletypes.Entry, bool)) *AssetProfileKeeper_GetEntryByDenom_Call { + _c.Call.Return(run) + return _c +} + // NewAssetProfileKeeper creates a new instance of AssetProfileKeeper. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewAssetProfileKeeper(t interface {