-
Notifications
You must be signed in to change notification settings - Fork 44
/
open_consolidate.go
60 lines (49 loc) · 1.66 KB
/
open_consolidate.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
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) OpenConsolidate(ctx sdk.Context, existingMtp *types.MTP, newMtp *types.MTP, msg *types.MsgOpen, baseCurrency string) (*types.MsgOpenResponse, error) {
if !existingMtp.Leverage.Equal(msg.Leverage) {
return nil, types.ErrInvalidLeverage
}
poolId := existingMtp.AmmPoolId
pool, found := k.OpenLongChecker.GetPool(ctx, poolId)
if !found {
return nil, errorsmod.Wrap(types.ErrPoolDoesNotExist, newMtp.CustodyAsset)
}
if !k.OpenLongChecker.IsPoolEnabled(ctx, poolId) {
return nil, errorsmod.Wrap(types.ErrMTPDisabled, existingMtp.CustodyAsset)
}
ammPool, err := k.OpenLongChecker.GetAmmPool(ctx, poolId, existingMtp.CustodyAsset)
if err != nil {
return nil, err
}
switch msg.Position {
case types.Position_LONG:
existingMtp, err = k.OpenConsolidateLong(ctx, poolId, existingMtp, newMtp, msg, baseCurrency)
if err != nil {
return nil, err
}
case types.Position_SHORT:
existingMtp, err = k.OpenConsolidateShort(ctx, poolId, existingMtp, newMtp, msg, baseCurrency)
if err != nil {
return nil, err
}
default:
return nil, errorsmod.Wrap(types.ErrInvalidPosition, msg.Position.String())
}
// calc and update open price
k.UpdateOpenPrice(ctx, existingMtp, ammPool, baseCurrency)
k.EmitOpenEvent(ctx, existingMtp)
if k.hooks != nil {
k.hooks.AfterPerpetualPositionModified(ctx, ammPool, pool)
}
return &types.MsgOpenResponse{
Id: existingMtp.Id,
}, nil
}
func (k Keeper) EmitOpenEvent(ctx sdk.Context, mtp *types.MTP) {
ctx.EventManager().EmitEvent(types.GenerateOpenEvent(mtp))
}