@across-protocol/sdk 3.1.18 → 3.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/cjs/index.d.ts +1 -0
  2. package/dist/cjs/index.js +2 -1
  3. package/dist/cjs/index.js.map +1 -1
  4. package/dist/cjs/providers/cachedProvider.d.ts +17 -0
  5. package/dist/cjs/providers/cachedProvider.js +149 -0
  6. package/dist/cjs/providers/cachedProvider.js.map +1 -0
  7. package/dist/cjs/providers/constants.d.ts +4 -0
  8. package/dist/cjs/providers/constants.js +8 -0
  9. package/dist/cjs/providers/constants.js.map +1 -0
  10. package/dist/cjs/providers/index.d.ts +5 -0
  11. package/dist/cjs/providers/index.js +9 -0
  12. package/dist/cjs/providers/index.js.map +1 -0
  13. package/dist/cjs/providers/rateLimitedProvider.d.ts +10 -0
  14. package/dist/cjs/providers/rateLimitedProvider.js +88 -0
  15. package/dist/cjs/providers/rateLimitedProvider.js.map +1 -0
  16. package/dist/cjs/providers/retryProvider.d.ts +16 -0
  17. package/dist/cjs/providers/retryProvider.js +224 -0
  18. package/dist/cjs/providers/retryProvider.js.map +1 -0
  19. package/dist/cjs/providers/utils.d.ts +21 -0
  20. package/dist/cjs/providers/utils.js +63 -0
  21. package/dist/cjs/providers/utils.js.map +1 -0
  22. package/dist/cjs/utils/NetworkUtils.d.ts +1 -0
  23. package/dist/cjs/utils/NetworkUtils.js +10 -1
  24. package/dist/cjs/utils/NetworkUtils.js.map +1 -1
  25. package/dist/cjs/utils/ObjectUtils.js.map +1 -1
  26. package/dist/esm/index.d.ts +1 -0
  27. package/dist/esm/index.js +2 -0
  28. package/dist/esm/index.js.map +1 -1
  29. package/dist/esm/providers/cachedProvider.d.ts +17 -0
  30. package/dist/esm/providers/cachedProvider.js +167 -0
  31. package/dist/esm/providers/cachedProvider.js.map +1 -0
  32. package/dist/esm/providers/constants.d.ts +7 -0
  33. package/dist/esm/providers/constants.js +10 -0
  34. package/dist/esm/providers/constants.js.map +1 -0
  35. package/dist/esm/providers/index.d.ts +5 -0
  36. package/dist/esm/providers/index.js +6 -0
  37. package/dist/esm/providers/index.js.map +1 -0
  38. package/dist/esm/providers/rateLimitedProvider.d.ts +10 -0
  39. package/dist/esm/providers/rateLimitedProvider.js +101 -0
  40. package/dist/esm/providers/rateLimitedProvider.js.map +1 -0
  41. package/dist/esm/providers/retryProvider.d.ts +16 -0
  42. package/dist/esm/providers/retryProvider.js +249 -0
  43. package/dist/esm/providers/retryProvider.js.map +1 -0
  44. package/dist/esm/providers/utils.d.ts +39 -0
  45. package/dist/esm/providers/utils.js +89 -0
  46. package/dist/esm/providers/utils.js.map +1 -0
  47. package/dist/esm/utils/NetworkUtils.d.ts +6 -0
  48. package/dist/esm/utils/NetworkUtils.js +13 -0
  49. package/dist/esm/utils/NetworkUtils.js.map +1 -1
  50. package/dist/esm/utils/ObjectUtils.js +1 -1
  51. package/dist/esm/utils/ObjectUtils.js.map +1 -1
  52. package/dist/types/index.d.ts +1 -0
  53. package/dist/types/index.d.ts.map +1 -1
  54. package/dist/types/providers/cachedProvider.d.ts +18 -0
  55. package/dist/types/providers/cachedProvider.d.ts.map +1 -0
  56. package/dist/types/providers/constants.d.ts +8 -0
  57. package/dist/types/providers/constants.d.ts.map +1 -0
  58. package/dist/types/providers/index.d.ts +6 -0
  59. package/dist/types/providers/index.d.ts.map +1 -0
  60. package/dist/types/providers/rateLimitedProvider.d.ts +11 -0
  61. package/dist/types/providers/rateLimitedProvider.d.ts.map +1 -0
  62. package/dist/types/providers/retryProvider.d.ts +17 -0
  63. package/dist/types/providers/retryProvider.d.ts.map +1 -0
  64. package/dist/types/providers/utils.d.ts +40 -0
  65. package/dist/types/providers/utils.d.ts.map +1 -0
  66. package/dist/types/utils/NetworkUtils.d.ts +6 -0
  67. package/dist/types/utils/NetworkUtils.d.ts.map +1 -1
  68. package/dist/types/utils/ObjectUtils.d.ts.map +1 -1
  69. package/package.json +5 -1
  70. package/src/index.ts +1 -0
  71. package/src/providers/cachedProvider.ts +155 -0
  72. package/src/providers/constants.ts +11 -0
  73. package/src/providers/index.ts +5 -0
  74. package/src/providers/rateLimitedProvider.ts +94 -0
  75. package/src/providers/retryProvider.ts +262 -0
  76. package/src/providers/utils.ts +119 -0
  77. package/src/utils/NetworkUtils.ts +13 -0
  78. package/src/utils/ObjectUtils.ts +1 -0
