Skip to content

Commit 18b66ae

Browse files
amityadav0Amit Yadav
andauthored
Weight rewards fix (#1238)
* fix amount * apr --------- Co-authored-by: Amit Yadav <ay@Mac.lan>
1 parent e2e87a8 commit 18b66ae

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

x/amm/keeper/apply_join_pool_state_change.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package keeper
22

33
import (
4+
"fmt"
5+
46
"cosmossdk.io/math"
57
sdkmath "cosmossdk.io/math"
68
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -82,7 +84,7 @@ func (k Keeper) ApplyJoinPoolStateChange(
8284

8385
// Track amount in pool
8486
weightRecoveryFeeForPool := weightBalanceBonus.Abs().Mul(sdkmath.LegacyOneDec().Sub(params.WeightBreakingFeePortion))
85-
k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(coin.Denom, sdkmath.Int(weightRecoveryFeeForPool.Mul(sdkmath.LegacyDec(weightRecoveryFeeAmount)))))
87+
k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(coin.Denom, sdkmath.Int(weightRecoveryFeeForPool.Mul(weightRecoveryFeeAmount.ToLegacyDec()))))
8688
}
8789
}
8890
}
@@ -100,7 +102,16 @@ func (k Keeper) ApplyJoinPoolStateChange(
100102
}
101103
treasuryTokenAmount := k.bankKeeper.GetBalance(ctx, rebalanceTreasuryAddr, otherAsset.Token.Denom).Amount
102104

103-
bonusTokenAmount := joinCoins[0].Amount.ToLegacyDec().Mul(weightBalanceBonus).TruncateInt()
105+
// ensure token prices for in/out tokens set properly
106+
inTokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, joinCoins[0].Denom)
107+
if inTokenPrice.IsZero() {
108+
return fmt.Errorf("price for inToken not set: %s", joinCoins[0].Denom)
109+
}
110+
outTokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, otherAsset.Token.Denom)
111+
if outTokenPrice.IsZero() {
112+
return fmt.Errorf("price for outToken not set: %s", otherAsset.Token.Denom)
113+
}
114+
bonusTokenAmount := ((joinCoins[0].Amount.ToLegacyDec().Mul(weightBalanceBonus)).Mul(inTokenPrice).Quo(outTokenPrice)).TruncateInt()
104115

105116
if treasuryTokenAmount.LT(bonusTokenAmount) {
106117
bonusTokenAmount = treasuryTokenAmount

x/amm/keeper/keeper_join_pool_no_swap.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,16 @@ func (k Keeper) JoinPoolNoSwap(
116116
}
117117
treasuryTokenAmount := k.bankKeeper.GetBalance(ctx, rebalanceTreasuryAddr, otherAsset.Token.Denom).Amount
118118

119-
bonusTokenAmount := tokensJoined[0].Amount.ToLegacyDec().Mul(weightBalanceBonus).TruncateInt()
119+
// ensure token prices for in/out tokens set properly
120+
inTokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, tokensJoined[0].Denom)
121+
if inTokenPrice.IsZero() {
122+
return nil, sdkmath.ZeroInt(), fmt.Errorf("price for inToken not set: %s", tokensJoined[0].Denom)
123+
}
124+
outTokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, otherAsset.Token.Denom)
125+
if outTokenPrice.IsZero() {
126+
return nil, sdkmath.ZeroInt(), fmt.Errorf("price for outToken not set: %s", otherAsset.Token.Denom)
127+
}
128+
bonusTokenAmount := ((tokensJoined[0].Amount.ToLegacyDec().Mul(weightBalanceBonus)).Mul(inTokenPrice).Quo(outTokenPrice)).TruncateInt()
120129

121130
if treasuryTokenAmount.LT(bonusTokenAmount) {
122131
weightBalanceBonus = treasuryTokenAmount.ToLegacyDec().Quo(tokensJoined[0].Amount.ToLegacyDec())

x/amm/keeper/query_join_pool_estimation.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keeper
22

33
import (
44
"context"
5+
"fmt"
56

67
"cosmossdk.io/math"
78
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -87,7 +88,16 @@ func (k Keeper) JoinPoolEst(
8788
otherAsset = asset
8889
}
8990
treasuryTokenAmount := k.bankKeeper.GetBalance(ctx, rebalanceTreasuryAddr, otherAsset.Token.Denom).Amount
90-
bonusTokenAmount = tokensJoined[0].Amount.ToLegacyDec().Mul(weightBalanceBonus).TruncateInt()
91+
// ensure token prices for in/out tokens set properly
92+
inTokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, tokensJoined[0].Denom)
93+
if inTokenPrice.IsZero() {
94+
return nil, math.ZeroInt(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), sdk.Coin{}, fmt.Errorf("price for inToken not set: %s", tokensJoined[0].Denom)
95+
}
96+
outTokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, otherAsset.Token.Denom)
97+
if outTokenPrice.IsZero() {
98+
return nil, math.ZeroInt(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), sdk.Coin{}, fmt.Errorf("price for outToken not set: %s", otherAsset.Token.Denom)
99+
}
100+
bonusTokenAmount = ((tokensJoined[0].Amount.ToLegacyDec().Mul(weightBalanceBonus)).Mul(inTokenPrice).Quo(outTokenPrice)).TruncateInt()
91101

92102
if treasuryTokenAmount.LT(bonusTokenAmount) {
93103
bonusTokenAmount = treasuryTokenAmount

0 commit comments

Comments
 (0)