Skip to content

Commit

Permalink
remove medianizer (#236)
Browse files Browse the repository at this point in the history
* remove medianizer

* medianizer -> resolver

* add resolver file

* add comment
  • Loading branch information
BrendanChou authored Sep 13, 2023
1 parent a0efdc8 commit 086d891
Show file tree
Hide file tree
Showing 39 changed files with 130 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (eqh *ExchangeQueryHandlerImpl) Query(
prices, unavailableTickers, err := exchangeQueryDetails.PriceFunction(
response,
tickerToPriceExponent,
&lib.MedianizerImpl{},
lib.Median[uint64],
)
if err != nil {
return nil, nil, price_function.NewExchangeError(exchangeQueryDetails.Exchange, err.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"context"
"errors"
"fmt"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed"
"net/http"
"testing"
"time"

pf_constants "github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/constants"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/constants/exchange_common"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
pft "github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/types"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestQuery(t *testing.T) {
priceFunc func(
response *http.Response,
tickerToPriceExponent map[string]int32,
medianizer lib.Medianizer,
resolver pft.Resolver,
) (prices map[string]uint64, unavailable map[string]error, err error)
marketIds []types.MarketId
requestHandler *mocks.RequestHandler
Expand Down Expand Up @@ -319,7 +319,7 @@ func generateTestMarketPriceExponentMap() map[types.MarketId]types.Exponent {
func priceFunc(
response *http.Response,
tickerToPriceExponent map[string]int32,
medianizer lib.Medianizer,
resolver pft.Resolver,
) (prices map[string]uint64, unavailable map[string]error, err error) {
prices = make(map[string]uint64, len(tickerToPriceExponent))
for ticker := range tickerToPriceExponent {
Expand All @@ -331,7 +331,7 @@ func priceFunc(
func priceFuncWithInvalidResponse(
response *http.Response,
tickerToPriceExponent map[string]int32,
medianizer lib.Medianizer,
resolver pft.Resolver,
) (prices map[string]uint64, unavailable map[string]error, err error) {
prices = make(map[string]uint64, len(tickerToPriceExponent))
for range tickerToPriceExponent {
Expand All @@ -343,7 +343,7 @@ func priceFuncWithInvalidResponse(
func priceFuncWithValidAndUnavailableTickers(
response *http.Response,
tickerToPriceExponent map[string]int32,
medianizer lib.Medianizer,
resolver pft.Resolver,
) (prices map[string]uint64, unavailable map[string]error, err error) {
prices = make(map[string]uint64, len(tickerToPriceExponent))
for ticker := range tickerToPriceExponent {
Expand All @@ -357,15 +357,15 @@ func priceFuncWithValidAndUnavailableTickers(
func priceFuncReturnsInvalidUnavailableTicker(
response *http.Response,
tickerToPriceExponent map[string]int32,
medianizer lib.Medianizer,
resolver pft.Resolver,
) (prices map[string]uint64, unavailable map[string]error, err error) {
return nil, map[string]error{noMarketTicker: tickerNotAvailableError}, nil
}

func priceFuncWithErr(
response *http.Response,
tickerToPriceExponent map[string]int32,
medianizer lib.Medianizer,
resolver pft.Resolver,
) (prices map[string]uint64, unavailable map[string]error, err error) {
return nil, nil, priceFuncError
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (p PriceEncoderImpl) convertPriceUpdate(marketPriceTimestamp *types.MarketP
adjustByIndexPrice, numPricesMedianized := p.exchangeToMarketPrices.GetIndexPrice(
conversionDetails.AdjustByMarketDetails.MarketId,
time.Now().Add(-pricefeedtypes.MaxPriceAge),
&lib.MedianizerImpl{},
lib.Median[uint64],
)
// If the index price is not valid due to insufficient pricing data, return an error.
if numPricesMedianized < int(conversionDetails.AdjustByMarketDetails.MinExchanges) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
pf_constants "github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/constants"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_fetcher"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
pft "github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/types"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/stretchr/testify/require"
"testing"
Expand Down Expand Up @@ -98,7 +98,7 @@ type MockExchangeToMarketPrices struct {
numPricesMedianized int
}

func (m *MockExchangeToMarketPrices) GetIndexPrice(types.MarketId, time.Time, lib.Medianizer) (uint64, int) {
func (m *MockExchangeToMarketPrices) GetIndexPrice(types.MarketId, time.Time, pft.Resolver) (uint64, int) {
return m.indexPrice, m.numPricesMedianized
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/types"
)

// BinanceTicker is our representation of ticker information returned in Binance response.
Expand Down Expand Up @@ -39,7 +39,7 @@ func (t BinanceTicker) GetLastPrice() string {
func BinancePriceFunction(
response *http.Response,
tickerToExponent map[string]int32,
medianizer lib.Medianizer,
resolver types.Resolver,
) (tickerToPrice map[string]uint64, unavailableTickers map[string]error, err error) {
// Unmarshal response body into a list of tickers.
var binanceTickers []BinanceTicker
Expand All @@ -51,6 +51,6 @@ func BinancePriceFunction(
return price_function.GetMedianPricesFromTickers(
binanceTickers,
tickerToExponent,
medianizer,
resolver,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package binance_test
import (
"errors"
"fmt"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"testing"

"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed"

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/binance"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/testutil"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/stretchr/testify/mock"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -215,11 +212,9 @@ func TestBinancePriceFunction_Mixed(t *testing.T) {
var unavailable map[string]error
var err error
if tc.medianFunctionFails {
medianizer := &mocks.Medianizer{}
medianizer.On("MedianUint64", mock.Anything).Return(uint64(0), testutil.MedianizationError)
prices, unavailable, err = binance.BinancePriceFunction(response, tc.exponentMap, medianizer)
prices, unavailable, err = binance.BinancePriceFunction(response, tc.exponentMap, testutil.MedianErr)
} else {
prices, unavailable, err = binance.BinancePriceFunction(response, tc.exponentMap, &lib.MedianizerImpl{})
prices, unavailable, err = binance.BinancePriceFunction(response, tc.exponentMap, lib.Median[uint64])
}

if tc.expectedError != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/types"
)

// These indices into the REST API response are defined in https://docs.bitfinex.com/reference/rest-public-tickers
Expand Down Expand Up @@ -52,7 +52,7 @@ func (t BitfinexTicker) GetLastPrice() string {
func BitfinexPriceFunction(
response *http.Response,
tickerToExponent map[string]int32,
medianizer lib.Medianizer,
resolver types.Resolver,
) (tickerToPrice map[string]uint64, unavailableTickers map[string]error, err error) {
// Unmarshal response body into raw format first.
var rawResponse [][]interface{}
Expand Down Expand Up @@ -105,7 +105,7 @@ func BitfinexPriceFunction(
tickerToPrice, unavailableTickers, err = price_function.GetMedianPricesFromTickers(
bitfinexTickers,
tickerToExponent,
medianizer,
resolver,
)

// Mark as unavailable requested tickers whose raw ticker response was invalid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/bitfinex"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/testutil"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -252,11 +250,9 @@ func TestBitfinexPriceFunction_Mixed(t *testing.T) {
var unavailable map[string]error
var err error
if tc.medianFunctionFails {
medianizer := &mocks.Medianizer{}
medianizer.On("MedianUint64", mock.Anything).Return(uint64(0), testutil.MedianizationError)
prices, unavailable, err = bitfinex.BitfinexPriceFunction(response, tc.exponentMap, medianizer)
prices, unavailable, err = bitfinex.BitfinexPriceFunction(response, tc.exponentMap, testutil.MedianErr)
} else {
prices, unavailable, err = bitfinex.BitfinexPriceFunction(response, tc.exponentMap, &lib.MedianizerImpl{})
prices, unavailable, err = bitfinex.BitfinexPriceFunction(response, tc.exponentMap, lib.Median[uint64])
}

if tc.expectedError != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/types"
)

// BitstampTicker is our representation of ticker information returned in Bitstamp response.
Expand Down Expand Up @@ -38,7 +38,7 @@ func (t BitstampTicker) GetLastPrice() string {
func BitstampPriceFunction(
response *http.Response,
tickerToExponent map[string]int32,
medianizer lib.Medianizer,
resolver types.Resolver,
) (tickerToPrice map[string]uint64, unavailableTickers map[string]error, err error) {
// Unmarshal response body into a list of tickers.
var bitstampTickers []BitstampTicker
Expand All @@ -50,6 +50,6 @@ func BitstampPriceFunction(
return price_function.GetMedianPricesFromTickers(
bitstampTickers,
tickerToExponent,
medianizer,
resolver,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package bitstamp_test
import (
"errors"
"fmt"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"testing"

"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed"

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/bitstamp"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/testutil"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/stretchr/testify/mock"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed"
"github.com/stretchr/testify/require"
)

Expand All @@ -39,11 +36,11 @@ var (

// Test response strings.
var (
btcTicker = `{"timestamp": "1686600672", "open": "25940", "high": "26209", "low": "25634", "last": "25846",
"volume": "1903.95560640", "vwap": "25868", "bid": "25841", "ask": "25842", "open_24": "26183",
btcTicker = `{"timestamp": "1686600672", "open": "25940", "high": "26209", "low": "25634", "last": "25846",
"volume": "1903.95560640", "vwap": "25868", "bid": "25841", "ask": "25842", "open_24": "26183",
"percent_change_24": "-1.29", "pair": "BTC/USD"}`
ethTicker = `{"timestamp": "1686600672", "open": "1753.4", "high": "1777.7", "low": "1720.1", "last": "1734.9",
"volume": "6462.32622552", "vwap": "1738.6", "bid": "1734.3", "ask": "1734.9", "open_24": "1777.0",
ethTicker = `{"timestamp": "1686600672", "open": "1753.4", "high": "1777.7", "low": "1720.1", "last": "1734.9",
"volume": "6462.32622552", "vwap": "1738.6", "bid": "1734.3", "ask": "1734.9", "open_24": "1777.0",
"percent_change_24": "-2.37", "pair": "ETH/USD"}`

BtcResponseString = fmt.Sprintf("[%s]", btcTicker)
Expand Down Expand Up @@ -193,11 +190,9 @@ func TestBitstampPriceFunction_Mixed(t *testing.T) {
var unavailable map[string]error
var err error
if tc.medianFunctionFails {
medianizer := &mocks.Medianizer{}
medianizer.On("MedianUint64", mock.Anything).Return(uint64(0), testutil.MedianizationError)
prices, unavailable, err = bitstamp.BitstampPriceFunction(response, tc.exponentMap, medianizer)
prices, unavailable, err = bitstamp.BitstampPriceFunction(response, tc.exponentMap, testutil.MedianErr)
} else {
prices, unavailable, err = bitstamp.BitstampPriceFunction(response, tc.exponentMap, &lib.MedianizerImpl{})
prices, unavailable, err = bitstamp.BitstampPriceFunction(response, tc.exponentMap, lib.Median[uint64])
}

if tc.expectedError != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/types"
)

// BybitResponseBody defines the overall Bybit response.
Expand Down Expand Up @@ -50,7 +50,7 @@ func (t BybitTicker) GetLastPrice() string {
func BybitPriceFunction(
response *http.Response,
tickerToExponent map[string]int32,
medianizer lib.Medianizer,
resolver types.Resolver,
) (tickerToPrice map[string]uint64, unavailableTickers map[string]error, err error) {
// Unmarshal response body into a list of tickers.
var bybitResponseBody BybitResponseBody
Expand All @@ -66,6 +66,6 @@ func BybitPriceFunction(
return price_function.GetMedianPricesFromTickers(
bybitResponseBody.Result.Tickers,
tickerToExponent,
medianizer,
resolver,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package bybit_test
import (
"errors"
"fmt"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"testing"

"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed"

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/bybit"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/testutil"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/stretchr/testify/mock"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/testutil/daemons/pricefeed"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -201,11 +198,9 @@ func TestBybitPriceFunction_Mixed(t *testing.T) {
var unavailable map[string]error
var err error
if tc.medianFunctionFails {
medianizer := &mocks.Medianizer{}
medianizer.On("MedianUint64", mock.Anything).Return(uint64(0), testutil.MedianizationError)
prices, unavailable, err = bybit.BybitPriceFunction(response, tc.exponentMap, medianizer)
prices, unavailable, err = bybit.BybitPriceFunction(response, tc.exponentMap, testutil.MedianErr)
} else {
prices, unavailable, err = bybit.BybitPriceFunction(response, tc.exponentMap, &lib.MedianizerImpl{})
prices, unavailable, err = bybit.BybitPriceFunction(response, tc.exponentMap, lib.Median[uint64])
}

if tc.expectedError != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/constants/exchange_common"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/types"
)

// CoinbaseProTicker is our representation of ticker information returned in CoinbasePro response.
Expand Down Expand Up @@ -40,7 +40,7 @@ func (t CoinbaseProTicker) GetLastPrice() string {
func CoinbaseProPriceFunction(
response *http.Response,
tickerToExponent map[string]int32,
medianizer lib.Medianizer,
resolver types.Resolver,
) (tickerToPrice map[string]uint64, unavailableTickers map[string]error, err error) {
// Get ticker. The API response should only contain information for one market.
ticker, _, err := price_function.GetOnlyTickerAndExponent(
Expand All @@ -64,6 +64,6 @@ func CoinbaseProPriceFunction(
return price_function.GetMedianPricesFromTickers(
[]CoinbaseProTicker{coinbaseProTicker},
tickerToExponent,
medianizer,
resolver,
)
}
Loading

0 comments on commit 086d891

Please sign in to comment.