Skip to content

Commit

Permalink
tvl fixes for accounted pool (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
avkr003 authored Oct 26, 2024
1 parent 6c3996c commit b40916a
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 49 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ func NewElysApp(
app.CommitmentKeeper,
app.AssetprofileKeeper,
app.MasterchefKeeper,
app.AccountedPoolKeeper,
)

app.TierKeeper = *tiermodulekeeper.NewKeeper(
Expand Down
1 change: 1 addition & 0 deletions testutil/keeper/leveragelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func LeveragelpKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
nil,
nil,
nil,
nil,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
4 changes: 2 additions & 2 deletions x/amm/client/wasm/query_earn_mining_pool_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (oq *Querier) generateEarnPool(ctx sdk.Context, ammPool *types.Pool, filter
prams := oq.stablestakeKeeper.GetParams(ctx)
borrowApr = prams.InterestRate
}
tvl, _ := ammPool.TVL(ctx, oq.oraclekeeper)
lpTokenPrice, _ := ammPool.LpTokenPrice(ctx, oq.oraclekeeper)
tvl, _ := ammPool.TVL(ctx, oq.oraclekeeper, oq.accountedpoolKeeper)
lpTokenPrice, _ := ammPool.LpTokenPrice(ctx, oq.oraclekeeper, oq.accountedpoolKeeper)

// Get rewards amount
// TODO: Remove wasmbindings, as of now this is not used by FE, so setting it to zero
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/keeper_create_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (k Keeper) CreatePool(ctx sdk.Context, msg *types.MsgCreatePool) (uint64, e
// - Records total liquidity increase
// - Calls the AfterPoolCreated hook
func (k Keeper) InitializePool(ctx sdk.Context, pool *types.Pool, sender sdk.AccAddress) (err error) {
tvl, err := pool.TVL(ctx, k.oracleKeeper)
tvl, err := pool.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (k msgServer) FeedMultipleExternalLiquidity(goCtx context.Context, msg *typ
return nil, types.ErrInvalidPoolId
}

tvl, err := pool.TVL(ctx, k.oracleKeeper)
tvl, err := pool.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (k Keeper) GetBestPoolWithDenoms(ctx sdk.Context, denoms []string, usesOrac
}
}

poolTvl, err := p.TVL(ctx, k.oracleKeeper)
poolTvl, err := p.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
if err != nil {
poolTvl = sdk.ZeroDec()
}
Expand Down
4 changes: 2 additions & 2 deletions x/amm/keeper/query_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

func (k Keeper) PoolExtraInfo(ctx sdk.Context, pool types.Pool) types.PoolExtraInfo {
tvl, _ := pool.TVL(ctx, k.oracleKeeper)
lpTokenPrice, _ := pool.LpTokenPrice(ctx, k.oracleKeeper)
tvl, _ := pool.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
lpTokenPrice, _ := pool.LpTokenPrice(ctx, k.oracleKeeper, k.accountedPoolKeeper)
return types.PoolExtraInfo{
Tvl: tvl,
LpTokenPrice: lpTokenPrice,
Expand Down
2 changes: 1 addition & 1 deletion x/amm/types/calc_exit_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func CalcExitValueWithoutSlippage(ctx sdk.Context, oracleKeeper OracleKeeper, accPoolKeeper AccountedPoolKeeper, pool Pool, exitingShares math.Int, tokenOutDenom string) (sdk.Dec, error) {
tvl, err := pool.TVL(ctx, oracleKeeper)
tvl, err := pool.TVL(ctx, oracleKeeper, accPoolKeeper)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down
19 changes: 9 additions & 10 deletions x/amm/types/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (p *Pool) CalcExitPoolCoinsFromShares(
return CalcExitPool(ctx, oracleKeeper, *p, accountedPoolKeeper, exitingShares, tokenOutDenom)
}

func (p *Pool) TVL(ctx sdk.Context, oracleKeeper OracleKeeper) (sdk.Dec, error) {
func (p *Pool) TVL(ctx sdk.Context, oracleKeeper OracleKeeper, accountedPoolKeeper AccountedPoolKeeper) (sdk.Dec, error) {
// OracleAssetsTVL * TotalWeight / OracleAssetsWeight
// E.g. JUNO / USDT / USDC (30:30:30)
// TVL = USDC_USDT_liquidity * 90 / 60
Expand All @@ -327,13 +327,12 @@ func (p *Pool) TVL(ctx sdk.Context, oracleKeeper OracleKeeper) (sdk.Dec, error)
}
} else {
amount := asset.Token.Amount
// TODO
//if p.PoolParams.UseOracle && accountedPoolKeeper != nil {
// accountedPoolAmt := accountedPoolKeeper.GetAccountedBalance(ctx, p.PoolId, asset.Token.Denom)
// if accountedPoolAmt.IsPositive() {
// amount = accountedPoolAmt
// }
//}
if p.PoolParams.UseOracle && accountedPoolKeeper != nil {
accountedPoolAmt := accountedPoolKeeper.GetAccountedBalance(ctx, p.PoolId, asset.Token.Denom)
if accountedPoolAmt.IsPositive() {
amount = accountedPoolAmt
}
}
v := amount.ToLegacyDec().Mul(tokenPrice)
oracleAssetsTVL = oracleAssetsTVL.Add(v)
oracleAssetsWeight = oracleAssetsWeight.Add(asset.Weight)
Expand All @@ -347,8 +346,8 @@ func (p *Pool) TVL(ctx sdk.Context, oracleKeeper OracleKeeper) (sdk.Dec, error)
return oracleAssetsTVL.Mul(sdk.NewDecFromInt(totalWeight)).Quo(sdk.NewDecFromInt(oracleAssetsWeight)), nil
}

func (p *Pool) LpTokenPrice(ctx sdk.Context, oracleKeeper OracleKeeper) (sdk.Dec, error) {
ammPoolTvl, err := p.TVL(ctx, oracleKeeper)
func (p *Pool) LpTokenPrice(ctx sdk.Context, oracleKeeper OracleKeeper, accPoolKeeper AccountedPoolKeeper) (sdk.Dec, error) {
ammPoolTvl, err := p.TVL(ctx, oracleKeeper, accPoolKeeper)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down
2 changes: 1 addition & 1 deletion x/amm/types/pool_join_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (p *Pool) JoinPool(
}

initialWeightDistance := p.WeightDistanceFromTarget(ctx, oracleKeeper, accountedPoolKeeper, p.PoolAssets)
tvl, err := p.TVL(ctx, oracleKeeper)
tvl, err := p.TVL(ctx, oracleKeeper, accountedPoolKeeper)
if err != nil {
return sdk.ZeroInt(), sdk.ZeroDec(), sdk.ZeroDec(), err
}
Expand Down
2 changes: 1 addition & 1 deletion x/amm/types/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (suite *TestSuite) TestPoolTVL() {
PoolAssets: tc.poolAssets,
TotalWeight: sdk.ZeroInt(),
}
tvl, err := pool.TVL(suite.ctx, suite.app.OracleKeeper)
tvl, err := pool.TVL(suite.ctx, suite.app.OracleKeeper, suite.app.AccountedPoolKeeper)
if tc.expError {
suite.Require().Error(err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion x/leveragelp/keeper/begin_blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (k Keeper) CheckAndCloseAtStopLoss(ctx sdk.Context, position *types.Positio
position.PositionHealth = h
k.SetPosition(ctx, position)

lpTokenPrice, err := ammPool.LpTokenPrice(ctx, k.oracleKeeper)
lpTokenPrice, err := ammPool.LpTokenPrice(ctx, k.oracleKeeper, k.accountedPoolKeeper)
if err != nil {
return false, false, err
}
Expand Down
9 changes: 5 additions & 4 deletions x/leveragelp/keeper/hooks_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
)

func (k Keeper) CheckAmmPoolUsdcBalance(ctx sdk.Context, ammPool ammtypes.Pool) error {
leveragelpPool, found := k.GetPool(ctx, ammPool.PoolId)
leveragePool, found := k.GetPool(ctx, ammPool.PoolId)
if !found {
return nil
}

tvl, err := ammPool.TVL(ctx, k.oracleKeeper)
// This is kind of health check so we should only use real amm pool balance
tvl, err := ammPool.TVL(ctx, k.oracleKeeper, nil)
if err != nil {
return err
}
leverageLpTvl := tvl.
Mul(sdk.NewDecFromInt(leveragelpPool.LeveragedLpAmount)).
Quo(sdk.NewDecFromInt(ammPool.TotalShares.Amount))
Mul(leveragePool.LeveragedLpAmount.ToLegacyDec()).
Quo(ammPool.TotalShares.Amount.ToLegacyDec())

depositDenom := k.stableKeeper.GetDepositDenom(ctx)
price := k.oracleKeeper.GetAssetPriceFromDenom(ctx, depositDenom)
Expand Down
47 changes: 25 additions & 22 deletions x/leveragelp/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ import (

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
authority string
amm types.AmmKeeper
bankKeeper types.BankKeeper
oracleKeeper ammtypes.OracleKeeper
stableKeeper types.StableStakeKeeper
commKeeper types.CommitmentKeeper
assetProfileKeeper types.AssetProfileKeeper
masterchefKeeper types.MasterchefKeeper
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
authority string
amm types.AmmKeeper
bankKeeper types.BankKeeper
oracleKeeper ammtypes.OracleKeeper
stableKeeper types.StableStakeKeeper
commKeeper types.CommitmentKeeper
assetProfileKeeper types.AssetProfileKeeper
masterchefKeeper types.MasterchefKeeper
accountedPoolKeeper types.AccountedPoolKeeper

hooks types.LeverageLpHooks
}
Expand All @@ -46,24 +47,26 @@ func NewKeeper(
commitmentKeeper types.CommitmentKeeper,
assetProfileKeeper types.AssetProfileKeeper,
masterchefKeeper types.MasterchefKeeper,
accountedPoolKeeper types.AccountedPoolKeeper,
) *Keeper {
// ensure that authority is a valid AccAddress
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
panic("authority is not a valid acc address")
}

keeper := &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
authority: authority,
amm: amm,
bankKeeper: bk,
oracleKeeper: oracleKeeper,
stableKeeper: stableKeeper,
commKeeper: commitmentKeeper,
assetProfileKeeper: assetProfileKeeper,
masterchefKeeper: masterchefKeeper,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
authority: authority,
amm: amm,
bankKeeper: bk,
oracleKeeper: oracleKeeper,
stableKeeper: stableKeeper,
commKeeper: commitmentKeeper,
assetProfileKeeper: assetProfileKeeper,
masterchefKeeper: masterchefKeeper,
accountedPoolKeeper: accountedPoolKeeper,
}

return keeper
Expand Down
4 changes: 4 additions & 0 deletions x/leveragelp/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ type MasterchefKeeper interface {
ClaimRewards(ctx sdk.Context, sender sdk.AccAddress, poolIds []uint64, recipient sdk.AccAddress) error
UserPoolPendingReward(ctx sdk.Context, user sdk.AccAddress, poolId uint64) sdk.Coins
}

type AccountedPoolKeeper interface {
GetAccountedBalance(sdk.Context, uint64, string) math.Int
}
4 changes: 2 additions & 2 deletions x/masterchef/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (k Keeper) GetPoolTVL(ctx sdk.Context, poolId uint64) math.LegacyDec {
}
ammPool, found := k.amm.GetPool(ctx, poolId)
if found {
tvl, err := ammPool.TVL(ctx, k.oracleKeeper)
tvl, err := ammPool.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
if err != nil {
return math.LegacyZeroDec()
}
Expand Down Expand Up @@ -602,7 +602,7 @@ func (k Keeper) UpdateAmmPoolAPR(ctx sdk.Context, totalBlocksPerYear int64, tota
usdcDenomPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, baseCurrency)

k.amm.IterateLiquidityPools(ctx, func(p ammtypes.Pool) bool {
tvl, err := p.TVL(ctx, k.oracleKeeper)
tvl, err := p.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
if err != nil {
return false
}
Expand Down

0 comments on commit b40916a

Please sign in to comment.