This repository was archived by the owner on May 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
fix(types): add typed responses for postOrder and market data #325
Open
Aboudjem
wants to merge
3
commits into
Polymarket:main
Choose a base branch
from
Aboudjem:fix/type-promise-any-returns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
bc5e60f
fix(types): add typed responses for postOrder and market data methods
Aboudjem 7404986
fix: reuse existing OrderResponse instead of duplicate PostOrderResponse
Aboudjem 633394a
fix(types): preserve original OrderResponse field requirements to avo…
Aboudjem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import { ClobClient } from "../../src/client.ts"; | ||
| import { Chain } from "../../src/types.ts"; | ||
| import type { | ||
| Market, | ||
| PaginationPayload, | ||
| PostOrderResponse, | ||
| SimplifiedMarket, | ||
| } from "../../src/types.ts"; | ||
|
|
||
| class StubClient extends ClobClient { | ||
| private _stub: unknown; | ||
| setStub(v: unknown) { | ||
| this._stub = v; | ||
| } | ||
| protected async get(): Promise<unknown> { | ||
| return this._stub; | ||
| } | ||
| protected async post(): Promise<unknown> { | ||
| return this._stub; | ||
| } | ||
| } | ||
|
|
||
| describe("typed return values", () => { | ||
| const client = new StubClient("http://localhost", Chain.AMOY); | ||
|
|
||
| describe("PaginationPayload<T>", () => { | ||
| it("data is typed as Market[] from getMarkets", async () => { | ||
| const payload: PaginationPayload<Market> = { | ||
| limit: 100, | ||
| count: 1, | ||
| next_cursor: "LTE=", | ||
| data: [ | ||
| { | ||
| condition_id: "0xabc", | ||
| question_id: "0xdef", | ||
| question: "Will X happen?", | ||
| description: "", | ||
| market_slug: "will-x-happen", | ||
| end_date_iso: "2025-01-01T00:00:00Z", | ||
| game_start_time: "", | ||
| seconds_delay: 0, | ||
| fpmm: "", | ||
| maker_base_fee: 0, | ||
| taker_base_fee: 0, | ||
| notifications_enabled: false, | ||
| neg_risk: false, | ||
| neg_risk_market_id: "", | ||
| neg_risk_request_id: "", | ||
| icon: "", | ||
| image: "", | ||
| tokens: [], | ||
| tags: [], | ||
| is_50_50_outcome: false, | ||
| accepting_orders: true, | ||
| accepting_order_timestamp: null, | ||
| minimum_order_size: "5", | ||
| minimum_tick_size: "0.01", | ||
| active: true, | ||
| closed: false, | ||
| archived: false, | ||
| enable_order_book: true, | ||
| rewards: { | ||
| min_size: 0, | ||
| max_spread: 0, | ||
| event_start_date: "", | ||
| event_end_date: "", | ||
| in_game_multiplier: 1, | ||
| reward_epoch: 0, | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| client.setStub(payload); | ||
| const result = await client.getMarkets(); | ||
| expect(result.data[0].condition_id).to.equal("0xabc"); | ||
| expect(result.data[0].accepting_orders).to.equal(true); | ||
| }); | ||
|
|
||
| it("data is typed as SimplifiedMarket[] from getSimplifiedMarkets", async () => { | ||
| const payload: PaginationPayload<SimplifiedMarket> = { | ||
| limit: 100, | ||
| count: 1, | ||
| next_cursor: "LTE=", | ||
| data: [ | ||
| { | ||
| condition_id: "0xabc", | ||
| tokens: [{ token_id: "1", outcome: "Yes", price: 0.5 }], | ||
| rewards: { min_size: 5, max_spread: 0.02 }, | ||
| min_incentive_size: "5", | ||
| max_incentive_spread: "0.02", | ||
| accepting_orders: true, | ||
| enable_order_book: true, | ||
| }, | ||
| ], | ||
| }; | ||
| client.setStub(payload); | ||
| const result = await client.getSimplifiedMarkets(); | ||
| expect(result.data[0].condition_id).to.equal("0xabc"); | ||
| expect(result.data[0].tokens[0].outcome).to.equal("Yes"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("PostOrderResponse", () => { | ||
| it("postOrder returns typed PostOrderResponse", async () => { | ||
| const response: PostOrderResponse = { | ||
| orderID: "0x123", | ||
| status: "matched", | ||
| transactTime: "1234567890", | ||
| owner: "0xabc", | ||
| }; | ||
| client.setStub(response); | ||
| // postOrder requires L2 auth; test the type shape directly | ||
| const typed: PostOrderResponse = response; | ||
| expect(typed.orderID).to.equal("0x123"); | ||
| expect(typed.status).to.equal("matched"); | ||
| expect(typed.transactTime).to.equal("1234567890"); | ||
| expect(typed.owner).to.equal("0xabc"); | ||
| }); | ||
|
|
||
| it("PostOrderResponse optional fields are optional", () => { | ||
| const minimal: PostOrderResponse = { | ||
| orderID: "0x456", | ||
| status: "live", | ||
| transactTime: "999", | ||
| owner: "0xdef", | ||
| }; | ||
| expect(minimal.errorMsg).to.be.undefined; | ||
| expect(minimal.takingAmount).to.be.undefined; | ||
| expect(minimal.makingAmount).to.be.undefined; | ||
| expect(minimal.transactionsHashes).to.be.undefined; | ||
| }); | ||
| }); | ||
|
|
||
| describe("getMarket", () => { | ||
| it("returns a Market (not any)", async () => { | ||
| const market: Market = { | ||
| condition_id: "0xfeed", | ||
| question_id: "0xbeef", | ||
| question: "Test?", | ||
| description: "", | ||
| market_slug: "test", | ||
| end_date_iso: "", | ||
| game_start_time: "", | ||
| seconds_delay: 0, | ||
| fpmm: "", | ||
| maker_base_fee: 0, | ||
| taker_base_fee: 0, | ||
| notifications_enabled: false, | ||
| neg_risk: false, | ||
| neg_risk_market_id: "", | ||
| neg_risk_request_id: "", | ||
| icon: "", | ||
| image: "", | ||
| tokens: [], | ||
| tags: [], | ||
| is_50_50_outcome: false, | ||
| accepting_orders: false, | ||
| accepting_order_timestamp: null, | ||
| minimum_order_size: "5", | ||
| minimum_tick_size: "0.01", | ||
| active: false, | ||
| closed: true, | ||
| archived: false, | ||
| enable_order_book: false, | ||
| rewards: { | ||
| min_size: 0, | ||
| max_spread: 0, | ||
| event_start_date: "", | ||
| event_end_date: "", | ||
| in_game_multiplier: 1, | ||
| reward_epoch: 0, | ||
| }, | ||
| }; | ||
| client.setStub(market); | ||
| const result = await client.getMarket("0xfeed"); | ||
| expect(result.condition_id).to.equal("0xfeed"); | ||
| expect(result.closed).to.equal(true); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PaginationPayloaddefault breaks consumers using bare typeMedium Severity
The default type parameter for
PaginationPayloadchanged from an effectiveany(viadata: any[]) toT = unknown, making barePaginationPayloadresolvedatatounknown[]instead ofany[]. Since this is a publicly exported type from an npm package, any downstream consumer referencingPaginationPayloadwithout a type argument will now get compilation errors when accessing properties ondataitems. UsingT = anyas the default would preserve backward compatibility while still enabling generic usage.