-
Notifications
You must be signed in to change notification settings - Fork 119
/
epoch_hooks.go
95 lines (77 loc) · 2.93 KB
/
epoch_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
package keeper
import (
"fmt"
"github.com/Sifchain/sifnode/x/clp/types"
epochstypes "github.com/Sifchain/sifnode/x/epochs/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// BeforeEpochStart performs a no-op
func (k Keeper) BeforeEpochStart(_ sdk.Context, _ string, _ int64) {}
// AfterEpochEnd distributes available rewards from rewards bucket to liquidity providers
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ int64) {
if !k.ShouldDistributeRewards(ctx, epochIdentifier) {
return
}
rewardsEligibleLps, err := k.GetRewardsEligibleLiquidityProviders(ctx)
if err != nil {
ctx.Logger().Error(types.ErrUnableToGetRewardsEligibleLiquidityProviders.Error(), "error", err)
return
}
for asset, assetLps := range rewardsEligibleLps {
// get reward bucket for given asset
rewardsBucket, found := k.GetRewardsBucket(ctx, asset.Symbol)
if !found {
continue
}
k.Logger(ctx).Info(fmt.Sprintf("rewards bucket not found for denom: %s", asset.Symbol))
rewardShares := k.CalculateRewardShareForLiquidityProviders(ctx, assetLps)
rewardAmounts := k.CalculateRewardAmountForLiquidityProviders(ctx, rewardShares, rewardsBucket.Amount)
for i, lp := range assetLps {
if k.ShouldDistributeRewardsToLPWallet(ctx) {
err := k.DistributeLiquidityProviderRewards(ctx, lp, asset.Symbol, rewardAmounts[i])
if err != nil {
ctx.Logger().Error(types.ErrUnableToDistributeLPRewards.Error(), "error", err)
}
} else {
err := k.AddRewardAmountToLiquidityPool(ctx, lp, asset, rewardAmounts[i])
if err != nil {
ctx.Logger().Error(types.ErrUnableToAddRewardAmountToLiquidityPool.Error(), "error", err)
}
}
// increment lp reward amount
lp.RewardAmount = lp.RewardAmount.Add(sdk.NewCoin(asset.Symbol, rewardAmounts[i]))
// update the liquidity provider
k.SetLiquidityProvider(ctx, lp)
}
// increment pool reward amount
pool, err := k.GetPool(ctx, asset.Symbol)
if err != nil {
ctx.Logger().Error(types.ErrPoolDoesNotExist.Error(), "error", err)
continue
}
pool.RewardAmountExternal = pool.RewardAmountExternal.Add(sdk.NewUintFromBigInt(rewardsBucket.Amount.BigInt()))
err = k.SetPool(ctx, &pool)
if err != nil {
ctx.Logger().Error(types.ErrUnableToSetPool.Error(), "error", err)
continue
}
}
}
// ___________________________________________________________________________________________________
// Hooks wrapper struct for commitments keeper
type Hooks struct {
k Keeper
}
var _ epochstypes.EpochHooks = Hooks{}
// Return the wrapper struct
func (k Keeper) Hooks() Hooks {
return Hooks{k}
}
// BeforeEpochStart implements EpochHooks
func (h Hooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
h.k.BeforeEpochStart(ctx, epochIdentifier, epochNumber)
}
// AfterEpochEnd implements EpochHooks
func (h Hooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
h.k.AfterEpochEnd(ctx, epochIdentifier, epochNumber)
}