From 1d258fbbb821bdb0d93ad3165c78748fd4339b72 Mon Sep 17 00:00:00 2001 From: Julian Compagni Portis Date: Tue, 3 Sep 2024 14:04:15 -0400 Subject: [PATCH] Always sort tokens when making pairID --- x/dex/keeper/grpc_query_simulate_deposit.go | 2 +- x/dex/keeper/grpc_query_simulate_withdrawal.go | 2 +- x/dex/keeper/msg_server.go | 4 ++-- x/dex/types/pair_id.go | 12 ++++-------- x/dex/types/trade_pair_id.go | 2 +- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/x/dex/keeper/grpc_query_simulate_deposit.go b/x/dex/keeper/grpc_query_simulate_deposit.go index d1f6ca489..ca3bb77c3 100644 --- a/x/dex/keeper/grpc_query_simulate_deposit.go +++ b/x/dex/keeper/grpc_query_simulate_deposit.go @@ -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 } diff --git a/x/dex/keeper/grpc_query_simulate_withdrawal.go b/x/dex/keeper/grpc_query_simulate_withdrawal.go index d0bcbfdf7..4348dd7eb 100644 --- a/x/dex/keeper/grpc_query_simulate_withdrawal.go +++ b/x/dex/keeper/grpc_query_simulate_withdrawal.go @@ -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 } diff --git a/x/dex/keeper/msg_server.go b/x/dex/keeper/msg_server.go index 20ee76cc4..df50b21a1 100644 --- a/x/dex/keeper/msg_server.go +++ b/x/dex/keeper/msg_server.go @@ -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 } @@ -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 } diff --git a/x/dex/types/pair_id.go b/x/dex/types/pair_id.go index dd2b9b9e6..4809e7e38 100644 --- a/x/dex/types/pair_id.go +++ b/x/dex/types/pair_id.go @@ -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) } @@ -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) } diff --git a/x/dex/types/trade_pair_id.go b/x/dex/types/trade_pair_id.go index 54b8469b6..565bd0185 100644 --- a/x/dex/types/trade_pair_id.go +++ b/x/dex/types/trade_pair_id.go @@ -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 {