Skip to content

Commit

Permalink
pass apikey and secret in Binance struct
Browse files Browse the repository at this point in the history
  • Loading branch information
asmodehn committed May 20, 2021
1 parent 63055e1 commit 44e92e3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 49 deletions.
5 changes: 0 additions & 5 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
50 changes: 24 additions & 26 deletions lib/binance.ex
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -254,10 +256,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
Expand Down Expand Up @@ -394,7 +398,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}}}

Expand Down Expand Up @@ -636,14 +646,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
Expand All @@ -658,14 +665,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
Expand Down Expand Up @@ -729,9 +733,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,
Expand All @@ -750,8 +751,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
Expand Down Expand Up @@ -821,9 +822,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,
Expand All @@ -848,8 +846,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
Expand Down
6 changes: 3 additions & 3 deletions lib/binance/rest/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -68,7 +68,7 @@ defmodule Binance.Rest.HTTPClient do
signature =
:crypto.hmac(
:sha256,
Application.get_env(:binance, :secret_key),
secret,
argument_string
)
|> Base.encode16()
Expand All @@ -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} ->
Expand Down
30 changes: 15 additions & 15 deletions test/binance_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,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"
Expand All @@ -286,7 +286,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"
Expand All @@ -306,7 +306,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"
Expand All @@ -326,7 +326,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"
Expand All @@ -346,7 +346,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{
Expand All @@ -366,7 +366,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"
Expand All @@ -388,7 +388,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"
Expand All @@ -408,7 +408,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"
Expand All @@ -428,7 +428,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"
Expand All @@ -450,7 +450,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
Expand Down Expand Up @@ -496,7 +496,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"
Expand All @@ -521,7 +521,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"
Expand All @@ -548,7 +548,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,
Expand Down Expand Up @@ -577,7 +577,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,
Expand Down Expand Up @@ -606,7 +606,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,
Expand Down

0 comments on commit 44e92e3

Please sign in to comment.