def _private_request( self, method, endpoint, data={}, ): now_iso_string = generate_now_iso() request_path = '/'.join(['/v3', endpoint]) signature = self.sign( request_path=request_path, method=method.upper(), iso_timestamp=now_iso_string, data=remove_nones(data), ) headers = { 'DYDX-SIGNATURE': signature, 'DYDX-API-KEY': self.api_key_credentials['key'], 'DYDX-TIMESTAMP': now_iso_string, 'DYDX-PASSPHRASE': self.api_key_credentials['passphrase'], } return request( self.host + request_path, method, headers, data, )
def _request(self, method, endpoint, opt_ethereum_address, data={}): ethereum_address = opt_ethereum_address or self.default_address request_path = '/'.join(['/v3', endpoint]) timestamp = generate_now_iso() signature = sign_off_chain_action( self.eth_signer, ethereum_address, generate_api_key_action( request_path, method, data, ), timestamp, ) return request( self.host + request_path, method, { 'DYDX-SIGNATURE': signature, 'DYDX-TIMESTAMP': timestamp, 'DYDX-ETHEREUM-ADDRESS': ethereum_address, }, data, )
def _post( self, endpoint, data, opt_ethereum_address, ): ethereum_address = opt_ethereum_address or self.default_address timestamp = generate_now_iso() signature = sign_off_chain_action( self.eth_signer, ethereum_address, generate_onboarding_action(), timestamp, ) request_path = '/'.join(['/v3', endpoint]) return request( self.host + request_path, 'post', { 'DYDX-SIGNATURE': signature, 'DYDX-ETHEREUM-ADDRESS': ethereum_address, }, data, )
def _request( self, method, endpoint, opt_ethereum_address, data={} ): ethereum_address = opt_ethereum_address or self.default_address request_path = '/'.join(['/v3', endpoint]) timestamp = generate_now_iso() signature = self.signer.sign( ethereum_address, method=method.upper(), request_path=request_path, body=json_stringify(data) if data else '{}', timestamp=timestamp, ) return request( self.host + request_path, method, { 'DYDX-SIGNATURE': signature, 'DYDX-TIMESTAMP': timestamp, 'DYDX-ETHEREUM-ADDRESS': ethereum_address, }, data, )
async def log_user_stream(self, output: asyncio.Queue): while True: try: print(self._wss_stream_url) async with websockets.connect(self._wss_stream_url) as ws: ws: websockets.WebSocketClientProtocol = ws client = Client( network_id=self._network_id, host=self._http_stream_url, default_ethereum_address=self._eth_address, api_key_credentials=self._api_key_credentials, ) now_iso_string = generate_now_iso() signature = client.private.sign( request_path='/ws/accounts', method='GET', iso_timestamp=now_iso_string, data={}, ) req = { 'type': 'subscribe', 'channel': 'v3_accounts', 'accountNumber': '0', 'apiKey': self._api_key_credentials['key'], 'passphrase': self._api_key_credentials['passphrase'], 'timestamp': now_iso_string, 'signature': signature, } # Send subscription message to websocket channel try: req = json.dumps(req) await ws.send(req) except Exception as e: print(e) async for raw_msg in self.ws_messages(ws): msg_json: Dict[str, any] = ujson.loads(raw_msg) output.put_nowait(msg_json) except asyncio.CancelledError: raise except Exception: self.logger().error( "Unexpected error. Retrying after 5 seconds... ", exc_info=True) await asyncio.sleep(5)
def get_ws_auth_params(self): ts = generate_now_iso() auth_sig = self._dydx_client.sign( request_path='/ws/accounts', method='GET', timestamp=ts, data={}, ) ws_auth_params = { "type": "subscribe", "channel": "v3_accounts", "accountNumber": self._dydx_client.account_number, "apiKey": self._dydx_client.api_credentials['key'], "passphrase": self._dydx_client.api_credentials['passphrase'], "timestamp": ts, "signature": auth_sig } return ws_auth_params
from web3 import Web3 # Ganache test address. ETHEREUM_ADDRESS = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b' # Ganache node. WEB_PROVIDER_URL = 'http://localhost:8545' client = Client( network_id=NETWORK_ID_ROPSTEN, host=API_HOST_ROPSTEN, default_ethereum_address=ETHEREUM_ADDRESS, web3=Web3(Web3.HTTPProvider(WEB_PROVIDER_URL)), ) now_iso_string = generate_now_iso() signature = client.private.sign( request_path='/ws/accounts', method='GET', iso_timestamp=now_iso_string, data={}, ) req = { 'type': 'subscribe', 'channel': 'v3_accounts', 'accountNumber': '0', 'apiKey': client.api_key_credentials['key'], 'passphrase': client.api_key_credentials['passphrase'], 'timestamp': now_iso_string, 'signature': signature, }