Skip to content

Commit

Permalink
useServerTime
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Kov committed Sep 18, 2024
1 parent 324416b commit d758ff8
Show file tree
Hide file tree
Showing 3 changed files with 40 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
32 changes: 29 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export class ClobClient {

readonly geoBlockToken?: string;

readonly useServerTime?: boolean;

constructor(
host: string,
chainId: Chain,
Expand All @@ -136,6 +138,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 +158,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 +316,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 +342,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 +377,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 +396,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 +415,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 +434,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 +467,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 +489,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 +513,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 +537,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 +566,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 +600,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 +623,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 +661,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 +680,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 +698,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 +715,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 +733,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 +751,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 +772,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 +795,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 +830,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 +861,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 +898,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 d758ff8

Please sign in to comment.