Skip to content

Commit 3433e43

Browse files
committed
fix: deliver order_changed pushes from real server format (topic private, event/data wrapper)
1 parent 6d4ef48 commit 3433e43

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

lib/longbridge/trade_context.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,21 @@ defmodule Longbridge.TradeContext do
777777
defp dispatch_push({:push, 18, body}, callbacks, default_callback) do
778778
notif = Protox.decode!(body, Longbridge.Trade.V1.Notification)
779779

780-
if notif.topic != "" and notif.content_type == :CONTENT_JSON and notif.data != "" do
780+
if notif.topic != "" and notif.data != "" do
781781
case JSON.decode(notif.data) do
782782
{:ok, event} ->
783+
event = unwrap_order_changed(event)
784+
783785
if callback = Map.get(callbacks, notif.topic) do
784786
callback.(event)
785787
else
786-
if default_callback, do: default_callback.(event)
788+
# The server echoes the subscribed topic ("private") on pushes,
789+
# while callbacks are registered under the canonical topic path.
790+
if callback = Map.get(callbacks, @order_changed_topic) do
791+
callback.(event)
792+
else
793+
if default_callback, do: default_callback.(event)
794+
end
787795
end
788796

789797
_ ->
@@ -802,4 +810,7 @@ defmodule Longbridge.TradeContext do
802810
end
803811

804812
defp dispatch_push(_other, _callbacks, _default), do: :ok
813+
814+
defp unwrap_order_changed(%{"event" => "order_changed_lb", "data" => %{} = data}), do: data
815+
defp unwrap_order_changed(event), do: event
805816
end

test/longbridge/trade_push_test.exs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,54 @@ defmodule Longbridge.TradePushTest do
8989
Process.exit(ctx, :kill)
9090
end
9191

92+
test "delivers pushes whose topic echoes the subscription (private) with event/data wrapper" do
93+
event_json =
94+
JSON.encode!(%{
95+
"event" => "order_changed_lb",
96+
"data" => %{
97+
"order_id" => "424242",
98+
"symbol" => "700.HK",
99+
"status" => "FilledStatus",
100+
"executed_quantity" => "200",
101+
"executed_price" => "50.5"
102+
}
103+
})
104+
105+
notification = %Longbridge.Trade.V1.Notification{
106+
topic: "private",
107+
content_type: :CONTENT_JSON,
108+
dispatch_type: :DISPATCH_DIRECT,
109+
data: event_json
110+
}
111+
112+
config =
113+
Config.new(
114+
token: "test-token",
115+
http_url: "http://127.0.0.1:1"
116+
)
117+
118+
{:ok, ctx} = TradeContext.start_link(config, skip_connection: true)
119+
Process.sleep(50)
120+
121+
parent = self()
122+
123+
TradeContext.set_on_order_changed(
124+
ctx,
125+
fn event -> send(parent, {:trade_event, event}) end
126+
)
127+
128+
push_msg = {:push, 18, encode_notification(notification)}
129+
send(ctx, {:longbridge, ctx, push_msg})
130+
131+
assert_receive {:trade_event, event}, 2_000
132+
assert event["order_id"] == "424242"
133+
assert event["status"] == "FilledStatus"
134+
assert event["executed_quantity"] == "200"
135+
assert event["executed_price"] == "50.5"
136+
137+
Process.exit(ctx, :kill)
138+
end
139+
92140
test "ignores non-JSON content type" do
93141
notification = %Longbridge.Trade.V1.Notification{
94142
topic: "/v1/trade/order_changed",

0 commit comments

Comments
 (0)