-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.go
57 lines (47 loc) · 1.86 KB
/
hooks.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
package types
import (
fmt "fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/percosis-labs/percosis/osmoutils"
)
type EpochHooks interface {
// the first block whose timestamp is after the duration is counted as the end of the epoch
AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error
// new epoch is next block of epoch end block
BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error
}
var _ EpochHooks = MultiEpochHooks{}
// combine multiple gamm hooks, all hook functions are run in array sequence.
type MultiEpochHooks []EpochHooks
func NewMultiEpochHooks(hooks ...EpochHooks) MultiEpochHooks {
return hooks
}
// AfterEpochEnd is called when epoch is going to be ended, epochNumber is the number of epoch that is ending.
func (h MultiEpochHooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
for i := range h {
panicCatchingEpochHook(ctx, h[i].AfterEpochEnd, epochIdentifier, epochNumber)
}
return nil
}
// BeforeEpochStart is called when epoch is going to be started, epochNumber is the number of epoch that is starting.
func (h MultiEpochHooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
for i := range h {
panicCatchingEpochHook(ctx, h[i].BeforeEpochStart, epochIdentifier, epochNumber)
}
return nil
}
func panicCatchingEpochHook(
ctx sdk.Context,
hookFn func(ctx sdk.Context, epochIdentifier string, epochNumber int64) error,
epochIdentifier string,
epochNumber int64,
) {
wrappedHookFn := func(ctx sdk.Context) error {
return hookFn(ctx, epochIdentifier, epochNumber)
}
// TODO: Thread info for which hook this is, may be dependent on larger hook system refactoring
err := osmoutils.ApplyFuncIfNoError(ctx, wrappedHookFn)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("error in epoch hook %v", err))
}
}