@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.js CHANGED
@@ -24,6 +24,268 @@ import { SwapRouter, PaymentsExtended } from '@uniswap/router-sdk';
24
24
  import { parseUnits } from '@ethersproject/units';
25
25
  import { BigNumber as BigNumber$2 } from '@ethersproject/bignumber';
26
26
 
27
+ const isNode$1 = () => typeof window === 'undefined';
28
+ const isBrowser$1 = () => !isNode$1();
29
+
30
+ var Detail$1;
31
+ (function (Detail) {
32
+ Detail["RUNTIME_ID"] = "rid";
33
+ Detail["PASSPORT_CLIENT_ID"] = "passportClientId";
34
+ Detail["ENVIRONMENT"] = "env";
35
+ Detail["PUBLISHABLE_API_KEY"] = "pak";
36
+ Detail["IDENTITY"] = "uid";
37
+ Detail["DOMAIN"] = "domain";
38
+ })(Detail$1 || (Detail$1 = {}));
39
+ var Store$1;
40
+ (function (Store) {
41
+ Store["EVENTS"] = "events";
42
+ Store["RUNTIME"] = "runtime";
43
+ })(Store$1 || (Store$1 = {}));
44
+ const POLLING_FREQUENCY$1 = 5000;
45
+
46
+ const IMTBL_API$1 = 'https://api.x.immutable.com';
47
+ async function post$1(path, data) {
48
+ const client = globalAxios$1.create({
49
+ baseURL: IMTBL_API$1,
50
+ });
51
+ const payload = JSON.stringify(data);
52
+ const body = {
53
+ payload: btoa(payload),
54
+ };
55
+ const response = await client.post(path, body);
56
+ return response.data;
57
+ }
58
+
59
+ /**
60
+ * Abstraction on localstorage
61
+ */
62
+ const localStoragePrefix$1 = '__IMX-';
63
+ const hasLocalstorage$1 = () => isBrowser$1() && window.localStorage;
64
+ const parseItem$1 = (payload) => {
65
+ // Try to parse, if can't be parsed assume string
66
+ // and return string
67
+ if (payload === null)
68
+ return undefined;
69
+ try {
70
+ return JSON.parse(payload);
71
+ }
72
+ catch (error) {
73
+ // Assumes it's a string.
74
+ return payload;
75
+ }
76
+ };
77
+ const serialiseItem$1 = (payload) => {
78
+ if (typeof payload === 'string') {
79
+ return payload;
80
+ }
81
+ return JSON.stringify(payload);
82
+ };
83
+ /**
84
+ * GenKey will take into account the namespace
85
+ * as well as if being run in the Link, it will tap into the link
86
+ * @param {string} key
87
+ * @returns key
88
+ */
89
+ const genKey$1 = (key) => `${localStoragePrefix$1}${key}`;
90
+ function getItem$1(key) {
91
+ if (hasLocalstorage$1()) {
92
+ return parseItem$1(window.localStorage.getItem(genKey$1(key)));
93
+ }
94
+ return undefined;
95
+ }
96
+ const setItem$1 = (key, payload) => {
97
+ if (hasLocalstorage$1()) {
98
+ window.localStorage.setItem(genKey$1(key), serialiseItem$1(payload));
99
+ return true;
100
+ }
101
+ return false;
102
+ };
103
+
104
+ // In memory storage for events and other data
105
+ let EVENT_STORE$1;
106
+ let RUNTIME_DETAILS$1;
107
+ // Initialise store and runtime
108
+ const initialise$1$1 = () => {
109
+ EVENT_STORE$1 = getItem$1(Store$1.EVENTS) || [];
110
+ RUNTIME_DETAILS$1 = getItem$1(Store$1.RUNTIME) || {};
111
+ };
112
+ initialise$1$1();
113
+ // Runtime Details
114
+ const storeDetail$1 = (key, value) => {
115
+ RUNTIME_DETAILS$1 = {
116
+ ...RUNTIME_DETAILS$1,
117
+ [key]: value,
118
+ };
119
+ setItem$1(Store$1.RUNTIME, RUNTIME_DETAILS$1);
120
+ };
121
+ const getDetail$1 = (key) => {
122
+ // Handle the scenario where detail is a falsy value
123
+ if (RUNTIME_DETAILS$1[key] === undefined) {
124
+ return undefined;
125
+ }
126
+ return RUNTIME_DETAILS$1[key];
127
+ };
128
+ const getAllDetails$1 = () => RUNTIME_DETAILS$1;
129
+ // Events
130
+ const getEvents$1 = () => EVENT_STORE$1;
131
+ const addEvent = (event) => {
132
+ EVENT_STORE$1.push(event);
133
+ setItem$1(Store$1.EVENTS, EVENT_STORE$1);
134
+ };
135
+ const removeSentEvents$1 = (numberOfEvents) => {
136
+ EVENT_STORE$1 = EVENT_STORE$1.slice(numberOfEvents);
137
+ setItem$1(Store$1.EVENTS, EVENT_STORE$1);
138
+ };
139
+ const flattenProperties$1 = (properties) => {
140
+ const propertyMap = [];
141
+ Object.entries(properties).forEach(([key, value]) => {
142
+ if (typeof value === 'string' || typeof value === 'number') {
143
+ propertyMap.push([key, value.toString()]);
144
+ }
145
+ });
146
+ return propertyMap;
147
+ };
148
+
149
+ let initialised$1 = false;
150
+ const isInitialised$1 = () => initialised$1;
151
+ const getReferrer$1 = () => {
152
+ if (isNode$1()) {
153
+ return '';
154
+ }
155
+ if (window.location.ancestorOrigins
156
+ && window.location.ancestorOrigins.length > 0) {
157
+ return new URL(window.location.ancestorOrigins[0]).hostname;
158
+ }
159
+ return document.referrer ? new URL(window.document.referrer).hostname : '';
160
+ };
161
+ const runtimeHost$1 = () => {
162
+ if (isNode$1()) {
163
+ return '';
164
+ }
165
+ if (window.opener || window.parent !== window) {
166
+ return getReferrer$1();
167
+ }
168
+ return new URL(window.location.href).hostname;
169
+ };
170
+ const getRuntimeDetails$1 = () => {
171
+ if (isNode$1()) {
172
+ return { browser: 'nodejs' };
173
+ }
174
+ const domain = runtimeHost$1();
175
+ if (domain) {
176
+ storeDetail$1(Detail$1.DOMAIN, domain);
177
+ }
178
+ return {
179
+ browser: window.navigator.userAgent,
180
+ domain,
181
+ tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
182
+ screen: `${window.screen.width}x${window.screen.height}`,
183
+ };
184
+ };
185
+ const initialise$2 = async () => {
186
+ const runtimeDetails = flattenProperties$1(getRuntimeDetails$1());
187
+ // Get any existing runtimeId and send it along with the initialise
188
+ const existingRuntimeId = getDetail$1(Detail$1.RUNTIME_ID);
189
+ const body = {
190
+ version: 1,
191
+ data: {
192
+ runtimeDetails,
193
+ runtimeId: existingRuntimeId,
194
+ },
195
+ };
196
+ const response = await post$1('/v1/sdk/initialise', body);
197
+ // Get runtimeId and store it
198
+ const { runtimeId } = response;
199
+ storeDetail$1(Detail$1.RUNTIME_ID, runtimeId);
200
+ initialised$1 = true;
201
+ };
202
+
203
+ function errorBoundary$1(fn) {
204
+ const wrappedFunction = ((...args) => {
205
+ try {
206
+ // Execute the original function
207
+ const result = fn(...args);
208
+ if (result instanceof Promise) {
209
+ // Silent fail for now, in future
210
+ // we can send errors to a logging service
211
+ return result.catch(() => undefined);
212
+ }
213
+ return result;
214
+ }
215
+ catch (error) {
216
+ // As above, fail silently for now
217
+ return undefined;
218
+ }
219
+ });
220
+ return wrappedFunction;
221
+ }
222
+
223
+ function isTestEnvironment$1() {
224
+ if (typeof process === 'undefined') {
225
+ return false;
226
+ }
227
+ return process.env.JEST_WORKER_ID !== undefined;
228
+ }
229
+
230
+ // Store the event in the event store
231
+ const trackFn = (moduleName, eventName, properties) => {
232
+ const event = {
233
+ event: `${moduleName}.${eventName}`,
234
+ time: new Date().toISOString(),
235
+ ...(properties && { properties: flattenProperties$1(properties) }),
236
+ };
237
+ addEvent(event);
238
+ };
239
+ const track = errorBoundary$1(trackFn);
240
+ // Sending events to the server
241
+ const flushFn$1 = async () => {
242
+ // Don't flush if not initialised
243
+ if (isInitialised$1() === false) {
244
+ await initialise$2();
245
+ return;
246
+ }
247
+ const events = getEvents$1();
248
+ if (events.length === 0) {
249
+ return;
250
+ }
251
+ // Track events length here, incase
252
+ const numEvents = events.length;
253
+ // Get details and send it with the track request
254
+ const details = getAllDetails$1();
255
+ const metricsPayload = {
256
+ version: 1,
257
+ data: {
258
+ events,
259
+ details,
260
+ },
261
+ };
262
+ const response = await post$1('/v1/sdk/metrics', metricsPayload);
263
+ if (response instanceof Error) {
264
+ return;
265
+ }
266
+ // Clear events if successfully posted
267
+ removeSentEvents$1(numEvents);
268
+ };
269
+ const flush$1 = errorBoundary$1(flushFn$1);
270
+ // Flush events every 5 seconds
271
+ const flushPoll$1 = async () => {
272
+ await flush$1();
273
+ setTimeout(flushPoll$1, POLLING_FREQUENCY$1);
274
+ };
275
+ // This will get initialised when module is imported.
276
+ if (!isTestEnvironment$1()) {
277
+ errorBoundary$1(flushPoll$1)();
278
+ }
279
+
280
+ const setEnvironmentFn = (env) => {
281
+ storeDetail$1(Detail$1.ENVIRONMENT, env);
282
+ };
283
+ const setEnvironment = errorBoundary$1(setEnvironmentFn);
284
+ const setPublishableApiKeyFn = (publishableApiKey) => {
285
+ storeDetail$1(Detail$1.PUBLISHABLE_API_KEY, publishableApiKey);
286
+ };
287
+ const setPublishableApiKey = errorBoundary$1(setPublishableApiKeyFn);
288
+
27
289
  var Environment$1;
