Skip to content

Commit

Permalink
Add slinky utils, use that to convert between market and currency pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Warehime committed Mar 8, 2024
1 parent 667a804 commit ba27204
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 19 deletions.
29 changes: 29 additions & 0 deletions protocol/lib/slinky/utils.go
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()
}
34 changes: 34 additions & 0 deletions protocol/lib/slinky/utils_test.go
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)
}
})
}
}
24 changes: 7 additions & 17 deletions protocol/x/prices/keeper/slinky_adapter.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package keeper

import (
"cosmossdk.io/math"
"fmt"
"strings"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
slinkytypes "github.com/skip-mev/slinky/pkg/types"
oracletypes "github.com/skip-mev/slinky/x/oracle/types"
"strings"

"github.com/dydxprotocol/v4-chain/protocol/lib/slinky"
)

/*
Expand All @@ -22,7 +25,7 @@ func (k Keeper) GetCurrencyPairFromID(ctx sdk.Context, id uint64) (cp slinkytype
if !found {
return cp, false
}
cp, err := MarketPairToCurrencyPair(mp.Pair)
cp, err := slinky.MarketPairToCurrencyPair(mp.Pair)
if err != nil {
k.Logger(ctx).Error("CurrencyPairFromString", "error", err)
return cp, false
Expand All @@ -33,7 +36,7 @@ func (k Keeper) GetCurrencyPairFromID(ctx sdk.Context, id uint64) (cp slinkytype
func (k Keeper) GetIDForCurrencyPair(ctx sdk.Context, cp slinkytypes.CurrencyPair) (uint64, bool) {
mps := k.GetAllMarketParams(ctx)
for _, mp := range mps {
mpCp, err := MarketPairToCurrencyPair(mp.Pair)
mpCp, err := slinky.MarketPairToCurrencyPair(mp.Pair)
if err != nil {
k.Logger(ctx).Error("market param pair invalid format", "pair", mp.Pair)
continue
Expand All @@ -58,16 +61,3 @@ func (k Keeper) GetPriceForCurrencyPair(ctx sdk.Context, cp slinkytypes.Currency
Price: math.NewIntFromUint64(mp.Price),
}, nil
}

func MarketPairToCurrencyPair(marketPair string) (slinkytypes.CurrencyPair, error) {
split := strings.Split(marketPair, "-")
if len(split) != 2 {
return slinkytypes.CurrencyPair{}, fmt.Errorf("incorrectly formatted CurrencyPair: %s", marketPair)
}
cp := slinkytypes.CurrencyPair{
Base: strings.ToUpper(split[0]),
Quote: strings.ToUpper(split[1]),
}

return cp, cp.ValidateBasic()
}
4 changes: 2 additions & 2 deletions protocol/x/prices/keeper/slinky_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package keeper_test

import (
"fmt"
"github.com/dydxprotocol/v4-chain/protocol/lib/slinky"
"testing"

oracletypes "github.com/skip-mev/slinky/pkg/types"
"github.com/stretchr/testify/require"

"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/prices/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)

Expand Down Expand Up @@ -117,7 +117,7 @@ func TestMarketPairToCurrencyPair(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.marketPair, func(t *testing.T) {
cp, err := keeper.MarketPairToCurrencyPair(tc.marketPair)
cp, err := slinky.MarketPairToCurrencyPair(tc.marketPair)
if tc.shouldFail {
require.Error(t, err)
} else {
Expand Down

0 comments on commit ba27204

Please sign in to comment.