feat(): added Latency fn, removed links from examples
This commit is contained in:
@@ -17,11 +17,80 @@ export class RestClientV2 extends BaseRestClient {
|
||||
return REST_CLIENT_TYPE_ENUM.v2;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Custom SDK functions
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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();
|
||||
console.log('serverTime', serverTime);
|
||||
|
||||
const serverTimeMs = Number(serverTime.data.serverTime);
|
||||
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;
|
||||
}
|
||||
|
||||
async fetchServerTime(): Promise<number> {
|
||||
const res = await this.getServerTime();
|
||||
return Number(res.data);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Bitget misc functions
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user