diff --git a/config/config.exs b/config/config.exs index 55be3ca..da04593 100644 --- a/config/config.exs +++ b/config/config.exs @@ -2,11 +2,6 @@ # and its dependencies with the aid of the Mix.Config module. use Mix.Config -config :binance, - api_key: "", - secret_key: "", - end_point: "https://api.binance.com" - config :exvcr, filter_request_headers: [ "X-MBX-APIKEY" diff --git a/lib/binance.ex b/lib/binance.ex index 2de83d6..e5a7523 100644 --- a/lib/binance.ex +++ b/lib/binance.ex @@ -1,7 +1,9 @@ defmodule Binance do alias Binance.Rest.HTTPClient - defstruct endpoint: "https://api.binance.com" + defstruct endpoint: "https://api.binance.com", + api_key: nil, + secret_key: nil # Server @@ -242,10 +244,12 @@ defmodule Binance do """ def get_account(%Binance{} = binance \\ %Binance{}) do - api_key = Application.get_env(:binance, :api_key) - secret_key = Application.get_env(:binance, :secret_key) - - case HTTPClient.get_binance(binance.endpoint <> "/api/v3/account", %{}, secret_key, api_key) do + case HTTPClient.get_binance( + binance.endpoint <> "/api/v3/account", + %{}, + binance.secret_key, + binance.api_key + ) do {:ok, data} -> {:ok, Binance.Account.new(data)} error -> error end @@ -382,7 +386,13 @@ defmodule Binance do |> Map.merge(unless(is_nil(time_in_force), do: %{timeInForce: time_in_force}, else: %{})) |> Map.merge(unless(is_nil(price), do: %{price: format_price(price)}, else: %{})) - case HTTPClient.signed_request_binance(binance.endpoint <> "/api/v3/order", arguments, :post) do + case HTTPClient.signed_request_binance( + binance.endpoint <> "/api/v3/order", + arguments, + :post, + binance.api_key, + binance.secret_key + ) do {:ok, %{"code" => code, "msg" => msg}} -> {:error, {:binance_error, %{code: code, msg: msg}}} @@ -624,14 +634,11 @@ defmodule Binance do ``` """ def get_open_orders(%Binance{} = binance \\ %Binance{}) do - api_key = Application.get_env(:binance, :api_key) - secret_key = Application.get_env(:binance, :secret_key) - case HTTPClient.get_binance( binance.endpoint <> "/api/v3/openOrders", %{}, - secret_key, - api_key + binance.secret_key, + binance.api_key ) do {:ok, data} -> {:ok, Enum.map(data, &Binance.Order.new(&1))} err -> err @@ -646,14 +653,11 @@ defmodule Binance do end def get_open_orders(%Binance{} = binance, symbol) when is_binary(symbol) do - api_key = Application.get_env(:binance, :api_key) - secret_key = Application.get_env(:binance, :secret_key) - case HTTPClient.get_binance( binance.endpoint <> "/api/v3/openOrders", %{:symbol => symbol}, - secret_key, - api_key + binance.secret_key, + binance.api_key ) do {:ok, data} -> {:ok, Enum.map(data, &Binance.Order.new(&1))} err -> err @@ -717,9 +721,6 @@ defmodule Binance do when is_binary(symbol) when is_integer(timestamp) when is_integer(order_id) or is_binary(orig_client_order_id) do - api_key = Application.get_env(:binance, :api_key) - secret_key = Application.get_env(:binance, :secret_key) - arguments = %{ symbol: symbol, @@ -738,8 +739,8 @@ defmodule Binance do case HTTPClient.get_binance( binance.endpoint <> "/api/v3/order", arguments, - secret_key, - api_key + binance.secret_key, + binance.api_key ) do {:ok, data} -> {:ok, Binance.Order.new(data)} err -> err @@ -809,9 +810,6 @@ defmodule Binance do when is_binary(symbol) when is_integer(timestamp) when is_integer(order_id) or is_binary(orig_client_order_id) do - api_key = Application.get_env(:binance, :api_key) - secret_key = Application.get_env(:binance, :secret_key) - arguments = %{ symbol: symbol, @@ -836,8 +834,8 @@ defmodule Binance do case HTTPClient.delete_binance( binance.endpoint <> "/api/v3/order", arguments, - secret_key, - api_key + binance.secret_key, + binance.api_key ) do {:ok, data} -> {:ok, Binance.Order.new(data)} err -> err diff --git a/lib/binance/rest/http_client.ex b/lib/binance/rest/http_client.ex index 6adcdc7..fab2908 100644 --- a/lib/binance/rest/http_client.ex +++ b/lib/binance/rest/http_client.ex @@ -59,7 +59,7 @@ defmodule Binance.Rest.HTTPClient do end end - def signed_request_binance(url, params, method) do + def signed_request_binance(url, params, method, apikey, secret) do argument_string = params |> prepare_query_params() @@ -68,7 +68,7 @@ defmodule Binance.Rest.HTTPClient do signature = :crypto.hmac( :sha256, - Application.get_env(:binance, :secret_key), + secret, argument_string ) |> Base.encode16() @@ -79,7 +79,7 @@ defmodule Binance.Rest.HTTPClient do url, body, [ - {"X-MBX-APIKEY", Application.get_env(:binance, :api_key)} + {"X-MBX-APIKEY", apikey} ] ]) do {:error, err} -> diff --git a/test/binance_test.exs b/test/binance_test.exs index 5880271..e8fc512 100644 --- a/test/binance_test.exs +++ b/test/binance_test.exs @@ -259,7 +259,7 @@ defmodule BinanceTest do test "creates an order with a duration of good til cancel by default" do use_cassette "order_limit_buy_good_til_cancel_default_duration_success" do assert {:ok, %Binance.OrderResponse{} = response} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_buy("LTCBTC", 0.1, 0.01) assert response.client_order_id == "9kITBshSwrClye1HJcLM3j" @@ -279,7 +279,7 @@ defmodule BinanceTest do test "creates an order with a duration of good til cancel by default(string quantity and price)" do use_cassette "order_limit_buy_good_til_cancel_default_duration_success" do assert {:ok, %Binance.OrderResponse{} = response} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_buy("LTCBTC", "0.1", "0.01") assert response.client_order_id == "9kITBshSwrClye1HJcLM3j" @@ -299,7 +299,7 @@ defmodule BinanceTest do test "can create an order with a fill or kill duration" do use_cassette "order_limit_buy_fill_or_kill_success" do assert {:ok, %Binance.OrderResponse{} = response} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_buy("LTCBTC", 0.1, 0.01, "FOK") assert response.client_order_id == "dY67P33S4IxPnJGx5EtuSf" @@ -319,7 +319,7 @@ defmodule BinanceTest do test "can create an order with am immediate or cancel duration" do use_cassette "order_limit_buy_immediate_or_cancel_success" do assert {:ok, %Binance.OrderResponse{} = response} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_buy("LTCBTC", 0.1, 0.01, "IOC") assert response.client_order_id == "zyMyhtRENlvFHrl4CitDe0" @@ -339,7 +339,7 @@ defmodule BinanceTest do test "returns an insufficient balance error tuple" do use_cassette "order_limit_buy_error_insufficient_balance" do assert {:error, reason} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_buy("LTCBTC", 10_000, 0.001, "FOK") assert reason == %Binance.InsufficientBalanceError{ @@ -359,7 +359,7 @@ defmodule BinanceTest do use_cassette "order_limit_buy_very_low_price", custom_matchers: [matches_price] do assert {:ok, %Binance.OrderResponse{} = response} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_buy("DOGEBTC", 100, 0.000001) assert response.client_order_id == "cyNmMk8rcgunB0REmUlbyv" @@ -381,7 +381,7 @@ defmodule BinanceTest do test "creates an order with a duration of good til cancel by default" do use_cassette "order_limit_sell_good_til_cancel_default_duration_success" do assert {:ok, %Binance.OrderResponse{} = response} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_sell("BTCUSDT", 0.001, 50_000) assert response.client_order_id == "9UFMPloZsQ3eshCx66PVqD" @@ -401,7 +401,7 @@ defmodule BinanceTest do test "can create an order with a fill or kill duration" do use_cassette "order_limit_sell_fill_or_kill_success" do assert {:ok, %Binance.OrderResponse{} = response} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_sell("BTCUSDT", 0.001, 50_000, "FOK") assert response.client_order_id == "lKYECwEPSTPzurwx6emuN2" @@ -421,7 +421,7 @@ defmodule BinanceTest do test "can create an order with am immediate or cancel duration" do use_cassette "order_limit_sell_immediate_or_cancel_success" do assert {:ok, %Binance.OrderResponse{} = response} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.order_limit_sell("BTCUSDT", 0.001, 50_000, "IOC") assert response.client_order_id == "roSkLhwX9KCgYqr4yFPx1V" @@ -443,7 +443,7 @@ defmodule BinanceTest do test "when called without symbol returns all open orders for all symbols" do use_cassette "get_open_orders_without_symbol_success" do assert {:ok, [%Binance.Order{} = order_1, %Binance.Order{} = order_2]} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.get_open_orders() # open order 1 @@ -489,7 +489,7 @@ defmodule BinanceTest do test "when called with symbol returns all open orders for that symbols(string)" do use_cassette "get_open_orders_with_symbol_string_success" do assert {:ok, [%Binance.Order{} = result]} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.get_open_orders("WABIBTC") assert result.client_order_id == "web_db04d8a507f14135a9a9d4467bc541a1" @@ -514,7 +514,7 @@ defmodule BinanceTest do test "when called with symbol returns all open orders for that symbols(TradePair struct)" do use_cassette "get_open_orders_with_trade_pair_struct_string_success" do assert {:ok, [%Binance.Order{} = result]} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.get_open_orders(%Binance.TradePair{:from => "WABI", :to => "BTC"}) assert result.client_order_id == "web_db04d8a507f14135a9a9d4467bc541a1" @@ -541,7 +541,7 @@ defmodule BinanceTest do test "when called with symbol(struct), orderId and timestamp cancels order" do use_cassette "cancel_order_by_struct_symbol_orderId_and_timestamp_success" do assert {:ok, %Binance.Order{} = order} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.cancel_order( %Binance.TradePair{:from => "XRP", :to => "USDT"}, 1_564_000_518_279, @@ -570,7 +570,7 @@ defmodule BinanceTest do test "when called with symbol(string), orderId and timestamp cancels order" do use_cassette "cancel_order_by_symbol_string_orderid_and_timestamp_success" do assert {:ok, %Binance.Order{} = order} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.cancel_order( "XRPUSDT", 1_564_000_518_279, @@ -599,7 +599,7 @@ defmodule BinanceTest do test "when called with symbol(string), clientOrderId and timestamp cancels order" do use_cassette "cancel_order_by_symbol_string_clientOrderId_and_timestamp_success" do assert {:ok, %Binance.Order{} = order} = - %Binance{} + %Binance{api_key: "", secret_key: ""} |> Binance.cancel_order( "XRPUSDT", 1_564_000_518_279,