From 888986e527a3142e241e492473e89ccc3d1f1f5d Mon Sep 17 00:00:00 2001 From: beer-1 Date: Mon, 19 Aug 2024 17:08:51 +0900 Subject: [PATCH 1/2] fix naming and do not truncate decimals at price calc fix comment --- x/move/ante/fee.go | 19 ++++++++++--------- x/move/ante/fee_test.go | 4 ++-- x/move/keeper/dex.go | 8 ++++---- x/move/keeper/dex_test.go | 4 ++-- x/move/types/connector.go | 8 ++++---- x/move/types/connector_test.go | 4 ++-- x/move/types/exported.go | 2 +- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/x/move/ante/fee.go b/x/move/ante/fee.go index d741d95c..8c94a295 100644 --- a/x/move/ante/fee.go +++ b/x/move/ante/fee.go @@ -40,7 +40,7 @@ func (fc MempoolFeeChecker) CheckTxFeeWithMinGasPrices(ctx sdk.Context, tx sdk.T priority := int64(1) if ctx.IsCheckTx() { minGasPrices := ctx.MinGasPrices() - feeValueInBaseUnit := math.ZeroInt() + totalFeeBaseAmount := math.ZeroInt() var baseDenom string var err error @@ -58,16 +58,17 @@ func (fc MempoolFeeChecker) CheckTxFeeWithMinGasPrices(ctx sdk.Context, tx sdk.T minGasPrices = combinedMinGasPrices(baseDenom, baseMinGasPrice, minGasPrices) for _, coin := range feeTx.GetFee() { - quotePrice, err := fc.fetchPrice(ctx, baseDenom, coin.Denom) + basePrice, err := fc.fetchPrice(ctx, baseDenom, coin.Denom) if err != nil { return nil, 1, err } - quoteValueInBaseUnit := quotePrice.MulInt(coin.Amount).TruncateInt() - feeValueInBaseUnit = feeValueInBaseUnit.Add(quoteValueInBaseUnit) + quoteAmount := coin.Amount + baseAmount := basePrice.MulInt(quoteAmount).TruncateInt() + totalFeeBaseAmount = totalFeeBaseAmount.Add(baseAmount) } - if feeValueInBaseUnit.GT(math.OneInt()) { - priority = feeValueInBaseUnit.Int64() + if totalFeeBaseAmount.GT(math.OneInt()) { + priority = totalFeeBaseAmount.Int64() } } @@ -84,7 +85,7 @@ func (fc MempoolFeeChecker) CheckTxFeeWithMinGasPrices(ctx sdk.Context, tx sdk.T requiredBaseAmount := requiredFees.AmountOfNoDenomValidation(baseDenom) // converting to base token only works when the requiredBaseAmount is non-zero. - isSufficient = !requiredBaseAmount.IsZero() && feeValueInBaseUnit.GTE(requiredBaseAmount) + isSufficient = !requiredBaseAmount.IsZero() && totalFeeBaseAmount.GTE(requiredBaseAmount) } if !isSufficient { @@ -114,9 +115,9 @@ func (fc MempoolFeeChecker) fetchPrice(ctx sdk.Context, baseDenom, quoteDenom st return math.LegacyZeroDec(), nil } - if quotePrice, err := fc.keeper.GetQuoteSpotPrice(ctx, quoteDenom); err != nil { + if basePrice, err := fc.keeper.GetBaseSpotPrice(ctx, quoteDenom); err != nil { return math.LegacyZeroDec(), err } else { - return quotePrice, nil + return basePrice, nil } } diff --git a/x/move/ante/fee_test.go b/x/move/ante/fee_test.go index 2a6e9488..4f154c9d 100644 --- a/x/move/ante/fee_test.go +++ b/x/move/ante/fee_test.go @@ -38,7 +38,7 @@ func (k TestAnteKeeper) HasDexPair(_ context.Context, denomQuote string) (bool, return true, nil } -func (k TestAnteKeeper) GetQuoteSpotPrice(_ context.Context, denomQuote string) (quotePrice math.LegacyDec, err error) { +func (k TestAnteKeeper) GetBaseSpotPrice(_ context.Context, denomQuote string) (quotePrice math.LegacyDec, err error) { balances, found := k.pools[denomQuote] if !found { return math.LegacyZeroDec(), fmt.Errorf("not found") @@ -49,7 +49,7 @@ func (k TestAnteKeeper) GetQuoteSpotPrice(_ context.Context, denomQuote string) return math.LegacyZeroDec(), fmt.Errorf("not found") } - return types.GetQuoteSpotPrice(balances[0], balances[1], weights[0], weights[1]), nil + return types.GetBaseSpotPrice(balances[0], balances[1], weights[0], weights[1]), nil } func (k TestAnteKeeper) BaseDenom(_ context.Context) (string, error) { diff --git a/x/move/keeper/dex.go b/x/move/keeper/dex.go index bb7a3063..0ff1a2a3 100644 --- a/x/move/keeper/dex.go +++ b/x/move/keeper/dex.go @@ -134,9 +134,9 @@ func (k Keeper) getMetadataLP( return vmtypes.NewAccountAddressFromBytes(bz) } -// GetQuoteSpotPrice return quote price in base unit -// `price` * `quote_amount` == `quote_value_in_base_unit` -func (k DexKeeper) GetQuoteSpotPrice( +// GetBaseSpotPrice return base coin spot price +// `base_price` * `quote_amount` == `base_amount` +func (k DexKeeper) GetBaseSpotPrice( ctx context.Context, denomQuote string, ) (math.LegacyDec, error) { @@ -150,7 +150,7 @@ func (k DexKeeper) GetQuoteSpotPrice( return math.LegacyZeroDec(), err } - return types.GetQuoteSpotPrice(balanceBase, balanceQuote, weightBase, weightQuote), nil + return types.GetBaseSpotPrice(balanceBase, balanceQuote, weightBase, weightQuote), nil } func (k DexKeeper) getPoolInfo(ctx context.Context, metadataLP vmtypes.AccountAddress) ( diff --git a/x/move/keeper/dex_test.go b/x/move/keeper/dex_test.go index 5c848b6b..8ee2d77d 100644 --- a/x/move/keeper/dex_test.go +++ b/x/move/keeper/dex_test.go @@ -165,7 +165,7 @@ func Test_ReadWeights(t *testing.T) { require.Equal(t, math.LegacyNewDecWithPrec(2, 1), weightQuote) } -func Test_GetQuoteSpotPrice(t *testing.T) { +func Test_GetBaseSpotPrice(t *testing.T) { ctx, input := createDefaultTestInput(t) dexKeeper := keeper.NewDexKeeper(&input.MoveKeeper) @@ -191,7 +191,7 @@ func Test_GetQuoteSpotPrice(t *testing.T) { }) require.NoError(t, err) - quotePrice, err := dexKeeper.GetQuoteSpotPrice(ctx, denomQuote) + quotePrice, err := dexKeeper.GetBaseSpotPrice(ctx, denomQuote) require.NoError(t, err) require.Equal(t, math.LegacyOneDec(), quotePrice) } diff --git a/x/move/types/connector.go b/x/move/types/connector.go index 7953c54a..d3dbb2e9 100644 --- a/x/move/types/connector.go +++ b/x/move/types/connector.go @@ -409,13 +409,13 @@ func GetPoolWeights( return weightCoinA, weightCoinB, nil } -// GetQuoteSpotPrice return quote price in base unit -func GetQuoteSpotPrice( +// GetBaseSpotPrice return base coin spot price +func GetBaseSpotPrice( balanceBase, balanceQuote math.Int, weightBase, weightQuote math.LegacyDec, ) math.LegacyDec { - numerator := weightQuote.MulInt(balanceBase).TruncateDec() - denominator := weightBase.MulInt(balanceQuote).TruncateDec() + numerator := weightQuote.MulInt(balanceBase) + denominator := weightBase.MulInt(balanceQuote) return numerator.Quo(denominator) } diff --git a/x/move/types/connector_test.go b/x/move/types/connector_test.go index b7d2b699..b110da87 100644 --- a/x/move/types/connector_test.go +++ b/x/move/types/connector_test.go @@ -26,8 +26,8 @@ func Test_GetDexWeight(t *testing.T) { require.Equal(t, math.LegacyNewDecWithPrec(4, 1), weightQuote) } -func Test_GetQuoteSpotPrice(t *testing.T) { - price := types.GetQuoteSpotPrice( +func Test_GetBaseSpotPrice(t *testing.T) { + price := types.GetBaseSpotPrice( math.NewInt(1_000_000), math.NewInt(8_000_000), math.LegacyNewDecWithPrec(2, 1), diff --git a/x/move/types/exported.go b/x/move/types/exported.go index f1e29dfb..d7ab20db 100644 --- a/x/move/types/exported.go +++ b/x/move/types/exported.go @@ -8,7 +8,7 @@ import ( type AnteKeeper interface { HasDexPair(ctx context.Context, denom string) (bool, error) - GetQuoteSpotPrice(ctx context.Context, denomQuote string) (math.LegacyDec, error) + GetBaseSpotPrice(ctx context.Context, denomQuote string) (math.LegacyDec, error) BaseDenom(ctx context.Context) (string, error) BaseMinGasPrice(ctx context.Context) (math.LegacyDec, error) } From dccf86adbaf54682eac52f0c262e24cbcc5f9d0f Mon Sep 17 00:00:00 2001 From: beer-1 Date: Tue, 20 Aug 2024 11:56:59 +0900 Subject: [PATCH 2/2] add pool check --- x/distribution/keeper/allocation.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index bfdd6600..dc9ad13e 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -84,10 +84,10 @@ func (k Keeper) AllocateTokens(ctx context.Context, totalPreviousPower int64, bo poolReward := feesCollected.MulDecTruncate(voteMultiplier).MulDecTruncate(poolFraction) poolDenom := rewardWeight.Denom - poolSize := bondedTokensSum[poolDenom] + poolSize, ok := bondedTokensSum[poolDenom] // if poolSize is zero, skip allocation and then the poolReward will be allocated to community pool - if poolSize.IsZero() { + if !ok || poolSize.IsZero() { continue }