Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
309 changes: 199 additions & 110 deletions coinbase/__init__.py

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion coinbase/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@


TEMP_CREDENTIALS = '''
{"_module": "oauth2client.client", "token_expiry": "2013-03-24T02:37:50Z", "access_token": "2a02d1fc82b1c42d4ea94d6866b5a232b53a3a50ad4ee899ead9afa6144c2ca3", "token_uri": "https://www.coinbase.com/oauth/token", "invalid": false, "token_response": {"access_token": "2a02d1fc82b1c42d4ea94d6866b5a232b53a3a50ad4ee899ead9afa6144c2ca3", "token_type": "bearer", "expires_in": 7200, "refresh_token": "ffec0153da773468c8cb418d07ced54c13ca8deceae813c9be0b90d25e7c3d71", "scope": "all"}, "client_id": "2df06cb383f4ffffac20e257244708c78a1150d128f37d420f11fdc069a914fc", "id_token": null, "client_secret": "7caedd79052d7e29aa0f2700980247e499ce85381e70e4a44de0c08f25bded8a", "revoke_uri": "https://accounts.google.com/o/oauth2/revoke", "_class": "OAuth2Credentials", "refresh_token": "ffec0153da773468c8cb418d07ced54c13ca8deceae813c9be0b90d25e7c3d71", "user_agent": null}'''
{"_module": "oauth2client.client", "token_expiry": "2013-03-24T02:37:50Z", "access_token": "2a02d1fc82b1c42d4ea94d6866b5a232b53a3a50ad4ee899ead9afa6144c2ca3", "token_uri": "https://www.coinbase.com/oauth/token", "invalid": false, "token_response": {"access_token": "2a02d1fc82b1c42d4ea94d6866b5a232b53a3a50ad4ee899ead9afa6144c2ca3", "token_type": "bearer", "expires_in": 7200, "refresh_token": "ffec0153da773468c8cb418d07ced54c13ca8deceae813c9be0b90d25e7c3d71", "scope": "all"}, "client_id": "2df06cb383f4ffffac20e257244708c78a1150d128f37d420f11fdc069a914fc", "id_token": null, "client_secret": "7caedd79052d7e29aa0f2700980247e499ce85381e70e4a44de0c08f25bded8a", "revoke_uri": "https://accounts.google.com/o/oauth2/revoke", "_class": "OAuth2Credentials", "refresh_token": "ffec0153da773468c8cb418d07ced54c13ca8deceae813c9be0b90d25e7c3d71", "user_agent": null}'''

CENTS_PER_BITCOIN = 100000000
CENTS_PER_OTHER = 100
COINBASE_ITEMS_PER_PAGE = 30
4 changes: 3 additions & 1 deletion coinbase/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
from transfer import CoinbaseTransfer
from contact import CoinbaseContact
from user import CoinbaseUser
from error import CoinbaseError
from error import CoinbaseError
from button import CoinbaseButton
from order import CoinbaseOrder
19 changes: 17 additions & 2 deletions coinbase/models/amount.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
__author__ = 'gsibble'

class CoinbaseAmount(float):
from decimal import Decimal
from coinbase.config import CENTS_PER_BITCOIN, CENTS_PER_OTHER

class CoinbaseAmount(Decimal):

def __new__(self, amount, currency):
return float.__new__(self, amount)
return Decimal.__new__(self, amount)

def __init__(self, amount, currency):
super(CoinbaseAmount, self).__init__()
self.currency = currency

def __eq__(self, other, *args, **kwargs):
if isinstance(other, self.__class__) and self.currency != other.currency:
return False
return super(CoinbaseAmount, self).__eq__(other, *args, **kwargs)

@classmethod
def from_cents(cls, cents, currency):
if currency == 'BTC':
return cls(Decimal(cents)/CENTS_PER_BITCOIN, currency)
else:
return cls(Decimal(cents)/CENTS_PER_OTHER, currency)
43 changes: 43 additions & 0 deletions coinbase/models/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
__author__ = 'vkhougaz'

from amount import CoinbaseAmount

class CoinbaseButton(object):

def __init__(self, button):
self.data = button
# Sometimes it's called code (create button) and sometimes id (sub item of create button)
# so we map them together
if 'id' in button:
self.button_id = button['id']
elif 'code' in button:
self.button_id = button['code']
else:
self.button_id = None
self.code = self.button_id

self.name = button['name']
if 'price' in button:
price_cents = button['price']['cents']
price_currency_iso = button['price']['currency_iso']
self.price = CoinbaseAmount.from_cents(price_cents, price_currency_iso)
else:
self.price = None
self.type = button.get('type', None)
self.style = button.get('style', None)
self.text = button.get('text', None)
self.description = button.get('description', None)
self.custom = button.get('custom', None)
self.callback_url = button.get('callback_url', None)
self.success_url = button.get('success_url', None)
self.cancel_url = button.get('cancel_url', None)
self.info_url = button.get('info_url', None)
self.variable_price = button.get('variable_price', None)
self.choose_price = button.get('choose_price', None)
self.include_address = button.get('include_address', None)
self.include_email = button.get('include_email', None)
self.price1 = button.get('price1', None)
self.price2 = button.get('price2', None)
self.price3 = button.get('price3', None)
self.price4 = button.get('price4', None)
self.price5 = button.get('price4', None)
10 changes: 7 additions & 3 deletions coinbase/models/error.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
__author__ = 'kroberts'



class CoinbaseError(object):
class CoinbaseError(BaseException):

def __init__(self, errorList):
self.error = errorList

def __unicode__(self):
return unicode(self.error)

def __str__(self):
return str(self.error)
31 changes: 31 additions & 0 deletions coinbase/models/order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
__author__ = 'vkhougaz'

from amount import CoinbaseAmount
from button import CoinbaseButton
from transaction import CoinbaseTransaction
# from datetime import datetime

class CoinbaseOrder(object):
def __init__(self, order):
self.data = order

self.order_id = order['id']
# TODO: Account for timezone properly
#self.created_at = datetime.strptime(order['created_at'], '%Y-%m-%dT%H:%M:%S-08:00')
self.created_at = order['created_at']
self.status = order['status']
self.custom = order['custom']

btc_cents = order['total_btc']['cents']
btc_currency_iso = order['total_btc']['currency_iso']
self.total_btc = CoinbaseAmount.from_cents(btc_cents, btc_currency_iso)

native_cents = order['total_native']['cents']
native_currency_iso = order['total_native']['currency_iso']
self.total_native = CoinbaseAmount.from_cents(native_cents, native_currency_iso)

self.button = CoinbaseButton(order['button'])
if 'transaction' in order and order['transaction'] is not None:
self.transaction = CoinbaseTransaction(order['transaction'])
else:
self.transaction = None
19 changes: 11 additions & 8 deletions coinbase/models/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
class CoinbaseTransaction(object):

def __init__(self, transaction):
self.data = transaction

self.transaction_id = transaction['id']
self.created_at = transaction['created_at']
self.notes = transaction['notes']
self.created_at = transaction.get('created_at', None)
self.notes = transaction.get('notes', '')

transaction_amount = transaction['amount']['amount']
transaction_currency = transaction['amount']['currency']

self.amount = CoinbaseAmount(transaction_amount, transaction_currency)
if 'amount' in transaction:
transaction_amount = transaction['amount']['amount']
transaction_currency = transaction['amount']['currency']
self.amount = CoinbaseAmount(transaction_amount, transaction_currency)
else:
self.amount = None

self.status = transaction['status']
self.request = transaction['request']
self.status = transaction.get('status', None)
self.request = transaction.get('request', None)


#Sender Information
Expand Down
10 changes: 6 additions & 4 deletions coinbase/models/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
class CoinbaseTransfer(object):

def __init__(self, transfer):
self.data = transfer

self.type = transfer['type']
self.code = transfer['code']
self.created_at = transfer['created_at']

fees_coinbase_cents = transfer['fees']['coinbase']['cents']
fees_coinbase_currency_iso = transfer['fees']['coinbase']['currency_iso']
self.fees_coinbase = CoinbaseAmount(fees_coinbase_cents, fees_coinbase_currency_iso)
self.fees_coinbase = CoinbaseAmount.from_cents(fees_coinbase_cents, fees_coinbase_currency_iso)

fees_bank_cents = transfer['fees']['bank']['cents']
fees_bank_currency_iso = transfer['fees']['bank']['currency_iso']
self.fees_bank = CoinbaseAmount(fees_bank_cents, fees_bank_currency_iso)
self.fees_bank = CoinbaseAmount.from_cents(fees_bank_cents, fees_bank_currency_iso)

self.payout_date = transfer['payout_date']
self.transaction_id = transfer.get('transaction_id','')
self.transaction_id = transfer.get('transaction_id', '')
self.status = transfer['status']

btc_amount = transfer['btc']['amount']
Expand All @@ -33,7 +35,7 @@ def __init__(self, transfer):
total_currency = transfer['total']['currency']
self.total_amount = CoinbaseAmount(total_amount, total_currency)

self.description = transfer.get('description','')
self.description = transfer.get('description', '')

def refresh(self):
pass
Expand Down
Loading