forked from BitonicNL/bl3p-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·208 lines (161 loc) · 5.75 KB
/
Copy pathexample.py
File metadata and controls
executable file
·208 lines (161 loc) · 5.75 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#! /usr/bin/python
# this code was written by folkert@vanheusden.com
# it has been released under AGPL v3.0
# it requires 'pycurl'
# in debian this can be found in the 'python-pycurl' package
import base64
import ConfigParser
import hashlib
import hmac
import json
import pycurl
import sys
import urllib
try:
from io import BytesIO
except ImportError:
from StringIO import StringIO as BytesIO
from datetime import datetime
from time import mktime
class Bl3pApi:
url = None
pubKey = None
secKey = None
verbose = False
def __init__(self, u, pk, sk):
self.url = u
self.pubKey = pk
self.secKey = sk
def setVerbose(self, v):
self.verbose = v
def apiCall(self, path, params):
dt = datetime.utcnow()
us = mktime(dt.timetuple()) * 1000 * 1000 + dt.microsecond
nonce = '%d' % us
# generate the POST data string
post_data = urllib.urlencode(params)
body = '%s%c%s' % (path, 0x00, post_data)
privkey_bin = base64.b64decode(self.secKey)
signature_bin = hmac.new(privkey_bin, body, hashlib.sha512).digest()
signature = base64.b64encode(signature_bin)
fullpath = '%s%s' % (self.url, path)
headers = [ 'Rest-Key: %s' % self.pubKey, 'Rest-Sign: %s' % signature ]
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.USERAGENT, 'Mozilla/4.0 (compatible; BL3P Python client written by folkert@vanheusden.com; 0.1)');
c.setopt(c.WRITEFUNCTION, buffer.write)
c.setopt(c.URL, fullpath);
c.setopt(c.POST, 1);
c.setopt(c.POSTFIELDS, post_data);
c.setopt(c.HTTPHEADER, headers);
c.setopt(c.SSLVERSION, 1);
c.setopt(c.SSL_VERIFYPEER, True);
c.setopt(c.SSL_VERIFYHOST, 2);
c.setopt(c.CONNECTTIMEOUT, 5);
c.setopt(c.TIMEOUT, 10);
if self.verbose:
c.setopt(c.VERBOSE, 1)
else:
c.setopt(c.VERBOSE, 0)
c.perform()
response_code = c.getinfo(c.RESPONSE_CODE)
if response_code != 200:
raise Exception('unexpected response code: %d' % response_code)
c.close()
return json.loads(buffer.getvalue())
# multiply the btc value (e.g 1.3BTC) with this and round-up/down
def getBtcMultiplier(self):
return 100000000
def getEurMutiplier(self):
return 100000
# Add order to your account.
# @method addOrder
# @param market 'EUR'
# @param order_type 'bid' or 'ask'
# bid: used if you want to buy bitcoins
# ask: if you want to sell bitcoins
# @param order_amount Amount to order *1e8 (so 1 bitcoin is 100000000)
# @param order_price Price of order *1e5 (1 euro is 100000)
# @return Result of the add order call
def addOrder(self, market, order_type, order_amount, order_price):
params = {
'type' : order_type,
'amount_int' : order_amount,
'price_int' : order_price,
'fee_currency' : 'BTC'
}
return self.apiCall('%sEUR/money/order/add' % market, params)
# Cancel a specific order.
# @method cancelOrder
# @param market 'EUR'
# @param order_id Id of the order
# @return Direct resulf of the '<market>/money/order/cancel' call
def cancelOrder(self, market, order_id):
params = { 'order_id' : order_id }
return self.apiCall("%sEUR/money/order/cancel" % market, params)
# Fetch information about an specific order
# @method orderInfo
# @param market 'EUR'
# @param order_id Id of the order
# @return Direct resulf of the '<market>/money/order/result' call
def orderInfo(self, market, order_id):
params = { 'order_id' : order_id }
return self.apiCall("%sEUR/money/order/result" % market, params)
# Fetch complete orderbook
# @method fullDepth
# @param market 'EUR'
# @return Direct resulf of the '<market>/money/depth/full' call
def fullDepth(self, market):
return self.apiCall("%sEUR/money/depth/full" % market, { })
# Get new deposit address.
# @method getNewDepositAddress
# @param market 'EUR'
# @return new deposit address
def getNewDepositAddress(self, market):
return self.apiCall("%sEUR/money/new_deposit_address" % market, { })
# Get the most recent generated deposit address
# @method getLastDepositAddress
# @param market 'EUR'
# @return most recent generated deposit address
def getLastDepositAddress(self, market):
return self.apiCall("%sEUR/money/deposit_address" % market, { })
# Get the last 1000 trades that where executed before an specific trade_id
# @method fetchTrades
# @param market 'EUR'
# @param trade_id id of the trade
# @return array of last 1000 executed trades.
def fetchLast1000Trades(self, market, trade_id):
params = { 'trade_id' : trade_id }
return self.apiCall("%sEUR/money/trades/fetch" % market, params)
# Get the transaction history
# @method walletHistory
# @param currency currency which currency
# @param n how many to retrieve
# @return array json structure with the transaction history
def walletHistory(self, currency, n):
params = { 'currency' : currency, 'recs_per_page' : n }
return self.apiCall('GENMKT/money/wallet/history', params)
# Get all open orders.
# @method getAllActiveOrders
# @param market 'EUR'
# @return array of open orders
def getAllActiveOrders(self, market):
return self.apiCall("%sEUR/money/orders" % market, { });
# Get the balances
# @method getBalances
# @return array json structure with the wallet balances
def getBalances(self):
params = { }
return self.apiCall('GENMKT/money/info', params)
def d(j):
print json.dumps(j, sort_keys=True, indent=4, separators=(',', ': '))
# example:
config = ConfigParser.RawConfigParser()
if len(sys.argv) == 2:
config.read(sys.argv[1])
else:
config.read('example.cfg')
public_key = config.get('bl3p', 'public_key') # ........-....-....-....-............
secret_key = config.get('bl3p', 'secret_key') # (long string with a-z/A-Z/0-9 and =)
b = Bl3pApi('https://api.bl3p.eu/1/', public_key, secret_key)
d(b.walletHistory('BTC', 10))