Skip to content

Commit

Permalink
[Perpetual]: Remove take profit trigger from health function (#896)
Browse files Browse the repository at this point in the history
remove
  • Loading branch information
amityadav0 authored Nov 3, 2024
1 parent 9858fa3 commit a2bfd93
Showing 1 changed file with 16 additions and 50 deletions.
66 changes: 16 additions & 50 deletions x/perpetual/keeper/process_mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ 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 @@ -55,58 +53,26 @@ func (k Keeper) CheckAndLiquidateUnhealthyPosition(ctx sdk.Context, mtp *types.M

// check MTP health against threshold
safetyFactor := k.GetSafetyFactor(ctx)
var mustForceClose bool = false

if mtp.MtpHealth.LTE(safetyFactor) {
// flag position as must force close
mustForceClose = true
} else {
ctx.Logger().Debug(errors.Wrap(types.ErrMTPHealthy, "skipping executing force close because mtp is healthy").Error())
}

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.EstimateSwapGivenIn(ctx, sdk.NewCoin(mtp.CustodyAsset, oneToken), baseCurrency, ammPool)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error estimating swap: %s", mtp.CustodyAsset))
}

// divide assetPrice by 10^baseCurrencyDecimal to get the actual price in decimal
assetPriceDec := math.LegacyNewDecFromBigInt(assetPrice.BigInt()).Quo(math.LegacyNewDec(10).Power(uint64(baseCurrencyDecimal)))

if types.ReachedTakeProfitPrice(mtp, assetPriceDec) {
// flag position as must force close
mustForceClose = true
} else {
ctx.Logger().Debug(fmt.Sprintf("skipping force close on position %s because take profit price %s <> %s", mtp.String(), mtp.TakeProfitPrice.String(), assetPrice.String()))
}

// if flag is false, then skip force close
if !mustForceClose {
return nil
}

var repayAmount math.Int
switch mtp.Position {
case types.Position_LONG:
repayAmount, err = k.ForceCloseLong(ctx, mtp, &pool, true, baseCurrency)
case types.Position_SHORT:
repayAmount, err = k.ForceCloseShort(ctx, mtp, &pool, true, baseCurrency)
default:
return errors.Wrap(types.ErrInvalidPosition, fmt.Sprintf("invalid position type: %s", mtp.Position))
}
var repayAmount math.Int
switch mtp.Position {
case types.Position_LONG:
repayAmount, err = k.ForceCloseLong(ctx, mtp, &pool, true, baseCurrency)
case types.Position_SHORT:
repayAmount, err = k.ForceCloseShort(ctx, mtp, &pool, true, baseCurrency)
default:
return errors.Wrap(types.ErrInvalidPosition, fmt.Sprintf("invalid position type: %s", mtp.Position))
}

if err == nil {
// Emit event if position was closed
k.EmitForceClose(ctx, mtp, repayAmount, "")
if err == nil {
// Emit event if position was closed
k.EmitForceClose(ctx, mtp, repayAmount, "")
} else {
return errors.Wrap(err, "error executing force close")
}
} else {
return errors.Wrap(err, "error executing force close")
ctx.Logger().Debug(errors.Wrap(types.ErrMTPHealthy, "skipping executing force close because mtp is healthy").Error())
}

return nil
Expand Down

0 comments on commit a2bfd93

Please sign in to comment.