-
Notifications
You must be signed in to change notification settings - Fork 119
/
abci.go
82 lines (69 loc) · 2.44 KB
/
abci.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
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/errors"
clptypes "github.com/Sifchain/sifnode/x/clp/types"
"github.com/Sifchain/sifnode/x/margin/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 := GetEpochPosition(ctx, epochLength)
if epochPosition == 0 { // if epoch has passed
currentHeight := ctx.BlockHeight()
pools := k.ClpKeeper().GetPools(ctx)
for _, pool := range pools {
pool.BlockInterestExternal = sdk.ZeroUint()
pool.BlockInterestNative = sdk.ZeroUint()
if k.IsPoolEnabled(ctx, pool.ExternalAsset.Symbol) {
rate, err := k.InterestRateComputation(ctx, *pool)
if err != nil {
ctx.Logger().Error(err.Error())
continue // ?
}
pool.InterestRate = rate
pool.LastHeightInterestRateComputed = currentHeight
_ = k.UpdatePoolHealth(ctx, pool)
k.TrackSQBeginBlock(ctx, pool)
mtps, _, _ := k.GetMTPsForPool(ctx, pool.ExternalAsset.Symbol, nil)
for _, mtp := range mtps {
BeginBlockerProcessMTP(ctx, k, mtp, pool)
}
}
_ = k.clpKeeper.SetPool(ctx, pool)
}
}
}
func BeginBlockerProcessMTP(ctx sdk.Context, k Keeper, mtp *types.MTP, pool *clptypes.Pool) {
defer func() {
if r := recover(); r != nil {
if msg, ok := r.(string); ok {
ctx.Logger().Error(msg)
}
}
}()
h, err := k.UpdateMTPHealth(ctx, *mtp, *pool)
if err != nil {
ctx.Logger().Error(errors.Wrap(err, fmt.Sprintf("error updating mtp health: %s", mtp.String())).Error())
return
}
mtp.MtpHealth = h
// compute interest
interestPayment := CalcMTPInterestLiabilities(mtp, pool.InterestRate, 0, 0)
finalInterestPayment := k.HandleInterestPayment(ctx, interestPayment, mtp, pool)
nativeAsset := types.GetSettlementAsset()
if types.StringCompare(mtp.CollateralAsset, nativeAsset) { // custody is external, payment is custody
pool.BlockInterestExternal = pool.BlockInterestExternal.Add(finalInterestPayment)
} else { // custody is native, payment is custody
pool.BlockInterestNative = pool.BlockInterestNative.Add(finalInterestPayment)
}
_ = k.SetMTP(ctx, mtp)
repayAmount, err := k.ForceCloseLong(ctx, mtp, pool, false, true)
if err == nil {
// Emit event if position was closed
k.EmitForceClose(ctx, mtp, repayAmount, "")
} else if err != types.ErrMTPHealthy {
ctx.Logger().Error(errors.Wrap(err, "error executing force close").Error())
}
}