def test_get_btcusdt_when_loaded_returns_expected_instrument(self):
        # Arrange
        mock_client = MagicMock()
        mock_client.name = "Binance"

        with open(TEST_PATH + "res_instruments.json") as response:
            instruments = json.load(response)

        mock_client.markets = instruments

        provider = BinanceInstrumentProvider(client=mock_client)
        provider.load_all()

        symbol = Symbol("BTC/USDT", Venue("BINANCE"))

        # Act
        instrument = provider.get(symbol)

        # Assert
        self.assertEqual(Instrument, type(instrument))
        self.assertEqual(AssetClass.CRYPTO, instrument.asset_class)
        self.assertEqual(AssetType.SPOT, instrument.asset_type)
        self.assertEqual(BTC, instrument.base_currency)
        self.assertEqual(USDT, instrument.quote_currency)
        self.assertEqual(USDT, instrument.settlement_currency)
Exemple #2
0
class TestBinanceInstrumentProvider:
    @pytest.mark.asyncio
    async def test_load_all_async(
        self,
        binance_http_client,
        live_logger,
        monkeypatch,
    ):
        # Arrange: prepare data for monkey patch
        response1 = pkgutil.get_data(
            package=
            "tests.integration_tests.adapters.binance.resources.responses",
            resource="wallet_trading_fee.json",
        )

        response2 = pkgutil.get_data(
            package=
            "tests.integration_tests.adapters.binance.resources.responses",
            resource="spot_market_exchange_info.json",
        )

        responses = [response2, response1]

        # Mock coroutine for patch
        async def mock_send_request(
                self,  # noqa (needed for mock)
                http_method: str,  # noqa (needed for mock)
                url_path: str,  # noqa (needed for mock)
                payload: Dict[str, str],  # noqa (needed for mock)
        ) -> bytes:
            return orjson.loads(responses.pop())

        # Apply mock coroutine to client
        monkeypatch.setattr(
            target=BinanceHttpClient,
            name="send_request",
            value=mock_send_request,
        )

        self.provider = BinanceInstrumentProvider(
            client=binance_http_client,
            logger=live_logger,
        )

        # Act
        await self.provider.load_all_async()

        # Assert
        assert self.provider.count == 2
        assert self.provider.find(
            InstrumentId(Symbol("BTCUSDT"), Venue("BINANCE"))) is not None
        assert self.provider.find(
            InstrumentId(Symbol("ETHUSDT"), Venue("BINANCE"))) is not None
        assert len(self.provider.currencies()) == 3
        assert "BTC" in self.provider.currencies()
        assert "ETH" in self.provider.currencies()
        assert "USDT" in self.provider.currencies()
    def test_get_all_when_not_loaded_returns_empty_dict(self):
        # Arrange
        mock_client = MagicMock()
        mock_client.name = "Binance"

        provider = BinanceInstrumentProvider(client=mock_client)

        # Act
        instruments = provider.get_all()

        # Assert
        self.assertTrue(len(instruments) == 0)
    def test_get_btcusdt_when_not_loaded_returns_none(self):
        # Arrange
        mock_client = MagicMock()
        mock_client.name = "Binance"

        provider = BinanceInstrumentProvider(client=mock_client)

        symbol = Symbol("BTC/USDT", Venue("BINANCE"))

        # Act
        instrument = provider.get(symbol)

        # Assert
        self.assertIsNone(instrument)
async def test_binance_spot_market_http_client():
    loop = asyncio.get_event_loop()
    clock = LiveClock()

    client = get_cached_binance_http_client(
        loop=loop,
        clock=clock,
        logger=Logger(clock=clock),
        key=os.getenv("BINANCE_API_KEY"),
        secret=os.getenv("BINANCE_API_SECRET"),
    )
    await client.connect()

    market = BinanceSpotMarketHttpAPI(client=client)
    response = await market.exchange_info(symbols=["BTCUSDT", "ETHUSDT"])
    print(json.dumps(response, indent=4))

    provider = BinanceInstrumentProvider(
        client=client,
        logger=Logger(clock=clock),
    )

    await provider.load_all_async()

    print(provider.count)

    await client.disconnect()
Exemple #6
0
def get_cached_binance_instrument_provider(
    client: BinanceHttpClient,
    logger: Logger,
) -> BinanceInstrumentProvider:
    """
    Cache and return a BinanceInstrumentProvider.

    If a cached provider already exists, then that cached provider will be returned.

    Parameters
    ----------
    client : BinanceHttpClient
        The client for the instrument provider.
    logger : Logger
        The logger for the instrument provider.

    Returns
    -------
    BinanceInstrumentProvider

    """
    return BinanceInstrumentProvider(
        client=client,
        logger=logger,
    )
    def test_load_all(self):
        # Arrange
        mock_client = MagicMock()
        mock_client.name = "Binance"

        with open(TEST_PATH + "res_instruments.json") as response:
            instruments = json.load(response)

        mock_client.markets = instruments

        provider = BinanceInstrumentProvider(client=mock_client)

        # Act
        provider.load_all()

        # Assert
        self.assertTrue(provider.count > 0)  # No exceptions raised
    def test_get_all_when_loaded_returns_instruments(self):
        # Arrange
        mock_client = MagicMock()
        mock_client.name = "Binance"

        with open(TEST_PATH + "res_instruments.json") as response:
            instruments = json.load(response)

        mock_client.markets = instruments

        provider = BinanceInstrumentProvider(client=mock_client)
        provider.load_all()

        # Act
        instruments = provider.get_all()

        # Assert
        self.assertTrue(len(instruments) > 0)
        self.assertEqual(dict, type(instruments))
        self.assertEqual(Symbol, type(next(iter(instruments))))
Exemple #9
0
    def setup(self):
        # Fixture Setup
        self.loop = asyncio.get_event_loop()
        self.loop.set_debug(True)

        self.clock = LiveClock()
        self.uuid_factory = UUIDFactory()
        self.logger = Logger(clock=self.clock)

        self.trader_id = TestStubs.trader_id()
        self.venue = BINANCE_VENUE
        self.account_id = AccountId(self.venue.value, "001")

        self.msgbus = MessageBus(
            trader_id=self.trader_id,
            clock=self.clock,
            logger=self.logger,
        )

        self.cache = TestStubs.cache()

        self.http_client = BinanceHttpClient(  # noqa: S106 (no hardcoded password)
            loop=asyncio.get_event_loop(),
            clock=self.clock,
            logger=self.logger,
            key="SOME_BINANCE_API_KEY",
            secret="SOME_BINANCE_API_SECRET",
        )

        self.provider = BinanceInstrumentProvider(
            client=self.http_client,
            logger=self.logger,
        )

        self.data_engine = DataEngine(
            msgbus=self.msgbus,
            cache=self.cache,
            clock=self.clock,
            logger=self.logger,
        )

        self.data_client = BinanceDataClient(
            loop=self.loop,
            client=self.http_client,
            msgbus=self.msgbus,
            cache=self.cache,
            clock=self.clock,
            logger=self.logger,
            instrument_provider=self.provider,
        )