Skip to content

Commit

Permalink
Always sort tokens when making pairID
Browse files Browse the repository at this point in the history
  • Loading branch information
jcompagni10 committed Sep 3, 2024
1 parent d557574 commit 1d258fb
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 13 deletions.
2 changes: 1 addition & 1 deletion x/dex/keeper/grpc_query_simulate_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (k Keeper) SimulateDeposit(

callerAddr := sdk.MustAccAddressFromBech32(msg.Creator)
receiverAddr := sdk.MustAccAddressFromBech32(msg.Receiver)
pairID, err := types.NewPairIDFromUnsorted(msg.TokenA, msg.TokenB)
pairID, err := types.NewPairID(msg.TokenA, msg.TokenB)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/dex/keeper/grpc_query_simulate_withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k Keeper) SimulateWithdrawal(

callerAddr := sdk.MustAccAddressFromBech32(msg.Creator)
receiverAddr := sdk.MustAccAddressFromBech32(msg.Receiver)
pairID, err := types.NewPairIDFromUnsorted(msg.TokenA, msg.TokenB)
pairID, err := types.NewPairID(msg.TokenA, msg.TokenB)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions x/dex/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (k MsgServer) Deposit(
callerAddr := sdk.MustAccAddressFromBech32(msg.Creator)
receiverAddr := sdk.MustAccAddressFromBech32(msg.Receiver)

pairID, err := types.NewPairIDFromUnsorted(msg.TokenA, msg.TokenB)
pairID, err := types.NewPairID(msg.TokenA, msg.TokenB)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (k MsgServer) Withdrawal(
callerAddr := sdk.MustAccAddressFromBech32(msg.Creator)
receiverAddr := sdk.MustAccAddressFromBech32(msg.Receiver)

pairID, err := types.NewPairIDFromUnsorted(msg.TokenA, msg.TokenB)
pairID, err := types.NewPairID(msg.TokenA, msg.TokenB)
if err != nil {
return nil, err
}
Expand Down
12 changes: 4 additions & 8 deletions x/dex/types/pair_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
sdkerrors "cosmossdk.io/errors"
)

func NewPairID(token0, token1 string) (*PairID, error) {
func NewPairID(tokenA, tokenB string) (*PairID, error) {
token0, token1 := SortTokens(tokenA, tokenB)
if token0 == token1 {
return nil, sdkerrors.Wrapf(ErrInvalidTradingPair, "%s<>%s", token0, token1)
}
Expand All @@ -17,19 +18,14 @@ func NewPairID(token0, token1 string) (*PairID, error) {
}, nil
}

func MustNewPairID(token0, token1 string) *PairID {
pairID, err := NewPairID(token0, token1)
func MustNewPairID(tokenA, tokenB string) *PairID {
pairID, err := NewPairID(tokenA, tokenB)
if err != nil {
panic(err)
}
return pairID
}

func NewPairIDFromUnsorted(tokenA, tokenB string) (*PairID, error) {
token0, token1 := SortTokens(tokenA, tokenB)
return NewPairID(token0, token1)
}

func (p *PairID) CanonicalString() string {
return fmt.Sprintf("%s<>%s", p.Token0, p.Token1)
}
Expand Down
2 changes: 1 addition & 1 deletion x/dex/types/trade_pair_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (p TradePairID) MustPairID() *PairID {
}

func (p TradePairID) PairID() (*PairID, error) {
return NewPairIDFromUnsorted(p.MakerDenom, p.TakerDenom)
return NewPairID(p.MakerDenom, p.TakerDenom)
}

func (p TradePairID) Reversed() *TradePairID {
Expand Down

0 comments on commit 1d258fb

Please sign in to comment.