Current section
Files
Jump to
Current section
Files
lib/ccxt/ws/subscription/method_as_topic.ex
defmodule CCXT.WS.Subscription.MethodAsTopic do
@moduledoc """
Coinex/Phemex-style subscribe frame where the method name IS the channel.
%{"method" => "ticker.subscribe", "params" => [], "id" => 1}
Callers pass full method names like `["ticker.subscribe"]` in the
channel list. Only the first entry is used — callers that need multiple
subscriptions call `subscribe/2` once per channel. `unsubscribe/2`
converts `.subscribe` suffixes to `.unsubscribe`.
Config keys: `:op_field` (default `"method"`), `:args_field`
(default `"params"`).
Not wired in `CCXT.WS.Config` today (no priority-tier exchange uses it).
"""
@behaviour CCXT.WS.Subscription.Behaviour
@impl true
def subscribe(channels, config) when is_list(channels) do
method = List.first(channels) || "subscribe"
build(method, config)
end
@impl true
def unsubscribe(channels, config) when is_list(channels) do
method =
channels
|> List.first()
|> to_string()
|> String.replace(".subscribe", ".unsubscribe")
build(method, config)
end
defp build(method_value, config) do
method_field = Map.get(config, :op_field, "method")
params_field = Map.get(config, :args_field, "params")
%{
method_field => method_value,
params_field => [],
"id" => System.unique_integer([:positive, :monotonic])
}
end
end