Fix /incentive/apy pricefeed denom conversion (#1340)

* Update APYs endpoint to correctly rewrite incentive denoms to pricefeed ids

* Wrap pricefeed err
This commit is contained in:
Derrick Lee 2022-10-07 18:35:15 -07:00 committed by GitHub
parent a39c413a82
commit e6fc0135aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 21 deletions

View File

@ -493,7 +493,10 @@ func GetAPYFromMultiRewardPeriod(
// Get USD value of collateral type // Get USD value of collateral type
collateralUSDValue, err := k.pricefeedKeeper.GetCurrentPrice(ctx, getMarketID(collateralType)) collateralUSDValue, err := k.pricefeedKeeper.GetCurrentPrice(ctx, getMarketID(collateralType))
if err != nil { if err != nil {
return sdk.ZeroDec(), err return sdk.ZeroDec(), fmt.Errorf(
"failed to get price for incentive collateralType %s with market ID %s: %w",
collateralType, getMarketID(collateralType), err,
)
} }
// Total USD value of the collateral type total supply // Total USD value of the collateral type total supply
@ -524,9 +527,22 @@ func GetAPYFromMultiRewardPeriod(
} }
func getMarketID(denom string) string { func getMarketID(denom string) string {
if denom == types.BondDenom { // Rewrite denoms as pricefeed has different names for some assets,
// Rewrite "ukava" to "kava" as pricefeed only has "kava" and not "ukava" // e.g. "ukava" -> "kava", "erc20/multichain/usdc" -> "usdc"
return getMarketID("kava") // bkava is not included as it is handled separately
// TODO: Replace hardcoded conversion with possible params set somewhere
// to be more flexible. E.g. a map of denoms to pricefeed market denoms in
// pricefeed params.
switch denom {
case types.BondDenom:
denom = "kava"
case "erc20/multichain/usdc":
denom = "usdc"
case "erc20/multichain/usdt":
denom = "usdt"
case "erc20/multichain/dai":
denom = "dai"
} }
return fmt.Sprintf("%s:usd:30", denom) return fmt.Sprintf("%s:usd:30", denom)

View File

@ -30,6 +30,13 @@ func (suite *QuerierTestSuite) TestGetStakingAPR() {
liquidStakedTokens := int64(60_000_000_000000) liquidStakedTokens := int64(60_000_000_000000)
totalSupply := int64(289_138_414_286684) totalSupply := int64(289_138_414_286684)
usdcDenom := "erc20/multichain/usdc"
usdcSupply := int64(2_500_000_000000)
earnKeeper := newFakeEarnKeeper().
addVault("bkava-asdf", earntypes.NewVaultShare("bkava-asdf", sdk.NewDec(liquidStakedTokens))).
addVault(usdcDenom, earntypes.NewVaultShare(usdcDenom, sdk.NewDec(usdcSupply)))
suite.keeper = suite.NewTestKeeper(&fakeParamSubspace{}). suite.keeper = suite.NewTestKeeper(&fakeParamSubspace{}).
WithDistrKeeper( WithDistrKeeper(
newFakeDistrKeeper().setCommunityTax(communityTax), newFakeDistrKeeper().setCommunityTax(communityTax),
@ -44,20 +51,14 @@ func (suite *QuerierTestSuite) TestGetStakingAPR() {
WithBankKeeper( WithBankKeeper(
newFakeBankKeeper().setSupply(sdk.NewCoin(types.BondDenom, sdk.NewInt(totalSupply))), newFakeBankKeeper().setSupply(sdk.NewCoin(types.BondDenom, sdk.NewInt(totalSupply))),
). ).
WithEarnKeeper( WithEarnKeeper(earnKeeper).
newFakeEarnKeeper().
addVault("bkava-asdf", earntypes.NewVaultShare("bkava-asdf", sdk.NewDec(liquidStakedTokens))),
).
WithLiquidKeeper( WithLiquidKeeper(
newFakeLiquidKeeper().addDerivative(suite.ctx, "bkava-asdf", sdk.NewInt(liquidStakedTokens)), newFakeLiquidKeeper().addDerivative(suite.ctx, "bkava-asdf", sdk.NewInt(liquidStakedTokens)),
). ).
WithPricefeedKeeper( WithPricefeedKeeper(
newFakePricefeedKeeper(). newFakePricefeedKeeper().
setPrice( setPrice(pricefeedtypes.NewCurrentPrice("kava:usd:30", sdk.MustNewDecFromStr("1.5"))).
pricefeedtypes.NewCurrentPrice( setPrice(pricefeedtypes.NewCurrentPrice("usdc:usd:30", sdk.OneDec())),
"kava:usd:30",
sdk.MustNewDecFromStr("1.5"),
)),
). ).
Build() Build()
@ -87,9 +88,19 @@ func (suite *QuerierTestSuite) TestGetStakingAPR() {
sdk.NewCoin("ukava", sdk.NewInt(190258)), sdk.NewCoin("ukava", sdk.NewInt(190258)),
), ),
}, },
{
Active: true,
CollateralType: "erc20/multichain/usdc",
Start: suite.ctx.BlockTime().Add(-time.Hour),
End: suite.ctx.BlockTime().Add(time.Hour),
RewardsPerSecond: sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(5284)),
),
},
}, },
} }
suite.Run("GetStakingAPR", func() {
aprWithIncentives, err := keeper.GetStakingAPR(suite.ctx, suite.keeper, params) aprWithIncentives, err := keeper.GetStakingAPR(suite.ctx, suite.keeper, params)
suite.Require().NoError(err) suite.Require().NoError(err)
// Approx 10% increase in APR from incentives // Approx 10% increase in APR from incentives
@ -99,4 +110,25 @@ func (suite *QuerierTestSuite) TestGetStakingAPR() {
aprWithIncentives.GT(aprWithoutIncentives), aprWithIncentives.GT(aprWithoutIncentives),
"APR with incentives (%s) should be greater than APR without incentives (%s)", "APR with incentives (%s) should be greater than APR without incentives (%s)",
) )
})
suite.Run("GetAPYFromMultiRewardPeriod", func() {
vaultTotalValue, err := earnKeeper.GetVaultTotalValue(suite.ctx, usdcDenom)
suite.Require().NoError(err)
suite.Require().True(vaultTotalValue.Amount.IsPositive())
apy, err := keeper.GetAPYFromMultiRewardPeriod(
suite.ctx,
suite.keeper,
usdcDenom,
params.EarnRewardPeriods[1],
vaultTotalValue.Amount,
)
suite.Require().NoError(err)
suite.Require().Equal(
sdk.MustNewDecFromStr("0.099981734400000000"),
apy,
"usdc apy should be approx 10%",
)
})
} }