From 6a810873629e7c02b11cd56c0d466abc94342e0b Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Fri, 9 Feb 2024 05:35:09 +0100 Subject: [PATCH] fix: repay flow errors --- .../keeper/begin_blocker_process_mtp.go | 12 ++++++- x/perpetual/keeper/repay.go | 34 ++++++++++--------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/x/perpetual/keeper/begin_blocker_process_mtp.go b/x/perpetual/keeper/begin_blocker_process_mtp.go index 09f5a3e95..f5f09c970 100644 --- a/x/perpetual/keeper/begin_blocker_process_mtp.go +++ b/x/perpetual/keeper/begin_blocker_process_mtp.go @@ -4,11 +4,13 @@ import ( "fmt" "cosmossdk.io/errors" + errorsmod "cosmossdk.io/errors" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" ammtypes "github.com/elys-network/elys/x/amm/types" + assetprofiletypes "github.com/elys-network/elys/x/assetprofile/types" "github.com/elys-network/elys/x/perpetual/types" ) @@ -62,7 +64,15 @@ func BeginBlockerProcessMTP(ctx sdk.Context, k Keeper, mtp *types.MTP, pool type ctx.Logger().Info(errors.Wrap(types.ErrMTPHealthy, "skipping executing force close because mtp is healthy").Error()) } - assetPrice, err := k.EstimateSwap(ctx, sdk.NewCoin(mtp.CustodyAsset, sdk.OneInt()), baseCurrency, ammPool) + entry, found := k.assetProfileKeeper.GetEntryByDenom(ctx, mtp.CustodyAsset) + if !found { + ctx.Logger().Error(errorsmod.Wrapf(assetprofiletypes.ErrAssetProfileNotFound, "asset %s not found", mtp.CustodyAsset).Error()) + } + custodyAssetDecimals := entry.Decimals + + oneToken := math.NewIntFromBigInt(math.LegacyNewDec(10).Power(uint64(custodyAssetDecimals)).TruncateInt().BigInt()) + + assetPrice, err := k.EstimateSwap(ctx, sdk.NewCoin(mtp.CustodyAsset, oneToken), baseCurrency, ammPool) if err != nil { return errors.Wrap(err, fmt.Sprintf("error estimating swap: %s", mtp.CustodyAsset)) } diff --git a/x/perpetual/keeper/repay.go b/x/perpetual/keeper/repay.go index 1ddda6b48..d9bb7f0bb 100644 --- a/x/perpetual/keeper/repay.go +++ b/x/perpetual/keeper/repay.go @@ -13,24 +13,26 @@ func (k Keeper) Repay(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, ammPool Liabilities := mtp.Liabilities BorrowInterestUnpaid := mtp.BorrowInterestUnpaidCollateral - if mtp.Position == types.Position_SHORT { - // swap to trading asset - unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAsset, mtp.BorrowInterestUnpaidCollateral) - C, err := k.EstimateSwapGivenOut(ctx, unpaidCollateralIn, mtp.TradingAsset, ammPool) - if err != nil { - return err - } + if mtp.BorrowInterestUnpaidCollateral.IsPositive() { + if mtp.Position == types.Position_SHORT { + // swap to trading asset + unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAsset, mtp.BorrowInterestUnpaidCollateral) + C, err := k.EstimateSwapGivenOut(ctx, unpaidCollateralIn, mtp.TradingAsset, ammPool) + if err != nil { + return err + } - BorrowInterestUnpaid = C - } else if mtp.CollateralAsset != baseCurrency { - // swap to base currency - unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAsset, mtp.BorrowInterestUnpaidCollateral) - C, err := k.EstimateSwapGivenOut(ctx, unpaidCollateralIn, baseCurrency, ammPool) - if err != nil { - return err - } + BorrowInterestUnpaid = C + } else if mtp.CollateralAsset != baseCurrency { + // swap to base currency + unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAsset, mtp.BorrowInterestUnpaidCollateral) + C, err := k.EstimateSwapGivenOut(ctx, unpaidCollateralIn, baseCurrency, ammPool) + if err != nil { + return err + } - BorrowInterestUnpaid = C + BorrowInterestUnpaid = C + } } var err error