Current section
Files
Jump to
Current section
Files
lib/bingex/swap/model/order_info.ex
defmodule Bingex.Swap.Model.OrderInfo do
@moduledoc """
Represents order information for a swap contract.
"""
import Bingex.{Helpers, Convertors}
alias Bingex.Model.Order
defstruct [
:symbol,
:side,
:position_side,
:price,
:stop_price,
:working_type,
:quantity,
:type,
:order_id,
:client_order_id,
:stop_loss,
:take_profit,
:time_in_force,
:reduce_only?,
:price_rate,
:activation_price,
:close_position?,
:stop_guaranteed?
]
@type t() :: %__MODULE__{
symbol: nil | binary(),
side: nil | Order.side(),
position_side: nil | Order.position_side(),
price: nil | float(),
stop_price: nil | float(),
working_type: nil | Order.working_type(),
quantity: nil | float(),
type: nil | Order.type(),
order_id: nil | binary(),
client_order_id: nil | binary(),
stop_loss: any(),
take_profit: any(),
time_in_force: any(),
reduce_only?: nil | boolean(),
price_rate: nil | float(),
activation_price: nil | float(),
close_position?: nil | boolean(),
stop_guaranteed?: nil | boolean()
}
@spec decode(map()) :: {:ok, t()} | :error
def decode(%{} = data) do
{:ok,
%__MODULE__{
order_id: get_and_transform(data, "orderId", &to_non_empty_binary/1),
client_order_id: get_and_transform(data, "clientOrderID", &to_non_empty_binary/1),
type: get_and_transform(data, "type", &decode_order_type/1),
symbol: get_and_transform(data, "symbol", &to_non_empty_binary/1),
side: get_and_transform(data, "side", &decode_order_side/1),
position_side: get_and_transform(data, "positionSide", &decode_position_side/1),
price: get_and_transform(data, "price", &to_float/1),
stop_price: get_and_transform(data, "stopPrice", &to_float/1),
working_type: get_and_transform(data, "workingType", &decode_working_type/1),
quantity: get_and_transform(data, "quantity", &to_float/1),
stop_loss: Map.get(data, "stopLoss"),
take_profit: Map.get(data, "takeProfit"),
time_in_force: Map.get(data, "timeInForce"),
reduce_only?: get_and_transform(data, "reduceOnly", &to_boolean/1),
price_rate: get_and_transform(data, "priceRate", &to_float/1),
activation_price: get_and_transform(data, "activationPrice", &to_float/1),
close_position?: get_and_transform(data, "closePosition", &to_boolean/1),
stop_guaranteed?: get_and_transform(data, "stopGuaranteed", &to_boolean/1)
}}
end
def decode(_data), do: :error
end