28
290
  (function (Environment) {
29
291
  Environment["PRODUCTION"] = "production";
@@ -36,6 +298,8 @@ class ImmutableConfiguration {
36
298
  publishableKey;
37
299
  constructor(options) {
38
300
  this.environment = options.environment;
301
+ setEnvironment(options.environment);
302
+ track('config', 'load_imtbl_config');
39
303
  }
40
304
  }
41
305
  const API_KEY_PREFIX = 'sk_imapik-';
@@ -51,6 +315,7 @@ const addPublishableKeyToAxiosHeader = (publishableKey) => {
51
315
  throw new Error('Invalid Publishable key. Create your Publishable key in Immutable developer hub.'
52
316
  + ' https://hub.immutable.com');
53
317
  }
318
+ setPublishableApiKey(publishableKey);
54
319
  globalAxios$1.defaults.headers.common['x-immutable-publishable-key'] = publishableKey;
55
320
  };
56
321
  const addRateLimitingKeyToAxiosHeader = (rateLimitingKey) => {
@@ -10619,7 +10884,7 @@ class MultiRollupApiClients {
10619
10884
  }
10620
10885
 
10621
10886
  // eslint-disable-next-line @typescript-eslint/naming-convention
10622
- const defaultHeaders$1 = { 'x-sdk-version': 'ts-immutable-sdk-0.45.4' };
10887
+ const defaultHeaders$1 = { 'x-sdk-version': 'ts-immutable-sdk-0.45.5' };
10623
10888
  const createConfig = ({ basePath, headers, }) => {
10624
10889
  if (!basePath.trim()) {
10625
10890
  throw Error('basePath can not be empty');
@@ -10691,7 +10956,7 @@ class APIError extends Error {
10691
10956
 
10692
10957
  /* eslint-disable implicit-arrow-linebreak */
10693
10958
  const defaultHeaders = {
10694
- sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-0.45.4',
10959
+ sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-0.45.5',
10695
10960
  };
10696
10961
  /**
10697
10962
  * createAPIConfiguration to create a custom Configuration
@@ -11130,7 +11395,7 @@ const createImmutableXConfiguration = ({ basePath, chainID, coreContractAddress,
11130
11395
  chainID,
11131
11396
  coreContractAddress,
11132
11397
  registrationContractAddress,
11133
- sdkVersion: 'ts-immutable-sdk-0.45.4',
11398
+ sdkVersion: 'ts-immutable-sdk-0.45.5',
11134
11399
  });
11135
11400
  /**
11136
11401
  * Sets `sdkVersion` at the time of build
@@ -36284,6 +36549,244 @@ class TokenBridge {
36284
36549
  }
36285
36550
  }
36286
36551
 
36552
+ const isNode = () => typeof window === 'undefined';
36553
+ const isBrowser = () => !isNode();
36554
+
36555
+ var Detail;
36556
+ (function (Detail) {
36557
+ Detail["RUNTIME_ID"] = "rid";
36558
+ Detail["PASSPORT_CLIENT_ID"] = "passportClientId";
36559
+ Detail["ENVIRONMENT"] = "env";
36560
+ Detail["PUBLISHABLE_API_KEY"] = "pak";
36561
+ Detail["IDENTITY"] = "uid";
36562
+ Detail["DOMAIN"] = "domain";
36563
+ })(Detail || (Detail = {}));
36564
+ var Store;
36565
+ (function (Store) {
36566
+ Store["EVENTS"] = "events";
36567
+ Store["RUNTIME"] = "runtime";
36568
+ })(Store || (Store = {}));
36569
+ const POLLING_FREQUENCY = 5000;
36570
+
36571
+ const IMTBL_API = 'https://api.x.immutable.com';
36572
+ async function post(path, data) {
36573
+ const client = axios$1.create({
36574
+ baseURL: IMTBL_API,
36575
+ });
36576
+ const payload = JSON.stringify(data);
36577
+ const body = {
36578
+ payload: btoa(payload),
36579
+ };
36580
+ const response = await client.post(path, body);
36581
+ return response.data;
36582
+ }
36583
+
36584
+ /**
36585
+ * Abstraction on localstorage
36586
+ */
36587
+ const localStoragePrefix = '__IMX-';
36588
+ const hasLocalstorage = () => isBrowser() && window.localStorage;
36589
+ const parseItem = (payload) => {
36590
+ // Try to parse, if can't be parsed assume string
36591
+ // and return string
36592
+ if (payload === null)
36593
+ return undefined;
36594
+ try {
36595
+ return JSON.parse(payload);
36596
+ }
36597
+ catch (error) {
36598
+ // Assumes it's a string.
36599
+ return payload;
36600
+ }
36601
+ };
36602
+ const serialiseItem = (payload) => {
36603
+ if (typeof payload === 'string') {
36604
+ return payload;
36605
+ }
36606
+ return JSON.stringify(payload);
36607
+ };
36608
+ /**
36609
+ * GenKey will take into account the namespace
36610
+ * as well as if being run in the Link, it will tap into the link
36611
+ * @param {string} key
36612
+ * @returns key
36613
+ */
36614
+ const genKey = (key) => `${localStoragePrefix}${key}`;
36615
+ function getItem(key) {
36616
+ if (hasLocalstorage()) {
36617
+ return parseItem(window.localStorage.getItem(genKey(key)));
36618
+ }
36619
+ return undefined;
36620
+ }
36621
+ const setItem = (key, payload) => {
36622
+ if (hasLocalstorage()) {
36623
+ window.localStorage.setItem(genKey(key), serialiseItem(payload));
36624
+ return true;
36625
+ }
36626
+ return false;
36627
+ };
36628
+
36629
+ // In memory storage for events and other data
36630
+ let EVENT_STORE;
36631
+ let RUNTIME_DETAILS;
36632
+ // Initialise store and runtime
36633
+ const initialise$1 = () => {
36634
+ EVENT_STORE = getItem(Store.EVENTS) || [];
36635
+ RUNTIME_DETAILS = getItem(Store.RUNTIME) || {};
36636
+ };
36637
+ initialise$1();
36638
+ // Runtime Details
36639
+ const storeDetail = (key, value) => {
36640
+ RUNTIME_DETAILS = {
36641
+ ...RUNTIME_DETAILS,
36642
+ [key]: value,
36643
+ };
36644
+ setItem(Store.RUNTIME, RUNTIME_DETAILS);
36645
+ };
36646
+ const getDetail = (key) => {
36647
+ // Handle the scenario where detail is a falsy value
36648
+ if (RUNTIME_DETAILS[key] === undefined) {
36649
+ return undefined;
36650
+ }
36651
+ return RUNTIME_DETAILS[key];
36652
+ };
36653
+ const getAllDetails = () => RUNTIME_DETAILS;
36654
+ // Events
36655
+ const getEvents = () => EVENT_STORE;
36656
+ const removeSentEvents = (numberOfEvents) => {
36657
+ EVENT_STORE = EVENT_STORE.slice(numberOfEvents);
36658
+ setItem(Store.EVENTS, EVENT_STORE);
36659
+ };
36660
+ const flattenProperties = (properties) => {
36661
+ const propertyMap = [];
36662
+ Object.entries(properties).forEach(([key, value]) => {
36663
+ if (typeof value === 'string' || typeof value === 'number') {
36664
+ propertyMap.push([key, value.toString()]);
36665
+ }
36666
+ });
36667
+ return propertyMap;
36668
+ };
36669
+
36670
+ let initialised = false;
36671
+ const isInitialised = () => initialised;
36672
+ const getReferrer = () => {
36673
+ if (isNode()) {
36674
+ return '';
36675
+ }
36676
+ if (window.location.ancestorOrigins
36677
+ && window.location.ancestorOrigins.length > 0) {
36678
+ return new URL(window.location.ancestorOrigins[0]).hostname;
36679
+ }
36680
+ return document.referrer ? new URL(window.document.referrer).hostname : '';
36681
+ };
36682
+ const runtimeHost = () => {
36683
+ if (isNode()) {
36684
+ return '';
36685
+ }
36686
+ if (window.opener || window.parent !== window) {
36687
+ return getReferrer();
36688
+ }
36689
+ return new URL(window.location.href).hostname;
36690
+ };
36691
+ const getRuntimeDetails = () => {
36692
+ if (isNode()) {
36693
+ return { browser: 'nodejs' };
36694
+ }
36695
+ const domain = runtimeHost();
36696
+ if (domain) {
36697
+ storeDetail(Detail.DOMAIN, domain);
36698
+ }
36699
+ return {
36700
+ browser: window.navigator.userAgent,
36701
+ domain,
36702
+ tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
36703
+ screen: `${window.screen.width}x${window.screen.height}`,
36704
+ };
36705
+ };
36706
+ const initialise = async () => {
36707
+ const runtimeDetails = flattenProperties(getRuntimeDetails());
36708
+ // Get any existing runtimeId and send it along with the initialise
36709
+ const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
36710
+ const body = {
36711
+ version: 1,
36712
+ data: {
36713
+ runtimeDetails,
36714
+ runtimeId: existingRuntimeId,
36715
+ },
36716
+ };
36717
+ const response = await post('/v1/sdk/initialise', body);
36718
+ // Get runtimeId and store it
36719
+ const { runtimeId } = response;
36720
+ storeDetail(Detail.RUNTIME_ID, runtimeId);
36721
+ initialised = true;
36722
+ };
36723
+
36724
+ function errorBoundary(fn) {
36725
+ const wrappedFunction = ((...args) => {
36726
+ try {
36727
+ // Execute the original function
36728
+ const result = fn(...args);
36729
+ if (result instanceof Promise) {
36730
+ // Silent fail for now, in future
36731
+ // we can send errors to a logging service
36732
+ return result.catch(() => undefined);
36733
+ }
36734
+ return result;
36735
+ }
36736
+ catch (error) {
36737
+ // As above, fail silently for now
36738
+ return undefined;
36739
+ }
36740
+ });
36741
+ return wrappedFunction;
36742
+ }
36743
+
36744
+ function isTestEnvironment() {
36745
+ if (typeof process === 'undefined') {
36746
+ return false;
36747
+ }
36748
+ return process.env.JEST_WORKER_ID !== undefined;
36749
+ }
36750
+ // Sending events to the server
36751
+ const flushFn = async () => {
36752
+ // Don't flush if not initialised
36753
+ if (isInitialised() === false) {
36754
+ await initialise();
36755
+ return;
36756
+ }
36757
+ const events = getEvents();
36758
+ if (events.length === 0) {
36759
+ return;
36760
+ }
36761
+ // Track events length here, incase
36762
+ const numEvents = events.length;
36763
+ // Get details and send it with the track request
36764
+ const details = getAllDetails();
36765
+ const metricsPayload = {
36766
+ version: 1,
36767
+ data: {
36768
+ events,
36769
+ details,
36770
+ },
36771
+ };
36772
+ const response = await post('/v1/sdk/metrics', metricsPayload);
36773
+ if (response instanceof Error) {
36774
+ return;
36775
+ }
36776
+ // Clear events if successfully posted
36777
+ removeSentEvents(numEvents);
36778
+ };
36779
+ const flush = errorBoundary(flushFn);
36780
+ // Flush events every 5 seconds
36781
+ const flushPoll = async () => {
36782
+ await flush();
36783
+ setTimeout(flushPoll, POLLING_FREQUENCY);
36784
+ };
36785
+ // This will get initialised when module is imported.
36786
+ if (!isTestEnvironment()) {
36787
+ errorBoundary(flushPoll)();
36788
+ }
36789
+
36287
36790
  var Environment;
36288
36791
  (function (Environment) {
36289
36792
  Environment["PRODUCTION"] = "production";
@@ -41263,7 +41766,7 @@ const IMMUTABLE_ZKVEM_GAS_OVERRIDES = {
41263
41766
  maxPriorityFeePerGas: BigNumber$1.from(10e9),
41264
41767
  };
41265
41768
 
41266
- const SDK_VERSION_MARKER = '0.45.4';
41769
+ const SDK_VERSION_MARKER = '0.45.5';
41267
41770
  // This SDK version is replaced by the `yarn build` command ran on the root level
41268
41771
  const globalPackageVersion = () => SDK_VERSION_MARKER;
41269
41772
 
@@ -41468,7 +41971,7 @@ async function getERC20TokenInfo(web3Provider, tokenAddress) {
41468
41971
  }, { type: CheckoutErrorType.GET_ERC20_INFO_ERROR });
41469
41972
  }
41470
41973
 
41471
- const CACHE_DATA_TTL = 60; // seconds
41974
+ const CACHE_DATA_TTL = 5; // seconds
41472
41975
  /**
41473
41976
  * Blockscout class provides a client abstraction for the Immutable 3rd party indexer.
41474
41977
  */
@@ -21,9 +21,9 @@ type ImmutableConfigurationWithRequireableFields<T> = ImmutableConfiguration & (
21
21
  } ? Required<{
22
22
  publishableKey: string;
23
23
  }> : {});
24
- type ImmutableConfigurationWithOmitableFields<T> = (T extends {
24
+ type ImmutableConfigurationWithOmitableFields<T> = T extends {
25
25
  apiKey: 'omit';
26
- } ? Omit<ImmutableConfigurationWithRequireableFields<T>, 'apiKey'> : ImmutableConfigurationWithRequireableFields<T>);
26
+ } ? Omit<ImmutableConfigurationWithRequireableFields<T>, 'apiKey'> : ImmutableConfigurationWithRequireableFields<T>;
27
27
  interface ModuleConfiguration<T> {
28
28
  baseConfig: ImmutableConfigurationWithOmitableFields<T>;
29
29
  overrides?: T;