/
hooks.go
145 lines (124 loc) · 5.02 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package keeper
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
epochstypes "github.com/Stride-Labs/stride/v21/x/epochs/types"
stakingibctypes "github.com/Stride-Labs/stride/v21/x/stakeibc/types"
"github.com/Stride-Labs/stride/v21/x/claim/types"
)
func (k Keeper) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
identifiers := k.GetAirdropIdentifiersForUser(ctx, delAddr)
for _, identifier := range identifiers {
cacheCtx, write := ctx.CacheContext()
_, err := k.ClaimCoinsForAction(cacheCtx, delAddr, types.ACTION_DELEGATE_STAKE, identifier)
if err == nil {
write()
} else {
k.Logger(ctx).Error(fmt.Sprintf("airdrop claim failure for %s on delegation hook: %s", delAddr.String(), err.Error()))
return err
}
}
return nil
}
func (k Keeper) AfterLiquidStake(ctx sdk.Context, addr sdk.AccAddress) {
identifiers := k.GetAirdropIdentifiersForUser(ctx, addr)
for _, identifier := range identifiers {
cacheCtx, write := ctx.CacheContext()
_, err := k.ClaimCoinsForAction(cacheCtx, addr, types.ACTION_LIQUID_STAKE, identifier)
if err == nil {
write()
} else {
k.Logger(ctx).Error(fmt.Sprintf("airdrop claim failure for %s on liquid staking hook: %s", addr.String(), err.Error()))
}
}
}
func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {
}
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {
// check if epochInfo.Identifier is an airdrop epoch
// if yes, reset claim status for all users
// check if epochInfo.Identifier starts with "airdrop"
k.Logger(ctx).Info(fmt.Sprintf("[CLAIM] checking if epoch %s is an airdrop epoch", epochInfo.Identifier))
if strings.HasPrefix(epochInfo.Identifier, "airdrop-") {
airdropIdentifier := strings.TrimPrefix(epochInfo.Identifier, "airdrop-")
k.Logger(ctx).Info(fmt.Sprintf("[CLAIM] trimmed airdrop identifier: %s", airdropIdentifier))
airdrop := k.GetAirdropByIdentifier(ctx, airdropIdentifier)
k.Logger(ctx).Info(fmt.Sprintf("[CLAIM] airdrop found: %v", airdrop))
if airdrop != nil {
k.Logger(ctx).Info(fmt.Sprintf("[CLAIM] resetting claims for airdrop %s", epochInfo.Identifier))
err := k.ResetClaimStatus(ctx, airdropIdentifier)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("[CLAIM] failed to reset claim status for epoch %s: %s", epochInfo.Identifier, err.Error()))
}
} else {
k.Logger(ctx).Info(fmt.Sprintf("[CLAIM] airdrop %s not found, skipping reset", airdropIdentifier))
}
}
}
// ________________________________________________________________________________________
// Hooks wrapper struct for claim keeper
type Hooks struct {
k Keeper
}
var _ stakingtypes.StakingHooks = Hooks{}
var _ stakingibctypes.StakeIBCHooks = Hooks{}
var _ epochstypes.EpochHooks = Hooks{}
// Return the wrapper struct
func (k Keeper) Hooks() Hooks {
return Hooks{k}
}
// ibcstaking hooks
func (h Hooks) AfterLiquidStake(ctx sdk.Context, addr sdk.AccAddress) {
h.k.AfterLiquidStake(ctx, addr)
}
// epochs hooks
func (h Hooks) BeforeEpochStart(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {
h.k.BeforeEpochStart(ctx, epochInfo)
}
func (h Hooks) AfterEpochEnd(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {
h.k.AfterEpochEnd(ctx, epochInfo)
}
func (h Hooks) AfterUnbondingInitiated(ctx sdk.Context, id uint64) error {
return nil
}
// staking hooks
func (h Hooks) AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeValidatorModified(ctx sdk.Context, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorBonded(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorBeginUnbonding(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
return h.k.AfterDelegationModified(ctx, delAddr, valAddr)
}
func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) error {
return nil
}
func (h Hooks) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation stakingtypes.UnbondingDelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
return nil
}
func (h Hooks) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator stakingtypes.Validator, redelegation stakingtypes.Redelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
return nil
}