Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change fee checker logic #8

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 53 additions & 26 deletions x/move/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand Down Expand Up @@ -51,40 +52,46 @@
// convert baseDenom min gas prices to quote denom prices
// and check the paid fee is enough or not.
isSufficient := false
sumInBaseUnit := math.ZeroInt()

if fc.keeper != nil {
baseDenom := fc.keeper.BaseDenom(ctx)
requiredBaseAmount := requiredFees.AmountOf(baseDenom)
for _, coin := range feeCoins {
if baseDenom == coin.Denom {
continue
}

if found, err := fc.keeper.HasDexPair(ctx, coin.Denom); err != nil {
return nil, 0, err
} else if !found {
continue
}

quotePrice, err := fc.keeper.GetPoolSpotPrice(ctx, coin.Denom)
if err != nil {
return nil, 0, err
}

if quotePrice.IsZero() {
continue
}

quoteValueInBaseUnit := quotePrice.MulInt(coin.Amount).TruncateInt()
if quoteValueInBaseUnit.GTE(requiredBaseAmount) {
isSufficient = true
break
requiredBaseAmount := requiredFees.AmountOfNoDenomValidation(baseDenom)

// If the requiredBaseAmount is zero, it means the operator
// do not want to receive base denom fee but want to get other
// denom fee.
if !requiredBaseAmount.IsZero() {
for _, coin := range feeCoins {
quotePrice, skip, err := fc.fetchOrSkipPrice(ctx, baseDenom, coin.Denom)
if err != nil {
return nil, 0, err
}

Check warning on line 69 in x/move/ante/fee.go

View check run for this annotation

Codecov / codecov/patch

x/move/ante/fee.go#L68-L69

Added lines #L68 - L69 were not covered by tests
if skip {
continue

Check warning on line 71 in x/move/ante/fee.go

View check run for this annotation

Codecov / codecov/patch

x/move/ante/fee.go#L71

Added line #L71 was not covered by tests
}

// sum the converted fee values
quoteValueInBaseUnit := quotePrice.MulInt(coin.Amount).TruncateInt()
sumInBaseUnit = sumInBaseUnit.Add(quoteValueInBaseUnit)

// check the sum is greater than the required.
if sumInBaseUnit.GTE(requiredBaseAmount) {
isSufficient = true
break
}
}
}
}

if !isSufficient {
return nil, 0, errors.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; got: %s required: %s", feeCoins, requiredFees)
return nil, 0, errors.Wrapf(
sdkerrors.ErrInsufficientFee,
"insufficient fees; got: %s (sum %s), required: %s",
feeCoins,
sumInBaseUnit,
requiredFees,
)
}
}
}
Expand All @@ -94,3 +101,23 @@
// then we need to compute all dex prices of all fee coins
return feeCoins, 1 /* FIFO */, nil
}

func (fc MempoolFeeChecker) fetchOrSkipPrice(ctx sdk.Context, baseDenom, quoteDenom string) (price sdk.Dec, skip bool, err error) {
if quoteDenom == baseDenom {
return sdk.OneDec(), false, nil
}

Check warning on line 108 in x/move/ante/fee.go

View check run for this annotation

Codecov / codecov/patch

x/move/ante/fee.go#L107-L108

Added lines #L107 - L108 were not covered by tests

if found, err := fc.keeper.HasDexPair(ctx, quoteDenom); err != nil {
return sdk.ZeroDec(), false, err

Check warning on line 111 in x/move/ante/fee.go

View check run for this annotation

Codecov / codecov/patch

x/move/ante/fee.go#L111

Added line #L111 was not covered by tests
} else if !found {
return sdk.ZeroDec(), true, nil
}

Check warning on line 114 in x/move/ante/fee.go

View check run for this annotation

Codecov / codecov/patch

x/move/ante/fee.go#L113-L114

Added lines #L113 - L114 were not covered by tests

if quotePrice, err := fc.keeper.GetPoolSpotPrice(ctx, quoteDenom); err != nil {
return sdk.ZeroDec(), false, err

Check warning on line 117 in x/move/ante/fee.go

View check run for this annotation

Codecov / codecov/patch

x/move/ante/fee.go#L117

Added line #L117 was not covered by tests
} else if quotePrice.IsZero() {
return sdk.ZeroDec(), true, nil

Check warning on line 119 in x/move/ante/fee.go

View check run for this annotation

Codecov / codecov/patch

x/move/ante/fee.go#L119

Added line #L119 was not covered by tests
} else {
return quotePrice, false, nil
}
}
Loading