Skip to content

Commit a2c2cb3

Browse files
committed
fix orderbook decimal json serialization
1 parent 4002b02 commit a2c2cb3

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

gdax/orderbook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ async def __aenter__(self):
5454
if self._trade_file:
5555
await asyncio.gather(
5656
*[self._trade_file.write(
57-
f'B {product_id} {json.dumps(book)}\n')
57+
f'B {product_id} '
58+
f'{json.dumps(book, cls=gdax.utils.DecimalEncoder)}\n')
5859
for product_id, book in zip(self.product_ids, books)])
5960

6061
for product_id, book in zip(self.product_ids, books):

gdax/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Utils for message signing, etc."""
22

33
import base64
4+
import decimal
45
import hashlib
56
import hmac
7+
import json
68

79

810
def get_signature(path, method, body, timestamp, api_secret):
@@ -19,3 +21,10 @@ def get_signature(path, method, body, timestamp, api_secret):
1921
signature_b64 = base64.b64encode(signature.digest())
2022

2123
return signature_b64.decode('ascii')
24+
25+
26+
class DecimalEncoder(json.JSONEncoder):
27+
def default(self, o):
28+
if isinstance(o, decimal.Decimal):
29+
return float(o)
30+

tests/test_orderbook.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import gdax
1111
import gdax.orderbook
12+
import gdax.utils
1213

1314
from tests.helpers import AsyncContextManagerMock, generate_id
1415

@@ -258,9 +259,16 @@ async def test_logfile(self, mock_book, mock_connect):
258259
json.dumps(message_expected)
259260
]
260261
product_id = 'ETH-USD'
261-
book = {'bids': [], 'asks': [], 'sequence': 1}
262+
book = {
263+
'bids': [[Decimal("849.6"), Decimal("100"), id2]],
264+
'asks': [[Decimal("849.61"), Decimal("0.02"), id1]],
265+
'sequence': 1,
266+
}
267+
262268
mock_book.return_value = book
263-
calls = [call(f'B {product_id} {json.dumps(book)}\n')]
269+
calls = [call(f'B {product_id} '
270+
f'{json.dumps(book, cls=gdax.utils.DecimalEncoder)}\n')]
271+
264272
with patch('aiofiles.open',
265273
new_callable=AsyncContextManagerMock) as mock_open:
266274
mock_open.return_value.aenter.write = CoroutineMock()

0 commit comments

Comments
 (0)