/
hooks_amm.go
94 lines (78 loc) · 3.2 KB
/
hooks_amm.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
package keeper
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/leveragelp/types"
)
func (k Keeper) CheckAmmPoolUsdcBalance(ctx sdk.Context, ammPool ammtypes.Pool) error {
leveragelpPool, found := k.GetPool(ctx, ammPool.PoolId)
if !found {
return nil
}
tvl, err := ammPool.TVL(ctx, k.oracleKeeper)
if err != nil {
return err
}
leverageLpTvl := tvl.
Mul(sdk.NewDecFromInt(leveragelpPool.LeveragedLpAmount)).
Quo(sdk.NewDecFromInt(ammPool.TotalShares.Amount))
depositDenom := k.stableKeeper.GetDepositDenom(ctx)
for _, asset := range ammPool.PoolAssets {
if asset.Token.Denom == depositDenom {
if asset.Token.Amount.LT(leverageLpTvl.RoundInt()) {
return types.ErrInsufficientUsdcAfterOp
}
}
}
return nil
}
// AfterPoolCreated is called after CreatePool
func (k Keeper) AfterPoolCreated(ctx sdk.Context, sender sdk.AccAddress, ammPool ammtypes.Pool) {
if k.hooks != nil {
k.hooks.AfterAmmPoolCreated(ctx, ammPool)
}
}
// AfterJoinPool is called after JoinPool, JoinSwapExternAmountIn, and JoinSwapShareAmountOut
func (k Keeper) AfterJoinPool(ctx sdk.Context, sender sdk.AccAddress, ammPool ammtypes.Pool, enterCoins sdk.Coins, shareOutAmount math.Int) {
leveragelpPool, found := k.GetPool(ctx, ammPool.PoolId)
if !found {
return
}
if k.hooks != nil {
k.hooks.AfterAmmJoinPool(ctx, ammPool, leveragelpPool)
}
}
// AfterExitPool is called after ExitPool, ExitSwapShareAmountIn, and ExitSwapExternAmountOut
func (k Keeper) AfterExitPool(ctx sdk.Context, sender sdk.AccAddress, ammPool ammtypes.Pool, shareInAmount math.Int, exitCoins sdk.Coins) error {
return k.CheckAmmPoolUsdcBalance(ctx, ammPool)
}
// AfterSwap is called after SwapExactAmountIn and SwapExactAmountOut
func (k Keeper) AfterSwap(ctx sdk.Context, sender sdk.AccAddress, ammPool ammtypes.Pool, input sdk.Coins, output sdk.Coins) error {
return k.CheckAmmPoolUsdcBalance(ctx, ammPool)
}
// Hooks wrapper struct for tvl keeper
type AmmHooks struct {
k Keeper
}
var _ ammtypes.AmmHooks = AmmHooks{}
// Return the wrapper struct
func (k Keeper) AmmHooks() AmmHooks {
return AmmHooks{k}
}
// AfterPoolCreated is called after CreatePool
func (h AmmHooks) AfterPoolCreated(ctx sdk.Context, sender sdk.AccAddress, pool ammtypes.Pool) {
h.k.AfterPoolCreated(ctx, sender, pool)
}
// AfterJoinPool is called after JoinPool, JoinSwapExternAmountIn, and JoinSwapShareAmountOut
func (h AmmHooks) AfterJoinPool(ctx sdk.Context, sender sdk.AccAddress, pool ammtypes.Pool, enterCoins sdk.Coins, shareOutAmount math.Int) {
h.k.AfterJoinPool(ctx, sender, pool, enterCoins, shareOutAmount)
}
// AfterExitPool is called after ExitPool, ExitSwapShareAmountIn, and ExitSwapExternAmountOut
func (h AmmHooks) AfterExitPool(ctx sdk.Context, sender sdk.AccAddress, pool ammtypes.Pool, shareInAmount math.Int, exitCoins sdk.Coins) error {
return h.k.AfterExitPool(ctx, sender, pool, shareInAmount, exitCoins)
}
// AfterSwap is called after SwapExactAmountIn and SwapExactAmountOut
func (h AmmHooks) AfterSwap(ctx sdk.Context, sender sdk.AccAddress, pool ammtypes.Pool, input sdk.Coins, output sdk.Coins) error {
return h.k.AfterSwap(ctx, sender, pool, input, output)
}