Current section
Files
Jump to
Current section
Files
src/glupbit@quotation@orderbook.erl
-module(glupbit@quotation@orderbook).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glupbit/quotation/orderbook.gleam").
-export([orderbook_decoder/0, get_orderbooks/4, get_supported_levels/1]).
-export_type([orderbook/0, orderbook_unit/0, orderbook_level/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" Orderbook snapshots — `GET /orderbook`, `/supported_levels`.\n").
-type orderbook() :: {orderbook,
binary(),
integer(),
float(),
float(),
list(orderbook_unit()),
gleam@option:option(float())}.
-type orderbook_unit() :: {orderbook_unit, float(), float(), float(), float()}.
-type orderbook_level() :: {orderbook_level, binary(), list(binary())}.
-file("src/glupbit/quotation/orderbook.gleam", 99).
-spec orderbook_unit_decoder() -> gleam@dynamic@decode:decoder(orderbook_unit()).
orderbook_unit_decoder() ->
gleam@dynamic@decode:field(
<<"ask_price"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Ask_price) ->
gleam@dynamic@decode:field(
<<"bid_price"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Bid_price) ->
gleam@dynamic@decode:field(
<<"ask_size"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Ask_size) ->
gleam@dynamic@decode:field(
<<"bid_size"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(Bid_size) ->
gleam@dynamic@decode:success(
{orderbook_unit,
Ask_price,
Bid_price,
Ask_size,
Bid_size}
)
end
)
end
)
end
)
end
).
-file("src/glupbit/quotation/orderbook.gleam", 75).
?DOC(" Decoder for an Orderbook JSON object.\n").
-spec orderbook_decoder() -> gleam@dynamic@decode:decoder(orderbook()).
orderbook_decoder() ->
gleam@dynamic@decode:field(
<<"market"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Market) ->
gleam@dynamic@decode:field(
<<"timestamp"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Timestamp) ->
gleam@dynamic@decode:field(
<<"total_ask_size"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Total_ask_size) ->
gleam@dynamic@decode:field(
<<"total_bid_size"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(Total_bid_size) ->
gleam@dynamic@decode:field(
<<"orderbook_units"/utf8>>,
gleam@dynamic@decode:list(
orderbook_unit_decoder()
),
fun(Orderbook_units) ->
gleam@dynamic@decode:optional_field(
<<"level"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_float/1}
),
fun(Level) ->
gleam@dynamic@decode:success(
{orderbook,
Market,
Timestamp,
Total_ask_size,
Total_bid_size,
Orderbook_units,
Level}
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glupbit/quotation/orderbook.gleam", 40).
?DOC(" Get orderbooks for trading pairs.\n").
-spec get_orderbooks(
glupbit@client:public_client(),
list(glupbit@types:market()),
gleam@option:option(binary()),
gleam@option:option(integer())
) -> {ok, glupbit@types:api_response(list(orderbook()))} |
{error, glupbit@types:api_error()}.
get_orderbooks(C, Markets, Level, Count) ->
Codes = begin
_pipe = Markets,
_pipe@1 = gleam@list:map(_pipe, fun glupbit@types:market_to_string/1),
gleam@string:join(_pipe@1, <<","/utf8>>)
end,
Query = begin
_pipe@4 = [{some, {<<"markets"/utf8>>, Codes}},
begin
_pipe@2 = Level,
gleam@option:map(_pipe@2, fun(L) -> {<<"level"/utf8>>, L} end)
end,
begin
_pipe@3 = Count,
gleam@option:map(
_pipe@3,
fun(N) ->
{<<"count"/utf8>>, erlang:integer_to_binary(N)}
end
)
end],
gleam@option:values(_pipe@4)
end,
glupbit@client:public_get(
C,
<<"/orderbook"/utf8>>,
Query,
gleam@dynamic@decode:list(orderbook_decoder())
).
-file("src/glupbit/quotation/orderbook.gleam", 107).
-spec orderbook_level_decoder() -> gleam@dynamic@decode:decoder(orderbook_level()).
orderbook_level_decoder() ->
gleam@dynamic@decode:field(
<<"market"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Market) ->
gleam@dynamic@decode:field(
<<"supported_levels"/utf8>>,
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Supported_levels) ->
gleam@dynamic@decode:success(
{orderbook_level, Market, Supported_levels}
)
end
)
end
).
-file("src/glupbit/quotation/orderbook.gleam", 63).
?DOC(" Get supported orderbook levels.\n").
-spec get_supported_levels(glupbit@client:public_client()) -> {ok,
glupbit@types:api_response(list(orderbook_level()))} |
{error, glupbit@types:api_error()}.
get_supported_levels(C) ->
glupbit@client:public_get(
C,
<<"/orderbook/supported_levels"/utf8>>,
[],
gleam@dynamic@decode:list(orderbook_level_decoder())
).