feat(): added latency check, updated example

This commit is contained in:
Jerko J
2024-10-22 11:08:01 +02:00
parent 5a4b9bd6a5
commit 60e34d01cf
4 changed files with 229 additions and 162 deletions

View File

@@ -201,6 +201,74 @@ import BaseRestClient from './util/BaseRestClient';
* https://bybit-exchange.github.io/docs/v5/intro
*/
export class RestClientV5 extends BaseRestClient {
/**
*
****** Custom SDK APIs
*
*/
/**
* This method is used to get the latency and time sync between the client and the server.
* This is not official API endpoint and is only used for internal testing purposes.
* Use this method to check the latency and time sync between the client and the server.
* Final values might vary slightly, but it should be within few ms difference.
* If you have any suggestions or improvements to this measurement, please create an issue or pull request on GitHub.
*/
async fetchLatencySummary(): Promise<any> {
const clientTimeReqStart = Date.now();
const serverTime = await this.getServerTime();
const clientTimeReqEnd = Date.now();
const serverTimeMs = serverTime.time;
const roundTripTime = clientTimeReqEnd - clientTimeReqStart;
const estimatedOneWayLatency = Math.floor(roundTripTime / 2);
// Adjust server time by adding estimated one-way latency
const adjustedServerTime = serverTimeMs + estimatedOneWayLatency;
// Calculate time difference between adjusted server time and local time
const timeDifference = adjustedServerTime - clientTimeReqEnd;
const result = {
localTime: clientTimeReqEnd,
serverTime: serverTimeMs,
roundTripTime,
estimatedOneWayLatency,
adjustedServerTime,
timeDifference,
};
console.log('Time synchronization results:');
console.log(result);
console.log(
`Your approximate latency to exchange server:
One way: ${estimatedOneWayLatency}ms.
Round trip: ${roundTripTime}ms.
`,
);
if (timeDifference > 500) {
console.warn(
`WARNING! Time difference between server and client clock is greater than 500ms. It is currently ${timeDifference}ms.
Consider adjusting your system clock to avoid unwanted clock sync errors!
Visit https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow for more information`,
);
} else {
console.log(
`Time difference between server and client clock is within acceptable range of 500ms. It is currently ${timeDifference}ms.`,
);
}
return result;
}
/**
*
****** Misc Bybit APIs
*
*/
getClientType() {
return REST_CLIENT_TYPE_ENUM.v3;
}