def test_eth_solo_withdraw_eth_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.solo.withdraw( market=consts.MARKET_WETH, wei=1000 ) self._validate_tx_hash(client, tx_hash)
def test_get_pairs_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_pairs_json rm.get('https://api.dydx.exchange/v1/dex/pairs', json=json_obj) result = client.get_pairs() assert result == json_obj
def test_eth_solo_deposit_dai_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.solo.deposit( market=consts.MARKET_DAI, wei=1000 ) self._validate_tx_hash(client, tx_hash)
def test_eth_perp_get_my_balances(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(web3.exceptions.BadFunctionCallOutput) as error: client.eth.perp.get_my_balances( market=consts.PAIR_PBTC_USDC, ) assert CONTRACT_NOT_FOUND_ERROR in str(error.value)
def test_get_pairs_fail(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: rm.get('https://api.dydx.exchange/v1/dex/pairs', status_code=400) with pytest.raises(Exception) as error: client.get_pairs() assert '400' in str(error.value)
def test_eth_get_wallet_balance_eth(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) balance = client.eth.get_wallet_balance( address=ADDRESS_2, market=consts.MARKET_ETH ) assert balance > 0
def test_eth_solo_withdraw_to_zero_dai_to_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.solo.withdraw_to_zero( market=consts.MARKET_DAI, to=ADDRESS_2 ) self._validate_tx_hash(client, tx_hash)
def test_constructor_account_number(self): client = Client(PRIVATE_KEY_1, account_number=consts.MAX_SOLIDITY_UINT) assert client.account_number == consts.MAX_SOLIDITY_UINT assert ( str(consts.MAX_SOLIDITY_UINT) == "115792089237316195423570985008687907853269984665640564039457584007913129639935" ) # noqa: E501
def __init__(self, node: str, private_key: str): assert (isinstance(node, str)) assert (isinstance(private_key, str)) self.client = Client(private_key=private_key, node=node) self.market_info = self.get_markets()
def test_eth_solo_deposit_unknown_failure(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(ValueError) as error: client.eth.solo.deposit( market=consts.MARKET_INVALID, wei=1000 ) assert 'Invalid market number' in str(error.value)
def test_eth_solo_deposit_eth_without_proxy_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.solo.deposit( market=consts.MARKET_WETH, wei=1000, asEth=False ) self._validate_tx_hash(client, tx_hash)
def test_eth_get_wallet_balance(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(web3.exceptions.BadFunctionCallOutput) as error: client.eth.get_wallet_balance( address=ADDRESS_2, market=consts.MARKET_DAI ) assert CONTRACT_NOT_FOUND_ERROR in str(error.value)
def test_get_perpetual_markets_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_markets_json uri = 'https://api.dydx.exchange/v1/perpetual-markets' rm.get(uri, json=json_obj) result = client.get_perpetual_markets() assert result == json_obj
def test_get_order_default_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_order_json uri = "https://api.dydx.exchange/v1/dex/orders/" + ORDER_HASH rm.get(uri, json=json_obj) result = client.get_order(ORDER_HASH) assert result == json_obj
def test_get_balances_address_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_balances_json uri = 'https://api.dydx.exchange/v1/accounts/' + ADDRESS_2 rm.get(uri, json=json_obj) result = client.get_balances(address=ADDRESS_2) assert result == json_obj
def test_get_balances(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(web3.exceptions.BadFunctionCallOutput) as error: client.eth.get_balances( address=ADDRESS_2, accountNumber=1111, ) assert CONTRACT_NOT_FOUND_ERROR in str(error.value)
def test_get_orderbook_success(self): client = Client(PRIVATE_KEY_1) market = MARKETS[0] with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_orders_json uri = 'https://api.dydx.exchange/v1/orderbook/' + market rm.get(uri, json=json_obj) result = client.get_orderbook(market=market, ) assert result == json_obj
def test_get_trades_default_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_trades_json uri = 'https://api.dydx.exchange/v2/trades' \ + '?market=' + ','.join(MARKETS) rm.get(uri, json=json_obj) result = client.get_trades(market=MARKETS, ) assert result == json_obj
def test_get_fills_default_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_fills_json uri = 'https://api.dydx.exchange/v1/dex/fills' \ + '?pairs=' + ','.join(PAIRS) rm.get(uri, json=json_obj) result = client.get_fills(pairs=PAIRS) assert result == json_obj
def test_get_trades_default_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_trades_json uri = ("https://api.dydx.exchange/v1/dex/trades" + "?pairs=" + ",".join(PAIRS)) rm.get(uri, json=json_obj) result = client.get_trades(pairs=PAIRS) assert result == json_obj
def test_get_my_perpetual_balances_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_balances_json uri = 'https://api.dydx.exchange/v1/perpetual-accounts/' \ + client.public_address rm.get(uri, json=json_obj) result = client.get_my_perpetual_balances() assert result == json_obj
def test_get_balances_address_and_number_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_balances_json uri = ("https://api.dydx.exchange/v1/accounts/" + ADDRESS_2 + "?number=" + str(1234)) rm.get(uri, json=json_obj) result = client.get_balances(address=ADDRESS_2, number=1234) assert result == json_obj
def __init__(self, node: str, private_key: str): assert (isinstance(node, str)) assert (isinstance(private_key, str)) self.client = Client(private_key=private_key, node=node, account_number=consts.ACCOUNT_NUMBERS_SPOT) public_key = keys.PrivateKey(decode_hex(private_key)).public_key self.address = public_key.to_checksum_address() self.market_info = self.get_markets()
def test_get_my_balances_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_balances_json uri = ("https://api.dydx.exchange/v1/accounts/" + client.public_address + "?number=" + str(client.account_number)) rm.get(uri, json=json_obj) result = client.get_my_balances() assert result == json_obj
def test_get_my_trades_default_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_trades_json uri = 'https://api.dydx.exchange/v2/trades' \ + '?accountOwner=' + client.public_address \ + '&accountNumber=' + str(client.account_number) \ + '&market=' + ','.join(MARKETS) rm.get(uri, json=json_obj) result = client.get_my_trades(market=MARKETS, ) assert result == json_obj
def test_get_my_fills_default_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_fills_json uri = 'https://api.dydx.exchange/v1/dex/fills' \ + '?makerAccountOwner=' + client.public_address \ + '&makerAccountNumber=' + str(client.account_number) \ + '&pairs=' + ','.join(PAIRS) rm.get(uri, json=json_obj) result = client.get_my_fills(pairs=PAIRS) assert result == json_obj
def test_get_my_trades_default_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_get_trades_json uri = ("https://api.dydx.exchange/v1/dex/trades" + "?makerAccountOwner=" + client.public_address + "&makerAccountNumber=" + str(client.account_number) + "&pairs=" + ",".join(PAIRS)) rm.get(uri, json=json_obj) result = client.get_my_trades(pairs=PAIRS) assert result == json_obj
def test_place_order_success(self): client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_place_order_json rm.post('https://api.dydx.exchange/v2/orders', additional_matcher=_create_additional_matcher(client), json=json_obj) result = client.place_order(market='WETH-DAI', side='BUY', amount=10000, price=250.01) assert result == json_obj
def test_eth_perp_withdraw_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.perp.withdraw( market=consts.PAIR_PBTC_USDC, amount=1000, ) self._validate_tx_hash(client, tx_hash) tx_hash = client.eth.perp.withdraw( market=consts.PAIR_WETH_PUSD, amount=1000, ) self._validate_tx_hash(client, tx_hash)
def test_create_order_success(self): def additional_matcher(request): body = json.loads(request.body) assert body["fillOrKill"] is False assert body["order"]["takerMarket"] == "0" assert body["order"]["makerMarket"] == "1" assert body["order"]["takerAmount"] == "1000" assert body["order"]["makerAmount"] == "2000" assert body["order"]["makerAccountOwner"] == client.public_address assert body["order"]["makerAccountNumber"] == str( client.account_number) assert body["order"][ "takerAccountOwner"] == client.TAKER_ACCOUNT_OWNER assert body["order"]["takerAccountNumber"] == str( client.TAKER_ACCOUNT_NUMBER) assert (abs( int(body["order"]["expiration"]) - utils.epoch_in_four_weeks()) <= 10) assert body["order"]["salt"].isnumeric() sent_order = body["order"] expected_signature = utils.sign_order( { "makerMarket": int(sent_order["makerMarket"]), "takerMarket": int(sent_order["takerMarket"]), "makerAmount": int(sent_order["makerAmount"]), "takerAmount": int(sent_order["takerAmount"]), "makerAccountOwner": sent_order["makerAccountOwner"], "makerAccountNumber": int( sent_order["makerAccountNumber"]), "takerAccountOwner": sent_order["takerAccountOwner"], "takerAccountNumber": int( sent_order["takerAccountNumber"]), "expiration": int(sent_order["expiration"]), "salt": int(sent_order["salt"]), }, client.private_key, ) assert body["order"]["typedSignature"] == expected_signature return True client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_create_order_json rm.post( "https://api.dydx.exchange/v1/dex/orders", additional_matcher=additional_matcher, json=json_obj, ) result = client.create_order(makerMarket=1, takerMarket=0, makerAmount=2000, takerAmount=1000) assert result == json_obj