def test_get_collateralization(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(web3.exceptions.BadFunctionCallOutput) as error: client.eth.get_collateralization( address=ADDRESS_2, accountNumber=1111, ) assert CONTRACT_NOT_FOUND_ERROR in str(error.value)
def test_cancel_order_success(self): def additional_matcher(request): return 'Bearer ' + CANCEL_ORDER_SIGNATURE == \ request.headers['Authorization'] client = Client(PRIVATE_KEY_1) with requests_mock.mock() as rm: json_obj = tests.test_json.mock_cancel_order_json rm.delete( 'https://api.dydx.exchange/v1/dex/orders/' + ORDER_HASH, additional_matcher=additional_matcher, json=json_obj ) result = client.cancel_order( hash=ORDER_HASH ) assert result == json_obj
def test_get_my_trades_specified_success(self): client = Client(PRIVATE_KEY_1) limit = 99 startingBefore = datetime.datetime.utcnow().isoformat() 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) \ + '&limit=' + str(limit) \ + '&startingBefore=' + startingBefore rm.get(uri, json=json_obj) result = client.get_my_trades( market=MARKETS, limit=limit, startingBefore=startingBefore ) assert result == json_obj
def test_get_my_fills_specified_success(self): client = Client(PRIVATE_KEY_1) limit = 1234 startingBefore = datetime.datetime.utcnow().isoformat() 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) \ + '&limit=' + str(limit) \ + '&startingBefore=' + startingBefore rm.get(uri, json=json_obj) result = client.get_my_fills( pairs=PAIRS, limit=limit, startingBefore=startingBefore ) assert result == json_obj
def test_replace_order_success(self): 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/replace', additional_matcher=_create_additional_matcher( client, cancelId=ORDER_HASH ), json=json_obj ) result = client.replace_order( makerMarket=1, takerMarket=0, makerAmount=2000, takerAmount=1000, cancelId=ORDER_HASH ) assert result == json_obj
def test_place_order_success_pbtc_usdc(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_perp_order_matcher( client, { 'market': 'PBTC-USDC', 'isBuy': False, 'limitPrice': '0.00000000025001', 'amount': '100000000', }, ), json=json_obj) result = client.place_order(market='PBTC-USDC', side='SELL', amount=100000000, price=Decimal('0.00000000025001')) assert result == json_obj
def test_place_order_success_weth_dai(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_solo_order_matcher( client, { 'baseMarket': '0', 'quoteMarket': '3', 'isBuy': True, 'limitPrice': '250.01', 'amount': '10000', }, ), json=json_obj) result = client.place_order(market='WETH-DAI', side='BUY', amount=10000, price=Decimal('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_constructor_bytes_private_key(self): client = Client(bytearray.fromhex(PRIVATE_KEY_1[2:])) assert client.public_address == ADDRESS_1 assert client.account_number == 0
def test_cancel_order_no_hash_error(self): client = Client(PRIVATE_KEY_1) with pytest.raises(TypeError) as error: client.cancel_order() assert 'required positional argument: \'hash\'' in str(error.value)
def test_get_balances_no_address_error(self): client = Client(PRIVATE_KEY_1) with pytest.raises(TypeError) as error: client.get_balances() assert 'required positional argument: \'address\'' in str(error.value)
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 test_withdraw_dai_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.withdraw(market=consts.MARKET_DAI, wei=1000) self._validate_tx_hash(client, tx_hash)
def test_deposit_eth_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.deposit(market=consts.MARKET_WETH, wei=1000) self._validate_tx_hash(client, tx_hash)
def test_eth_solo_get_my_collateralization(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(web3.exceptions.BadFunctionCallOutput) as error: client.eth.solo.get_my_collateralization() assert CONTRACT_NOT_FOUND_ERROR in str(error.value)
def test_set_allowance_dai_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.set_allowance(market=consts.MARKET_DAI) self._validate_tx_hash(client, tx_hash)
def test_set_allowance_unknown_failure(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(ValueError) as error: client.eth.set_allowance(market=consts.MARKET_INVALID) assert 'Invalid market number' in str(error.value)
def test_withdraw_to_zero_unknown_failure(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(ValueError) as error: client.eth.withdraw_to_zero(market=consts.MARKET_INVALID) assert "Invalid market number" in str(error.value)
def test_deposit_unknown_failure(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(ValueError) as error: client.eth.deposit(market=consts.MARKET_INVALID, wei=1000) assert 'Invalid market number' in str(error.value)
def test_withdraw_to_zero_dai_to_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.withdraw_to_zero(market=consts.MARKET_DAI, to=ADDRESS_2) self._validate_tx_hash(client, tx_hash)
def test_withdraw_eth_to_success(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) tx_hash = client.eth.withdraw(market=consts.MARKET_WETH, wei=1000, to=ADDRESS_2) self._validate_tx_hash(client, tx_hash)
def test_get_oracle_price(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(web3.exceptions.BadFunctionCallOutput) as error: client.eth.get_oracle_price(consts.MARKET_DAI) assert CONTRACT_NOT_FOUND_ERROR in str(error.value)
def test_constructor_no_private_key(self): with pytest.raises(TypeError): Client()
def test_get_my_wallet_balance_eth(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) balance = client.eth.get_my_wallet_balance(consts.MARKET_ETH) assert balance > 0
def test_get_my_trades_no_pairs_error(self): client = Client(PRIVATE_KEY_1) with pytest.raises(TypeError) as error: client.get_my_trades() assert 'required positional argument: \'market\'' in str(error.value)
def test_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_constructor_string_private_key(self): client = Client(PRIVATE_KEY_1) assert client.public_address == ADDRESS_1 assert client.account_number == 0
def test_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 __init__(self, private_key, node, account_number): self.client = Client(private_key=private_key, node=node, account_number=account_number) self._loop = asyncio.get_event_loop()
def test_get_my_balances(self): client = Client(PRIVATE_KEY_1, node=LOCAL_NODE) with pytest.raises(web3.exceptions.BadFunctionCallOutput) as error: client.eth.get_my_balances() assert CONTRACT_NOT_FOUND_ERROR in str(error.value)