Current section

Files

Jump to
bingex lib bingex swap models contract_info.ex
Raw

lib/bingex/swap/models/contract_info.ex

defmodule Bingex.Swap.ContractInfo do
@moduledoc """
Represents contract information for a swap contract in BingX.
This module provides structured details about a swap contract, including precision settings,
trading fees, status, and lifecycle timestamps.
"""
import Bingex.{Helpers, Convertors}
defstruct [
:contract_id,
:symbol,
:quantity_precision,
:price_precision,
:taker_fee_rate,
:maker_fee_rate,
:min_quantity,
:min_quote_amount,
:currency,
:asset,
:status,
:opening_status,
:closing_status,
:guaranteed_stop_status,
:guaranteed_stop_fee_rate,
:transactions_status,
:launch_time,
:maintain_time,
:off_time
]
@type contract_status() :: :online | :pre_online | :offline | :forbidden
@typedoc """
Contract (market) information.
Fields:
- `contract_id` (binary): Unique identifier for the contract.
- `symbol` (binary): Trading pair symbol (e.g., "BTC-USDT").
- `quantity_precision` (integer): Precision of order quantity.
- `price_precision` (integer): Precision of price values.
- `taker_fee_rate` (float): Taker fee rate as a percentage.
- `maker_fee_rate` (float): Maker fee rate as a percentage.
- `min_quantity` (float): Minimum order quantity.
- `min_quote_amount` (float): Minimum order value in quote currency.
- `currency` (binary): Settlement currency (e.g., "USDT").
- `asset` (binary): Base asset of the contract.
- `status` (`contract_status()`): Current contract status (`:online`, `:pre_online`, `:offline`, `:forbidden`).
- `opening_status` (boolean): Whether trading is open.
- `closing_status` (boolean): Whether closing of positions is allowed.
- `guaranteed_stop_status` (boolean): Whether guaranteed stop-loss is available.
- `guaranteed_stop_fee_rate` (float): Fee rate for guaranteed stop-loss.
- `transactions_status` (boolean): Whether contract transactions are enabled.
- `launch_time` (non_neg_integer): Timestamp of contract launch.
- `maintain_time` (non_neg_integer): Timestamp of next maintenance.
- `off_time` (non_neg_integer): Timestamp when contract will be taken offline.
"""
@type t() :: %__MODULE__{
contract_id: term(),
symbol: binary(),
quantity_precision: integer(),
price_precision: integer(),
taker_fee_rate: float(),
maker_fee_rate: float(),
min_quantity: float(),
min_quote_amount: float(),
currency: binary(),
asset: binary(),
status: contract_status(),
opening_status: binary(),
closing_status: binary(),
guaranteed_stop_status: boolean(),
guaranteed_stop_fee_rate: binary(),
transactions_status: boolean(),
launch_time: non_neg_integer(),
maintain_time: non_neg_integer(),
off_time: non_neg_integer()
}
@spec decode(any()) :: {:ok, t()} | :error
def decode(%{} = data) do
struct = %__MODULE__{
contract_id: get_and_transform(data, "contractId", &to_non_empty_binary/1),
symbol: get_and_transform(data, "symbol", &to_non_empty_binary/1),
quantity_precision: get_and_transform(data, "quantityPrecision", &to_integer/1),
price_precision: get_and_transform(data, "pricePrecision", &to_integer/1),
taker_fee_rate: get_and_transform(data, "takerFeeRate", &to_float/1),
maker_fee_rate: get_and_transform(data, "makerFeeRate", &to_float/1),
min_quantity: get_and_transform(data, "tradeMinQuantity", &to_float/1),
min_quote_amount: get_and_transform(data, "tradeMinUSDT", &to_float/1),
currency: get_and_transform(data, "currency", &to_non_empty_binary/1),
asset: get_and_transform(data, "asset", &to_non_empty_binary/1),
status: get_and_transform(data, "status", &decode_contract_status/1),
opening_status: get_and_transform(data, "apiStateOpen", &to_boolean/1),
closing_status: get_and_transform(data, "apiStateClose", &to_boolean/1),
guaranteed_stop_status: get_and_transform(data, "ensureTrigger", &to_boolean/1),
guaranteed_stop_fee_rate: get_and_transform(data, "triggerFeeRate", &to_float/1),
transactions_status: get_and_transform(data, "brokerState", &to_boolean/1),
launch_time: get_and_transform(data, "launchTime", &to_integer/1),
maintain_time: get_and_transform(data, "maintainTime", &to_integer/1),
off_time: get_and_transform(data, "offTime", &to_integer/1)
}
{:ok, struct}
end
def decode(_data), do: :error
defp decode_contract_status(1), do: :online
defp decode_contract_status(25), do: :forbidden
defp decode_contract_status(5), do: :pre_online
defp decode_contract_status(0), do: :offline
defp decode_contract_status(_), do: nil
end