Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdks/typescript/pmxt/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,9 @@ export abstract class Exchange {
resolvedDenom = "shares";
}

if (!Number.isFinite(Number(params.amount))) {
throw new InvalidOrder("amount must be a finite positive number");
}
if (!(Number(params.amount) > 0)) {
throw new InvalidOrder("amount must be positive");
}
Expand Down
45 changes: 44 additions & 1 deletion sdks/typescript/tests/hosted-dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Polymarket } from "../pmxt/client";
import {
HOSTED_TRADING_BASE_URL,
} from "../pmxt/hosted-routing";
import { NotSupported } from "../pmxt/errors";
import { NotSupported, InvalidOrder } from "../pmxt/errors";
import { MissingWalletAddress } from "../pmxt/hosted-errors";

const PMXT_API_KEY = "test_pmxt_key_xxx";
Expand Down Expand Up @@ -396,3 +396,46 @@ describe("hosted write dispatch", () => {
expect(reqs[1].url).toContain("/v0/orders/cancel");
});
});

// --------------------------------------------------------------------------
// Hosted build-order amount validation (issue #2423)
// --------------------------------------------------------------------------

describe("hosted buildOrder amount validation", () => {
const baseParams = {
marketId: "11111111-1111-4111-8111-111111111111",
outcomeId: "22222222-2222-4222-8222-222222222222",
side: "buy",
type: "market",
denom: "usdc",
};

// The amount guard runs before any network or wallet access, so these
// reject locally. `amount` is typed `number`, so malformed inputs are cast.
function expectAmountError(amount: unknown, message: string) {
const api = makePolymarket();
return expect(
api.buildOrder({ ...baseParams, amount } as any),
).rejects.toThrow(new InvalidOrder(message));
}

it("non-numeric string amount -> 'amount must be a finite positive number'", () => {
return expectAmountError("not-a-number", "amount must be a finite positive number");
});

it("negative numeric amount -> 'amount must be positive'", () => {
return expectAmountError(-5, "amount must be positive");
});

it("zero amount -> 'amount must be positive'", () => {
return expectAmountError(0, "amount must be positive");
});

it("NaN amount -> 'amount must be a finite positive number'", () => {
return expectAmountError(NaN, "amount must be a finite positive number");
});

it("Infinity amount -> 'amount must be a finite positive number'", () => {
return expectAmountError(Infinity, "amount must be a finite positive number");
});
});