diff --git a/fixture/vcr_cassettes/order_limit_buy_error_insufficient_balance.json b/fixture/vcr_cassettes/order_limit_buy_error_insufficient_balance.json new file mode 100644 index 0000000..336cbc2 --- /dev/null +++ b/fixture/vcr_cassettes/order_limit_buy_error_insufficient_balance.json @@ -0,0 +1,40 @@ +[ + { + "request": { + "body": "price=0.001&quantity=10000&recvWindow=1000&side=BUY&symbol=LTCBTC&timeInForce=FOK×tamp=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" + } + } +] \ No newline at end of file diff --git a/lib/binance.ex b/lib/binance.ex index 0a02726..306d931 100644 --- a/lib/binance.ex +++ b/lib/binance.ex @@ -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 """ diff --git a/lib/binance/insufficient_balance_error.ex b/lib/binance/insufficient_balance_error.ex new file mode 100644 index 0000000..d7d26a1 --- /dev/null +++ b/lib/binance/insufficient_balance_error.ex @@ -0,0 +1,4 @@ +defmodule Binance.InsufficientBalanceError do + @enforce_keys [:reason] + defstruct [:reason] +end diff --git a/test/binance_test.exs b/test/binance_test.exs index b95c5a0..70ccbd5 100644 --- a/test/binance_test.exs +++ b/test/binance_test.exs @@ -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