@@ -0,0 +1,119 @@
1
+ // The async/queue library has a task-based interface for building a concurrent queue.
2
+
3
+ import { providers } from "ethers";
4
+ import { isDefined } from "../utils";
5
+ import { isEqual } from "lodash";
6
+
7
+ /**
8
+ * Deletes keys from an object and returns new copy of object without ignored keys
9
+ * @param ignoredKeys
10
+ * @param obj
11
+ * @returns Objects with ignored keys removed
12
+ */
13
+ function deleteIgnoredKeys(ignoredKeys: string[], obj: Record<string, unknown>) {
14
+ if (!isDefined(obj)) {
15
+ return;
16
+ }
17
+ const newObj = { ...obj };
18
+ for (const key of ignoredKeys) {
19
+ delete newObj[key];
20
+ }
21
+ return newObj;
22
+ }
23
+
24
+ export function compareResultsAndFilterIgnoredKeys(
25
+ ignoredKeys: string[],
26
+ _objA: Record<string, unknown>,
27
+ _objB: Record<string, unknown>
28
+ ): boolean {
29
+ // Remove ignored keys from copied objects.
30
+ const filteredA = deleteIgnoredKeys(ignoredKeys, _objA);
31
+ const filteredB = deleteIgnoredKeys(ignoredKeys, _objB);
32
+
33
+ // Compare objects without the ignored keys.
34
+ return isEqual(filteredA, filteredB);
35
+ }
36
+
37
+ export function compareArrayResultsWithIgnoredKeys(ignoredKeys: string[], objA: unknown[], objB: unknown[]): boolean {
38
+ // Remove ignored keys from each element of copied arrays.
39
+ const filteredA = objA?.map((obj) => deleteIgnoredKeys(ignoredKeys, obj as Record<string, unknown>));
40
+ const filteredB = objB?.map((obj) => deleteIgnoredKeys(ignoredKeys, obj as Record<string, unknown>));
41
+
42
+ // Compare objects without the ignored keys.
43
+ return isDefined(filteredA) && isDefined(filteredB) && isEqual(filteredA, filteredB);
44
+ }
45
+
46
+ /**
47
+ * This is the type we pass to define a request "task".
48
+ */
49
+ export interface RateLimitTask {
50
+ // These are the arguments to be passed to super.send().
51
+ sendArgs: [string, Array<unknown>];
52
+
53
+ // These are the promise callbacks that will cause the initial send call made by the user to either return a result
54
+ // or fail.
55
+ resolve: (result: unknown) => void;
56
+ reject: (err: unknown) => void;
57
+ }
58
+
59
+ /**
60
+ * A helper function to format an error message for a provider.
61
+ * @param provider The provider that failed.
62
+ * @param rawErrorText The raw error text.
63
+ * @returns The formatted error message.
64
+ */
65
+ export function formatProviderError(provider: providers.StaticJsonRpcProvider, rawErrorText: string) {
66
+ return `Provider ${provider.connection.url} failed with error: ${rawErrorText}`;
67
+ }
68
+
69
+ export function createSendErrorWithMessage(message: string, sendError: Record<string, unknown>) {
70
+ const error = new Error(message);
71
+ return { ...sendError, ...error };
72
+ }
73
+
74
+ /**
75
+ * Compares two RPC results, filtering out fields that are known to differ between providers.
76
+ * Note: this function references `IGNORED_ERROR_CODES` which is a record of error codes that correspond to fields
77
+ * that should be ignored when comparing RPC results.
78
+ * @param method The method that was called - conditionally filters out fields based on the method.
79
+ * @param rpcResultA The first RPC result.
80
+ * @param rpcResultB The second RPC result.
81
+ * @returns True if the results are equal, false otherwise.
82
+ */
83
+ export function compareRpcResults(method: string, rpcResultA: unknown, rpcResultB: unknown): boolean {
84
+ if (method === "eth_getBlockByNumber") {
85
+ // We've seen RPC's disagree on the miner field, for example when Polygon nodes updated software that
86
+ // led alchemy and quicknode to disagree on the miner field's value.
87
+ return compareResultsAndFilterIgnoredKeys(
88
+ [
89
+ "miner", // polygon (sometimes)
90
+ "l1BatchNumber", // zkSync
91
+ "l1BatchTimestamp", // zkSync
92
+ "size", // Alchemy/Arbitrum (temporary)
93
+ "totalDifficulty", // Quicknode/Alchemy (sometimes)
94
+ ],
95
+ rpcResultA as Record<string, unknown>,
96
+ rpcResultB as Record<string, unknown>
97
+ );
98
+ } else if (method === "eth_getLogs") {
99
+ // We've seen some RPC's like QuickNode add in transactionLogIndex which isn't in the
100
+ // JSON RPC spec: https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getfilterchanges
101
+ // Additional reference: https://github.com/ethers-io/ethers.js/issues/1721
102
+ // 2023-08-31 Added blockHash because of upstream zkSync provider disagreements. Consider removing later.
103
+ // 2024-05-07 Added l1BatchNumber and logType due to Alchemy. Consider removing later.
104
+ // 2024-07-11 Added blockTimestamp after zkSync rolled out a new node release.
105
+ return compareArrayResultsWithIgnoredKeys(
106
+ ["blockTimestamp", "transactionLogIndex", "l1BatchNumber", "logType"],
107
+ rpcResultA as unknown[],
108
+ rpcResultB as unknown[]
109
+ );
110
+ } else {
111
+ return isEqual(rpcResultA, rpcResultB);
112
+ }
113
+ }
114
+
115
+ export enum CacheType {
116
+ NONE, // Do not cache
117
+ WITH_TTL, // Cache with TTL
118
+ NO_TTL, // Cache with infinite TTL
119
+ }
@@ -115,3 +115,16 @@ export function chainIsCCTPEnabled(chainId: number): boolean {
115
115
  export function chainRequiresL1ToL2Finalization(chainId: number): boolean {
116
116
  return chainIsCCTPEnabled(chainId) || chainIsLinea(chainId);
117
117
  }
118
+
119
+ /**
120
+ * Returns the origin of a URL.
121
+ * @param url A URL.
122
+ * @returns The origin of the URL, or "UNKNOWN" if the URL is invalid.
123
+ */
124
+ export function getOriginFromURL(url: string): string {
125
+ try {
126
+ return new URL(url).origin;
127
+ } catch (e) {
128
+ return "UNKNOWN";
129
+ }
130
+ }
@@ -2,6 +2,7 @@
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
3
  // Append value along the keyPath to object. For example assign(deposits, ['1337', '31337'], [{depositId:1}]) will create
4
4
  // deposits = {1337:{31337:[{depositId:1}]}}. Note that if the path into the object exists then this will append. This
5
+
5
6
  // function respects the destination type; if it is an object then deep merge and if an array effectively will push.
6
7
  export function assign(obj: any, keyPath: any[], value: any): void {
7
8
  const lastKeyIndex = keyPath.length - 1;