Skip to content

Commit

Permalink
[IND-515] add fixed price exchange for e2e testing purposes (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill authored Dec 9, 2023
1 parent 37c05f4 commit bfd2d4c
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ const (
EXCHANGE_ID_TEST_EXCHANGE types.ExchangeId = "TestExchange"
// EXCHANGE_ID_TEST_VOLATILE_EXCHANGE is the id for test volatile exchange.
EXCHANGE_ID_TEST_VOLATILE_EXCHANGE types.ExchangeId = "TestVolatileExchange"
// EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE is the id for test fixed price exchange.
EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE types.ExchangeId = "TestFixedPriceExchange"
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/kucoin"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/mexc"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/okx"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/test_fixed_price_exchange"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/test_volatile_exchange"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function/testexchange"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
Expand All @@ -22,20 +23,21 @@ import (
var (
// StaticExchangeDetails is the static mapping of `ExchangeId` to its `ExchangeQueryDetails`.
StaticExchangeDetails = map[types.ExchangeId]types.ExchangeQueryDetails{
exchange_common.EXCHANGE_ID_BINANCE: binance.BinanceDetails,
exchange_common.EXCHANGE_ID_BINANCE_US: binance.BinanceUSDetails,
exchange_common.EXCHANGE_ID_BITFINEX: bitfinex.BitfinexDetails,
exchange_common.EXCHANGE_ID_KRAKEN: kraken.KrakenDetails,
exchange_common.EXCHANGE_ID_GATE: gate.GateDetails,
exchange_common.EXCHANGE_ID_BITSTAMP: bitstamp.BitstampDetails,
exchange_common.EXCHANGE_ID_BYBIT: bybit.BybitDetails,
exchange_common.EXCHANGE_ID_CRYPTO_COM: crypto_com.CryptoComDetails,
exchange_common.EXCHANGE_ID_HUOBI: huobi.HuobiDetails,
exchange_common.EXCHANGE_ID_KUCOIN: kucoin.KucoinDetails,
exchange_common.EXCHANGE_ID_OKX: okx.OkxDetails,
exchange_common.EXCHANGE_ID_MEXC: mexc.MexcDetails,
exchange_common.EXCHANGE_ID_COINBASE_PRO: coinbase_pro.CoinbaseProDetails,
exchange_common.EXCHANGE_ID_TEST_EXCHANGE: testexchange.TestExchangeDetails,
exchange_common.EXCHANGE_ID_TEST_VOLATILE_EXCHANGE: test_volatile_exchange.TestVolatileExchangeDetails,
exchange_common.EXCHANGE_ID_BINANCE: binance.BinanceDetails,
exchange_common.EXCHANGE_ID_BINANCE_US: binance.BinanceUSDetails,
exchange_common.EXCHANGE_ID_BITFINEX: bitfinex.BitfinexDetails,
exchange_common.EXCHANGE_ID_KRAKEN: kraken.KrakenDetails,
exchange_common.EXCHANGE_ID_GATE: gate.GateDetails,
exchange_common.EXCHANGE_ID_BITSTAMP: bitstamp.BitstampDetails,
exchange_common.EXCHANGE_ID_BYBIT: bybit.BybitDetails,
exchange_common.EXCHANGE_ID_CRYPTO_COM: crypto_com.CryptoComDetails,
exchange_common.EXCHANGE_ID_HUOBI: huobi.HuobiDetails,
exchange_common.EXCHANGE_ID_KUCOIN: kucoin.KucoinDetails,
exchange_common.EXCHANGE_ID_OKX: okx.OkxDetails,
exchange_common.EXCHANGE_ID_MEXC: mexc.MexcDetails,
exchange_common.EXCHANGE_ID_COINBASE_PRO: coinbase_pro.CoinbaseProDetails,
exchange_common.EXCHANGE_ID_TEST_EXCHANGE: testexchange.TestExchangeDetails,
exchange_common.EXCHANGE_ID_TEST_VOLATILE_EXCHANGE: test_volatile_exchange.TestVolatileExchangeDetails,
exchange_common.EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE: test_fixed_price_exchange.TestFixedPriceExchangeDetails,
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func GenerateExchangeConfigJson(
if id == exchange_common.EXCHANGE_ID_TEST_EXCHANGE {
continue
}
if id == exchange_common.EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE {
continue
}
for marketId, config := range exchangeConfig.MarketToMarketConfig {
marketExchangeConfigs, ok := marketToExchangeMarketConfigs[marketId]
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,11 @@ var (
TimeoutMs: defaultTimeoutMs,
MaxQueries: defaultMaxQueries,
},
exchange_common.EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE: {
ExchangeId: exchange_common.EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE,
IntervalMs: defaultIntervalMs,
TimeoutMs: defaultTimeoutMs,
MaxQueries: defaultMaxQueries,
},
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,5 @@ func TestStaticExchangeQueryConfigCache(t *testing.T) {
}

func TestStaticExchangeQueryConfigCacheLength(t *testing.T) {
require.Len(t, constants.StaticExchangeQueryConfig, 14)
require.Len(t, constants.StaticExchangeQueryConfig, 15)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package test_fixed_price_exchange

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

// Fixed prices for BTC-USD, ETH-USD, SOL-USD
const (
BTC_USD_PRICE = 50000
ETH_USD_PRICE = 4000
SOL_USD_PRICE = 100
)

type FixedPriceExchangeParams struct {
BTCUSDPrice float64
ETHUSDPrice float64
SOLUSDPrice float64
}

var (
TestFixedPriceExchangeParams = FixedPriceExchangeParams{
BTCUSDPrice: BTC_USD_PRICE,
ETHUSDPrice: ETH_USD_PRICE,
SOLUSDPrice: SOL_USD_PRICE,
}
TestFixedPriceExchangeDetails = types.ExchangeQueryDetails{
Exchange: exchange_common.EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE,
Url: "https://jsonplaceholder.typicode.com/users",
PriceFunction: FixedExchangePriceFunction,
IsMultiMarket: false,
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package test_fixed_price_exchange

import (
"fmt"
"net/http"

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

type FixedPriceTicker struct {
Pair string
Price string
}

var _ price_function.Ticker = (*FixedPriceTicker)(nil)

func (t FixedPriceTicker) GetPair() string {
return t.Pair
}

func (t FixedPriceTicker) GetAskPrice() string {
return t.Price
}

func (t FixedPriceTicker) GetBidPrice() string {
return t.Price
}

func (t FixedPriceTicker) GetLastPrice() string {
return t.Price
}

func FixedExchangePriceFunction(
response *http.Response,
tickerToExponent map[string]int32,
resolver types.Resolver,
) (tickerToPrice map[string]uint64, unavailableTickers map[string]error, err error) {
btcTicker := FixedPriceTicker{
Pair: "BTC-USD",
Price: fmt.Sprintf("%f", TestFixedPriceExchangeParams.BTCUSDPrice),
}
ethTicker := FixedPriceTicker{
Pair: "ETH-USD",
Price: fmt.Sprintf("%f", TestFixedPriceExchangeParams.ETHUSDPrice),
}
solTicker := FixedPriceTicker{
Pair: "SOL-USD",
Price: fmt.Sprintf("%f", TestFixedPriceExchangeParams.SOLUSDPrice),
}
return price_function.GetMedianPricesFromTickers(
[]FixedPriceTicker{btcTicker, ethTicker, solTicker},
tickerToExponent,
resolver,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,20 @@ var (
},
},
},
exchange_common.EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE: {
Id: exchange_common.EXCHANGE_ID_TEST_FIXED_PRICE_EXCHANGE,
MarketToMarketConfig: map[types.MarketId]types.MarketConfig{
MARKET_BTC_USD: {
Ticker: "BTC-USD",
},
MARKET_ETH_USD: {
Ticker: "ETH-USD",
},
MARKET_SOL_USD: {
Ticker: "SOL-USD",
},
},
},
}
)

Expand Down

0 comments on commit bfd2d4c

Please sign in to comment.