-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsell.py
63 lines (47 loc) · 1.51 KB
/
sell.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
import base64
import hashlib
import hmac
import json
import time
from urllib.request import urlopen, Request
ACCESS_TOKEN = ''
SECRET_KEY = ''
UPPERCASE_SECRET_KEY = SECRET_KEY.upper()
HOST = 'https://api.coinone.co.kr/'
def get_base_payload():
return {
'access_token': ACCESS_TOKEN,
}
def str_2_byte(s, encode='utf-8'):
return bytes(s, encode)
def get_encoded_payload(payload):
payload['nonce'] = int(time.time() * 1000)
dumped_json = json.dumps(payload)
encoded_json = base64.b64encode(str_2_byte(dumped_json))
return encoded_json
def get_signature(encoded_payload):
signature = hmac.new(str_2_byte(UPPERCASE_SECRET_KEY), encoded_payload, hashlib.sha512)
return signature.hexdigest()
def get_response(url, payload):
encoded_payload = get_encoded_payload(payload)
signature = get_signature(encoded_payload)
headers = {
'Content-Type': 'application/json',
'X-COINONE-PAYLOAD': encoded_payload,
'X-COINONE-SIGNATURE': signature,
}
api_url = HOST + url
req = Request(api_url, data=encoded_payload, headers=headers, method='POST')
with urlopen(req) as res:
data = res.read().decode('utf-8')
return json.loads(data)
def create_payload(data):
payload = get_base_payload()
payload.update(data)
return payload
data = get_response('v2/order/limit_sell/', create_payload({
'price': 50000000,
'qty': 0.1,
'currency': 'btc',
}))
print(data)