-
Notifications
You must be signed in to change notification settings - Fork 1
/
dydxl2.py
108 lines (95 loc) · 4.33 KB
/
dydxl2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from dydx3 import Client
from dydx3 import private_key_to_public_key_pair_hex
from dydx3.constants import API_HOST_ROPSTEN, API_HOST_MAINNET
from dydx3.constants import NETWORK_ID_ROPSTEN, NETWORK_ID_MAINNET
from dydx3.errors import DydxApiError
from web3 import Web3
import pickle
if __name__ == "__main__":
################################# Settings ##############################################
ETHEREUM_ADDRESS = "0xADDRESS"
ETH_PRIVATE_KEY = "PRIVATEKEY" # Set to None if you decide to use an ETH node instead of a private key
# geth --ropsten --syncmode "full" --http --rpc --rpcapi="db,eth,net,web3,personal,web3" --rpcaddr "localhost" --rpcport "8545" # This *SHOULD* work for starting a local ETH node with GETH. run it in a command prompt seperately
WEB_PROVIDER_URL = "http://localhost:8545" # RPC connection, only needed when you are running a ETH node
# testing = True # Use the ropsten testing network instead of the main ETH network
testing = False # Use the ropsten testing network instead of the main ETH network
##########################################################################################
if ETH_PRIVATE_KEY == "None":
ETH_PRIVATE_KEY = None
if testing:
API_HOST = API_HOST_ROPSTEN
NETWORK_ID = NETWORK_ID_ROPSTEN
else:
API_HOST = API_HOST_MAINNET
NETWORK_ID = NETWORK_ID_MAINNET
public_client1 = Client(
host=API_HOST,
)
# print(public_client1.public.get_markets()) # Get DYDX exchange market info as a dictionary
if ETH_PRIVATE_KEY:
client = Client(
network_id=NETWORK_ID,
host=API_HOST,
eth_private_key=ETH_PRIVATE_KEY,
default_ethereum_address=ETHEREUM_ADDRESS,
)
else:
client = Client(
network_id=NETWORK_ID,
host=API_HOST,
default_ethereum_address=ETHEREUM_ADDRESS,
web3=Web3(Web3.HTTPProvider(WEB_PROVIDER_URL)),
)
# Set STARK key.
stark_private_key = client.onboarding.derive_stark_key()
client.stark_private_key = stark_private_key
public_x, public_y = private_key_to_public_key_pair_hex(stark_private_key)
try:
onboarded_users = pickle.load(open("onboarded_users.data", "rb"))
except:
onboarded_users = []
if client.stark_private_key not in onboarded_users:
# Onboard the account.
try:
onboarding_response = client.onboarding.create_user(
stark_public_key=public_x,
stark_public_key_y_coordinate=public_y,
)
#print('onboarding_response', onboarding_response)
onboarded_users.append(client.stark_private_key)
pickle.dump(onboarded_users, open("onboarded_users.data", "wb"))
except Exception as e:
if "User wallet has no transactions, Ethereum or USDC" in str(e):
print("User wallet has no transactions, Ethereum or USDC")
while True:
input()
else:
# print("User already created!\n\n")
onboarded_users.append(client.stark_private_key)
pickle.dump(onboarded_users, open("onboarded_users.data", "wb"))
else:
# print("User already created!\n\n")
pass
# Query a private endpoint.
try:
accounts_response = client.private.get_accounts()
#print('accounts_response', accounts_response)
# print("Accounts: \n")
for account in accounts_response["accounts"]:
# print("accountNumber: " + account["accountNumber"])
# print("id: " + account["id"])
# print("starkKey: " + account["starkKey"])
# print("positionId: " + account["positionId"])
# print("equity: " + account["equity"])
print(account["equity"])
# print("freeCollateral: " + account["freeCollateral"])
# print("pendingDeposits: " + account["pendingDeposits"])
# print("pendingWithdrawals: " + account["pendingWithdrawals"])
# for open_pos in account["openPositions"]:
# print(str(open_pos) + " : " + str(account["openPositions"][open_pos]))
# print("quoteBalance: " + account["quoteBalance"])
# print("\n" * 1)
except DydxApiError:
print("API key not found")
# while True:
# input()