Skip to content

Instantly share code, notes, and snippets.

@SC4RECOIN
Created July 10, 2022 20:19
Show Gist options
  • Save SC4RECOIN/a43acd60e405647deed0d92437d3ad20 to your computer and use it in GitHub Desktop.
Save SC4RECOIN/a43acd60e405647deed0d92437d3ad20 to your computer and use it in GitHub Desktop.
A script for completing the second part of a slow dYdX withdrawal. I needed to create this script as there was bug in the web app preventing me from completing this step so my withdrawal was stuck.
import requests
from web3 import Web3
from dydx3 import constants
from dydx3 import Client
WEB_PROVIDER_URL = "https://mainnet.infura.io/v3/..."
ETHEREUM_ADDRESS = "0x6E..."
ETHEREUM_KEY = "da..."
w3 = Web3(Web3.HTTPProvider(WEB_PROVIDER_URL))
# recover stark key
client = Client(
network_id=constants.NETWORK_ID_MAINNET,
host=constants.API_HOST_MAINNET,
default_ethereum_address=ETHEREUM_ADDRESS,
eth_private_key=ETHEREUM_KEY,
web3=w3,
)
recovery = client.eth_private.recovery(ethereum_address=ETHEREUM_ADDRESS)
stark_key = recovery.data["starkKey"]
## dYdX info
network = constants.NETWORK_ID_MAINNET
DYDX_ADDRESS = constants.STARKWARE_PERPETUALS_CONTRACT[network]
DYDX_ASSET_TYPE = constants.COLLATERAL_ASSET_ID_BY_NETWORK_ID[network]
abi_url = "https://raw.githubusercontent.com/dydxprotocol/starkex-eth/master/src/contracts/starkware-perpetual-abi.json"
abi = requests.get(url=abi_url).json()
# contract
contract = w3.eth.contract(address=DYDX_ADDRESS, abi=abi["abi"])
withdraw = contract.functions.withdraw(
starkKey=int(stark_key, 16),
assetType=DYDX_ASSET_TYPE,
)
necessary_gas = withdraw.estimateGas()
gas_eth = w3.fromWei((necessary_gas * w3.eth.gasPrice), "ether")
print(f"gas cost estimation = {gas_eth:.6f} ETH")
# create txn
transaction = withdraw.buildTransaction()
transaction.update({"from": ETHEREUM_ADDRESS})
transaction.update({"gas": necessary_gas})
transaction.update({"nonce": w3.eth.get_transaction_count(ETHEREUM_ADDRESS)})
# sign and send
signed_tx = w3.eth.account.sign_transaction(transaction, ETHEREUM_KEY)
txn_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
txn_receipt = w3.eth.wait_for_transaction_receipt(txn_hash)
print(txn_receipt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment