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: uni-directional trading pair taker fee support #510

Merged
merged 2 commits into from
Sep 11, 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
7 changes: 4 additions & 3 deletions domain/mvc/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/sqsdomain"

"github.com/osmosis-labs/osmosis/osmomath"
)

// CandidateRouteSearchDataUpdateListener is the interface for the candidate route search data holder.
Expand All @@ -29,13 +30,13 @@ type RouterRepository interface {
CandidateRouteSearchDataHolder

// GetTakerFee returns the taker fee for a given pair of denominations
// Sorts the denominations lexicographically before looking up the taker fee.
// Sorting is no longer performed before looking up as bi-directional taker fees are stored.
// Returns true if the taker fee for a given denomimnation is found. False otherwise.
GetTakerFee(denom0, denom1 string) (osmomath.Dec, bool)
// GetAllTakerFees returns all taker fees
GetAllTakerFees() sqsdomain.TakerFeeMap
// SetTakerFee sets the taker fee for a given pair of denominations
// Sorts the denominations lexicographically before storing the taker fee.
// Sorting is no longer performed before storing as bi-directional taker fee is supported.
SetTakerFee(denom0, denom1 string, takerFee osmomath.Dec)
// SetTakerFees sets taker fees on router repository
SetTakerFees(takerFees sqsdomain.TakerFeeMap)
Expand Down
17 changes: 4 additions & 13 deletions router/repository/memory_router_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import (
"sync"

"cosmossdk.io/math"
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/mvc"
"github.com/osmosis-labs/sqs/log"
"github.com/osmosis-labs/sqs/sqsdomain"

"github.com/osmosis-labs/osmosis/osmomath"
)

// RouterRepository represents the contract for a repository handling router information
type RouterRepository interface {
mvc.CandidateRouteSearchDataHolder

// GetTakerFee returns the taker fee for a given pair of denominations
// Sorts the denominations lexicographically before looking up the taker fee.
// Sorting is no longer performed before looking up as bi-directional taker fees are stored.
// Returns true if the taker fee for a given denomimnation is found. False otherwise.
GetTakerFee(denom0, denom1 string) (osmomath.Dec, bool)
// GetAllTakerFees returns all taker fees
GetAllTakerFees() sqsdomain.TakerFeeMap
// SetTakerFee sets the taker fee for a given pair of denominations
// Sorts the denominations lexicographically before storing the taker fee.
// Sorting is no longer performed before storing as bi-directional taker fee is supported.
SetTakerFee(denom0, denom1 string, takerFee osmomath.Dec)
SetTakerFees(takerFees sqsdomain.TakerFeeMap)
}
Expand Down Expand Up @@ -75,11 +76,6 @@ func (r *routerRepo) GetAllTakerFees() sqsdomain.TakerFeeMap {

// GetTakerFee implements RouterRepository.
func (r *routerRepo) GetTakerFee(denom0 string, denom1 string) (math.LegacyDec, bool) {
// Ensure increasing lexicographic order.
if denom1 < denom0 {
denom0, denom1 = denom1, denom0
}

takerFeeAny, ok := r.takerFeeMap.Load(sqsdomain.DenomPair{Denom0: denom0, Denom1: denom1})

if !ok {
Expand All @@ -96,11 +92,6 @@ func (r *routerRepo) GetTakerFee(denom0 string, denom1 string) (math.LegacyDec,

// SetTakerFee implements RouterRepository.
func (r *routerRepo) SetTakerFee(denom0 string, denom1 string, takerFee math.LegacyDec) {
// Ensure increasing lexicographic order.
if denom1 < denom0 {
denom0, denom1 = denom1, denom0
}

r.takerFeeMap.Store(sqsdomain.DenomPair{Denom0: denom0, Denom1: denom1}, takerFee)
}

Expand Down
2 changes: 1 addition & 1 deletion router/usecase/routertesting/parsing/taker_fees.json

Large diffs are not rendered by default.

22 changes: 4 additions & 18 deletions sqsdomain/taker_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import (
)

// DenomPair encapsulates a pair of denoms.
// The order of the denoms ius that Denom0 precedes
// Denom1 lexicographically.
type DenomPair struct {
Denom0 string
Denom1 string
}

// TakerFeeMap is a map of DenomPair to taker fee.
// It sorts the denoms lexicographically before looking up the taker fee.
// Sorting is no longer performed since bi-directional taker fee is supported.
type TakerFeeMap map[DenomPair]osmomath.Dec

var _ json.Marshaler = &TakerFeeMap{}
Expand Down Expand Up @@ -56,25 +54,17 @@ func (tfm TakerFeeMap) UnmarshalJSON(data []byte) error {
}

// Has returns true if the taker fee for the given denoms is found.
// It sorts the denoms lexicographically before looking up the taker fee.
// Sorting is no longer performed since bi-directional taker fees are stored.
func (tfm TakerFeeMap) Has(denom0, denom1 string) bool {
// Ensure increasing lexicographic order.
if denom1 < denom0 {
denom0, denom1 = denom1, denom0
}

_, found := tfm[DenomPair{Denom0: denom0, Denom1: denom1}]
return found
}

// GetTakerFee returns the taker fee for the given denoms.
// It sorts the denoms lexicographically before looking up the taker fee.
// Sorting is no longer performed since bi-directional taker fees are stored.
// Returns error if the taker fee is not found.
func (tfm TakerFeeMap) GetTakerFee(denom0, denom1 string) osmomath.Dec {
// Ensure increasing lexicographic order.
if denom1 < denom0 {
denom0, denom1 = denom1, denom0
}

takerFee, found := tfm[DenomPair{Denom0: denom0, Denom1: denom1}]

Expand All @@ -86,12 +76,8 @@ func (tfm TakerFeeMap) GetTakerFee(denom0, denom1 string) osmomath.Dec {
}

// SetTakerFee sets the taker fee for the given denoms.
// It sorts the denoms lexicographically before setting the taker fee.
// Sorting is no longer performed since bi-directional taker fee is supported.
func (tfm TakerFeeMap) SetTakerFee(denom0, denom1 string, takerFee osmomath.Dec) {
// Ensure increasing lexicographic order.
if denom1 < denom0 {
denom0, denom1 = denom1, denom0
}

tfm[DenomPair{Denom0: denom0, Denom1: denom1}] = takerFee
}
Expand Down
Loading