Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Add allPrices endpoint, and all_prices method #26

Merged
merged 1 commit into from
Jul 4, 2018

Conversation

glennfu
Copy link
Contributor

@glennfu glennfu commented Jul 3, 2018

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 craysiii merged commit c39a1f5 into craysiii:master Jul 4, 2018
@craysiii
Copy link
Owner

craysiii commented Jul 4, 2018

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

@glennfu
Copy link
Contributor Author

glennfu commented Jul 4, 2018

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

@craysiii
Copy link
Owner

craysiii commented Jul 4, 2018

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

@craysiii
Copy link
Owner

craysiii commented Jul 4, 2018

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

@glennfu
Copy link
Contributor Author

glennfu commented Jul 4, 2018

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
Copy link
Owner

craysiii commented Jul 4, 2018

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
Copy link
Contributor Author

glennfu commented Jul 5, 2018

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants