Skip to content

Commit 401b123

Browse files
committed
convert more return values to Decimal
1 parent 409b6e0 commit 401b123

File tree

3 files changed

+460
-40
lines changed

3 files changed

+460
-40
lines changed

TODO.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# TODO
22

3-
- improve test coverage, especially for the order book
43
- client-side rate limiting
5-
- convert return API values from string to Decimal
64
- better enforce API rules
75
- re-raise aiohttp.client_exceptions.ClientResponseError into a more meaningful type
86
- fix the 'change' order book message

gdax/trader.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ def _convert_return_fields(self, fields, decimal_fields, convert_all):
5959
elif isinstance(fields, dict):
6060
new_fields = {}
6161
for k, v in fields.items():
62-
if (decimal_fields is not None and k in decimal_fields) \
63-
or convert_all:
62+
if isinstance(v, dict):
63+
new_fields[k] = self._convert_return_fields(
64+
v, decimal_fields, convert_all)
65+
elif ((decimal_fields is not None and k in decimal_fields)
66+
or convert_all):
6467
if isinstance(v, list):
6568
new_fields[k] = self._convert_return_fields(
6669
v, decimal_fields, convert_all)
@@ -193,7 +196,6 @@ async def get_currencies(self):
193196
async def get_time(self):
194197
return await self._get('/time')
195198

196-
# TODO: convert return values
197199
# authenticated API
198200
async def get_account(self, account_id=''):
199201
assert self.authenticated
@@ -320,7 +322,13 @@ async def margin_transfer(self, margin_profile_id, transfer_type,
320322

321323
async def get_position(self):
322324
assert self.authenticated
323-
return await self._get('/position')
325+
return await self._get(
326+
'/position',
327+
decimal_return_fields={'max_funding_value', 'funding_value',
328+
'amount', 'balance', 'hold',
329+
'funded_amount', 'default_amount', 'price',
330+
'sell', 'size', 'funds', 'complement',
331+
'max_size'})
324332

325333
async def close_position(self, repay_only=False):
326334
assert self.authenticated
@@ -336,7 +344,8 @@ async def deposit(self, amount, currency, payment_method_id):
336344
"currency": currency,
337345
"payment_method_id": payment_method_id,
338346
}
339-
return await self._post('/deposits/payment-method', data=payload)
347+
return await self._post('/deposits/payment-method', data=payload,
348+
decimal_return_fields={'amount'})
340349

341350
async def coinbase_deposit(self, amount, currency, coinbase_account_id):
342351
assert self.authenticated
@@ -345,7 +354,8 @@ async def coinbase_deposit(self, amount, currency, coinbase_account_id):
345354
"currency": currency,
346355
"coinbase_account_id": coinbase_account_id,
347356
}
348-
return await self._post('/deposits/coinbase-account', data=payload)
357+
return await self._post('/deposits/coinbase-account', data=payload,
358+
decimal_return_fields={'amount'})
349359

350360
async def withdraw(self, amount, currency, payment_method_id):
351361
assert self.authenticated
@@ -354,7 +364,8 @@ async def withdraw(self, amount, currency, payment_method_id):
354364
"currency": currency,
355365
"payment_method_id": payment_method_id,
356366
}
357-
return await self._post('/withdrawals/payment-method', data=payload)
367+
return await self._post('/withdrawals/payment-method', data=payload,
368+
decimal_return_fields={'amount'})
358369

359370
async def coinbase_withdraw(self, amount, currency, coinbase_account_id):
360371
assert self.authenticated
@@ -363,7 +374,8 @@ async def coinbase_withdraw(self, amount, currency, coinbase_account_id):
363374
"currency": currency,
364375
"coinbase_account_id": coinbase_account_id,
365376
}
366-
return await self._post('/withdrawals/coinbase', data=payload)
377+
return await self._post('/withdrawals/coinbase', data=payload,
378+
decimal_return_fields={'amount'})
367379

368380
async def crypto_withdraw(self, amount, currency, crypto_address):
369381
assert self.authenticated
@@ -372,15 +384,17 @@ async def crypto_withdraw(self, amount, currency, crypto_address):
372384
"currency": currency,
373385
"crypto_address": crypto_address
374386
}
375-
return await self._post('/withdrawals/crypto', data=payload)
387+
return await self._post('/withdrawals/crypto', data=payload,
388+
decimal_return_fields={'amount'})
376389

377390
async def get_payment_methods(self):
378391
assert self.authenticated
379392
return await self._get('/payment-methods')
380393

381394
async def get_coinbase_accounts(self):
382395
assert self.authenticated
383-
return await self._get('/coinbase-accounts')
396+
return await self._get('/coinbase-accounts',
397+
decimal_return_fields={'balance'})
384398

385399
async def create_report(self, report_type, start_date, end_date,
386400
product_id=None, account_id=None,
@@ -417,7 +431,9 @@ async def get_report(self, report_id):
417431

418432
async def get_trailing_volume(self):
419433
assert self.authenticated
420-
return await self._get("/users/self/trailing-volume")
434+
return await self._get('/users/self/trailing-volume',
435+
decimal_return_fields={'exchange_volume',
436+
'volume'})
421437

422438

423439
async def main(): # pragma: no cover

0 commit comments

Comments
 (0)