Skip to content

Commit 2b2b7e7

Browse files
v1.0.11: add withdraw and withdrawTo [BAC-1607] (#58)
* add withdraw and withdrawTo * update test
1 parent a0bdc5d commit 2b2b7e7

3 files changed

Lines changed: 112 additions & 1 deletion

File tree

dydx3/modules/eth.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,73 @@ def deposit_to_exchange(
301301
options=send_options,
302302
)
303303

304+
def withdraw(
305+
self,
306+
stark_public_key=None,
307+
send_options=None,
308+
):
309+
'''
310+
Withdraw from exchange.
311+
312+
:param stark_public_key: optional
313+
:type stark_public_key: string
314+
315+
:param send_options: optional
316+
:type send_options: dict
317+
318+
:returns: transactionHash
319+
320+
:raises: ValueError
321+
'''
322+
stark_public_key = stark_public_key or self.stark_public_key
323+
if stark_public_key is None:
324+
raise ValueError('No stark_public_key was provided')
325+
326+
contract = self.get_exchange_contract()
327+
return self.send_eth_transaction(
328+
method=contract.functions.withdraw(
329+
int(stark_public_key, 16),
330+
COLLATERAL_ASSET_ID_BY_NETWORK_ID[self.network_id],
331+
),
332+
options=send_options,
333+
)
334+
335+
def withdraw_to(
336+
self,
337+
recipient,
338+
stark_public_key=None,
339+
send_options=None,
340+
):
341+
'''
342+
Withdraw from exchange to address.
343+
344+
:param recipient: required
345+
:type recipient: string
346+
347+
:param stark_public_key: optional
348+
:type stark_public_key: string
349+
350+
:param send_options: optional
351+
:type send_options: dict
352+
353+
:returns: transactionHash
354+
355+
:raises: ValueError
356+
'''
357+
stark_public_key = stark_public_key or self.stark_public_key
358+
if stark_public_key is None:
359+
raise ValueError('No stark_public_key was provided')
360+
361+
contract = self.get_exchange_contract()
362+
return self.send_eth_transaction(
363+
method=contract.functions.withdrawTo(
364+
int(stark_public_key, 16),
365+
COLLATERAL_ASSET_ID_BY_NETWORK_ID[self.network_id],
366+
recipient,
367+
),
368+
options=send_options,
369+
)
370+
304371
def transfer_eth(
305372
self,
306373
to_address=None, # Require keyword args to avoid confusing the amount.

integration_tests/test_integration.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,47 @@ def test_integration(self):
355355
client_id='mock-client-id',
356356
expiration_epoch_seconds=expiration_epoch_seconds,
357357
)
358+
359+
# Send an on-chain withdraw.
360+
withdraw_tx_hash = client.eth.withdraw()
361+
print('Waiting for withdraw...')
362+
client.eth.wait_for_tx(withdraw_tx_hash)
363+
print('...done.')
364+
365+
# Wait for the withdraw to be processed.
366+
print('Waiting for withdraw to be processed on dYdX...')
367+
wait_for_condition(
368+
lambda: len(client.private.get_transfers()['transfers']) > 0,
369+
True,
370+
60,
371+
)
372+
print('...transfer was recorded, waiting for confirmation...')
373+
wait_for_condition(
374+
lambda: client.private.get_account()['account']['quoteBalance'],
375+
'2',
376+
180,
377+
)
378+
print('...done.')
379+
380+
# Send an on-chain withdraw_to.
381+
withdraw_to_tx_hash = client.eth.withdraw_to(
382+
recipient=ethereum_address,
383+
)
384+
print('Waiting for withdraw_to...')
385+
client.eth.wait_for_tx(withdraw_to_tx_hash)
386+
print('...done.')
387+
388+
# Wait for the withdraw_to to be processed.
389+
print('Waiting for withdraw_to to be processed on dYdX...')
390+
wait_for_condition(
391+
lambda: len(client.private.get_transfers()['transfers']) > 0,
392+
True,
393+
60,
394+
)
395+
print('...transfer was recorded, waiting for confirmation...')
396+
wait_for_condition(
397+
lambda: client.private.get_account()['account']['quoteBalance'],
398+
'2',
399+
180,
400+
)
401+
print('...done.')

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
setup(
2121
name='dydx-v3-python',
22-
version='1.0.10',
22+
version='1.0.11',
2323
packages=find_packages(),
2424
package_data={
2525
'dydx3': [

0 commit comments

Comments
 (0)