forked from dydxprotocol/v4-chain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slinky utils, use that to convert between market and currency pair
- Loading branch information
1 parent
667a804
commit ba27204
Showing
4 changed files
with
72 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package slinky | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/skip-mev/slinky/pkg/types" | ||
) | ||
|
||
/* | ||
* Slinky utility functions | ||
* | ||
* This file contains functions for converting between x/prices types and slinky's x/oracle equivalents. | ||
*/ | ||
|
||
// MarketPairToCurrencyPair converts a base and quote pair from MarketPrice format (for example BTC-ETH) | ||
// to a currency pair type. Returns an error if unable to convert. | ||
func MarketPairToCurrencyPair(marketPair string) (types.CurrencyPair, error) { | ||
split := strings.Split(marketPair, "-") | ||
if len(split) != 2 { | ||
return types.CurrencyPair{}, fmt.Errorf("incorrectly formatted CurrencyPair: %s", marketPair) | ||
} | ||
cp := types.CurrencyPair{ | ||
Base: strings.ToUpper(split[0]), | ||
Quote: strings.ToUpper(split[1]), | ||
} | ||
|
||
return cp, cp.ValidateBasic() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package slinky_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/skip-mev/slinky/pkg/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/lib/slinky" | ||
) | ||
|
||
func TestMarketPairToCurrencyPair(t *testing.T) { | ||
testCases := []struct { | ||
mp string | ||
cp types.CurrencyPair | ||
err error | ||
}{ | ||
{mp: "FOO-BAR", cp: types.CurrencyPair{Base: "FOO", Quote: "BAR"}, err: nil}, | ||
{mp: "FOOBAR", cp: types.CurrencyPair{}, err: fmt.Errorf("incorrectly formatted CurrencyPair: FOOBAR")}, | ||
{mp: "FOO/BAR", cp: types.CurrencyPair{}, err: fmt.Errorf("incorrectly formatted CurrencyPair: FOOBAR")}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("TestMarketPair %s", tc.mp), func(t *testing.T) { | ||
cp, err := slinky.MarketPairToCurrencyPair(tc.mp) | ||
if tc.err != nil { | ||
require.Error(t, err, tc.err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tc.cp, cp) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters