Cinderella-Man / hands-on-elixir-and-otp-cryptocurrency-trading-bot-source-code

Resources related to the "Hands-on Elixir & OTP: Cryptocurrency trading bot" book
https://elixircryptobot.com
76 stars 25 forks source link

Getting an error trying to run the first actual test against Binance api #19

Closed Neophen closed 3 years ago

Neophen commented 3 years ago

Hey, i've ran in to a problem here link in book

19:38:08.647 [error] GenServer :trader terminating
** (stop) {:badarg, {'mac.c', 216}, 'Bad key'}
    (crypto 4.8.3) :crypto.mac_nif(:hmac, :sha256, nil, "price=0.94420000&quantity=10&recvWindow=1000&side=BUY&symbol=XRPUSDT&timeInForce=GTC&timestamp=1632674288644&type=LIMIT")
    (binance 1.0.1) lib/binance/rest/http_client.ex:76: Binance.Rest.HTTPClient.signed_request_binance/3
    (binance 1.0.1) lib/binance.ex:384: Binance.create_order/11
    (binance 1.0.1) lib/binance.ex:422: Binance.order_limit_buy/4
    (naive 0.1.0) lib/naive/trader.ex:48: Naive.Trader.handle_cast/2
    (stdlib 3.14) gen_server.erl:689: :gen_server.try_dispatch/4
    (stdlib 3.14) gen_server.erl:765: :gen_server.handle_msg/6
    (stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_cast", %Streamer.Binance.TradeEvent{buyer_market_maker: true, buyer_order_id: 3319802220, event_time: 1632674288500, event_type: "trade", price: "0.94420000", quantity: "27.00000000", seller_order_id: 3319802263, symbol: "XRPUSDT", trade_id: 344326919, trade_time: 1632674288500}}
** (EXIT from #PID<0.339.0>) shell process exited with reason: an exception was raised:
    ** (ErlangError) Erlang error: {:badarg, {'mac.c', 216}, 'Bad key'}
        (crypto 4.8.3) :crypto.mac_nif(:hmac, :sha256, nil, "price=0.94420000&quantity=10&recvWindow=1000&side=BUY&symbol=XRPUSDT&timeInForce=GTC&timestamp=1632674288644&type=LIMIT")
        (binance 1.0.1) lib/binance/rest/http_client.ex:76: Binance.Rest.HTTPClient.signed_request_binance/3
        (binance 1.0.1) lib/binance.ex:384: Binance.create_order/11
        (binance 1.0.1) lib/binance.ex:422: Binance.order_limit_buy/4
        (naive 0.1.0) lib/naive/trader.ex:48: Naive.Trader.handle_cast/2
        (stdlib 3.14) gen_server.erl:689: :gen_server.try_dispatch/4
        (stdlib 3.14) gen_server.erl:765: :gen_server.handle_msg/6
        (stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Cinderella-Man commented 3 years ago

Hello :wave:

Thank you for raising the issue. I looked into the Erlang docs and there's mac/4 function which looks like takes the key as a 3rd argument where for you it looks like it's nil: https://erlang.org/doc/man/crypto.html#mac-4

The binance.ex module is using the configuration to sign the request (https://github.com/dvcrn/binance.ex/blob/master/lib/binance/rest/http_client.ex#L73)

Are you sure that you added the api key and api secret to the configuration? :thinking:

Please let me know

Neophen commented 3 years ago

Ahh yeah, i named it secrets.ex rather than secrets.exs silly mistake! thank you for your fast response.