-
Notifications
You must be signed in to change notification settings - Fork 51
/
begin_blocker.go
78 lines (69 loc) · 2.38 KB
/
begin_blocker.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
package keeper
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/types/errors"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/leveragelp/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k Keeper) BeginBlocker(ctx sdk.Context) {
// check if epoch has passed then execute
epochLength := k.GetEpochLength(ctx)
epochPosition := k.GetEpochPosition(ctx, epochLength)
if epochPosition == 0 { // if epoch has passed
pools := k.GetAllPools(ctx)
for _, pool := range pools {
ammPool, err := k.GetAmmPool(ctx, pool.AmmPoolId)
if err != nil {
ctx.Logger().Error(errors.Wrap(err, fmt.Sprintf("error getting amm pool: %d", pool.AmmPoolId)).Error())
continue
}
if k.IsPoolEnabled(ctx, pool.AmmPoolId) {
positions, _, _ := k.GetPositionsForPool(ctx, pool.AmmPoolId, nil)
for _, position := range positions {
k.LiquidatePositionIfUnhealthy(ctx, position, pool, ammPool)
}
}
k.SetPool(ctx, pool)
}
}
}
func (k Keeper) LiquidatePositionIfUnhealthy(ctx sdk.Context, position *types.Position, pool types.Pool, ammPool ammtypes.Pool) {
defer func() {
if r := recover(); r != nil {
if msg, ok := r.(string); ok {
ctx.Logger().Error(msg)
}
}
}()
h, err := k.GetPositionHealth(ctx, *position, ammPool)
if err != nil {
ctx.Logger().Error(errors.Wrap(err, fmt.Sprintf("error updating position health: %s", position.String())).Error())
return
}
position.PositionHealth = h
k.SetPosition(ctx, position)
lpTokenPrice, err := k.GetLpTokenPrice(ctx, &ammPool)
if err != nil {
return
}
params := k.GetParams(ctx)
if position.PositionHealth.GT(params.SafetyFactor) && lpTokenPrice.GT(position.StopLossPrice) {
return
}
repayAmount, err := k.ForceCloseLong(ctx, *position, pool, position.LeveragedLpAmount)
if err == nil {
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventClose,
sdk.NewAttribute("id", strconv.FormatInt(int64(position.Id), 10)),
sdk.NewAttribute("address", position.Address),
sdk.NewAttribute("collateral", position.Collateral.String()),
sdk.NewAttribute("repay_amount", repayAmount.String()),
sdk.NewAttribute("leverage", position.Leverage.String()),
sdk.NewAttribute("liabilities", position.Liabilities.String()),
sdk.NewAttribute("health", position.PositionHealth.String()),
))
} else {
ctx.Logger().Error(errors.Wrap(err, "error executing force close").Error())
}
}