Created
August 7, 2018 13:16
-
-
Save vsmelov/b25ef2dee1da2e9c7021ac6c5976b0c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import hashlib | |
import base64 | |
from pprint import pformat | |
import json | |
import asyncio | |
from collections import OrderedDict | |
from websockets import connect | |
def generate_signature(key, secret, action, data): | |
tstamp = int(time.time() * 1000) | |
signature_data = { | |
'_': tstamp, | |
'_ackey': key, | |
'_acsec': secret, | |
'_action': action | |
} | |
signature_data.update(data) | |
sorted_signature_data = OrderedDict(sorted(signature_data.items(), key=lambda t: t[0])) | |
def converter(data): | |
key = data[0] | |
value = data[1] | |
if isinstance(value, list): | |
return '='.join([str(key), ''.join(value)]) | |
else: | |
return '='.join([str(key), str(value)]) | |
items = map(converter, sorted_signature_data.items()) | |
signature_string = '&'.join(items) | |
sha256 = hashlib.sha256() | |
sha256.update(signature_string.encode("utf-8")) | |
sig = key + "." + str(tstamp) + "." | |
sig += base64.b64encode(sha256.digest()).decode("utf-8") | |
return sig | |
async def main(): | |
URL = "wss://test.deribit.com/ws/api/v1" | |
# URL = "wss://www.deribit.com/ws/api/v1" | |
key, secret = 'KEY', 'SECRET' ############################################################### | |
_conn = connect(URL) | |
ws = await _conn.__aenter__() | |
action = "/api/v1/private/cancelall" | |
data = {'instrument': 'BTC-31AUG18'} | |
signature = generate_signature(key, secret, action, data) | |
msg = { | |
'id': 0, | |
'action': action, | |
'arguments': data, | |
'sig': signature | |
} | |
print('send:\n{}'.format(pformat(msg))) | |
await ws.send(json.dumps(msg)) | |
resp = json.loads(await ws.recv()) | |
print('resp:\n{}'.format(pformat(resp))) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment