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

[performance]: Better key formatting in taker fee #7497

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions x/poolmanager/taker_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ func (k Keeper) GetDefaultTakerFee(ctx sdk.Context) sdk.Dec {
// it is deleted from state.
func (k Keeper) SetDenomPairTakerFee(ctx sdk.Context, denom0, denom1 string, takerFee osmomath.Dec) {
store := ctx.KVStore(k.storeKey)
key := types.FormatDenomTradePairKey(denom0, denom1)
// if given taker fee is equal to the default taker fee,
// delete whatever we have in current state to use default taker fee.
// TODO: This logic is actually wrong imo, where it can be valid to set an override over the default.
if takerFee.Equal(k.GetDefaultTakerFee(ctx)) {
store.Delete(types.FormatDenomTradePairKey(denom0, denom1))
store.Delete(key)
return
} else {
osmoutils.MustSetDec(store, types.FormatDenomTradePairKey(denom0, denom1), takerFee)
osmoutils.MustSetDec(store, key, takerFee)
}
}

Expand Down
12 changes: 8 additions & 4 deletions x/poolmanager/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package types

import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -67,9 +67,13 @@ func FormatModuleRouteKey(poolId uint64) []byte {
// FormatDenomTradePairKey serializes denom trade pair to bytes.
// Denom trade pair is automatically sorted lexicographically.
func FormatDenomTradePairKey(denom0, denom1 string) []byte {
denoms := []string{denom0, denom1}
sort.Strings(denoms)
return []byte(fmt.Sprintf("%s%s%s%s%s", DenomTradePairPrefix, KeySeparator, denoms[0], KeySeparator, denoms[1]))
denomA, denomB := denom0, denom1
if denom0 > denom1 {
denomA, denomB = denom1, denom0
}
var buffer bytes.Buffer
fmt.Fprintf(&buffer, "%s%s%s%s%s", DenomTradePairPrefix, KeySeparator, denomA, KeySeparator, denomB)
return buffer.Bytes()
}

// ParseModuleRouteFromBz parses the raw bytes into ModuleRoute.
Expand Down