craysiii / binance

API Wrapper for the Binance cryptocurrency exchange written in Ruby.
MIT License
97 stars 79 forks source link

Add allPrices endpoint, and all_prices method #26

Closed glennfu closed 6 years ago

glennfu commented 6 years ago

With this endpoint added, #21 can be solved with 2 api calls.

def btc_balance
  price_data = $client.all_prices.inject({}) do |hash, data|
    hash.merge(data["symbol"] => data["price"].to_f)
  end

  info = $client.account_info(recvWindow: 100000)

  btc_value = info["balances"].sum do |balance|
    asset = balance["asset"]
    asset_qty = balance["free"].to_f + balance["locked"].to_f

    if asset_qty < 0.00000001
      0.0
    elsif asset == 'BTC'
      asset_qty
    else
      asset_qty * price_data["#{asset}BTC"]
    end
  end

  btc_value.round(8)
end
craysiii commented 6 years ago

Haven’t been keeping up with the API. Thanks!

glennfu commented 6 years ago

Np! I'm enjoying your library and happy to have found a spot to contribute.

craysiii commented 6 years ago

Ok go ahead and release a new gem version tonight so others can benefit from your contribution.

craysiii commented 6 years ago

Actually, just spotted something. Seems that in methods, you are using :price instead of :all_prices endpoint reference. New PR? :)

glennfu commented 6 years ago

That's a good question. Rest api here:

https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#symbol-price-ticker

Shows that the /v3/price endpoint with no arguments will return all prices. /v3/allPrices does not exist. However /v1/allPrices exists and seems to return the same thing as /v3/price. Which is preferable?

craysiii commented 6 years ago

Hmm.... if /v3/price works for the new method, then I don't see a reason to have the /allPrices endpoint that isn't a dependency on any other methods. We should probably just remove it, unless it becomes relevant later on.

glennfu commented 6 years ago

Is there something structurally different that needs to happen for it vs the regular singular /price endpoint where you pass params to it?