-
Notifications
You must be signed in to change notification settings - Fork 568
/
epoch_hook.go
182 lines (150 loc) · 5.69 KB
/
epoch_hook.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/v25/x/protorev/types"
epochstypes "github.com/osmosis-labs/osmosis/x/epochs/types"
)
type EpochHooks struct {
k Keeper
}
// Struct used to track the pool with the highest liquidity
type LiquidityPoolStruct struct {
Liquidity osmomath.Int
PoolId uint64
}
var _ epochstypes.EpochHooks = EpochHooks{}
func (k Keeper) EpochHooks() epochstypes.EpochHooks {
return EpochHooks{k}
}
// GetModuleName implements types.EpochHooks.
func (EpochHooks) GetModuleName() string {
return types.ModuleName
}
// BeforeEpochStart is the epoch start hook.
func (h EpochHooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
return nil
}
// AfterEpochEnd is the epoch end hook.
func (h EpochHooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
if h.k.GetProtoRevEnabled(ctx) {
switch epochIdentifier {
case "day":
// Calculate and distribute protorev profits
err := h.CalculateAndDistributeProfits(ctx)
if err != nil {
return err
}
// Increment number of days since module genesis to properly calculate developer fees after cyclic arbitrage trades
if daysSinceGenesis, err := h.k.GetDaysSinceModuleGenesis(ctx); err != nil {
h.k.SetDaysSinceModuleGenesis(ctx, 1)
} else {
h.k.SetDaysSinceModuleGenesis(ctx, daysSinceGenesis+1)
}
// Update the pools in the store
return h.k.UpdatePools(ctx)
}
}
return nil
}
// CalculateAndDistributeProfits is executed after epoch. It gets the current base denom profits and distributes them.
func (h EpochHooks) CalculateAndDistributeProfits(ctx sdk.Context) error {
// Get the current arb profits (only in base denoms to prevent spam vector)
profit, err := h.k.CurrentBaseDenomProfits(ctx)
if err != nil {
return err
}
// Distribute profits to developer account, community pool, and burn osmo
err = h.k.DistributeProfit(ctx, profit)
if err != nil {
return err
}
return nil
}
// CurrentBaseDenomProfits retrieves the current balance of the protorev module account and filters for base denoms.
func (k Keeper) CurrentBaseDenomProfits(ctx sdk.Context) (sdk.Coins, error) {
moduleAcc := k.accountKeeper.GetModuleAddress(types.ModuleName)
baseDenoms, err := k.GetAllBaseDenoms(ctx)
if err != nil {
return nil, err
}
// Get the current protorev balance of all denoms
protorevBalanceAllDenoms := k.bankKeeper.GetAllBalances(ctx, moduleAcc)
// Filter for base denoms
var protorevBalanceBaseDenoms sdk.Coins
for _, baseDenom := range baseDenoms {
amountOfBaseDenom := protorevBalanceAllDenoms.AmountOf(baseDenom.Denom)
if !amountOfBaseDenom.IsZero() {
protorevBalanceBaseDenoms = append(protorevBalanceBaseDenoms, sdk.NewCoin(baseDenom.Denom, amountOfBaseDenom))
}
}
return protorevBalanceBaseDenoms.Sort(), nil
}
// UpdatePools first deletes all of the pools paired with any base denom in the store and then adds the highest liquidity pools that match to the store
func (k Keeper) UpdatePools(ctx sdk.Context) error {
// baseDenomPools maps each base denom to a map of the highest liquidity pools paired with that base denom
// ex. {osmo -> {atom : 100, weth : 200}}
baseDenomPools := make(map[string]map[string]LiquidityPoolStruct)
baseDenoms, err := k.GetAllBaseDenoms(ctx)
if err != nil {
return err
}
// Delete any pools that currently exist in the store + initialize baseDenomPools
for _, baseDenom := range baseDenoms {
k.DeleteAllPoolsForBaseDenom(ctx, baseDenom.Denom)
baseDenomPools[baseDenom.Denom] = make(map[string]LiquidityPoolStruct)
}
// Update baseDenomPools with the highest liquidity pools
if err := k.UpdateHighestLiquidityPools(ctx, baseDenomPools); err != nil {
return err
}
// Update the pools in the store
for baseDenom, pools := range baseDenomPools {
for denom, pool := range pools {
k.SetPoolForDenomPair(ctx, baseDenom, denom, pool.PoolId)
}
}
return nil
}
// UpdateHighestLiquidityPools updates the baseDenomPools map (passed in by reference) with the
// highest liquidity pools for each base denom by iterating through all pools, getting the
// total liquidity for each pool, and updating the highest liquidity pools based upon comparing total liquidity.
func (k Keeper) UpdateHighestLiquidityPools(ctx sdk.Context, baseDenomPools map[string]map[string]LiquidityPoolStruct) error {
pools, err := k.poolmanagerKeeper.AllPools(ctx)
if err != nil {
return err
}
for _, pool := range pools {
coins, err := k.poolmanagerKeeper.GetTotalPoolLiquidity(ctx, pool.GetId())
if err != nil {
return err
}
// Pool must be active and the number of coins must be 2
if pool.IsActive(ctx) && len(coins) == 2 {
tokenA := coins[0]
tokenB := coins[1]
newPool := LiquidityPoolStruct{
PoolId: pool.GetId(),
Liquidity: tokenA.Amount.Mul(tokenB.Amount),
}
// Update happens both ways to ensure the pools that contain multiple base denoms are properly updated
if highestLiquidityPools, ok := baseDenomPools[tokenA.Denom]; ok {
k.compareAndStoreHighestLiquidityPool(tokenB.Denom, highestLiquidityPools, newPool)
}
if highestLiquidityPools, ok := baseDenomPools[tokenB.Denom]; ok {
k.compareAndStoreHighestLiquidityPool(tokenA.Denom, highestLiquidityPools, newPool)
}
}
}
return nil
}
// compareAndStoreHighestLiquidityPool updates the pool with the highest liquidity for the base denom
func (k Keeper) compareAndStoreHighestLiquidityPool(denom string, pools map[string]LiquidityPoolStruct, newPool LiquidityPoolStruct) {
if currPool, ok := pools[denom]; !ok {
pools[denom] = newPool
} else {
if newPool.Liquidity.GT(currPool.Liquidity) {
pools[denom] = newPool
}
}
}