diff --git a/lib/binance.ex b/lib/binance.ex index dbc8ea8..f88f07d 100644 --- a/lib/binance.ex +++ b/lib/binance.ex @@ -267,6 +267,28 @@ defmodule Binance do end end + @doc """ + Fetches user trades from binance + + Returns `{:ok, [%Binance.Trade{}]}` or `{:error, reason}`. + + In the case of a error on binance, for example with invalid parameters, `{:error, {:binance_error, %{code: code, msg: msg}}}` will be returned. + + Please read https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data to understand API + + """ + + def get_trades(%Binance{} = binance \\ %Binance{}, symbol) when is_binary(symbol) do + case HTTPClient.get_binance( + binance.endpoint <> "/api/v3/myTrades", + %{symbol: symbol}, + binance.secret_key, + binance.api_key + ) do + {:ok, data} -> {:ok, data |> Enum.map(fn t -> Binance.Trade.new(t) end ) } + error -> error + end + end # User data streams @doc """ diff --git a/lib/binance/trade.ex b/lib/binance/trade.ex new file mode 100644 index 0000000..6325aff --- /dev/null +++ b/lib/binance/trade.ex @@ -0,0 +1,40 @@ +defmodule Binance.Trade do + @moduledoc """ + Struct for representing a result row as returned by /api/v3/myTrades + + ``` + defstruct [ + :commission, + :commissionAsset, + :id, + :isBestMatch, + :isBuyer, + :isMaker, + :orderId, + :orderListId, + :price, + :qty, + :quoteQty, + :symbol, + :time + ] + ``` + """ + + defstruct [:commission, + :commissionAsset, + :id, + :isBestMatch, + :isBuyer, + :isMaker, + :orderId, + :orderListId, + :price, + :qty, + :quoteQty, + :symbol, + :time] + +use ExConstructor + +end