Skip to content

Commit

Permalink
fix naming and do not truncate decimals at price calc
Browse files Browse the repository at this point in the history
fix comment
  • Loading branch information
beer-1 committed Aug 19, 2024
1 parent 99bc757 commit 888986e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 24 deletions.
19 changes: 10 additions & 9 deletions x/move/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions x/move/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions x/move/keeper/dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) (
Expand Down
4 changes: 2 additions & 2 deletions x/move/keeper/dex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions x/move/types/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions x/move/types/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion x/move/types/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 888986e

Please sign in to comment.