Skip to content

Commit

Permalink
Better key formatting (#7497) (#7505)
Browse files Browse the repository at this point in the history
(cherry picked from commit a767fab)

Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
mergify[bot] and ValarDragon authored Feb 16, 2024
1 parent 73d4377 commit f0b487c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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

0 comments on commit f0b487c

Please sign in to comment.