utils.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { HexString } from "@pythnetwork/price-service-client";
  2. export type PctNumber = number;
  3. export type DurationInSeconds = number;
  4. export const txSpeeds = ["slow", "standard", "fast"] as const;
  5. export type TxSpeed = typeof txSpeeds[number];
  6. export const customGasChainIds = [137] as const;
  7. export type CustomGasChainId = typeof customGasChainIds[number];
  8. export async function sleep(ms: number): Promise<void> {
  9. return new Promise((resolve) => setTimeout(resolve, ms));
  10. }
  11. export function removeLeading0x(id: HexString): HexString {
  12. if (id.startsWith("0x")) {
  13. return id.substring(2);
  14. }
  15. return id;
  16. }
  17. export function addLeading0x(id: HexString): HexString {
  18. if (id.startsWith("0x")) {
  19. return id;
  20. }
  21. return "0x" + id;
  22. }
  23. export function isWsEndpoint(endpoint: string): boolean {
  24. const url = new URL(endpoint);
  25. const protocol = url.protocol;
  26. if (protocol === "ws:" || protocol == "wss:") {
  27. return true;
  28. }
  29. return false;
  30. }
  31. export function verifyValidOption<
  32. options extends Readonly<Array<any>>,
  33. validOption extends options[number]
  34. >(option: any, validOptions: options) {
  35. if (validOptions.includes(option)) {
  36. return option as validOption;
  37. }
  38. const errorString =
  39. option + " is not a valid option. Please choose between " + validOptions;
  40. throw new Error(errorString);
  41. }