-
Notifications
You must be signed in to change notification settings - Fork 559
/
epoch_hook.go
129 lines (107 loc) · 4.05 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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmomath"
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}
}
// 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":
// 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
}
// 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
}
}
}