Skip to content

Commit

Permalink
Merge pull request #4 from luizParreira/implement-balances
Browse files Browse the repository at this point in the history
Implement `get_account`
  • Loading branch information
dvcrn authored Mar 22, 2018
2 parents e330ca7 + 03cd8f0 commit 153b7c1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
59 changes: 57 additions & 2 deletions lib/binance.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Binance do
@endpoint "https://api.binance.com"

defp get_binance(url) do
case HTTPoison.get("#{@endpoint}#{url}") do
defp get_binance(url, headers \\ []) do
case HTTPoison.get("#{@endpoint}#{url}", headers) do
{:error, err} ->
{:error, {:http_error, err}}

Expand All @@ -14,6 +14,39 @@ defmodule Binance do
end
end

defp get_binance(_url, _params, nil, nil),
do: {:error, {:config_missing, "Secret and API key missing"}}

defp get_binance(_url, _params, nil, _api_key),
do: {:error, {:config_missing, "Secret key missing"}}

defp get_binance(_url, _params, _secret_key, nil),
do: {:error, {:config_missing, "API key missing"}}

defp get_binance(url, params, secret_key, api_key) do
headers = [{"X-MBX-APIKEY", api_key}]
receive_window = 5000
ts = DateTime.utc_now() |> DateTime.to_unix(:millisecond)

params =
Map.merge(params, %{
timestamp: ts,
recvWindow: receive_window
})

argument_string = URI.encode_query(params)

signature =
:crypto.hmac(
:sha256,
secret_key,
argument_string
)
|> Base.encode16()

get_binance("#{url}?#{argument_string}&signature=#{signature}", headers)
end

defp post_binance(url, params) do
argument_string =
params
Expand Down Expand Up @@ -135,6 +168,28 @@ defmodule Binance do
end
end

# Account

@doc """
Fetches user account from binance
Returns `{:ok, %Binance.Account{}}` or `{:error, reason}`.
In the case of a error on binance, for example with invalid parameters, `{:error, {:binance_error, %{code: code, msg: msg}}}` will be returned.
Please read https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data to understand API
"""

def get_account() do
api_key = Application.get_env(:binance, :api_key)
secret_key = Application.get_env(:binance, :secret_key)

case get_binance("/api/v3/account", %{}, secret_key, api_key) do
{:ok, data} -> {:ok, Binance.Account.new(data)}
error -> error
end
end

# Order

@doc """
Expand Down
33 changes: 33 additions & 0 deletions lib/binance/account.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule Binance.Account do
@moduledoc """
Struct for representing a result row as returned by /api/v3/account
```
defstruct [
:maker_commission,
:taker_commission,
:buyer_commission,
:seller_commission,
:can_trade,
:can_withdrawl,
:can_deposit,
:update_time,
:balances
]
```
"""

defstruct [
:maker_commission,
:taker_commission,
:buyer_commission,
:seller_commission,
:can_trade,
:can_withdrawl,
:can_deposit,
:update_time,
:balances
]

use ExConstructor
end

0 comments on commit 153b7c1

Please sign in to comment.