Skip to content

Commit

Permalink
feat: add additional short flow logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Sep 20, 2023
1 parent f4b06af commit c173118
Show file tree
Hide file tree
Showing 5 changed files with 860 additions and 54 deletions.
2 changes: 1 addition & 1 deletion x/margin/keeper/open_long_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (k Keeper) ProcessOpenLong(ctx sdk.Context, mtp *types.MTP, leverage sdk.De
return nil, err
}
if !k.OpenLongChecker.HasSufficientPoolBalance(ctx, ammPool, ptypes.BaseCurrency, borrowingAmount) {
return nil, sdkerrors.Wrap(types.ErrBorrowTooHigh, leveragedAmount.String())
return nil, sdkerrors.Wrap(types.ErrBorrowTooHigh, borrowingAmount.String())
}
} else {
if !k.OpenLongChecker.HasSufficientPoolBalance(ctx, ammPool, msg.CollateralAsset, leveragedAmount) {
Expand Down
152 changes: 99 additions & 53 deletions x/margin/keeper/open_short_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,110 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/elys-network/elys/x/margin/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)

func (k Keeper) ProcessOpenShort(ctx sdk.Context, mtp *types.MTP, leverage sdk.Dec, eta sdk.Dec, collateralAmountDec sdk.Dec, poolId uint64, msg *types.MsgOpen) (*types.MTP, error) {
// Determine the trading asset.
// tradingAsset := k.OpenShortChecker.GetTradingAsset(msg.CollateralAsset, msg.BorrowAsset)

// // Fetch the pool associated with the given pool ID.
// pool, found := k.OpenShortChecker.GetPool(ctx, poolId)
// if !found {
// return nil, sdkerrors.Wrap(types.ErrPoolDoesNotExist, tradingAsset)
// }

// // Check if the pool is enabled.
// if !k.OpenShortChecker.IsPoolEnabled(ctx, poolId) {
// return nil, sdkerrors.Wrap(types.ErrMTPDisabled, tradingAsset)
// }

// // Fetch the corresponding AMM (Automated Market Maker) pool.
// ammPool, err := k.OpenShortChecker.GetAmmPool(ctx, poolId, tradingAsset)
// if err != nil {
// return nil, err
// }

// // Calculate the leveraged amount based on the collateral provided and the leverage.
// leveragedAmount := sdk.NewInt(collateralAmountDec.Mul(leverage).TruncateInt().Int64())

// // Borrow the asset the user wants to short.
// // err = k.OpenLongChecker.Borrow(ctx, msg.CollateralAsset, msg.BorrowAsset, msg.CollateralAmount, custodyAmount, mtp, &ammPool, &pool, eta)
// // if err != nil {
// // return nil, err
// // }

// // Calculate the custody amount.
// swappedAmount, err := k.OpenShortChecker.EstimateSwap(ctx, leveragedAmount, ptypes.BaseCurrency, ammPool)
// if err != nil {
// return nil, err
// }

// // Ensure the AMM pool has enough balance.
// if !k.OpenShortChecker.HasSufficientPoolBalance(ctx, ammPool, msg.BorrowAsset, swappedAmount) {
// return nil, sdkerrors.Wrap(types.ErrSwapTooHigh, swappedAmount.String())
// }

// // Additional checks and operations:
// // 1. Check minimum liabilities.
// err = k.OpenShortChecker.CheckMinLiabilities(ctx, swappedAmount, eta, pool, ammPool, msg.CollateralAsset)
// if err != nil {
// return nil, err
// }

// // 2. Update the pool and MTP health.
// if err = k.OpenShortChecker.UpdatePoolHealth(ctx, &pool); err != nil {
// return nil, err
// }
// if err = k.OpenShortChecker.UpdateMTPHealth(ctx, *mtp, ammPool); err != nil {
// return nil, err
// }
tradingAsset := k.OpenShortChecker.GetTradingAsset(msg.CollateralAsset, msg.BorrowAsset)

// Fetch the pool associated with the given pool ID.
pool, found := k.OpenShortChecker.GetPool(ctx, poolId)
if !found {
return nil, sdkerrors.Wrap(types.ErrPoolDoesNotExist, tradingAsset)
}

// Check if the pool is enabled.
if !k.OpenShortChecker.IsPoolEnabled(ctx, poolId) {
return nil, sdkerrors.Wrap(types.ErrMTPDisabled, tradingAsset)
}

// Fetch the corresponding AMM (Automated Market Maker) pool.
ammPool, err := k.OpenShortChecker.GetAmmPool(ctx, poolId, tradingAsset)
if err != nil {
return nil, err
}

// Calculate the leveraged amount based on the collateral provided and the leverage.
leveragedAmount := sdk.NewInt(collateralAmountDec.Mul(leverage).TruncateInt().Int64())

if msg.CollateralAsset != ptypes.BaseCurrency {
return nil, sdkerrors.Wrap(types.ErrInvalidBorrowingAsset, "collateral must be base currency")
}

custodyAmtToken := sdk.NewCoin(ptypes.BaseCurrency, leveragedAmount)
borrowingAmount, err := k.OpenShortChecker.EstimateSwapGivenOut(ctx, custodyAmtToken, msg.BorrowAsset, ammPool)
if err != nil {
return nil, err
}

// check the balance
if !k.OpenShortChecker.HasSufficientPoolBalance(ctx, ammPool, ptypes.BaseCurrency, borrowingAmount) {
return nil, sdkerrors.Wrap(types.ErrBorrowTooHigh, borrowingAmount.String())
}

// Check minimum liabilities.
collateralTokenAmt := sdk.NewCoin(msg.CollateralAsset, msg.CollateralAmount)
err = k.OpenShortChecker.CheckMinLiabilities(ctx, collateralTokenAmt, eta, pool, ammPool, msg.BorrowAsset)
if err != nil {
return nil, err
}

// Calculate custody amount.
leveragedAmtTokenIn := sdk.NewCoin(msg.BorrowAsset, borrowingAmount)
custodyAmount, err := k.OpenShortChecker.EstimateSwap(ctx, leveragedAmtTokenIn, ptypes.BaseCurrency, ammPool)
if err != nil {
return nil, err
}

// Ensure the AMM pool has enough balance.
if !k.OpenShortChecker.HasSufficientPoolBalance(ctx, ammPool, ptypes.BaseCurrency, custodyAmount) {
return nil, sdkerrors.Wrap(types.ErrCustodyTooHigh, custodyAmount.String())
}

// if position is short then override the custody asset to the base currency
if mtp.Position == types.Position_SHORT {
mtp.CustodyAssets = []string{ptypes.BaseCurrency}
}

// Borrow the asset the user wants to short.
err = k.OpenShortChecker.Borrow(ctx, msg.CollateralAsset, ptypes.BaseCurrency, msg.CollateralAmount, custodyAmount, mtp, &ammPool, &pool, eta)
if err != nil {
return nil, err
}

// Update the pool health.
if err = k.OpenShortChecker.UpdatePoolHealth(ctx, &pool); err != nil {
return nil, err
}

// Take custody from the pool balance.
if err = k.OpenShortChecker.TakeInCustody(ctx, *mtp, &pool); err != nil {
return nil, err
}

// Update the MTP health.
lr, err := k.OpenShortChecker.UpdateMTPHealth(ctx, *mtp, ammPool)
if err != nil {
return nil, err
}

// Check if the MTP is unhealthy
safetyFactor := k.OpenShortChecker.GetSafetyFactor(ctx)
if lr.LTE(safetyFactor) {
return nil, types.ErrMTPUnhealthy
}

// Update consolidated collateral amount
k.OpenShortChecker.CalcMTPConsolidateCollateral(ctx, mtp)

// Calculate consolidate liabiltiy
k.OpenShortChecker.CalcMTPConsolidateLiability(ctx, mtp)

// Set MTP
k.OpenShortChecker.SetMTP(ctx, mtp)

// Return the updated Margin Trading Position (MTP).
return mtp, nil
Expand Down
Loading

0 comments on commit c173118

Please sign in to comment.