diff --git a/package.json b/package.json index 7623959..8c71e9d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@polymarket/clob-client", "description": "Typescript client for Polymarket's CLOB", - "version": "4.8.1", + "version": "4.9.1", "contributors": [ { "name": "Jonathan Amenechi", diff --git a/src/client.ts b/src/client.ts index e07cc51..913392a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -128,6 +128,9 @@ export class ClobClient { readonly geoBlockToken?: string; + readonly useServerTime?: boolean; + + // eslint-disable-next-line max-params constructor( host: string, chainId: Chain, @@ -136,6 +139,7 @@ export class ClobClient { signatureType?: SignatureType, funderAddress?: string, geoBlockToken?: string, + useServerTime?: boolean, ) { this.host = host.endsWith("/") ? host.slice(0, -1) : host; this.chainId = chainId; @@ -155,6 +159,7 @@ export class ClobClient { this.tickSizes = {}; this.negRisk = {}; this.geoBlockToken = geoBlockToken; + this.useServerTime = useServerTime; } // Public endpoints @@ -162,7 +167,7 @@ export class ClobClient { return this.get(`${this.host}/`); } - public async getServerTime(): Promise { + public async getServerTime(): Promise { return this.get(`${this.host}${TIME}`); } @@ -312,6 +317,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.chainId, nonce, + this.useServerTime ? await this.getServerTime() : undefined, ); return await this.post(endpoint, { headers }).then((apiKeyRaw: ApiKeyRaw) => { @@ -337,6 +343,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.chainId, nonce, + this.useServerTime ? await this.getServerTime() : undefined, ); return await this.get(endpoint, { headers }).then((apiKeyRaw: ApiKeyRaw) => { @@ -371,6 +378,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.get(`${this.host}${endpoint}`, { headers }); @@ -389,6 +397,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.del(`${this.host}${endpoint}`, { headers }); @@ -407,6 +416,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.get(`${this.host}${endpoint}`, { headers }); @@ -425,6 +435,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); let results: Trade[] = []; @@ -457,6 +468,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.get(`${this.host}${endpoint}`, { @@ -478,6 +490,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, l2HeaderArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.del(`${this.host}${endpoint}`, { @@ -501,6 +514,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); const _params = { @@ -524,6 +538,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); const _params = { @@ -552,7 +567,7 @@ export class ClobClient { ); } - const negRisk = options?.negRisk ?? await this.getNegRisk(tokenID); + const negRisk = options?.negRisk ?? (await this.getNegRisk(tokenID)); return this.orderBuilder.buildOrder(userOrder, { tickSize, @@ -586,7 +601,7 @@ export class ClobClient { ); } - const negRisk = options?.negRisk ?? await this.getNegRisk(tokenID); + const negRisk = options?.negRisk ?? (await this.getNegRisk(tokenID)); return this.orderBuilder.buildMarketOrder(userMarketOrder, { tickSize, @@ -609,6 +624,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, l2HeaderArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); let results: OpenOrder[] = []; @@ -646,6 +662,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, l2HeaderArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.post(`${this.host}${endpoint}`, { headers, data: orderPayload }); @@ -664,6 +681,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, l2HeaderArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.del(`${this.host}${endpoint}`, { headers, data: payload }); } @@ -681,6 +699,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, l2HeaderArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.del(`${this.host}${endpoint}`, { headers, data: ordersHashes }); } @@ -697,6 +716,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, l2HeaderArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.del(`${this.host}${endpoint}`, { headers }); } @@ -714,6 +734,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, l2HeaderArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.del(`${this.host}${endpoint}`, { headers, data: payload }); } @@ -731,6 +752,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.get(`${this.host}${endpoint}`, { headers, params }); @@ -751,6 +773,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); return this.post(`${this.host}${endpoint}`, { @@ -773,6 +796,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); let results: UserEarning[] = []; @@ -807,6 +831,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); const params = { @@ -837,6 +862,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); let results: UserRewardsEarning[] = []; @@ -873,6 +899,7 @@ export class ClobClient { this.signer as Wallet | JsonRpcSigner, this.creds as ApiKeyCreds, headerArgs, + this.useServerTime ? await this.getServerTime() : undefined, ); const _params = { diff --git a/src/headers/index.ts b/src/headers/index.ts index d7126be..29fbdb6 100644 --- a/src/headers/index.ts +++ b/src/headers/index.ts @@ -7,8 +7,12 @@ export const createL1Headers = async ( signer: Wallet | JsonRpcSigner, chainId: Chain, nonce?: number, + timestamp?: number, ): Promise => { - const ts = Math.floor(Date.now() / 1000); + let ts = Math.floor(Date.now() / 1000); + if (timestamp !== undefined) { + ts = timestamp; + } let n = 0; // Default nonce is 0 if (nonce !== undefined) { n = nonce; @@ -30,9 +34,13 @@ export const createL2Headers = async ( signer: Wallet | JsonRpcSigner, creds: ApiKeyCreds, l2HeaderArgs: L2HeaderArgs, + timestamp?: number, ): Promise => { + let ts = Math.floor(Date.now() / 1000); + if (timestamp !== undefined) { + ts = timestamp; + } const address = await signer.getAddress(); - const ts = Math.floor(Date.now() / 1000); const sig = buildPolyHmacSignature( creds.secret,