coinbase / coinbase-python

DEPRECATED — Coinbase Python API
Apache License 2.0
528 stars 217 forks source link

Basic call for ETH-USD returning BTC-USD #47

Closed jasonmellone closed 7 years ago

jasonmellone commented 7 years ago

Hello

I am new to the library, but with a very basic piece of code that looks like the following:

from coinbase.wallet.client import Client
api_key = "api_key"
api_secret = "api_secret"

client = Client(api_key, api_secret)

currency_code = 'ETH-USD'  # can also use EUR, CAD, etc.

# Make the request
price = client.get_spot_price(currency_code = 'ETH-USD')

print 'Current  price in %s: %s' % (currency_code, price.amount)

But it is returning: Current price in ETH-USD: 2615.60

Which is obviously the BTC-USD price.

What am I doing wrong here?

mavieth commented 7 years ago

Same issue.

spiliopoulos commented 7 years ago

Take a look at my comment here . It might be that you have not updated your api version.

jasonmellone commented 7 years ago

I'm on 2.0.6 and getting the issue. I don't think this is a version thing.

JLDevOps commented 7 years ago

I'm getting the same issue, but have found the solution. In your python library, if you installed via pip, go to /site-packages/coinbase/wallet/client.py:

Change this section:

 def get_spot_price(self, **params):
    """https://developers.coinbase.com/api/v2#get-spot-price"""
    response = self._get('v2', 'prices', 'spot', data=params)
    return self._make_api_object(response, APIObject)

to:

def get_spot_price(self, **params):
"""https://developers.coinbase.com/api/v2#get-spot-price"""

    if 'currency_pair' in params:
        currency_pair = params['currency_pair']
   else:
       currency_pair = 'BTC-USD'
    response = self._get('v2', 'prices', currency_pair, 'spot', data=params)
    return self._make_api_object(response, APIObject)

This will get you the correct results for ETH-USD prices.

The problem now is that, the client.py file in the repo has already been updated to the code above, but has not been pushed for pip installation, so either you install the package from the repo or have to go in and change the settings in Python.


Update: If you don't like that option above (like messing with the client file), put this in your "own" python file:

def _get_COIN_prices_(price_option, coin_option, currency):
    """ Gets the prices of coins with "coin_option"
        "coin_options" = BTC, ETH, LTC
        "price_options" = buy, sell, spot
        "currency" = USD, EUR, etc

        Base API endpoint = "https://api.coinbase.com/v2/prices/

        Currently using version 2, if you want to use version 1, switch to:
        Base API endpoint = "https://api.coinbase.com/v1/prices/
    """
    url = "https://api.coinbase.com/v2/prices/" + coin_option + "-" + currency + "/" + price_option
    header = ({'Accept': 'application/json',
               'Content-Type': 'application/json',
               'User-Agent': 'coinbase/python/2.0'})
    response = requests.get(url, headers=header)
    jsonData = json.loads(response.content)
    return jsonData

Then in your main method do:

coin_price = _get_COIN_prices_("buy", "ETH", "USD")
ETH_price = coin_price['data']['amount']
print ETH_price
jasonmellone commented 7 years ago

I chose to modify the client.py directly. Thanks

lyx-x commented 6 years ago

This may be a silly question. I installed coinbase via pip(3). The package I got includes the old version of client where the 'currency_pair' isn't used. Will we get a new release or it is recommended to install from source?

mego1st commented 6 years ago

Just to update, if anyone is using the code above, changing

price = client.get_spot_price(currency_code = 'ETH-USD')

to

price = client.get_spot_price(currency_pair = 'ETH-USD')

makes the code work as intended.