|
45 | 45 | #from decimal import Decimal |
46 | 46 |
|
47 | 47 | 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 |
49 | 49 |
|
50 | 50 |
|
51 | 51 | class CoinbaseAccount(object): |
@@ -193,6 +193,10 @@ def contacts(self): |
193 | 193 | response = self.session.get(url, params=self.global_request_params) |
194 | 194 | return [contact['contact'] for contact in response.json()['contacts']] |
195 | 195 |
|
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
196 | 200 | def buy_price(self, qty=1): |
197 | 201 | """ |
198 | 202 | Return the buy price of BitCoin in USD |
@@ -225,6 +229,48 @@ def sell_price(self, qty=1): |
225 | 229 | # response = self.session.get(url) |
226 | 230 | # return response.json() |
227 | 231 |
|
| 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 | + |
228 | 274 | def request(self, from_email, amount, notes='', currency='BTC'): |
229 | 275 | """ |
230 | 276 | Request BitCoin from an email address to be delivered to this account |
|
0 commit comments