forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 4
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
fix(txfees, gamm): remove taker fee swap event from trade events #87
Draft
mtsitrin
wants to merge
3
commits into
main-dym
Choose a base branch
from
86-remove-taker-fee-swap-from-trade-events
base: main-dym
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
package keeper | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/dymensionxyz/sdk-utils/utils/uevent" | ||
|
||
"github.com/osmosis-labs/osmosis/v15/osmoutils" | ||
poolmanagertypes "github.com/osmosis-labs/osmosis/v15/x/poolmanager/types" | ||
"github.com/osmosis-labs/osmosis/v15/x/txfees/types" | ||
) | ||
|
||
var ( | ||
errUnswappableFeeToken = fmt.Errorf("fee token cannot be swapped to base denom") | ||
) | ||
|
||
// ChargeFeesFromPayer charges the specified taker fee from the payer's account and | ||
// processes it according to the fee token's properties. | ||
// Wrapper for ChargeFees that sends the fee to x/txfees in advance. | ||
|
@@ -44,74 +48,73 @@ func (k Keeper) ChargeFeesFromPayer( | |
// If the fee token is unknown, it is sent to the community pool. | ||
func (k Keeper) ChargeFees( | ||
ctx sdk.Context, | ||
takerFeeCoin sdk.Coin, | ||
takerFee sdk.Coin, | ||
beneficiary *sdk.AccAddress, | ||
payer string, // optional, only used for the event | ||
) error { | ||
if takerFeeCoin.IsZero() { | ||
if takerFee.IsZero() { | ||
// Nothing to charge | ||
return nil | ||
} | ||
|
||
// Swap the taker fee to the base denom | ||
baseDenomFee, communityPoolCoins, err := k.swapFeeToBaseDenom(ctx, takerFeeCoin) | ||
if err != nil { | ||
return fmt.Errorf("swap fee to base denom: %w", err) | ||
} | ||
takerFeeBaseDenom, err := k.swapFeeToBaseDenom(ctx, takerFee) | ||
// Send unknown fee tokens to the community pool | ||
if errors.Is(err, errUnswappableFeeToken) { | ||
k.Logger(ctx).With("fee", takerFee.String(), "payer", payer). | ||
Debug("Cannot swap fee to base denom. Sending it to the community pool.") | ||
|
||
// If the fee token is unknown or the swap is unsuccessful, the fee is sent to the community pool. | ||
if !communityPoolCoins.Empty() { | ||
// Send unknown fee tokens to the community pool | ||
err = k.communityPool.FundCommunityPool(ctx, communityPoolCoins, k.accountKeeper.GetModuleAddress(types.ModuleName)) | ||
err = k.communityPool.FundCommunityPool(ctx, sdk.NewCoins(takerFee), k.accountKeeper.GetModuleAddress(types.ModuleName)) | ||
if err != nil { | ||
return fmt.Errorf("unknown fee token: func community pool: %w", err) | ||
return fmt.Errorf("fund community pool: %w", err) | ||
} | ||
|
||
k.Logger(ctx).With("fee", communityPoolCoins.String(), "error", err). | ||
Error("Cannot swap fee to base denom. Send it to the community pool.") | ||
} | ||
|
||
// If the fee token is unknown or the swap is unsuccessful, emit event and return | ||
if baseDenomFee.Empty() { | ||
err = uevent.EmitTypedEvent(ctx, &types.EventChargeFee{ | ||
Payer: payer, | ||
TakerFee: takerFeeCoin.String(), | ||
Beneficiary: ValueFromPtr(beneficiary).String(), | ||
BeneficiaryRevenue: "", | ||
Payer: payer, | ||
TakerFee: takerFee.String(), | ||
CommunityPool: true, | ||
}) | ||
if err != nil { | ||
k.Logger(ctx).Error("Failed to emit event", "event", "EventChargeFee", "error", err) | ||
k.Logger(ctx).Error("emit event", "event", "EventChargeFee", "error", err) | ||
} | ||
return nil | ||
} else if err != nil { | ||
return fmt.Errorf("swap fee to base denom: %w", err) | ||
} | ||
|
||
// Nothing to charge after swapping (not supposed to happen actually) | ||
if takerFeeBaseDenom.IsNil() || takerFeeBaseDenom.IsZero() { | ||
k.Logger(ctx).With("fee", takerFee.String(), "payer", payer). | ||
Error("Fee after swapping to base denom is zero. Nothing to charge.") | ||
return nil | ||
} | ||
|
||
// Send 50% of the base denom fee to the beneficiary if presented | ||
var beneficiaryCoins sdk.Coins | ||
beneficiaryFee := sdk.Coins{} | ||
if beneficiary != nil { | ||
fee := baseDenomFee[0] | ||
// beneficiaryCoin = takerFeeCoin / 2 | ||
// note that beneficiaryCoin * 2 != takerFeeCoin because of the integer division rounding | ||
beneficiaryCoins = sdk.Coins{{Denom: fee.Denom, Amount: fee.Amount.QuoRaw(2)}} | ||
// takerFeeCoin = takerFeeCoin - beneficiaryCoin | ||
baseDenomFee = baseDenomFee.Sub(beneficiaryCoins...) | ||
|
||
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, *beneficiary, beneficiaryCoins) | ||
// beneficiaryFee = takerFee / 2 | ||
beneficiaryFee = sdk.Coins{sdk.NewCoin(takerFeeBaseDenom.Denom, takerFeeBaseDenom.Amount.QuoRaw(2))} | ||
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, *beneficiary, beneficiaryFee) | ||
if err != nil { | ||
return fmt.Errorf("send coins from fee payer to beneficiary: %w", err) | ||
} | ||
|
||
// update remaining taker fee | ||
takerFeeBaseDenom = takerFeeBaseDenom.Sub(beneficiaryFee[0]) | ||
} | ||
|
||
// Burn the remaining base denom fee | ||
err = k.bankKeeper.BurnCoins(ctx, types.ModuleName, baseDenomFee) | ||
err = k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(takerFeeBaseDenom)) | ||
if err != nil { | ||
return fmt.Errorf("burn coins: %w", err) | ||
} | ||
|
||
err = uevent.EmitTypedEvent(ctx, &types.EventChargeFee{ | ||
Payer: payer, | ||
TakerFee: baseDenomFee.String(), | ||
TakerFee: takerFee.String(), | ||
SwappedTakerFee: takerFeeBaseDenom.String(), | ||
Beneficiary: ValueFromPtr(beneficiary).String(), | ||
BeneficiaryRevenue: beneficiaryCoins.String(), | ||
BeneficiaryRevenue: beneficiaryFee.String(), | ||
}) | ||
if err != nil { | ||
k.Logger(ctx).Error("Failed to emit event", "event", "EventChargeFee", "error", err) | ||
|
@@ -128,44 +131,44 @@ func ValueFromPtr[T any](ptr *T) (zero T) { | |
} | ||
|
||
// swapFeeToBaseDenom swaps the taker fee coin to the base denom. | ||
// If the fee token is unknown, it is sent to the community pool. | ||
// Returns error if the fee token is unknown or if swapping fails. | ||
// The fee must be sent to the txfees module account beforehand. | ||
func (k Keeper) swapFeeToBaseDenom( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
ctx sdk.Context, | ||
takerFeeCoin sdk.Coin, | ||
) (baseDenomFee, communityPoolFee sdk.Coins, err error) { | ||
) (sdk.Coin, error) { | ||
baseDenom, err := k.GetBaseDenom(ctx) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("get base denom: %w", err) | ||
return sdk.Coin{}, fmt.Errorf("get base denom: %w", err) | ||
} | ||
moduleAddr := k.accountKeeper.GetModuleAddress(types.ModuleName) | ||
|
||
// The fee is already in the base denom | ||
if takerFeeCoin.Denom == baseDenom { | ||
return sdk.Coins{takerFeeCoin}, nil, nil | ||
return takerFeeCoin, nil | ||
} | ||
|
||
// Get a fee token for the coin | ||
feetoken, err := k.GetFeeToken(ctx, takerFeeCoin.Denom) | ||
if err != nil { | ||
return nil, sdk.Coins{takerFeeCoin}, nil | ||
return sdk.Coin{}, errUnswappableFeeToken | ||
} | ||
|
||
// Swap the coin to base denom | ||
var ( | ||
tokenOutAmount = sdk.ZeroInt() // Token amount in base denom | ||
route = []poolmanagertypes.SwapAmountInRoute{{ | ||
route = []poolmanagertypes.SwapAmountInRoute{{ | ||
PoolId: feetoken.PoolID, | ||
TokenOutDenom: baseDenom, | ||
}} | ||
) | ||
err = osmoutils.ApplyFuncIfNoError(ctx, func(ctx sdk.Context) error { | ||
tokenOutAmount, err = k.poolManager.RouteExactAmountIn(ctx, moduleAddr, route, takerFeeCoin, sdk.ZeroInt()) | ||
return err | ||
}) | ||
|
||
cacheCtx, write := ctx.CacheContext() | ||
tokenOutAmount, err := k.poolManager.RouteExactAmountIn(cacheCtx, moduleAddr, route, takerFeeCoin, sdk.ZeroInt()) | ||
if err != nil { | ||
return nil, sdk.Coins{takerFeeCoin}, nil | ||
return sdk.Coin{}, fmt.Errorf("failed to swap fee token: %w", err) | ||
} | ||
cacheCtx.WithEventManager(sdk.NewEventManager()) // This clears any events that were emitted | ||
write() // This writes the state changes but with cleared events | ||
|
||
return sdk.Coins{{Denom: baseDenom, Amount: tokenOutAmount}}, nil, nil | ||
return sdk.NewCoin(baseDenom, tokenOutAmount), nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chargeTakerFee
used to swap if it's unknown denom.as we don't expect pools with non-dym, we don't need this logic.
And if we would like it in the future, it needs to be part of the
x/txfees
logic anywayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's correct we don't expect pools with non-dym.
we may have usdc pools.