@imtbl/sdk 0.45.4 → 0.45.5

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.
package/dist/index.cjs CHANGED
@@ -46,6 +46,268 @@ var crypto__namespace = /*#__PURE__*/_interopNamespaceDefault(crypto);
46
46
  var encUtils__namespace = /*#__PURE__*/_interopNamespaceDefault(encUtils);
47
47
  var Uniswap__namespace = /*#__PURE__*/_interopNamespaceDefault(Uniswap);
48
48
 
49
+ const isNode$1 = () => typeof window === 'undefined';
50
+ const isBrowser$1 = () => !isNode$1();
51
+
52
+ var Detail$1;
53
+ (function (Detail) {
54
+ Detail["RUNTIME_ID"] = "rid";
55
+ Detail["PASSPORT_CLIENT_ID"] = "passportClientId";
56
+ Detail["ENVIRONMENT"] = "env";
57
+ Detail["PUBLISHABLE_API_KEY"] = "pak";
58
+ Detail["IDENTITY"] = "uid";
59
+ Detail["DOMAIN"] = "domain";
60
+ })(Detail$1 || (Detail$1 = {}));
61
+ var Store$1;
62
+ (function (Store) {
63
+ Store["EVENTS"] = "events";
64
+ Store["RUNTIME"] = "runtime";
65
+ })(Store$1 || (Store$1 = {}));
66
+ const POLLING_FREQUENCY$1 = 5000;
67
+
68
+ const IMTBL_API$1 = 'https://api.x.immutable.com';
69
+ async function post$1(path, data) {
70
+ const client = globalAxios$1.create({
71
+ baseURL: IMTBL_API$1,
72
+ });
73
+ const payload = JSON.stringify(data);
74
+ const body = {
75
+ payload: btoa(payload),
76
+ };
77
+ const response = await client.post(path, body);
78
+ return response.data;
79
+ }
80
+
81
+ /**
82
+ * Abstraction on localstorage
83
+ */
84
+ const localStoragePrefix$1 = '__IMX-';
85
+ const hasLocalstorage$1 = () => isBrowser$1() && window.localStorage;
86
+ const parseItem$1 = (payload) => {
87
+ // Try to parse, if can't be parsed assume string
88
+ // and return string
89
+ if (payload === null)
90
+ return undefined;
91
+ try {
92
+ return JSON.parse(payload);
93
+ }
94
+ catch (error) {
95
+ // Assumes it's a string.
96
+ return payload;
97
+ }
98
+ };
99
+ const serialiseItem$1 = (payload) => {
100
+ if (typeof payload === 'string') {
101
+ return payload;
102
+ }
103
+ return JSON.stringify(payload);
104
+ };
105
+ /**
106
+ * GenKey will take into account the namespace
107
+ * as well as if being run in the Link, it will tap into the link
108
+ * @param {string} key
109
+ * @returns key
110
+ */
111
+ const genKey$1 = (key) => `${localStoragePrefix$1}${key}`;
112
+ function getItem$1(key) {
113
+ if (hasLocalstorage$1()) {
114
+ return parseItem$1(window.localStorage.getItem(genKey$1(key)));
115
+ }
116
+ return undefined;
117
+ }
118
+ const setItem$1 = (key, payload) => {
119
+ if (hasLocalstorage$1()) {
120
+ window.localStorage.setItem(genKey$1(key), serialiseItem$1(payload));
121
+ return true;
122
+ }
123
+ return false;
124
+ };
125
+
126
+ // In memory storage for events and other data
127
+ let EVENT_STORE$1;
128
+ let RUNTIME_DETAILS$1;
129
+ // Initialise store and runtime
130
+ const initialise$1$1 = () => {
131
+ EVENT_STORE$1 = getItem$1(Store$1.EVENTS) || [];
132
+ RUNTIME_DETAILS$1 = getItem$1(Store$1.RUNTIME) || {};
133
+ };
134
+ initialise$1$1();
135
+ // Runtime Details
136
+ const storeDetail$1 = (key, value) => {
137
+ RUNTIME_DETAILS$1 = {
138
+ ...RUNTIME_DETAILS$1,
139
+ [key]: value,
140
+ };
141
+ setItem$1(Store$1.RUNTIME, RUNTIME_DETAILS$1);
142
+ };
143
+ const getDetail$1 = (key) => {
144
+ // Handle the scenario where detail is a falsy value
145
+ if (RUNTIME_DETAILS$1[key] === undefined) {
146
+ return undefined;
147
+ }
148
+ return RUNTIME_DETAILS$1[key];
149
+ };
150
+ const getAllDetails$1 = () => RUNTIME_DETAILS$1;
151
+ // Events
152
+ const getEvents$1 = () => EVENT_STORE$1;
153
+ const addEvent = (event) => {
154
+ EVENT_STORE$1.push(event);
155
+ setItem$1(Store$1.EVENTS, EVENT_STORE$1);
156
+ };
157
+ const removeSentEvents$1 = (numberOfEvents) => {
158
+ EVENT_STORE$1 = EVENT_STORE$1.slice(numberOfEvents);
159
+ setItem$1(Store$1.EVENTS, EVENT_STORE$1);
160
+ };
161
+ const flattenProperties$1 = (properties) => {
162
+ const propertyMap = [];
163
+ Object.entries(properties).forEach(([key, value]) => {
164
+ if (typeof value === 'string' || typeof value === 'number') {
165
+ propertyMap.push([key, value.toString()]);
166
+ }
167
+ });
168
+ return propertyMap;
169
+ };
170
+
171
+ let initialised$1 = false;
172
+ const isInitialised$1 = () => initialised$1;
173
+ const getReferrer$1 = () => {
174
+ if (isNode$1()) {
175
+ return '';
176
+ }
177
+ if (window.location.ancestorOrigins
178
+ && window.location.ancestorOrigins.length > 0) {
179
+ return new URL(window.location.ancestorOrigins[0]).hostname;
180
+ }
181
+ return document.referrer ? new URL(window.document.referrer).hostname : '';
182
+ };
183
+ const runtimeHost$1 = () => {
184
+ if (isNode$1()) {
185
+ return '';
186
+ }
187
+ if (window.opener || window.parent !== window) {
188
+ return getReferrer$1();
189
+ }
190
+ return new URL(window.location.href).hostname;
191
+ };
192
+ const getRuntimeDetails$1 = () => {
193
+ if (isNode$1()) {
194
+ return { browser: 'nodejs' };
195
+ }
196
+ const domain = runtimeHost$1();
197
+ if (domain) {
198
+ storeDetail$1(Detail$1.DOMAIN, domain);
199
+ }
200
+ return {
201
+ browser: window.navigator.userAgent,
202
+ domain,
203
+ tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
204
+ screen: `${window.screen.width}x${window.screen.height}`,
205
+ };
206
+ };
207
+ const initialise$2 = async () => {
208
+ const runtimeDetails = flattenProperties$1(getRuntimeDetails$1());
209
+ // Get any existing runtimeId and send it along with the initialise
210
+ const existingRuntimeId = getDetail$1(Detail$1.RUNTIME_ID);
211
+ const body = {
212
+ version: 1,
213
+ data: {
214
+ runtimeDetails,
215
+ runtimeId: existingRuntimeId,
216
+ },
217
+ };
218
+ const response = await post$1('/v1/sdk/initialise', body);
219
+ // Get runtimeId and store it
220
+ const { runtimeId } = response;
221
+ storeDetail$1(Detail$1.RUNTIME_ID, runtimeId);
222
+ initialised$1 = true;
223
+ };
224
+
225
+ function errorBoundary$1(fn) {
226
+ const wrappedFunction = ((...args) => {
227
+ try {
228
+ // Execute the original function
229
+ const result = fn(...args);
230
+ if (result instanceof Promise) {
231
+ // Silent fail for now, in future
232
+ // we can send errors to a logging service
233
+ return result.catch(() => undefined);
234
+ }
235
+ return result;
236
+ }
237
+ catch (error) {
238
+ // As above, fail silently for now
239
+ return undefined;
240
+ }
241
+ });
242
+ return wrappedFunction;
243
+ }
244
+
245
+ function isTestEnvironment$1() {
246
+ if (typeof process === 'undefined') {
247
+ return false;
248
+ }
249
+ return process.env.JEST_WORKER_ID !== undefined;
250
+ }
251
+
252
+ // Store the event in the event store
253
+ const trackFn = (moduleName, eventName, properties) => {
254
+ const event = {
255
+ event: `${moduleName}.${eventName}`,
256
+ time: new Date().toISOString(),
257
+ ...(properties && { properties: flattenProperties$1(properties) }),
258
+ };
259
+ addEvent(event);
260
+ };
261
+ const track = errorBoundary$1(trackFn);
262
+ // Sending events to the server
263
+ const flushFn$1 = async () => {
264
+ // Don't flush if not initialised
265
+ if (isInitialised$1() === false) {
266
+ await initialise$2();
267
+ return;
268
+ }
269
+ const events = getEvents$1();
270
+ if (events.length === 0) {
271
+ return;
272
+ }
273
+ // Track events length here, incase
274
+ const numEvents = events.length;
275
+ // Get details and send it with the track request
276
+ const details = getAllDetails$1();
277
+ const metricsPayload = {
278
+ version: 1,
279
+ data: {
280
+ events,
281
+ details,
282
+ },
283
+ };
284
+ const response = await post$1('/v1/sdk/metrics', metricsPayload);
285
+ if (response instanceof Error) {
286
+ return;
287
+ }
288
+ // Clear events if successfully posted
289
+ removeSentEvents$1(numEvents);
290
+ };
291
+ const flush$1 = errorBoundary$1(flushFn$1);
292
+ // Flush events every 5 seconds
293
+ const flushPoll$1 = async () => {
294
+ await flush$1();
295
+ setTimeout(flushPoll$1, POLLING_FREQUENCY$1);
296
+ };
297
+ // This will get initialised when module is imported.
298
+ if (!isTestEnvironment$1()) {
299
+ errorBoundary$1(flushPoll$1)();
300
+ }
301
+
302
+ const setEnvironmentFn = (env) => {
303
+ storeDetail$1(Detail$1.ENVIRONMENT, env);
304
+ };
305
+ const setEnvironment = errorBoundary$1(setEnvironmentFn);
306
+ const setPublishableApiKeyFn = (publishableApiKey) => {
307
+ storeDetail$1(Detail$1.PUBLISHABLE_API_KEY, publishableApiKey);
308
+ };
309
+ const setPublishableApiKey = errorBoundary$1(setPublishableApiKeyFn);
310
+
49
311
  var Environment$1;
