Packages

Type-safe Gleam client library for the Upbit cryptocurrency exchange API

Current section

Files

Jump to
glupbit src glupbit@exchange@order.erl
Raw

src/glupbit@exchange@order.erl

-module(glupbit@exchange@order).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glupbit/exchange/order.gleam").
-export([order_response_decoder/0, create_order/2, test_order/2, order_detail_decoder/0, get_order/2, get_order_by_identifier/2, list_open_orders/3, list_closed_orders/6, list_orders_by_uuids/2, cancel_order/2, cancel_order_by_identifier/2, cancel_orders_by_uuids/2, batch_cancel_orders/3, get_order_chance/2]).
-export_type([new_order/0, order_response/0, order_detail/0, trade_execution/0, order_chance/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(" Order management — create, query, cancel orders.\n").
-type new_order() :: {new_order,
glupbit@types:market(),
glupbit@types:order_side(),
glupbit@types:order_type(),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(glupbit@types:time_in_force()),
gleam@option:option(glupbit@types:smp_type())}.
-type order_response() :: {order_response,
binary(),
binary(),
binary(),
binary(),
gleam@option:option(binary()),
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
binary(),
binary(),
binary(),
binary(),
binary(),
integer(),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type order_detail() :: {order_detail,
binary(),
binary(),
binary(),
binary(),
gleam@option:option(binary()),
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
binary(),
integer(),
gleam@option:option(list(trade_execution()))}.
-type trade_execution() :: {trade_execution,
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary()}.
-type order_chance() :: {order_chance,
binary(),
binary(),
binary(),
binary(),
binary(),
binary(),
binary()}.
-file("src/glupbit/exchange/order.gleam", 289).
-spec encode_order(new_order()) -> {gleam@json:json(),
list({binary(), binary()})}.
encode_order(Order) ->
Market_str = glupbit@types:market_to_string(erlang:element(2, Order)),
Side_str = glupbit@types:order_side_to_string(erlang:element(3, Order)),
Ord_type_str = glupbit@types:order_type_to_string(erlang:element(4, Order)),
Pairs = begin
_pipe@5 = [{some, {<<"market"/utf8>>, Market_str}},
{some, {<<"side"/utf8>>, Side_str}},
{some, {<<"ord_type"/utf8>>, Ord_type_str}},
begin
_pipe = erlang:element(5, Order),
gleam@option:map(_pipe, fun(V) -> {<<"volume"/utf8>>, V} end)
end,
begin
_pipe@1 = erlang:element(6, Order),
gleam@option:map(
_pipe@1,
fun(V@1) -> {<<"price"/utf8>>, V@1} end
)
end,
begin
_pipe@2 = erlang:element(7, Order),
gleam@option:map(
_pipe@2,
fun(V@2) -> {<<"identifier"/utf8>>, V@2} end
)
end,
begin
_pipe@3 = erlang:element(8, Order),
gleam@option:map(
_pipe@3,
fun(T) ->
{<<"time_in_force"/utf8>>,
glupbit@types:time_in_force_to_string(T)}
end
)
end,
begin
_pipe@4 = erlang:element(9, Order),
gleam@option:map(
_pipe@4,
fun(S) ->
{<<"smp_type"/utf8>>,
glupbit@types:smp_type_to_string(S)}
end
)
end],
gleam@option:values(_pipe@5)
end,
Body = begin
_pipe@6 = Pairs,
_pipe@7 = gleam@list:map(
_pipe@6,
fun(P) ->
{erlang:element(1, P), gleam@json:string(erlang:element(2, P))}
end
),
gleam@json:object(_pipe@7)
end,
{Body, Pairs}.
-file("src/glupbit/exchange/order.gleam", 318).
-spec optional_string() -> gleam@dynamic@decode:decoder(gleam@option:option(binary())).
optional_string() ->
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
).
-file("src/glupbit/exchange/order.gleam", 323).
?DOC(" Decoder for OrderResponse.\n").
-spec order_response_decoder() -> gleam@dynamic@decode:decoder(order_response()).
order_response_decoder() ->
gleam@dynamic@decode:field(
<<"market"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Market) ->
gleam@dynamic@decode:field(
<<"uuid"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Uuid) ->
gleam@dynamic@decode:field(
<<"side"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Side) ->
gleam@dynamic@decode:field(
<<"ord_type"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Ord_type) ->
gleam@dynamic@decode:optional_field(
<<"price"/utf8>>,
none,
optional_string(),
fun(Price) ->
gleam@dynamic@decode:field(
<<"state"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(State) ->
gleam@dynamic@decode:field(
<<"created_at"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Created_at) ->
gleam@dynamic@decode:optional_field(
<<"volume"/utf8>>,
none,
optional_string(
),
fun(Volume) ->
gleam@dynamic@decode:optional_field(
<<"remaining_volume"/utf8>>,
none,
optional_string(
),
fun(
Remaining_volume
) ->
gleam@dynamic@decode:field(
<<"executed_volume"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(
Executed_volume
) ->
gleam@dynamic@decode:field(
<<"reserved_fee"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(
Reserved_fee
) ->
gleam@dynamic@decode:field(
<<"remaining_fee"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(
Remaining_fee
) ->
gleam@dynamic@decode:field(
<<"paid_fee"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(
Paid_fee
) ->
gleam@dynamic@decode:field(
<<"locked"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(
Locked
) ->
gleam@dynamic@decode:field(
<<"trades_count"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(
Trades_count
) ->
gleam@dynamic@decode:optional_field(
<<"time_in_force"/utf8>>,
none,
optional_string(
),
fun(
Time_in_force
) ->
gleam@dynamic@decode:optional_field(
<<"identifier"/utf8>>,
none,
optional_string(
),
fun(
Identifier
) ->
gleam@dynamic@decode:optional_field(
<<"smp_type"/utf8>>,
none,
optional_string(
),
fun(
Smp_type
) ->
gleam@dynamic@decode:success(
{order_response,
Market,
Uuid,
Side,
Ord_type,
Price,
State,
Created_at,
Volume,
Remaining_volume,
Executed_volume,
Reserved_fee,
Remaining_fee,
Paid_fee,
Locked,
Trades_count,
Time_in_force,
Identifier,
Smp_type}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glupbit/exchange/order.gleam", 101).
?DOC(" Create a new order. `POST /orders`\n").
-spec create_order(glupbit@client:auth_client(), new_order()) -> {ok,
glupbit@types:api_response(order_response())} |
{error, glupbit@types:api_error()}.
create_order(C, Order) ->
{Body, Params} = encode_order(Order),
glupbit@client:auth_post(
C,
<<"/orders"/utf8>>,
Body,
Params,
order_response_decoder()
).
-file("src/glupbit/exchange/order.gleam", 116).
?DOC(" Create a test order (no execution). `POST /orders/test`\n").
-spec test_order(glupbit@client:auth_client(), new_order()) -> {ok,
glupbit@types:api_response(order_response())} |
{error, glupbit@types:api_error()}.
test_order(C, Order) ->
{Body, Params} = encode_order(Order),
glupbit@client:auth_post(
C,
<<"/orders/test"/utf8>>,
Body,
Params,
order_response_decoder()
).
-file("src/glupbit/exchange/order.gleam", 410).
-spec trade_execution_decoder() -> gleam@dynamic@decode:decoder(trade_execution()).
trade_execution_decoder() ->
gleam@dynamic@decode:field(
<<"market"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Market) ->
gleam@dynamic@decode:field(
<<"uuid"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Uuid) ->
gleam@dynamic@decode:field(
<<"price"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Price) ->
gleam@dynamic@decode:field(
<<"volume"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Volume) ->
gleam@dynamic@decode:field(
<<"funds"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Funds) ->
gleam@dynamic@decode:field(
<<"created_at"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Created_at) ->
gleam@dynamic@decode:field(
<<"side"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Side) ->
gleam@dynamic@decode:success(
{trade_execution,
Market,
Uuid,
Price,
Volume,
Funds,
Created_at,
Side}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glupbit/exchange/order.gleam", 373).
?DOC(" Decoder for OrderDetail.\n").
-spec order_detail_decoder() -> gleam@dynamic@decode:decoder(order_detail()).
order_detail_decoder() ->
gleam@dynamic@decode:field(
<<"market"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Market) ->
gleam@dynamic@decode:field(
<<"uuid"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Uuid) ->
gleam@dynamic@decode:field(
<<"side"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Side) ->
gleam@dynamic@decode:field(
<<"ord_type"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Ord_type) ->
gleam@dynamic@decode:optional_field(
<<"price"/utf8>>,
none,
optional_string(),
fun(Price) ->
gleam@dynamic@decode:field(
<<"state"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(State) ->
gleam@dynamic@decode:field(
<<"created_at"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Created_at) ->
gleam@dynamic@decode:optional_field(
<<"volume"/utf8>>,
none,
optional_string(
),
fun(Volume) ->
gleam@dynamic@decode:optional_field(
<<"remaining_volume"/utf8>>,
none,
optional_string(
),
fun(
Remaining_volume
) ->
gleam@dynamic@decode:field(
<<"executed_volume"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(
Executed_volume
) ->
gleam@dynamic@decode:field(
<<"trades_count"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(
Trades_count
) ->
gleam@dynamic@decode:optional_field(
<<"trades"/utf8>>,
none,
gleam@dynamic@decode:optional(
gleam@dynamic@decode:list(
trade_execution_decoder(
)
)
),
fun(
Trades
) ->
gleam@dynamic@decode:success(
{order_detail,
Market,
Uuid,
Side,
Ord_type,
Price,
State,
Created_at,
Volume,
Remaining_volume,
Executed_volume,
Trades_count,
Trades}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glupbit/exchange/order.gleam", 131).
?DOC(" Get a single order by UUID. `GET /order`\n").
-spec get_order(glupbit@client:auth_client(), binary()) -> {ok,
glupbit@types:api_response(order_detail())} |
{error, glupbit@types:api_error()}.
get_order(C, Uuid) ->
glupbit@client:auth_get(
C,
<<"/order"/utf8>>,
[{<<"uuid"/utf8>>, Uuid}],
order_detail_decoder()
).
-file("src/glupbit/exchange/order.gleam", 144).
?DOC(" Get a single order by client identifier. `GET /order`\n").
-spec get_order_by_identifier(glupbit@client:auth_client(), binary()) -> {ok,
glupbit@types:api_response(order_detail())} |
{error, glupbit@types:api_error()}.
get_order_by_identifier(C, Id) ->
glupbit@client:auth_get(
C,
<<"/order"/utf8>>,
[{<<"identifier"/utf8>>, Id}],
order_detail_decoder()
).
-file("src/glupbit/exchange/order.gleam", 157).
?DOC(" List open orders. `GET /orders/open`\n").
-spec list_open_orders(
glupbit@client:auth_client(),
gleam@option:option(glupbit@types:market()),
gleam@option:option(binary())
) -> {ok, glupbit@types:api_response(list(order_detail()))} |
{error, glupbit@types:api_error()}.
list_open_orders(C, Market, State) ->
Query = begin
_pipe@2 = [begin
_pipe = Market,
gleam@option:map(
_pipe,
fun(M) ->
{<<"market"/utf8>>, glupbit@types:market_to_string(M)}
end
)
end,
begin
_pipe@1 = State,
gleam@option:map(_pipe@1, fun(S) -> {<<"state"/utf8>>, S} end)
end],
gleam@option:values(_pipe@2)
end,
glupbit@client:auth_get(
C,
<<"/orders/open"/utf8>>,
Query,
gleam@dynamic@decode:list(order_detail_decoder())
).
-file("src/glupbit/exchange/order.gleam", 177).
?DOC(" List closed orders. `GET /orders/closed`\n").
-spec list_closed_orders(
glupbit@client:auth_client(),
gleam@option:option(glupbit@types:market()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(integer())
) -> {ok, glupbit@types:api_response(list(order_detail()))} |
{error, glupbit@types:api_error()}.
list_closed_orders(C, Market, State, Start_time, End_time, Limit) ->
Query = begin
_pipe@5 = [begin
_pipe = Market,
gleam@option:map(
_pipe,
fun(M) ->
{<<"market"/utf8>>, glupbit@types:market_to_string(M)}
end
)
end,
begin
_pipe@1 = State,
gleam@option:map(_pipe@1, fun(S) -> {<<"state"/utf8>>, S} end)
end,
begin
_pipe@2 = Start_time,
gleam@option:map(
_pipe@2,
fun(T) -> {<<"start_time"/utf8>>, T} end
)
end,
begin
_pipe@3 = End_time,
gleam@option:map(
_pipe@3,
fun(T@1) -> {<<"end_time"/utf8>>, T@1} end
)
end,
begin
_pipe@4 = Limit,
gleam@option:map(
_pipe@4,
fun(N) ->
{<<"limit"/utf8>>, erlang:integer_to_binary(N)}
end
)
end],
gleam@option:values(_pipe@5)
end,
glupbit@client:auth_get(
C,
<<"/orders/closed"/utf8>>,
Query,
gleam@dynamic@decode:list(order_detail_decoder())
).
-file("src/glupbit/exchange/order.gleam", 203).
?DOC(" List orders by UUIDs. `GET /orders/uuids`\n").
-spec list_orders_by_uuids(glupbit@client:auth_client(), list(binary())) -> {ok,
glupbit@types:api_response(list(order_detail()))} |
{error, glupbit@types:api_error()}.
list_orders_by_uuids(C, Uuids) ->
glupbit@client:auth_get(
C,
<<"/orders/uuids"/utf8>>,
glupbit@client:build_array_query(<<"uuids"/utf8>>, Uuids),
gleam@dynamic@decode:list(order_detail_decoder())
).
-file("src/glupbit/exchange/order.gleam", 216).
?DOC(" Cancel a single order by UUID. `DELETE /order`\n").
-spec cancel_order(glupbit@client:auth_client(), binary()) -> {ok,
glupbit@types:api_response(order_detail())} |
{error, glupbit@types:api_error()}.
cancel_order(C, Uuid) ->
glupbit@client:auth_delete(
C,
<<"/order"/utf8>>,
[{<<"uuid"/utf8>>, Uuid}],
order_detail_decoder()
).
-file("src/glupbit/exchange/order.gleam", 229).
?DOC(" Cancel a single order by identifier. `DELETE /order`\n").
-spec cancel_order_by_identifier(glupbit@client:auth_client(), binary()) -> {ok,
glupbit@types:api_response(order_detail())} |
{error, glupbit@types:api_error()}.
cancel_order_by_identifier(C, Id) ->
glupbit@client:auth_delete(
C,
<<"/order"/utf8>>,
[{<<"identifier"/utf8>>, Id}],
order_detail_decoder()
).
-file("src/glupbit/exchange/order.gleam", 242).
?DOC(" Cancel multiple orders by UUIDs. `DELETE /orders/uuids`\n").
-spec cancel_orders_by_uuids(glupbit@client:auth_client(), list(binary())) -> {ok,
glupbit@types:api_response(list(order_detail()))} |
{error, glupbit@types:api_error()}.
cancel_orders_by_uuids(C, Uuids) ->
glupbit@client:auth_delete(
C,
<<"/orders/uuids"/utf8>>,
glupbit@client:build_array_query(<<"uuids"/utf8>>, Uuids),
gleam@dynamic@decode:list(order_detail_decoder())
).
-file("src/glupbit/exchange/order.gleam", 255).
?DOC(" Batch-cancel open orders. `DELETE /orders/open`\n").
-spec batch_cancel_orders(
glupbit@client:auth_client(),
gleam@option:option(glupbit@types:market()),
gleam@option:option(glupbit@types:order_side())
) -> {ok, glupbit@types:api_response(list(order_detail()))} |
{error, glupbit@types:api_error()}.
batch_cancel_orders(C, Market, Side) ->
Query = begin
_pipe@2 = [begin
_pipe = Market,
gleam@option:map(
_pipe,
fun(M) ->
{<<"market"/utf8>>, glupbit@types:market_to_string(M)}
end
)
end,
begin
_pipe@1 = Side,
gleam@option:map(
_pipe@1,
fun(S) ->
{<<"side"/utf8>>, glupbit@types:order_side_to_string(S)}
end
)
end],
gleam@option:values(_pipe@2)
end,
glupbit@client:auth_delete(
C,
<<"/orders/open"/utf8>>,
Query,
gleam@dynamic@decode:list(order_detail_decoder())
).
-file("src/glupbit/exchange/order.gleam", 429).
-spec order_chance_decoder() -> gleam@dynamic@decode:decoder(order_chance()).
order_chance_decoder() ->
gleam@dynamic@decode:field(
<<"bid_fee"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Bid_fee) ->
gleam@dynamic@decode:field(
<<"ask_fee"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Ask_fee) ->
gleam@dynamic@decode:subfield(
[<<"market"/utf8>>, <<"id"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Market_id) ->
gleam@dynamic@decode:subfield(
[<<"bid_account"/utf8>>, <<"currency"/utf8>>],
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Bid_account_currency) ->
gleam@dynamic@decode:subfield(
[<<"bid_account"/utf8>>,
<<"balance"/utf8>>],
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Bid_account_balance) ->
gleam@dynamic@decode:subfield(
[<<"ask_account"/utf8>>,
<<"currency"/utf8>>],
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Ask_account_currency) ->
gleam@dynamic@decode:subfield(
[<<"ask_account"/utf8>>,
<<"balance"/utf8>>],
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Ask_account_balance) ->
gleam@dynamic@decode:success(
{order_chance,
Bid_fee,
Ask_fee,
Market_id,
Bid_account_currency,
Bid_account_balance,
Ask_account_currency,
Ask_account_balance}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glupbit/exchange/order.gleam", 275).
?DOC(" Get order constraints for a market. `GET /orders/chance`\n").
-spec get_order_chance(glupbit@client:auth_client(), glupbit@types:market()) -> {ok,
glupbit@types:api_response(order_chance())} |
{error, glupbit@types:api_error()}.
get_order_chance(C, Market) ->
glupbit@client:auth_get(
C,
<<"/orders/chance"/utf8>>,
[{<<"market"/utf8>>, glupbit@types:market_to_string(Market)}],
order_chance_decoder()
).