Skip to content

Commit

Permalink
fix: repay flow errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Feb 9, 2024
1 parent 41d0fdc commit 6a81087
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
12 changes: 11 additions & 1 deletion x/perpetual/keeper/begin_blocker_process_mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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))
}
Expand Down
34 changes: 18 additions & 16 deletions x/perpetual/keeper/repay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6a81087

Please sign in to comment.