50
312
  (function (Environment) {
51
313
  Environment["PRODUCTION"] = "production";
@@ -58,6 +320,8 @@ class ImmutableConfiguration {
58
320
  publishableKey;
59
321
  constructor(options) {
60
322
  this.environment = options.environment;
323
+ setEnvironment(options.environment);
324
+ track('config', 'load_imtbl_config');
61
325
  }
62
326
  }
63
327
  const API_KEY_PREFIX = 'sk_imapik-';
@@ -73,6 +337,7 @@ const addPublishableKeyToAxiosHeader = (publishableKey) => {
73
337
  throw new Error('Invalid Publishable key. Create your Publishable key in Immutable developer hub.'
74
338
  + ' https://hub.immutable.com');
75
339
  }
340
+ setPublishableApiKey(publishableKey);
76
341
  globalAxios$1.defaults.headers.common['x-immutable-publishable-key'] = publishableKey;
77
342
  };
78
343
  const addRateLimitingKeyToAxiosHeader = (rateLimitingKey) => {
@@ -10641,7 +10906,7 @@ class MultiRollupApiClients {
10641
10906
  }
10642
10907
 
10643
10908
  // eslint-disable-next-line @typescript-eslint/naming-convention
10644
- const defaultHeaders$1 = { 'x-sdk-version': 'ts-immutable-sdk-0.45.4' };
10909
+ const defaultHeaders$1 = { 'x-sdk-version': 'ts-immutable-sdk-0.45.5' };
10645
10910
  const createConfig = ({ basePath, headers, }) => {
10646
10911
  if (!basePath.trim()) {
10647
10912
  throw Error('basePath can not be empty');
@@ -10713,7 +10978,7 @@ class APIError extends Error {
10713
10978
 
10714
10979
  /* eslint-disable implicit-arrow-linebreak */
10715
10980
  const defaultHeaders = {
10716
- sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-0.45.4',
10981
+ sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-0.45.5',
10717
10982
  };
10718
10983
  /**
10719
10984
  * createAPIConfiguration to create a custom Configuration
@@ -11152,7 +11417,7 @@ const createImmutableXConfiguration = ({ basePath, chainID, coreContractAddress,
11152
11417
  chainID,
11153
11418
  coreContractAddress,
11154
11419
  registrationContractAddress,
11155
- sdkVersion: 'ts-immutable-sdk-0.45.4',
11420
+ sdkVersion: 'ts-immutable-sdk-0.45.5',
11156
11421
  });
11157
11422
  /**
11158
11423
  * Sets `sdkVersion` at the time of build
@@ -36306,6 +36571,244 @@ class TokenBridge {
36306
36571
  }
36307
36572
  }
36308
36573
 
36574
+ const isNode = () => typeof window === 'undefined';
36575
+ const isBrowser = () => !isNode();
36576
+
36577
+ var Detail;
36578
+ (function (Detail) {
36579
+ Detail["RUNTIME_ID"] = "rid";
36580
+ Detail["PASSPORT_CLIENT_ID"] = "passportClientId";
36581
+ Detail["ENVIRONMENT"] = "env";
36582
+ Detail["PUBLISHABLE_API_KEY"] = "pak";
36583
+ Detail["IDENTITY"] = "uid";
36584
+ Detail["DOMAIN"] = "domain";
36585
+ })(Detail || (Detail = {}));
36586
+ var Store;
36587
+ (function (Store) {
36588
+ Store["EVENTS"] = "events";
36589
+ Store["RUNTIME"] = "runtime";
36590
+ })(Store || (Store = {}));
36591
+ const POLLING_FREQUENCY = 5000;
36592
+
36593
+ const IMTBL_API = 'https://api.x.immutable.com';
36594
+ async function post(path, data) {
36595
+ const client = axios$1.create({
36596
+ baseURL: IMTBL_API,
36597
+ });
36598
+ const payload = JSON.stringify(data);
36599
+ const body = {
36600
+ payload: btoa(payload),
36601
+ };
36602
+ const response = await client.post(path, body);
36603
+ return response.data;
36604
+ }
36605
+
36606
+ /**
36607
+ * Abstraction on localstorage
36608
+ */
36609
+ const localStoragePrefix = '__IMX-';
36610
+ const hasLocalstorage = () => isBrowser() && window.localStorage;
36611
+ const parseItem = (payload) => {
36612
+ // Try to parse, if can't be parsed assume string
36613
+ // and return string
36614
+ if (payload === null)
36615
+ return undefined;
36616
+ try {
36617
+ return JSON.parse(payload);
36618
+ }
36619
+ catch (error) {
36620
+ // Assumes it's a string.
36621
+ return payload;
36622
+ }
36623
+ };
36624
+ const serialiseItem = (payload) => {
36625
+ if (typeof payload === 'string') {
36626
+ return payload;
36627
+ }
36628
+ return JSON.stringify(payload);
36629
+ };
36630
+ /**
36631
+ * GenKey will take into account the namespace
36632
+ * as well as if being run in the Link, it will tap into the link
36633
+ * @param {string} key
36634
+ * @returns key
36635
+ */
36636
+ const genKey = (key) => `${localStoragePrefix}${key}`;
36637
+ function getItem(key) {
36638
+ if (hasLocalstorage()) {
36639
+ return parseItem(window.localStorage.getItem(genKey(key)));
36640
+ }
36641
+ return undefined;
36642
+ }
36643
+ const setItem = (key, payload) => {
36644
+ if (hasLocalstorage()) {
36645
+ window.localStorage.setItem(genKey(key), serialiseItem(payload));
36646
+ return true;
36647
+ }
36648
+ return false;
36649
+ };
36650
+
36651
+ // In memory storage for events and other data
36652
+ let EVENT_STORE;
36653
+ let RUNTIME_DETAILS;
36654
+ // Initialise store and runtime
36655
+ const initialise$1 = () => {
36656
+ EVENT_STORE = getItem(Store.EVENTS) || [];
36657
+ RUNTIME_DETAILS = getItem(Store.RUNTIME) || {};
36658
+ };
36659
+ initialise$1();
36660
+ // Runtime Details
36661
+ const storeDetail = (key, value) => {
36662
+ RUNTIME_DETAILS = {
36663
+ ...RUNTIME_DETAILS,
36664
+ [key]: value,
36665
+ };
36666
+ setItem(Store.RUNTIME, RUNTIME_DETAILS);
36667
+ };
36668
+ const getDetail = (key) => {
36669
+ // Handle the scenario where detail is a falsy value
36670
+ if (RUNTIME_DETAILS[key] === undefined) {
36671
+ return undefined;
36672
+ }
36673
+ return RUNTIME_DETAILS[key];
36674
+ };
36675
+ const getAllDetails = () => RUNTIME_DETAILS;
36676
+ // Events
36677
+ const getEvents = () => EVENT_STORE;
36678
+ const removeSentEvents = (numberOfEvents) => {
36679
+ EVENT_STORE = EVENT_STORE.slice(numberOfEvents);
36680
+ setItem(Store.EVENTS, EVENT_STORE);
36681
+ };
36682
+ const flattenProperties = (properties) => {
36683
+ const propertyMap = [];
36684
+ Object.entries(properties).forEach(([key, value]) => {
36685
+ if (typeof value === 'string' || typeof value === 'number') {
36686
+ propertyMap.push([key, value.toString()]);
36687
+ }
36688
+ });
36689
+ return propertyMap;
36690
+ };
36691
+
36692
+ let initialised = false;
36693
+ const isInitialised = () => initialised;
36694
+ const getReferrer = () => {
36695
+ if (isNode()) {
36696
+ return '';
36697
+ }
36698
+ if (window.location.ancestorOrigins
36699
+ && window.location.ancestorOrigins.length > 0) {
36700
+ return new URL(window.location.ancestorOrigins[0]).hostname;
36701
+ }
36702
+ return document.referrer ? new URL(window.document.referrer).hostname : '';
36703
+ };
36704
+ const runtimeHost = () => {
36705
+ if (isNode()) {
36706
+ return '';
36707
+ }
36708
+ if (window.opener || window.parent !== window) {
36709
+ return getReferrer();
36710
+ }
36711
+ return new URL(window.location.href).hostname;
36712
+ };
36713
+ const getRuntimeDetails = () => {
36714
+ if (isNode()) {
36715
+ return { browser: 'nodejs' };
36716
+ }
36717
+ const domain = runtimeHost();
36718
+ if (domain) {
36719
+ storeDetail(Detail.DOMAIN, domain);
36720
+ }
36721
+ return {
36722
+ browser: window.navigator.userAgent,
36723
+ domain,
36724
+ tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
36725
+ screen: `${window.screen.width}x${window.screen.height}`,
36726
+ };
36727
+ };
36728
+ const initialise = async () => {
36729
+ const runtimeDetails = flattenProperties(getRuntimeDetails());
36730
+ // Get any existing runtimeId and send it along with the initialise
36731
+ const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
36732
+ const body = {
36733
+ version: 1,
36734
+ data: {
36735
+ runtimeDetails,
36736
+ runtimeId: existingRuntimeId,
36737
+ },
36738
+ };
36739
+ const response = await post('/v1/sdk/initialise', body);
36740
+ // Get runtimeId and store it
36741
+ const { runtimeId } = response;
36742
+ storeDetail(Detail.RUNTIME_ID, runtimeId);
36743
+ initialised = true;
36744
+ };
36745
+
36746
+ function errorBoundary(fn) {
36747
+ const wrappedFunction = ((...args) => {
36748
+ try {
36749
+ // Execute the original function
36750
+ const result = fn(...args);
36751
+ if (result instanceof Promise) {
36752
+ // Silent fail for now, in future
36753
+ // we can send errors to a logging service
36754
+ return result.catch(() => undefined);
36755
+ }
36756
+ return result;
36757
+ }
36758
+ catch (error) {
36759
+ // As above, fail silently for now
36760
+ return undefined;
36761
+ }
36762
+ });
36763
+ return wrappedFunction;
36764
+ }
36765
+
36766
+ function isTestEnvironment() {
36767
+ if (typeof process === 'undefined') {
36768
+ return false;
36769
+ }
36770
+ return process.env.JEST_WORKER_ID !== undefined;
36771
+ }
36772
+ // Sending events to the server
36773
+ const flushFn = async () => {
36774
+ // Don't flush if not initialised
36775
+ if (isInitialised() === false) {
36776
+ await initialise();
36777
+ return;
36778
+ }
36779
+ const events = getEvents();
36780
+ if (events.length === 0) {
36781
+ return;
36782
+ }
36783
+ // Track events length here, incase
36784
+ const numEvents = events.length;
36785
+ // Get details and send it with the track request
36786
+ const details = getAllDetails();
36787
+ const metricsPayload = {
36788
+ version: 1,
36789
+ data: {
36790
+ events,
36791
+ details,
36792
+ },
36793
+ };
36794
+ const response = await post('/v1/sdk/metrics', metricsPayload);
36795
+ if (response instanceof Error) {
36796
+ return;
36797
+ }
36798
+ // Clear events if successfully posted
36799
+ removeSentEvents(numEvents);
36800
+ };
36801
+ const flush = errorBoundary(flushFn);
36802
+ // Flush events every 5 seconds
36803
+ const flushPoll = async () => {
36804
+ await flush();
36805
+ setTimeout(flushPoll, POLLING_FREQUENCY);
36806
+ };
36807
+ // This will get initialised when module is imported.
36808
+ if (!isTestEnvironment()) {
36809
+ errorBoundary(flushPoll)();
36810
+ }
36811
+
36309
36812
  var Environment;
36310
36813
  (function (Environment) {
36311
36814
  Environment["PRODUCTION"] = "production";
@@ -41285,7 +41788,7 @@ const IMMUTABLE_ZKVEM_GAS_OVERRIDES = {
41285
41788
  maxPriorityFeePerGas: ethers.BigNumber.from(10e9),
41286
41789
  };
41287
41790
 
41288
- const SDK_VERSION_MARKER = '0.45.4';
41791
+ const SDK_VERSION_MARKER = '0.45.5';
41289
41792
  // This SDK version is replaced by the `yarn build` command ran on the root level
41290
41793
  const globalPackageVersion = () => SDK_VERSION_MARKER;
41291
41794
 
@@ -41490,7 +41993,7 @@ async function getERC20TokenInfo(web3Provider, tokenAddress) {
41490
41993
  }, { type: CheckoutErrorType.GET_ERC20_INFO_ERROR });
41491
41994
  }
41492
41995
 
41493
- const CACHE_DATA_TTL = 60; // seconds
41996
+ const CACHE_DATA_TTL = 5; // seconds
41494
41997
  /**
41495
41998
  * Blockscout class provides a client abstraction for the Immutable 3rd party indexer.
41496
41999
  */
package/dist/index.d.ts CHANGED
@@ -24,9 +24,9 @@ type ImmutableConfigurationWithRequireableFields<T> = ImmutableConfiguration & (
24
24
  } ? Required<{
25
25
  publishableKey: string;
26
26
  }> : {});
27
- type ImmutableConfigurationWithOmitableFields<T> = (T extends {
27
+ type ImmutableConfigurationWithOmitableFields<T> = T extends {
28
28
  apiKey: 'omit';
29
- } ? Omit<ImmutableConfigurationWithRequireableFields<T>, 'apiKey'> : ImmutableConfigurationWithRequireableFields<T>);
29
+ } ? Omit<ImmutableConfigurationWithRequireableFields<T>, 'apiKey'> : ImmutableConfigurationWithRequireableFields<T>;
30
30
  interface ModuleConfiguration<T> {
31
31
  baseConfig: ImmutableConfigurationWithOmitableFields<T>;
32
32
  overrides?: T;