Skip to content

Commit

Permalink
Merge pull request #129 from Polymarket/mttwlkr/getTrades-with-cursor
Browse files Browse the repository at this point in the history
feat: add getTradesPaginated
  • Loading branch information
mttwlkr authored Dec 11, 2024
2 parents 611c1c7 + b4c9932 commit 35005a0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/getTradesPaginated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, Chain, ClobClient } from "../src";

dotenvConfig({ path: resolve(__dirname, "../.env") });

async function main() {
const wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.AMOY}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

const host = process.env.CLOB_API_URL || "http://localhost:8080";
const creds: ApiKeyCreds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
const clobClient = new ClobClient(host, chainId, wallet, creds);

// only first page
console.log(
await clobClient.getTradesPaginated({
market: "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
maker_address: await wallet.getAddress(),
})
);

// fetch only second page
console.log(
await clobClient.getTradesPaginated({
market: "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
maker_address: await wallet.getAddress(),
}, "MzAw")
);
}

main();
37 changes: 37 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,43 @@ export class ClobClient {
return results;
}


public async getTradesPaginated(
params?: TradeParams,
next_cursor?: string
): Promise<{trades: Trade[], next_cursor: string, limit: number, count: number }> {
this.canL2Auth();

const endpoint = GET_TRADES;
const headerArgs = {
method: GET,
requestPath: endpoint,
};

const headers = await createL2Headers(
this.signer as Wallet | JsonRpcSigner,
this.creds as ApiKeyCreds,
headerArgs,
this.useServerTime ? await this.getServerTime() : undefined,
);

next_cursor = next_cursor || INITIAL_CURSOR;

const _params: any = { ...params, next_cursor };

const {data, ...rest }: {
data: Trade[],
next_cursor: string,
limit: number,
count: number
} = await this.get(`${this.host}${endpoint}`, {
headers,
params: _params,
});

return { trades: Array.isArray(data) ? [...data] :[], ...rest}
}

public async getNotifications(): Promise<Notification[]> {
this.canL2Auth();

Expand Down

0 comments on commit 35005a0

Please sign in to comment.