|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import * as grpc from "@grpc/grpc-js"; |
| 3 | +import ccxt, { type Exchange } from "@usherlabs/ccxt"; |
| 4 | +import type { ExecuteActionContext } from "../src/handlers/execute-action/context"; |
| 5 | +import { handleOrders } from "../src/handlers/execute-action/orders"; |
| 6 | +import { Action } from "../src/helpers/constants"; |
| 7 | +import type { PolicyConfig } from "../src/types"; |
| 8 | + |
| 9 | +type CallbackError = { code?: number; message?: string }; |
| 10 | +type CallbackResponse = { result?: string }; |
| 11 | + |
| 12 | +function createFixture(createOrderResult: unknown = { id: "order-1" }) { |
| 13 | + const createOrderCalls: unknown[][] = []; |
| 14 | + const broker = { |
| 15 | + loadMarkets: async () => {}, |
| 16 | + markets: { |
| 17 | + "USDC/USDT": { |
| 18 | + symbol: "USDC/USDT", |
| 19 | + base: "USDC", |
| 20 | + quote: "USDT", |
| 21 | + spot: true, |
| 22 | + type: "spot", |
| 23 | + }, |
| 24 | + }, |
| 25 | + createOrder: async (...args: unknown[]) => { |
| 26 | + createOrderCalls.push(args); |
| 27 | + if (createOrderResult instanceof Error) { |
| 28 | + throw createOrderResult; |
| 29 | + } |
| 30 | + return createOrderResult; |
| 31 | + }, |
| 32 | + } as unknown as Exchange; |
| 33 | + |
| 34 | + let callbackError: CallbackError | null = null; |
| 35 | + let callbackResponse: CallbackResponse | null = null; |
| 36 | + const ctx = { |
| 37 | + action: Action.CreateOrder, |
| 38 | + call: { request: { payload: {} } }, |
| 39 | + wrappedCallback: ( |
| 40 | + error: CallbackError | null, |
| 41 | + response: CallbackResponse | null, |
| 42 | + ) => { |
| 43 | + callbackError = error; |
| 44 | + callbackResponse = response; |
| 45 | + }, |
| 46 | + cex: "binance", |
| 47 | + normalizedCex: "binance", |
| 48 | + symbol: "USDC/USDT", |
| 49 | + broker, |
| 50 | + verity: { proof: "" }, |
| 51 | + policy: { |
| 52 | + order: { rule: { markets: ["*"], limits: [] } }, |
| 53 | + } as unknown as PolicyConfig, |
| 54 | + brokers: {}, |
| 55 | + } as unknown as ExecuteActionContext; |
| 56 | + |
| 57 | + return { |
| 58 | + ctx, |
| 59 | + createOrderCalls, |
| 60 | + getError: () => callbackError, |
| 61 | + getResponse: () => callbackResponse, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +function createOrderPayload( |
| 66 | + overrides: Record<string, string> = {}, |
| 67 | +): Record<string, string> { |
| 68 | + return { |
| 69 | + orderType: "limit", |
| 70 | + amount: "10", |
| 71 | + fromToken: "USDC", |
| 72 | + toToken: "USDT", |
| 73 | + price: "1", |
| 74 | + marketType: "spot", |
| 75 | + ...overrides, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +describe("passive CreateOrder", () => { |
| 80 | + test("keeps the existing ccxt request and response byte-for-byte when intent is absent", async () => { |
| 81 | + const order = { id: "ordinary-1", status: "open" }; |
| 82 | + const fixture = createFixture(order); |
| 83 | + fixture.ctx.call.request.payload = createOrderPayload({ |
| 84 | + clientOrderId: "client-1", |
| 85 | + params: JSON.stringify({ timeInForce: "IOC", strategyId: 7 }), |
| 86 | + }); |
| 87 | + |
| 88 | + await handleOrders(fixture.ctx); |
| 89 | + |
| 90 | + expect(fixture.createOrderCalls).toEqual([ |
| 91 | + [ |
| 92 | + "USDC/USDT", |
| 93 | + "limit", |
| 94 | + "sell", |
| 95 | + 10, |
| 96 | + 1, |
| 97 | + { |
| 98 | + timeInForce: "IOC", |
| 99 | + strategyId: 7, |
| 100 | + clientOrderId: "client-1", |
| 101 | + }, |
| 102 | + ], |
| 103 | + ]); |
| 104 | + expect(fixture.getError()).toBeNull(); |
| 105 | + expect(fixture.getResponse()?.result).toBe(JSON.stringify(order)); |
| 106 | + }); |
| 107 | + |
| 108 | + test("adds postOnly without clobbering params and reports accepted passive placement", async () => { |
| 109 | + const order = { id: "passive-1", status: "open" }; |
| 110 | + const fixture = createFixture(order); |
| 111 | + fixture.ctx.call.request.payload = createOrderPayload({ |
| 112 | + orderIntent: "passive_only", |
| 113 | + params: JSON.stringify({ timeInForce: "GTC", strategyId: 9 }), |
| 114 | + }); |
| 115 | + |
| 116 | + await handleOrders(fixture.ctx); |
| 117 | + |
| 118 | + expect(fixture.createOrderCalls[0]?.[5]).toEqual({ |
| 119 | + timeInForce: "GTC", |
| 120 | + strategyId: 9, |
| 121 | + postOnly: true, |
| 122 | + }); |
| 123 | + expect(JSON.parse(fixture.getResponse()?.result ?? "{}")).toEqual({ |
| 124 | + ...order, |
| 125 | + passivePlacementOutcome: "accepted_passive", |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + test("overrides a conflicting caller postOnly value to preserve passive intent", async () => { |
| 130 | + const fixture = createFixture(); |
| 131 | + fixture.ctx.call.request.payload = createOrderPayload({ |
| 132 | + orderIntent: "passive_only", |
| 133 | + params: JSON.stringify({ postOnly: 0, strategyId: 9 }), |
| 134 | + }); |
| 135 | + |
| 136 | + await handleOrders(fixture.ctx); |
| 137 | + |
| 138 | + expect(fixture.createOrderCalls[0]?.[5]).toEqual({ |
| 139 | + postOnly: true, |
| 140 | + strategyId: 9, |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + test("rejects passive market orders as invalid before calling ccxt", async () => { |
| 145 | + const fixture = createFixture(); |
| 146 | + fixture.ctx.call.request.payload = createOrderPayload({ |
| 147 | + orderType: "market", |
| 148 | + orderIntent: "passive_only", |
| 149 | + }); |
| 150 | + |
| 151 | + await handleOrders(fixture.ctx); |
| 152 | + |
| 153 | + expect(fixture.createOrderCalls).toHaveLength(0); |
| 154 | + expect(fixture.getError()).toEqual({ |
| 155 | + code: grpc.status.INVALID_ARGUMENT, |
| 156 | + message: |
| 157 | + "ValidationError: passive_only order intent requires a limit order", |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + test("rejects unknown order intents in the payload schema", async () => { |
| 162 | + const fixture = createFixture(); |
| 163 | + fixture.ctx.call.request.payload = createOrderPayload({ |
| 164 | + orderIntent: "maker_if_possible", |
| 165 | + }); |
| 166 | + |
| 167 | + await handleOrders(fixture.ctx); |
| 168 | + |
| 169 | + expect(fixture.createOrderCalls).toHaveLength(0); |
| 170 | + expect(fixture.getError()?.code).toBe(grpc.status.INVALID_ARGUMENT); |
| 171 | + expect(fixture.getError()?.message).toContain("orderIntent"); |
| 172 | + }); |
| 173 | + |
| 174 | + test("maps an immediately fillable venue rejection to would-cross", async () => { |
| 175 | + const fixture = createFixture( |
| 176 | + new ccxt.InvalidOrder("binance Order would immediately match and take."), |
| 177 | + ); |
| 178 | + fixture.ctx.call.request.payload = createOrderPayload({ |
| 179 | + orderIntent: "passive_only", |
| 180 | + }); |
| 181 | + |
| 182 | + await handleOrders(fixture.ctx); |
| 183 | + |
| 184 | + expect(fixture.getError()?.code).toBe(grpc.status.FAILED_PRECONDITION); |
| 185 | + expect(fixture.getError()?.message).toStartWith( |
| 186 | + "passive_order_would_cross:", |
| 187 | + ); |
| 188 | + }); |
| 189 | + |
| 190 | + test("maps missing ccxt post-only support to unsupported", async () => { |
| 191 | + const fixture = createFixture( |
| 192 | + new ccxt.NotSupported("binance post-only orders are not supported"), |
| 193 | + ); |
| 194 | + fixture.ctx.call.request.payload = createOrderPayload({ |
| 195 | + orderIntent: "passive_only", |
| 196 | + }); |
| 197 | + |
| 198 | + await handleOrders(fixture.ctx); |
| 199 | + |
| 200 | + expect(fixture.getError()?.code).toBe(grpc.status.UNIMPLEMENTED); |
| 201 | + expect(fixture.getError()?.message).toStartWith( |
| 202 | + "passive_order_unsupported:", |
| 203 | + ); |
| 204 | + }); |
| 205 | + |
| 206 | + test("maps any other passive venue rejection to rejected", async () => { |
| 207 | + const fixture = createFixture( |
| 208 | + new ccxt.InvalidOrder("binance passive order rejected: invalid price"), |
| 209 | + ); |
| 210 | + fixture.ctx.call.request.payload = createOrderPayload({ |
| 211 | + orderIntent: "passive_only", |
| 212 | + }); |
| 213 | + |
| 214 | + await handleOrders(fixture.ctx); |
| 215 | + |
| 216 | + expect(fixture.getError()?.code).toBe(grpc.status.FAILED_PRECONDITION); |
| 217 | + expect(fixture.getError()?.message).toStartWith("passive_order_rejected:"); |
| 218 | + }); |
| 219 | +}); |
0 commit comments