Skip to content

Commit eeede0c

Browse files
author
kroberts
committed
Added Buy and Sell support
Added buy_btc and sell_btc functions which buy or sell a specified quantity of bitcoins through Coinbase. Also added CoinbaseError class which currently only contains the list of strings returned by the 'errors' index into the response. Tested with api_key.
1 parent 2f9cf0c commit eeede0c

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

coinbase/__init__.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#from decimal import Decimal
4646

4747
from coinbase.config import COINBASE_ENDPOINT
48-
from coinbase.models import CoinbaseAmount, CoinbaseTransaction, CoinbaseUser, CoinbaseTransfer
48+
from coinbase.models import CoinbaseAmount, CoinbaseTransaction, CoinbaseUser, CoinbaseTransfer, CoinbaseError
4949

5050

5151
class CoinbaseAccount(object):
@@ -193,6 +193,10 @@ def contacts(self):
193193
response = self.session.get(url, params=self.global_request_params)
194194
return [contact['contact'] for contact in response.json()['contacts']]
195195

196+
197+
198+
199+
196200
def buy_price(self, qty=1):
197201
"""
198202
Return the buy price of BitCoin in USD
@@ -225,6 +229,48 @@ def sell_price(self, qty=1):
225229
# response = self.session.get(url)
226230
# return response.json()
227231

232+
233+
def buy_btc(self, qty, pricevaries=False):
234+
"""
235+
Buy BitCoin from Coinbase for USD
236+
:param qty: BitCoin quantity to be bought
237+
:param pricevaries: Boolean value that indicates whether or not the transaction should
238+
be processed if Coinbase cannot gaurentee the current price.
239+
:return: CoinbaseTransfer with all transfer details on success or
240+
CoinbaseError with the error list received from Coinbase on failure
241+
"""
242+
url = COINBASE_ENDPOINT + '/buys'
243+
request_data = {
244+
"qty": qty,
245+
"agree_btc_amount_varies": pricevaries
246+
}
247+
response = self.session.post(url=url, data=json.dumps(request_data), params=self.global_request_params)
248+
response_parsed = response.json()
249+
if response_parsed['success'] == False:
250+
return CoinbaseError(response_parsed['errors'])
251+
252+
return CoinbaseTransfer(response_parsed['transfer'])
253+
254+
255+
def sell_btc(self, qty):
256+
"""
257+
Sell BitCoin to Coinbase for USD
258+
:param qty: BitCoin quantity to be sold
259+
:return: CoinbaseTransfer with all transfer details on success or
260+
CoinbaseError with the error list received from Coinbase on failure
261+
"""
262+
url = COINBASE_ENDPOINT + '/sells'
263+
request_data = {
264+
"qty": qty,
265+
}
266+
response = self.session.post(url=url, data=json.dumps(request_data), params=self.global_request_params)
267+
response_parsed = response.json()
268+
if response_parsed['success'] == False:
269+
return CoinbaseError(response_parsed['errors'])
270+
271+
return CoinbaseTransfer(response_parsed['transfer'])
272+
273+
228274
def request(self, from_email, amount, notes='', currency='BTC'):
229275
"""
230276
Request BitCoin from an email address to be delivered to this account

coinbase/models/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
from transaction import CoinbaseTransaction
55
from transfer import CoinbaseTransfer
66
from contact import CoinbaseContact
7-
from user import CoinbaseUser
7+
from user import CoinbaseUser
8+
from error import CoinbaseError

coinbase/models/error.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
__author__ = 'kroberts'
2+
3+
4+
5+
class CoinbaseError(object):
6+
7+
def __init__(self, errorList):
8+
self.error = errorList

coinbase/models/transfer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, transfer):
1818
self.fees_bank = CoinbaseAmount(fees_bank_cents, fees_bank_currency_iso)
1919

2020
self.payout_date = transfer['payout_date']
21-
self.transaction_id = transfer['transaction_id']
21+
self.transaction_id = transfer.get('transaction_id','')
2222
self.status = transfer['status']
2323

2424
btc_amount = transfer['btc']['amount']
@@ -33,7 +33,7 @@ def __init__(self, transfer):
3333
total_currency = transfer['total']['currency']
3434
self.total_amount = CoinbaseAmount(total_amount, total_currency)
3535

36-
self.description = transfer['description']
36+
self.description = transfer.get('description','')
3737

3838
def refresh(self):
3939
pass

0 commit comments

Comments
 (0)