-
Notifications
You must be signed in to change notification settings - Fork 44
/
msg_server_update_stop_loss.go
56 lines (44 loc) · 1.66 KB
/
msg_server_update_stop_loss.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
package keeper
import (
"context"
"fmt"
"strconv"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/leveragelp/types"
)
func (k msgServer) UpdateStopLoss(goCtx context.Context, msg *types.MsgUpdateStopLoss) (*types.MsgUpdateStopLossResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
position, found := k.GetPositionWithId(ctx, sdk.MustAccAddressFromBech32(msg.Creator), uint64(msg.Position))
if !found {
return nil, errorsmod.Wrap(types.ErrPositionDoesNotExist, fmt.Sprintf("positionId: %d", msg.Position))
}
poolId := position.AmmPoolId
pool, found := k.GetPool(ctx, poolId)
if !found {
return nil, errorsmod.Wrap(types.ErrPoolDoesNotExist, fmt.Sprintf("poolId: %d", poolId))
}
if !k.IsPoolEnabled(ctx, poolId) {
return nil, errorsmod.Wrap(types.ErrPositionDisabled, fmt.Sprintf("poolId: %d", poolId))
}
ammPool, err := k.GetAmmPool(ctx, poolId)
if err != nil {
return nil, err
}
position.StopLossPrice = msg.Price
k.SetPosition(ctx, position)
event := sdk.NewEvent(types.EventOpen,
sdk.NewAttribute("id", strconv.FormatInt(int64(position.Id), 10)),
sdk.NewAttribute("address", position.Address),
sdk.NewAttribute("collateral", position.Collateral.String()),
sdk.NewAttribute("leverage", position.Leverage.String()),
sdk.NewAttribute("liabilities", position.Liabilities.String()),
sdk.NewAttribute("health", position.PositionHealth.String()),
sdk.NewAttribute("stop_loss", position.StopLossPrice.String()),
)
ctx.EventManager().EmitEvent(event)
if k.hooks != nil {
k.hooks.AfterLeveragelpPositionModified(ctx, ammPool, pool)
}
return &types.MsgUpdateStopLossResponse{}, nil
}