Skip to content

Commit

Permalink
feat: Cache clock skew (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode authored Jan 21, 2021
1 parent bfd1369 commit 7e20b50
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/CoinbasePro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export class CoinbasePro {
},
};

private clockSkew: number = -1;

constructor(
auth: ClientAuthentication = {
apiKey: '',
Expand All @@ -71,9 +73,16 @@ export class CoinbasePro {
}

const signRequest = async (setup: RequestSetup): Promise<SignedRequest> => {
const time = await this.rest.time.getTime();
const clockSkew = await this.rest.time.getClockSkew(time);
return RequestSigner.signRequest(auth, setup, clockSkew);
/**
* Cache clock skew to reduce the amount of API requests:
* https://github.com/bennycode/coinbase-pro-node/issues/368#issuecomment-762122575
*/
if (this.clockSkew === -1) {
const time = await this.rest.time.getTime();
this.clockSkew = await this.rest.time.getClockSkew(time);
}

return RequestSigner.signRequest(auth, setup, this.clockSkew);
};

this.rest = new RESTClient(this.url.REST, signRequest);
Expand Down

0 comments on commit 7e20b50

Please sign in to comment.