Skip to content

Commit

Permalink
Merge pull request #17 from fremantle-capital/struct-insufficient-bal…
Browse files Browse the repository at this point in the history
…ance-error

Return struct for insufficient balance error
  • Loading branch information
dvcrn authored Jul 9, 2018
2 parents 189f785 + eff7703 commit 40f0424
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
{
"request": {
"body": "price=0.001&quantity=10000&recvWindow=1000&side=BUY&symbol=LTCBTC&timeInForce=FOK&timestamp=1528091845716&type=LIMIT&signature=***",
"headers": {
"X-MBX-APIKEY": "***"
},
"method": "post",
"options": [],
"request_body": "",
"url": "https://api.binance.com/api/v3/order"
},
"response": {
"binary": false,
"body": "{\"code\":-2010,\"msg\":\"Account has insufficient balance for requested action.\"}",
"headers": {
"Content-Type": "application/json",
"Transfer-Encoding": "chunked",
"Connection": "keep-alive",
"Date": "Mon, 04 Jun 2018 05:57:25 GMT",
"Server": "nginx",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains",
"X-Frame-Options": "SAMEORIGIN",
"X-Xss-Protection": "1; mode=block",
"X-Content-Type-Options": "nosniff",
"Content-Security-Policy": "default-src 'self'",
"X-Content-Security-Policy": "default-src 'self'",
"X-WebKit-CSP": "default-src 'self'",
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
"X-Cache": "Error from cloudfront",
"Via": "1.1 a8a06e035420932f2808c2efee52f455.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "UF3P5hkV9dU_7OiQrz4RkgvAwZhaJy0AyTZDasjdwOyz0ax44Lhbdw=="
},
"status_code": 400,
"type": "ok"
}
}
]
10 changes: 10 additions & 0 deletions lib/binance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,16 @@ defmodule Binance do
{:ok, Binance.OrderResponse.new(response)}
end

defp parse_order_response({
:error,
{
:binance_error,
%{code: -2010, msg: "Account has insufficient balance for requested action."} = reason
}
}) do
{:error, %Binance.InsufficientBalanceError{reason: reason}}
end

# Misc

@doc """
Expand Down
4 changes: 4 additions & 0 deletions lib/binance/insufficient_balance_error.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Binance.InsufficientBalanceError do
@enforce_keys [:reason]
defstruct [:reason]
end
13 changes: 13 additions & 0 deletions test/binance_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ defmodule BinanceTest do
assert response.type == "LIMIT"
end
end

test "returns an insufficient balance error tuple" do
use_cassette "order_limit_buy_error_insufficient_balance" do
assert {:error, reason} = Binance.order_limit_buy("LTCBTC", 10_000, 0.001, "FOK")

assert reason == %Binance.InsufficientBalanceError{
reason: %{
code: -2010,
msg: "Account has insufficient balance for requested action."
}
}
end
end
end

describe ".order_limit_sell" do
Expand Down

0 comments on commit 40f0424

Please sign in to comment.