|
| 1 | +import { Polymarket } from "../pmxt/client"; |
| 2 | +import { LOCAL_URL } from "../pmxt/constants"; |
| 3 | + |
| 4 | +interface CapturedFetch { |
| 5 | + url: string; |
| 6 | + init?: RequestInit; |
| 7 | +} |
| 8 | + |
| 9 | +function installFetchSpy(handler: (req: CapturedFetch) => Response): jest.SpyInstance { |
| 10 | + const captured: CapturedFetch[] = []; |
| 11 | + const spy = jest.spyOn(global, "fetch").mockImplementation(async (input, init) => { |
| 12 | + const url = typeof input === "string" ? input : (input as URL | Request).toString(); |
| 13 | + const rec: CapturedFetch = { url, init }; |
| 14 | + captured.push(rec); |
| 15 | + return handler(rec); |
| 16 | + }); |
| 17 | + (spy as unknown as { captured: CapturedFetch[] }).captured = captured; |
| 18 | + return spy; |
| 19 | +} |
| 20 | + |
| 21 | +function captured(spy: jest.SpyInstance): CapturedFetch[] { |
| 22 | + return (spy as unknown as { captured: CapturedFetch[] }).captured; |
| 23 | +} |
| 24 | + |
| 25 | +function jsonResponse(payload: unknown, status = 200): Response { |
| 26 | + return new Response(JSON.stringify(payload), { |
| 27 | + status, |
| 28 | + headers: { "Content-Type": "application/json" }, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +function makePolymarket(): Polymarket { |
| 33 | + return new Polymarket({ autoStartServer: false, baseUrl: LOCAL_URL }); |
| 34 | +} |
| 35 | + |
| 36 | +afterEach(() => { |
| 37 | + jest.restoreAllMocks(); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("sidecar order param forwarding", () => { |
| 41 | + it("buildOrder forwards tickSize, negRisk, and onBehalfOf", async () => { |
| 42 | + const spy = installFetchSpy(() => |
| 43 | + jsonResponse({ |
| 44 | + success: true, |
| 45 | + data: { id: "built-1" }, |
| 46 | + }), |
| 47 | + ); |
| 48 | + const api = makePolymarket(); |
| 49 | + |
| 50 | + await api.buildOrder({ |
| 51 | + marketId: "market-1", |
| 52 | + outcomeId: "outcome-1", |
| 53 | + side: "buy", |
| 54 | + type: "limit", |
| 55 | + amount: 10, |
| 56 | + price: 0.45, |
| 57 | + tickSize: 0.01, |
| 58 | + negRisk: false, |
| 59 | + onBehalfOf: 123, |
| 60 | + }); |
| 61 | + |
| 62 | + const reqs = captured(spy); |
| 63 | + expect(reqs).toHaveLength(1); |
| 64 | + expect(reqs[0].url).toBe(`${LOCAL_URL}/api/polymarket/buildOrder`); |
| 65 | + const body = JSON.parse((reqs[0].init?.body as string) ?? "{}"); |
| 66 | + expect(body.args[0]).toMatchObject({ |
| 67 | + tickSize: 0.01, |
| 68 | + negRisk: false, |
| 69 | + onBehalfOf: 123, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("createOrder forwards tickSize, negRisk, and onBehalfOf", async () => { |
| 74 | + const spy = installFetchSpy(() => |
| 75 | + jsonResponse({ |
| 76 | + success: true, |
| 77 | + data: { id: "order-1", status: "open" }, |
| 78 | + }), |
| 79 | + ); |
| 80 | + const api = makePolymarket(); |
| 81 | + |
| 82 | + await api.createOrder({ |
| 83 | + marketId: "market-1", |
| 84 | + outcomeId: "outcome-1", |
| 85 | + side: "buy", |
| 86 | + type: "limit", |
| 87 | + amount: 10, |
| 88 | + price: 0.45, |
| 89 | + tickSize: 0.01, |
| 90 | + negRisk: false, |
| 91 | + onBehalfOf: 0, |
| 92 | + }); |
| 93 | + |
| 94 | + const reqs = captured(spy); |
| 95 | + expect(reqs).toHaveLength(1); |
| 96 | + expect(reqs[0].url).toBe(`${LOCAL_URL}/api/polymarket/createOrder`); |
| 97 | + const body = JSON.parse((reqs[0].init?.body as string) ?? "{}"); |
| 98 | + expect(body.args[0]).toMatchObject({ |
| 99 | + tickSize: 0.01, |
| 100 | + negRisk: false, |
| 101 | + onBehalfOf: 0, |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments