-
Notifications
You must be signed in to change notification settings - Fork 40
/
open_short_process.go
87 lines (69 loc) · 2.75 KB
/
open_short_process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package keeper
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/perpetual/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, baseCurrency string, isBroker bool) (*types.MTP, error) {
// Fetch the pool associated with the given pool ID.
pool, found := k.OpenShortChecker.GetPool(ctx, poolId)
if !found {
return nil, errorsmod.Wrap(types.ErrPoolDoesNotExist, mtp.TradingAsset)
}
// Check if the pool is enabled.
if !k.OpenShortChecker.IsPoolEnabled(ctx, poolId) {
return nil, errorsmod.Wrap(types.ErrMTPDisabled, mtp.TradingAsset)
}
// Fetch the corresponding AMM (Automated Market Maker) pool.
ammPool, err := k.OpenShortChecker.GetAmmPool(ctx, poolId, mtp.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 mtp.CollateralAsset != baseCurrency {
return nil, errorsmod.Wrap(types.ErrInvalidBorrowingAsset, "collateral must be base currency")
}
// Check minimum liabilities.
err = k.OpenShortChecker.CheckMinLiabilities(ctx, msg.Collateral, eta, ammPool, mtp.CustodyAsset, baseCurrency)
if err != nil {
return nil, err
}
// Define custody amount
custodyAmount := leveragedAmount
// check the balance
if !types.HasSufficientPoolBalance(ammPool, mtp.CustodyAsset, custodyAmount) {
return nil, errorsmod.Wrap(types.ErrBorrowTooHigh, custodyAmount.String())
}
// Borrow the asset the user wants to short.
err = k.OpenShortChecker.Borrow(ctx, msg.Collateral.Amount, custodyAmount, mtp, &ammPool, &pool, eta, baseCurrency, isBroker)
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, baseCurrency)
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, baseCurrency)
// Calculate consolidate liabiltiy and update consolidate leverage
mtp.ConsolidateLeverage = types.CalcMTPConsolidateLiability(mtp)
// Set MTP
k.OpenShortChecker.SetMTP(ctx, mtp)
// Return the updated Perpetual Trading Position (MTP).
return mtp, nil
}