/
hooks.go
71 lines (57 loc) · 2.5 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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmoutils"
txfeestypes "github.com/percosis-labs/percosis/x/txfees/types"
epochstypes "github.com/osmosis-labs/osmosis/x/epochs/types"
)
func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
return nil
}
// at the end of each epoch, swap all non-PERCO fees into PERCO and transfer to fee module account
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
nonNativeFeeAddr := k.accountKeeper.GetModuleAddress(txfeestypes.NonNativeFeeCollectorName)
baseDenom, _ := k.GetBaseDenom(ctx)
feeTokens := k.GetFeeTokens(ctx)
for _, feetoken := range feeTokens {
if feetoken.Denom == baseDenom {
continue
}
coinBalance := k.bankKeeper.GetBalance(ctx, nonNativeFeeAddr, feetoken.Denom)
if coinBalance.Amount.IsZero() {
continue
}
// Do the swap of this fee token denom to base denom.
_ = osmoutils.ApplyFuncIfNoError(ctx, func(cacheCtx sdk.Context) error {
// We allow full slippage. Theres not really an effective way to bound slippage until TWAP's land,
// but even then the point is a bit moot.
// The only thing that could be done is a costly griefing attack to reduce the amount of perco given as tx fees.
// However the idea of the txfees FeeToken gating is that the pool is sufficiently liquid for that base token.
minAmountOut := sdk.ZeroInt()
_, err := k.poolManager.SwapExactAmountIn(cacheCtx, nonNativeFeeAddr, feetoken.PoolID, coinBalance, baseDenom, minAmountOut)
return err
})
}
// Get all of the txfee payout denom in the module account
baseDenomCoins := sdk.NewCoins(k.bankKeeper.GetBalance(ctx, nonNativeFeeAddr, baseDenom))
_ = osmoutils.ApplyFuncIfNoError(ctx, func(cacheCtx sdk.Context) error {
err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, txfeestypes.NonNativeFeeCollectorName, txfeestypes.FeeCollectorName, baseDenomCoins)
return err
})
return nil
}
// Hooks wrapper struct for incentives keeper
type Hooks struct {
k Keeper
}
var _ epochstypes.EpochHooks = Hooks{}
// Return the wrapper struct
func (k Keeper) Hooks() Hooks {
return Hooks{k}
}
func (h Hooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
return h.k.BeforeEpochStart(ctx, epochIdentifier, epochNumber)
}
func (h Hooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
return h.k.AfterEpochEnd(ctx, epochIdentifier, epochNumber)
}