-
Notifications
You must be signed in to change notification settings - Fork 87
splitting perps liquidation in 2 parts #1414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1c1fb55
splitting perps liquidation in 2 parts
avkr003 78eb565
splitting perps liquidation in 2 parts
avkr003 1861c44
fixing for closing ratio on liquidation
avkr003 a31d942
fixing for closing ratio on liquidation
avkr003 aa22cf8
splitting liquidation in 2
avkr003 1110c60
splitting liquidation in 2
avkr003 cf4f7a6
Correcting minimum liabilities ratio check for perps
avkr003 b34e67c
Correcting minimum liabilities ratio check for perps
avkr003 26419c1
refund
avkr003 6f4d259
Merge branch 'main' into avkr003/liquidationSplit
avkr003 84cebe6
fixing for SUT
avkr003 3b5764a
Merge remote-tracking branch 'origin/avkr003/liquidationSplit' into a…
avkr003 a52bb57
updating queries
avkr003 64d8c5e
Merge branch 'main' into avkr003/liquidationSplit
avkr003 816a4ce
adding new price feeders
avkr003 dc602ca
Merge remote-tracking branch 'origin/avkr003/liquidationSplit' into a…
avkr003 f10d2ef
adding new price feeders
avkr003 920ec41
making first liqudiation closing ratio as param
avkr003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package keeper | ||
|
|
||
| import ( | ||
| errorsmod "cosmossdk.io/errors" | ||
| "cosmossdk.io/math" | ||
| "errors" | ||
| "fmt" | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| ammtypes "github.com/elys-network/elys/v7/x/amm/types" | ||
| assetprofiletypes "github.com/elys-network/elys/v7/x/assetprofile/types" | ||
| ptypes "github.com/elys-network/elys/v7/x/parameter/types" | ||
| "github.com/elys-network/elys/v7/x/perpetual/types" | ||
| ) | ||
|
|
||
| func (k Keeper) AddCollateral(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, collateral sdk.Coin, ammPool *ammtypes.Pool) (sdk.Coin, error) { | ||
| entry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency) | ||
| if !found { | ||
| return sdk.Coin{}, errorsmod.Wrapf(assetprofiletypes.ErrAssetProfileNotFound, "asset %s not found", ptypes.BaseCurrency) | ||
| } | ||
| baseCurrency := entry.Denom | ||
|
|
||
| if mtp.Position == types.Position_LONG && mtp.CollateralAsset == baseCurrency { | ||
| if collateral.Denom != mtp.CollateralAsset { | ||
| return sdk.Coin{}, errors.New("denom not same as collateral asset") | ||
| } | ||
|
|
||
| if collateral.Denom != mtp.LiabilitiesAsset { | ||
| return sdk.Coin{}, errors.New("denom not same as liabilities asset") | ||
| } | ||
|
|
||
| creator := mtp.GetAccountAddress() | ||
|
|
||
| // interest amount has been paid from custody | ||
| params := k.GetParams(ctx) | ||
| maxAmount := mtp.Liabilities.Sub(params.LongMinimumLiabilityAmount) | ||
| if !maxAmount.IsPositive() { | ||
| return sdk.Coin{}, fmt.Errorf("cannot reduce liabilties less than %s", params.LongMinimumLiabilityAmount.String()) | ||
| } | ||
|
|
||
| var finalAmount math.Int | ||
| if collateral.Amount.LT(maxAmount) { | ||
| finalAmount = collateral.Amount | ||
| } else { | ||
| finalAmount = maxAmount | ||
| } | ||
|
|
||
| mtp.Liabilities = mtp.Liabilities.Sub(finalAmount) | ||
| err := pool.UpdateLiabilities(mtp.LiabilitiesAsset, finalAmount, false, mtp.Position) | ||
| if err != nil { | ||
| return sdk.Coin{}, err | ||
| } | ||
|
|
||
| mtp.Collateral = mtp.Collateral.Add(finalAmount) | ||
| err = pool.UpdateCollateral(mtp.CollateralAsset, finalAmount, true, mtp.Position) | ||
| if err != nil { | ||
| return sdk.Coin{}, err | ||
| } | ||
|
|
||
| finalCollateralCoin := sdk.NewCoin(collateral.Denom, finalAmount) | ||
| err = k.SendToAmmPool(ctx, creator, ammPool, sdk.NewCoins(finalCollateralCoin)) | ||
| if err != nil { | ||
| return sdk.Coin{}, err | ||
| } | ||
|
|
||
| err = k.SetMTP(ctx, mtp) | ||
| if err != nil { | ||
| return sdk.Coin{}, err | ||
| } | ||
| k.SetPool(ctx, *pool) | ||
|
|
||
| if k.hooks != nil { | ||
| err = k.hooks.AfterPerpetualPositionModified(ctx, *ammPool, *pool, creator) | ||
| if err != nil { | ||
| return sdk.Coin{}, err | ||
| } | ||
| } | ||
| return finalCollateralCoin, nil | ||
|
|
||
| } else { | ||
| msgOpen := types.MsgOpen{ | ||
| Creator: mtp.Address, | ||
| Position: mtp.Position, | ||
| Leverage: math.LegacyZeroDec(), | ||
| Collateral: collateral, | ||
| TakeProfitPrice: math.LegacyZeroDec(), | ||
| StopLossPrice: math.LegacyZeroDec(), | ||
| PoolId: mtp.AmmPoolId, | ||
| } | ||
| if err := msgOpen.ValidateBasic(); err != nil { | ||
| return sdk.Coin{}, err | ||
| } | ||
| _, err := k.Open(ctx, &msgOpen) | ||
| if err != nil { | ||
| return sdk.Coin{}, err | ||
| } | ||
| return sdk.Coin{}, nil | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.