-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding single perpetual trigger (#1105)
* Improving MTP triggers for funding fee and interest payments * fixing test cases * improving test coverage * Update x/perpetual/keeper/mtp_trigger_test.go Co-authored-by: Amit Yadav <[email protected]> * adding events for perpetual --------- Co-authored-by: Amit Yadav <[email protected]>
- Loading branch information
1 parent
9b5196f
commit 19837d3
Showing
42 changed files
with
776 additions
and
628 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,35 +1,46 @@ | ||
package keeper | ||
|
||
import ( | ||
errorsmod "cosmossdk.io/errors" | ||
sdkmath "cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
assetprofiletypes "github.com/elys-network/elys/x/assetprofile/types" | ||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
"github.com/elys-network/elys/x/perpetual/types" | ||
"strconv" | ||
) | ||
|
||
func (k Keeper) Close(ctx sdk.Context, msg *types.MsgClose) (*types.MsgCloseResponse, error) { | ||
entry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency) | ||
if !found { | ||
return nil, errorsmod.Wrapf(assetprofiletypes.ErrAssetProfileNotFound, "asset %s not found", ptypes.BaseCurrency) | ||
closedMtp, repayAmount, closingRatio, returnAmt, fundingFeeAmt, fundingAmtDistributed, interestAmt, insuranceAmt, allInterestsPaid, forceClosed, err := k.ClosePosition(ctx, msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
baseCurrency := entry.Denom | ||
|
||
closedMtp, repayAmount, closingRatio, err := k.ClosePosition(ctx, msg, baseCurrency) | ||
tradingAssetPrice, err := k.GetAssetPrice(ctx, closedMtp.TradingAsset) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Emit close event | ||
k.EmitCloseEvent(ctx, closedMtp, repayAmount, closingRatio) | ||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent(types.EventClose, | ||
sdk.NewAttribute("mtp_id", strconv.FormatInt(int64(closedMtp.Id), 10)), | ||
sdk.NewAttribute("owner", closedMtp.Address), | ||
sdk.NewAttribute("amm_pool_id", strconv.FormatInt(int64(closedMtp.AmmPoolId), 10)), | ||
sdk.NewAttribute("collateral_asset", closedMtp.CollateralAsset), | ||
sdk.NewAttribute("position", closedMtp.Position.String()), | ||
sdk.NewAttribute("mtp_health", closedMtp.MtpHealth.String()), // should be there if it's partial close | ||
sdk.NewAttribute("repay_amount", repayAmount.String()), | ||
sdk.NewAttribute("return_amount", returnAmt.String()), | ||
sdk.NewAttribute("funding_fee_amount", fundingFeeAmt.String()), | ||
sdk.NewAttribute("funding_amount_distributed", fundingAmtDistributed.String()), | ||
sdk.NewAttribute("interest_amount", interestAmt.String()), | ||
sdk.NewAttribute("insurance_amount", insuranceAmt.String()), | ||
sdk.NewAttribute("funding_fee_paid_custody", closedMtp.FundingFeePaidCustody.String()), | ||
sdk.NewAttribute("funding_fee_received_custody", closedMtp.FundingFeeReceivedCustody.String()), | ||
sdk.NewAttribute("closing_ratio", closingRatio.String()), | ||
sdk.NewAttribute("trading_asset_price", tradingAssetPrice.String()), | ||
sdk.NewAttribute("all_interests_paid", strconv.FormatBool(allInterestsPaid)), // Funding Fee is fully paid but interest amount is only partially paid then this will be false | ||
sdk.NewAttribute("force_closed", strconv.FormatBool(forceClosed)), | ||
)) | ||
|
||
return &types.MsgCloseResponse{ | ||
Id: closedMtp.Id, | ||
Amount: repayAmount, | ||
}, nil | ||
} | ||
|
||
func (k Keeper) EmitCloseEvent(ctx sdk.Context, mtp *types.MTP, repayAmount sdkmath.Int, closingRatio sdkmath.LegacyDec) { | ||
ctx.EventManager().EmitEvent(types.GenerateCloseEvent(mtp, repayAmount, closingRatio)) | ||
} |
This file contains 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 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 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 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 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,32 @@ | ||
package keeper | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
ammtypes "github.com/elys-network/elys/x/amm/types" | ||
"github.com/elys-network/elys/x/perpetual/types" | ||
) | ||
|
||
func (k Keeper) ForceClose(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, ammPool *ammtypes.Pool) (math.Int, math.Int, error) { | ||
repayAmount := math.ZeroInt() | ||
|
||
// Estimate swap and repay | ||
repayAmt, returnAmount, err := k.EstimateAndRepay(ctx, mtp, pool, ammPool, math.LegacyOneDec()) | ||
if err != nil { | ||
return math.ZeroInt(), math.ZeroInt(), err | ||
} | ||
|
||
repayAmount = repayAmount.Add(repayAmt) | ||
|
||
address := sdk.MustAccAddressFromBech32(mtp.Address) | ||
// EpochHooks after perpetual position closed | ||
if k.hooks != nil { | ||
params := k.GetParams(ctx) | ||
err = k.hooks.AfterPerpetualPositionClosed(ctx, *ammPool, *pool, address, params.EnableTakeProfitCustodyLiabilities) | ||
if err != nil { | ||
return math.Int{}, math.Int{}, err | ||
} | ||
} | ||
|
||
return repayAmt, returnAmount, nil | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.