Skip to content

Commit

Permalink
Merge pull request #121 from Polymarket/feat/use-server-time
Browse files Browse the repository at this point in the history
Feat: allow optional fetching of server time for l1/l2 headers
  • Loading branch information
L-Kov authored Sep 19, 2024
2 parents 324416b + 06e472e commit f7f67e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
33 changes: 30 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ export class ClobClient {

readonly geoBlockToken?: string;

readonly useServerTime?: boolean;

// eslint-disable-next-line max-params
constructor(
host: string,
chainId: Chain,
Expand All @@ -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;
Expand All @@ -155,14 +159,15 @@ export class ClobClient {
this.tickSizes = {};
this.negRisk = {};
this.geoBlockToken = geoBlockToken;
this.useServerTime = useServerTime;
}

// Public endpoints
public async getOk(): Promise<any> {
return this.get(`${this.host}/`);
}

public async getServerTime(): Promise<any> {
public async getServerTime(): Promise<number> {
return this.get(`${this.host}${TIME}`);
}

Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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 });
Expand All @@ -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 });
Expand All @@ -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 });
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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}`, {
Expand All @@ -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}`, {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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 });
Expand All @@ -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 });
}
Expand All @@ -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 });
}
Expand All @@ -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 });
}
Expand All @@ -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 });
}
Expand All @@ -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 });
Expand All @@ -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}`, {
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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 = {
Expand Down
12 changes: 10 additions & 2 deletions src/headers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ export const createL1Headers = async (
signer: Wallet | JsonRpcSigner,
chainId: Chain,
nonce?: number,
timestamp?: number,
): Promise<L1PolyHeader> => {
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;
Expand All @@ -30,9 +34,13 @@ export const createL2Headers = async (
signer: Wallet | JsonRpcSigner,
creds: ApiKeyCreds,
l2HeaderArgs: L2HeaderArgs,
timestamp?: number,
): Promise<L2PolyHeader> => {
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,
Expand Down

0 comments on commit f7f67e1

Please sign in to comment.