-
Notifications
You must be signed in to change notification settings - Fork 0
/
funding.py
179 lines (140 loc) · 5.56 KB
/
funding.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from time import sleep
from typing import List
from dydx3 import private_key_to_public_key_pair_hex
from dydx3.constants import *
from dydx3.helpers.request_helpers import random_client_id, iso_to_epoch_seconds
from dydx3.starkex.withdrawal import SignableWithdrawal
from eulith_web3.erc20 import EulithERC20
from eulith_web3.eulith_web3 import EulithWeb3
from eulith_web3.signing import construct_signing_middleware
from accounts import _get_account_internal
from credentials import *
from private_request import private_request
from utils import (
get_exchange_contract_address,
get_exchange_contract,
get_dydx_host,
now_timestamp_plus_minutes,
get_signer,
)
def approve_exchange_contract(args):
network_id = int(args.network_id)
amount = float(args.amount)
if network_id != 1:
raise Exception("unsupported network_id, can only approve on mainnet")
signer = get_signer()
print(f"Executing from eth address: {signer.address}")
with EulithWeb3(
"https://eth-main.eulithrpc.com/v0",
eulith_token=EULITH_TOKEN,
signing_middle_ware=construct_signing_middleware(signer),
) as ew3:
usdc_address = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
contract = EulithERC20(ew3, ew3.to_checksum_address(usdc_address))
spender = get_exchange_contract_address(network_id)
approve_tx = contract.approve_float(
spender, amount, {"from": signer.address, "gas": 100000}
)
tx_hash = ew3.eth.send_transaction(approve_tx)
print(f"Approve tx hash {tx_hash.hex()} for amount: {amount} USDC")
def get_transfers(args):
network_id = int(args.network_id)
transfers = _get_transfers_internal(network_id)
if len(transfers) == 0:
print(
"Could not find any active transfers, you may need to wait a few minutes..."
)
for i, t in enumerate(transfers):
print(f"Transfer {i}")
print(t)
print()
def _get_transfers_internal(network_id: int) -> List:
host = get_dydx_host(network_id)
response = private_request(host, "get", "transfers")
return response.get("transfers")
def deposit_to_dydx(args):
network_id = int(args.network_id)
amount = float(args.amount)
if network_id != 1:
raise Exception("unsupported network_id, can only deposit on mainnet")
signer = get_signer()
print(f"Executing from eth address: {signer.address}")
with EulithWeb3(
"https://eth-main.eulithrpc.com/v0",
eulith_token=EULITH_TOKEN,
signing_middle_ware=construct_signing_middleware(signer),
) as ew3:
contract = get_exchange_contract(network_id, ew3)
public_x, public_y = private_key_to_public_key_pair_hex(STARK_PRIVATE_KEY)
account = _get_account_internal(network_id)
position_id = account.get("positionId")
tx_params = contract.functions.deposit(
int(public_x, 16),
COLLATERAL_ASSET_ID_BY_NETWORK_ID[network_id],
int(position_id),
int(float(amount) * float(ASSET_RESOLUTION[COLLATERAL_ASSET])),
).build_transaction({"from": signer.address, "gas": 150000})
tx_hash = ew3.eth.send_transaction(tx_params)
print(f"Deposit to DyDx tx hash: {tx_hash.hex()}")
print()
print("Fetching transfers into dydx...")
attempt = 0
printed = False
while attempt < 15:
attempt += 1
transfers = _get_transfers_internal(network_id)
if len(transfers) == 0:
sleep(2)
continue
else:
printed = True
print(transfers)
if not printed:
print(
f"Unable to find transfer, double check after a few minutes by running: "
f"python manage.py get-transfers --network-id 1"
)
def start_withdraw_from_dydx(args):
network_id = int(args.network_id)
amount = int(args.amount)
print(f"Withdrawing amount: {amount} USDC")
client_id = random_client_id()
expiration = now_timestamp_plus_minutes(60 * 24 * 30) # 30 days
expiration_epoch_seconds = iso_to_epoch_seconds(expiration)
account = _get_account_internal(network_id)
position_id = account.get("positionId")
withdrawal_to_sign = SignableWithdrawal(
network_id=network_id,
position_id=position_id,
client_id=client_id,
human_amount=str(amount),
expiration_epoch_seconds=expiration_epoch_seconds,
)
signature = withdrawal_to_sign.sign(STARK_PRIVATE_KEY)
params = {
"amount": str(amount),
"asset": "USDC",
"expiration": expiration,
"clientId": client_id,
"signature": signature,
}
host = get_dydx_host(network_id)
print("Withdraw response: ")
print(private_request(host, "post", "withdrawals", params))
def execute_withdraws(args):
network_id = int(args.network_id)
signer = get_signer()
with EulithWeb3(
"https://eth-main.eulithrpc.com/v0",
eulith_token=EULITH_TOKEN,
signing_middle_ware=construct_signing_middleware(signer),
) as ew3:
contract = get_exchange_contract(network_id, ew3)
public_x, public_y = private_key_to_public_key_pair_hex(STARK_PRIVATE_KEY)
tx_params = contract.functions.withdraw(
int(public_x, 16),
COLLATERAL_ASSET_ID_BY_NETWORK_ID[network_id],
).build_transaction({"from": signer.address, "gas": 200000})
tx_hash = ew3.eth.send_transaction(tx_params)
print(f"Withdraw tx hash: {tx_hash.hex()}")
print()