@imtbl/sdk 1.5.1 → 1.5.3-alpha

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/checkout.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import globalAxios, { HttpStatusCode as HttpStatusCode$2 } from 'axios';
2
+ import { memorise } from 'lru-memorise';
3
+ import { getGlobalisedValue as getGlobalisedValue$3 } from 'global-const';
2
4
  import { ethers, ContractFactory, utils as utils$2, Contract as Contract$1, BigNumber as BigNumber$1, providers, constants as constants$1 } from 'ethers';
3
5
  import { Web3Provider, JsonRpcProvider } from '@ethersproject/providers';
4
6
  import detectEthereumProvider from '@metamask/detect-provider';
@@ -24,12 +26,6 @@ var Detail$1;
24
26
  Detail["DOMAIN"] = "domain";
25
27
  Detail["SDK_VERSION"] = "sdkVersion";
26
28
  })(Detail$1 || (Detail$1 = {}));
27
- var Store$1;
28
- (function (Store) {
29
- Store["EVENTS"] = "events";
30
- Store["RUNTIME"] = "runtime";
31
- })(Store$1 || (Store$1 = {}));
32
- const POLLING_FREQUENCY$1 = 5000;
33
29
 
34
30
  const IMTBL_API$1 = 'https://api.x.immutable.com';
35
31
  async function post$1(path, data) {
@@ -89,6 +85,11 @@ const setItem$1 = (key, payload) => {
89
85
  return false;
90
86
  };
91
87
 
88
+ var Store$1;
89
+ (function (Store) {
90
+ Store["EVENTS"] = "events";
91
+ Store["RUNTIME"] = "runtime";
92
+ })(Store$1 || (Store$1 = {}));
92
93
  // In memory storage for events and other data
93
94
  let EVENT_STORE$1;
94
95
  let RUNTIME_DETAILS$1;
@@ -116,7 +117,7 @@ const getDetail$1 = (key) => {
116
117
  const getAllDetails$1 = () => RUNTIME_DETAILS$1;
117
118
  // Events
118
119
  const getEvents$1 = () => EVENT_STORE$1;
119
- const addEvent = (event) => {
120
+ const addEvent$1 = (event) => {
120
121
  EVENT_STORE$1.push(event);
121
122
  setItem$1(Store$1.EVENTS, EVENT_STORE$1);
122
123
  };
@@ -138,9 +139,7 @@ const flattenProperties$1 = (properties) => {
138
139
  };
139
140
 
140
141
  // WARNING: DO NOT CHANGE THE STRING BELOW. IT GETS REPLACED AT BUILD TIME.
141
- const SDK_VERSION$1 = '1.5.1';
142
- let initialised$1 = false;
143
- const isInitialised$1 = () => initialised$1;
142
+ const SDK_VERSION$1 = '1.5.3-alpha';
144
143
  const getReferrer$1 = () => {
145
144
  if (isNode$1()) {
146
145
  return '';
@@ -177,21 +176,28 @@ const getRuntimeDetails$1 = () => {
177
176
  screen: `${window.screen.width}x${window.screen.height}`,
178
177
  };
179
178
  };
179
+ let initialised$1 = false;
180
+ const isInitialised$1 = () => initialised$1;
180
181
  const initialise$2 = async () => {
181
- const runtimeDetails = flattenProperties$1(getRuntimeDetails$1());
182
- const existingRuntimeId = getDetail$1(Detail$1.RUNTIME_ID);
183
- const body = {
184
- version: 1,
185
- data: {
186
- runtimeDetails,
187
- runtimeId: existingRuntimeId,
188
- },
189
- };
190
- const response = await post$1('/v1/sdk/initialise', body);
191
- // Get runtimeId and store it
192
- const { runtimeId } = response;
193
- storeDetail$1(Detail$1.RUNTIME_ID, runtimeId);
194
182
  initialised$1 = true;
183
+ try {
184
+ const runtimeDetails = flattenProperties$1(getRuntimeDetails$1());
185
+ const existingRuntimeId = getDetail$1(Detail$1.RUNTIME_ID);
186
+ const body = {
187
+ version: 1,
188
+ data: {
189
+ runtimeDetails,
190
+ runtimeId: existingRuntimeId,
191
+ },
192
+ };
193
+ const response = await post$1('/v1/sdk/initialise', body);
194
+ // Get runtimeId and store it
195
+ const { runtimeId } = response;
196
+ storeDetail$1(Detail$1.RUNTIME_ID, runtimeId);
197
+ }
198
+ catch (error) {
199
+ initialised$1 = false;
200
+ }
195
201
  };
196
202
 
197
203
  function errorBoundary$1(fn) {
@@ -223,16 +229,31 @@ function isTestEnvironment$1() {
223
229
  return process.env.JEST_WORKER_ID !== undefined;
224
230
  }
225
231
 
232
+ const GLOBALISE_KEY$1 = 'imtbl__metrics';
233
+ const MEMORISE_TIMEFRAME$1 = 1000;
234
+ const MEMORISE_MAX$1 = 1000;
235
+ const getGlobalisedValue$2 = (key, value) => getGlobalisedValue$3(GLOBALISE_KEY$1, key, value);
236
+ const getGlobalisedCachedFunction$1 = (key, fn) => {
237
+ // Some applications (esp backend, or frontends using the split bundles) can sometimes
238
+ // initialise the same request multiple times. This will prevent multiple of the
239
+ // same event,value from being reported in a 1 second period.
240
+ const memorisedFn = memorise(fn, {
241
+ lruOptions: { ttl: MEMORISE_TIMEFRAME$1, max: MEMORISE_MAX$1 },
242
+ });
243
+ return getGlobalisedValue$3(GLOBALISE_KEY$1, key, memorisedFn);
244
+ };
245
+
246
+ const POLLING_FREQUENCY$1 = 5000;
226
247
  // Store the event in the event store
227
- const trackFn = (moduleName, eventName, properties) => {
248
+ const trackFn$1 = (moduleName, eventName, properties) => {
228
249
  const event = {
229
250
  event: `${moduleName}.${eventName}`,
230
251
  time: new Date().toISOString(),
231
252
  ...(properties && { properties: flattenProperties$1(properties) }),
232
253
  };
233
- addEvent(event);
254
+ addEvent$1(event);
234
255
  };
235
- const track = errorBoundary$1(trackFn);
256
+ const track = errorBoundary$1(getGlobalisedCachedFunction$1('track', trackFn$1));
236
257
  // Sending events to the server
237
258
  const flushFn$1 = async () => {
238
259
  // Don't flush if not initialised
@@ -268,15 +289,31 @@ const flushPoll$1 = async () => {
268
289
  await flush$1();
269
290
  setTimeout(flushPoll$1, POLLING_FREQUENCY$1);
270
291
  };
292
+ let flushingStarted$1 = false;
293
+ const startFlushing$1 = () => {
294
+ if (flushingStarted$1) {
295
+ return;
296
+ }
297
+ flushingStarted$1 = true;
298
+ flushPoll$1();
299
+ };
271
300
  // This will get initialised when module is imported.
272
301
  if (!isTestEnvironment$1()) {
273
- errorBoundary$1(flushPoll$1)();
302
+ errorBoundary$1(getGlobalisedValue$2('startFlushing', startFlushing$1))();
274
303
  }
275
304
 
276
- const setEnvironmentFn = (env) => {
305
+ const setEnvironmentFn$1 = (env) => {
277
306
  storeDetail$1(Detail$1.ENVIRONMENT, env);
278
307
  };
279
- const setEnvironment = errorBoundary$1(setEnvironmentFn);
308
+ const setEnvironment = errorBoundary$1(getGlobalisedValue$2('setEnvironment', setEnvironmentFn$1));
309
+ const setPassportClientIdFn$1 = (passportClientId) => {
310
+ storeDetail$1(Detail$1.PASSPORT_CLIENT_ID, passportClientId);
311
+ };
312
+ errorBoundary$1(getGlobalisedValue$2('setPassportClientId', setPassportClientIdFn$1));
313
+ const setPublishableApiKeyFn$1 = (publishableApiKey) => {
314
+ storeDetail$1(Detail$1.PUBLISHABLE_API_KEY, publishableApiKey);
315
+ };
316
+ errorBoundary$1(getGlobalisedValue$2('setPublishableApiKey', setPublishableApiKeyFn$1));
280
317
 
281
318
  var Environment$1;
282
319
  (function (Environment) {
@@ -16965,6 +17002,356 @@ class TokenBridge {
16965
17002
  }
16966
17003
  }
16967
17004
 
17005
+ var lib$1 = {};
17006
+
17007
+ var tinyLru = {};
17008
+
17009
+ /**
17010
+ * tiny-lru
17011
+ *
17012
+ * @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
17013
+ * @license BSD-3-Clause
17014
+ * @version 11.2.5
17015
+ */
17016
+
17017
+ class LRU {
17018
+ constructor (max = 0, ttl = 0, resetTtl = false) {
17019
+ this.first = null;
17020
+ this.items = Object.create(null);
17021
+ this.last = null;
17022
+ this.max = max;
17023
+ this.resetTtl = resetTtl;
17024
+ this.size = 0;
17025
+ this.ttl = ttl;
17026
+ }
17027
+
17028
+ clear () {
17029
+ this.first = null;
17030
+ this.items = Object.create(null);
17031
+ this.last = null;
17032
+ this.size = 0;
17033
+
17034
+ return this;
17035
+ }
17036
+
17037
+ delete (key) {
17038
+ if (this.has(key)) {
17039
+ const item = this.items[key];
17040
+
17041
+ delete this.items[key];
17042
+ this.size--;
17043
+
17044
+ if (item.prev !== null) {
17045
+ item.prev.next = item.next;
17046
+ }
17047
+
17048
+ if (item.next !== null) {
17049
+ item.next.prev = item.prev;
17050
+ }
17051
+
17052
+ if (this.first === item) {
17053
+ this.first = item.next;
17054
+ }
17055
+
17056
+ if (this.last === item) {
17057
+ this.last = item.prev;
17058
+ }
17059
+ }
17060
+
17061
+ return this;
17062
+ }
17063
+
17064
+ entries (keys = this.keys()) {
17065
+ return keys.map(key => [key, this.get(key)]);
17066
+ }
17067
+
17068
+ evict (bypass = false) {
17069
+ if (bypass || this.size > 0) {
17070
+ const item = this.first;
17071
+
17072
+ delete this.items[item.key];
17073
+
17074
+ if (--this.size === 0) {
17075
+ this.first = null;
17076
+ this.last = null;
17077
+ } else {
17078
+ this.first = item.next;
17079
+ this.first.prev = null;
17080
+ }
17081
+ }
17082
+
17083
+ return this;
17084
+ }
17085
+
17086
+ expiresAt (key) {
17087
+ let result;
17088
+
17089
+ if (this.has(key)) {
17090
+ result = this.items[key].expiry;
17091
+ }
17092
+
17093
+ return result;
17094
+ }
17095
+
17096
+ get (key) {
17097
+ let result;
17098
+
17099
+ if (this.has(key)) {
17100
+ const item = this.items[key];
17101
+
17102
+ if (this.ttl > 0 && item.expiry <= Date.now()) {
17103
+ this.delete(key);
17104
+ } else {
17105
+ result = item.value;
17106
+ this.set(key, result, true);
17107
+ }
17108
+ }
17109
+
17110
+ return result;
17111
+ }
17112
+
17113
+ has (key) {
17114
+ return key in this.items;
17115
+ }
17116
+
17117
+ keys () {
17118
+ const result = [];
17119
+ let x = this.first;
17120
+
17121
+ while (x !== null) {
17122
+ result.push(x.key);
17123
+ x = x.next;
17124
+ }
17125
+
17126
+ return result;
17127
+ }
17128
+
17129
+ set (key, value, bypass = false, resetTtl = this.resetTtl) {
17130
+ let item;
17131
+
17132
+ if (bypass || this.has(key)) {
17133
+ item = this.items[key];
17134
+ item.value = value;
17135
+
17136
+ if (bypass === false && resetTtl) {
17137
+ item.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;
17138
+ }
17139
+
17140
+ if (this.last !== item) {
17141
+ const last = this.last,
17142
+ next = item.next,
17143
+ prev = item.prev;
17144
+
17145
+ if (this.first === item) {
17146
+ this.first = item.next;
17147
+ }
17148
+
17149
+ item.next = null;
17150
+ item.prev = this.last;
17151
+ last.next = item;
17152
+
17153
+ if (prev !== null) {
17154
+ prev.next = next;
17155
+ }
17156
+
17157
+ if (next !== null) {
17158
+ next.prev = prev;
17159
+ }
17160
+ }
17161
+ } else {
17162
+ if (this.max > 0 && this.size === this.max) {
17163
+ this.evict(true);
17164
+ }
17165
+
17166
+ item = this.items[key] = {
17167
+ expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl,
17168
+ key: key,
17169
+ prev: this.last,
17170
+ next: null,
17171
+ value
17172
+ };
17173
+
17174
+ if (++this.size === 1) {
17175
+ this.first = item;
17176
+ } else {
17177
+ this.last.next = item;
17178
+ }
17179
+ }
17180
+
17181
+ this.last = item;
17182
+
17183
+ return this;
17184
+ }
17185
+
17186
+ values (keys = this.keys()) {
17187
+ return keys.map(key => this.get(key));
17188
+ }
17189
+ }
17190
+
17191
+ function lru (max = 1000, ttl = 0, resetTtl = false) {
17192
+ if (isNaN(max) || max < 0) {
17193
+ throw new TypeError("Invalid max value");
17194
+ }
17195
+
17196
+ if (isNaN(ttl) || ttl < 0) {
17197
+ throw new TypeError("Invalid ttl value");
17198
+ }
17199
+
17200
+ if (typeof resetTtl !== "boolean") {
17201
+ throw new TypeError("Invalid resetTtl value");
17202
+ }
17203
+
17204
+ return new LRU(max, ttl, resetTtl);
17205
+ }
17206
+
17207
+ tinyLru.LRU = LRU;
17208
+ tinyLru.lru = lru;
17209
+
17210
+ (function (exports) {
17211
+ Object.defineProperty(exports, "__esModule", { value: true });
17212
+ exports.memorise = void 0;
17213
+ const tiny_lru_1 = tinyLru;
17214
+ const defaultLRUOptions = {
17215
+ max: 1000,
17216
+ };
17217
+ /**
17218
+ * Returns a memorised version of the target function.
17219
+ * The cache key depends on the arguments passed to the function.
17220
+ *
17221
+ * Pass in options to customise the caching behaviour
17222
+ * @param cachedFn Function to cache
17223
+ * @param options Memorization and LRUCache Options
17224
+ */
17225
+ const memorise = (cachedFn, options = {}) => {
17226
+ // Extract defaults
17227
+ const { cache, cacheKeyResolver = defaultGenCacheKey, onHit, lruOptions = {}, } = options;
17228
+ const cacheOptions = { ...defaultLRUOptions, ...lruOptions };
17229
+ const _cache = (cache ||
17230
+ (0, tiny_lru_1.lru)(cacheOptions.max, cacheOptions.ttl));
17231
+ // Cached fn
17232
+ const returnFn = (...args) => {
17233
+ const cacheKey = cacheKeyResolver(...args);
17234
+ const cachedValue = _cache.get(cacheKey);
17235
+ const keyCached = _cache.has(cacheKey);
17236
+ // Use cached key, even if value is void
17237
+ if (keyCached) {
17238
+ // If onHit handler is there, run it
17239
+ if (onHit) {
17240
+ onHit(cacheKey, cachedValue, _cache);
17241
+ }
17242
+ return cachedValue;
17243
+ }
17244
+ // No cache hit, run function and cache result
17245
+ const result = cachedFn.apply(this, args);
17246
+ _cache.set(cacheKey, result);
17247
+ return result;
17248
+ };
17249
+ // Expose the cache
17250
+ returnFn._cache = _cache;
17251
+ return returnFn;
17252
+ };
17253
+ exports.memorise = memorise;
17254
+ // Default export
17255
+ exports.default = exports.memorise;
17256
+ /**
17257
+ * Generic args handler function.
17258
+ * @param args Argument array
17259
+ */
17260
+ const defaultGenCacheKey = (...args) => {
17261
+ if (args.length === 0) {
17262
+ return "no-args";
17263
+ }
17264
+ return args
17265
+ .map((val) => {
17266
+ if (val === undefined) {
17267
+ return "undefined";
17268
+ }
17269
+ if (val === null) {
17270
+ return "null";
17271
+ }
17272
+ if (Array.isArray(val)) {
17273
+ return `[${defaultGenCacheKey(...val)}]`;
17274
+ }
17275
+ if (typeof val === "object") {
17276
+ return `{${defaultGenCacheKey(...sortedObjectEntries(val))}}`;
17277
+ }
17278
+ return JSON.stringify(val);
17279
+ })
17280
+ .join(",");
17281
+ };
17282
+ const sortedObjectEntries = (obj) => {
17283
+ return Object.entries(obj).sort((a, b) => {
17284
+ if (a[0] < b[0]) {
17285
+ return -1;
17286
+ }
17287
+ return 1;
17288
+ });
17289
+ };
17290
+
17291
+ } (lib$1));
17292
+
17293
+ var lib = {};
17294
+
17295
+ Object.defineProperty(lib, "__esModule", { value: true });
17296
+ lib.clearGlobalNamespace = getGlobalisedValue_1 = lib.getGlobalisedValue = void 0;
17297
+ const GLOBALISE_KEY_PREFIX = "globalise__singleton__";
17298
+ const fallbackGlobal = {};
17299
+ const getGlobalObject = () => {
17300
+ if (typeof window !== "undefined") {
17301
+ return window;
17302
+ }
17303
+ if (typeof globalThis !== "undefined") {
17304
+ return globalThis;
17305
+ }
17306
+ return fallbackGlobal;
17307
+ };
17308
+ const validateInputs = (namespace, key) => {
17309
+ if (typeof namespace !== "string") {
17310
+ throw "Invalid namespace key";
17311
+ }
17312
+ if (typeof key !== "string") {
17313
+ throw "Invalid item key";
17314
+ }
17315
+ };
17316
+ const createGlobalisedKey = (namespace) => {
17317
+ return `${GLOBALISE_KEY_PREFIX}${namespace}`;
17318
+ };
17319
+ const getGlobalScopedObject = (namespace) => {
17320
+ const globalObject = getGlobalObject();
17321
+ const GLOBALISE_KEY = createGlobalisedKey(namespace);
17322
+ // Initialise global object
17323
+ if (!globalObject[GLOBALISE_KEY]) {
17324
+ globalObject[GLOBALISE_KEY] = {};
17325
+ }
17326
+ return globalObject[GLOBALISE_KEY];
17327
+ };
17328
+ const getSingleton = (namespace, key) => {
17329
+ const scopedObject = getGlobalScopedObject(namespace);
17330
+ return scopedObject[key] || undefined;
17331
+ };
17332
+ const setSingleton = (namespace, key, value) => {
17333
+ const scopedObject = getGlobalScopedObject(namespace);
17334
+ scopedObject[key] = value;
17335
+ };
17336
+ const getGlobalisedValue$1 = (namespace, key, value) => {
17337
+ validateInputs(namespace, key);
17338
+ const existing = getSingleton(namespace, key);
17339
+ if (existing !== undefined) {
17340
+ return existing;
17341
+ }
17342
+ setSingleton(namespace, key, value);
17343
+ return value;
17344
+ };
17345
+ var getGlobalisedValue_1 = lib.getGlobalisedValue = getGlobalisedValue$1;
17346
+ const clearGlobalNamespace = (namespace) => {
17347
+ const globalObject = getGlobalObject();
17348
+ const globalisedKey = createGlobalisedKey(namespace);
17349
+ if (globalObject[globalisedKey] !== undefined) {
17350
+ delete globalObject[globalisedKey];
17351
+ }
17352
+ };
17353
+ lib.clearGlobalNamespace = clearGlobalNamespace;
17354
+
16968
17355
  const isNode = () => typeof window === 'undefined';
16969
17356
  const isBrowser = () => !isNode();
16970
17357
 
@@ -16978,12 +17365,6 @@ var Detail;
16978
17365
  Detail["DOMAIN"] = "domain";
16979
17366
  Detail["SDK_VERSION"] = "sdkVersion";
16980
17367
  })(Detail || (Detail = {}));
16981
- var Store;
16982
- (function (Store) {
16983
- Store["EVENTS"] = "events";
16984
- Store["RUNTIME"] = "runtime";
16985
- })(Store || (Store = {}));
16986
- const POLLING_FREQUENCY = 5000;
16987
17368
 
16988
17369
  const IMTBL_API = 'https://api.x.immutable.com';
16989
17370
  async function post(path, data) {
@@ -17043,6 +17424,11 @@ const setItem = (key, payload) => {
17043
17424
  return false;
17044
17425
  };
17045
17426
 
17427
+ var Store;
17428
+ (function (Store) {
17429
+ Store["EVENTS"] = "events";
17430
+ Store["RUNTIME"] = "runtime";
17431
+ })(Store || (Store = {}));
17046
17432
  // In memory storage for events and other data
17047
17433
  let EVENT_STORE;
17048
17434
  let RUNTIME_DETAILS;
@@ -17070,6 +17456,10 @@ const getDetail = (key) => {
17070
17456
  const getAllDetails = () => RUNTIME_DETAILS;
17071
17457
  // Events
17072
17458
  const getEvents = () => EVENT_STORE;
17459
+ const addEvent = (event) => {
17460
+ EVENT_STORE.push(event);
17461
+ setItem(Store.EVENTS, EVENT_STORE);
17462
+ };
17073
17463
  const removeSentEvents = (numberOfEvents) => {
17074
17464
  EVENT_STORE = EVENT_STORE.slice(numberOfEvents);
17075
17465
  setItem(Store.EVENTS, EVENT_STORE);
@@ -17088,9 +17478,7 @@ const flattenProperties = (properties) => {
17088
17478
  };
17089
17479
 
17090
17480
  // WARNING: DO NOT CHANGE THE STRING BELOW. IT GETS REPLACED AT BUILD TIME.
17091
- const SDK_VERSION = '1.5.1';
17092
- let initialised = false;
17093
- const isInitialised = () => initialised;
17481
+ const SDK_VERSION = '1.5.3-alpha';
17094
17482
  const getReferrer = () => {
17095
17483
  if (isNode()) {
17096
17484
  return '';
@@ -17127,21 +17515,28 @@ const getRuntimeDetails = () => {
17127
17515
  screen: `${window.screen.width}x${window.screen.height}`,
17128
17516
  };
17129
17517
  };
17518
+ let initialised = false;
17519
+ const isInitialised = () => initialised;
17130
17520
  const initialise = async () => {
17131
- const runtimeDetails = flattenProperties(getRuntimeDetails());
17132
- const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
17133
- const body = {
17134
- version: 1,
17135
- data: {
17136
- runtimeDetails,
17137
- runtimeId: existingRuntimeId,
17138
- },
17139
- };
17140
- const response = await post('/v1/sdk/initialise', body);
17141
- // Get runtimeId and store it
17142
- const { runtimeId } = response;
17143
- storeDetail(Detail.RUNTIME_ID, runtimeId);
17144
17521
  initialised = true;
17522
+ try {
17523
+ const runtimeDetails = flattenProperties(getRuntimeDetails());
17524
+ const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
17525
+ const body = {
17526
+ version: 1,
17527
+ data: {
17528
+ runtimeDetails,
17529
+ runtimeId: existingRuntimeId,
17530
+ },
17531
+ };
17532
+ const response = await post('/v1/sdk/initialise', body);
17533
+ // Get runtimeId and store it
17534
+ const { runtimeId } = response;
17535
+ storeDetail(Detail.RUNTIME_ID, runtimeId);
17536
+ }
17537
+ catch (error) {
17538
+ initialised = false;
17539
+ }
17145
17540
  };
17146
17541
 
17147
17542
  function errorBoundary(fn) {
@@ -17172,6 +17567,32 @@ function isTestEnvironment() {
17172
17567
  // Just use process.env.CI for now.
17173
17568
  return process.env.JEST_WORKER_ID !== undefined;
17174
17569
  }
17570
+
17571
+ const GLOBALISE_KEY = 'imtbl__metrics';
17572
+ const MEMORISE_TIMEFRAME = 1000;
17573
+ const MEMORISE_MAX = 1000;
17574
+ const getGlobalisedValue = (key, value) => getGlobalisedValue_1(GLOBALISE_KEY, key, value);
17575
+ const getGlobalisedCachedFunction = (key, fn) => {
17576
+ // Some applications (esp backend, or frontends using the split bundles) can sometimes
17577
+ // initialise the same request multiple times. This will prevent multiple of the
17578
+ // same event,value from being reported in a 1 second period.
17579
+ const memorisedFn = lib$1.memorise(fn, {
17580
+ lruOptions: { ttl: MEMORISE_TIMEFRAME, max: MEMORISE_MAX },
17581
+ });
17582
+ return getGlobalisedValue_1(GLOBALISE_KEY, key, memorisedFn);
17583
+ };
17584
+
17585
+ const POLLING_FREQUENCY = 5000;
17586
+ // Store the event in the event store
17587
+ const trackFn = (moduleName, eventName, properties) => {
17588
+ const event = {
17589
+ event: `${moduleName}.${eventName}`,
17590
+ time: new Date().toISOString(),
17591
+ ...(properties && { properties: flattenProperties(properties) }),
17592
+ };
17593
+ addEvent(event);
17594
+ };
17595
+ errorBoundary(getGlobalisedCachedFunction('track', trackFn));
17175
17596
  // Sending events to the server
17176
17597
  const flushFn = async () => {
17177
17598
  // Don't flush if not initialised
@@ -17207,11 +17628,32 @@ const flushPoll = async () => {
17207
17628
  await flush();
17208
17629
  setTimeout(flushPoll, POLLING_FREQUENCY);
17209
17630
  };
17631
+ let flushingStarted = false;
17632
+ const startFlushing = () => {
17633
+ if (flushingStarted) {
17634
+ return;
17635
+ }
17636
+ flushingStarted = true;
17637
+ flushPoll();
17638
+ };
17210
17639
  // This will get initialised when module is imported.
17211
17640
  if (!isTestEnvironment()) {
17212
- errorBoundary(flushPoll)();
17641
+ errorBoundary(getGlobalisedValue('startFlushing', startFlushing))();
17213
17642
  }
17214
17643
 
17644
+ const setEnvironmentFn = (env) => {
17645
+ storeDetail(Detail.ENVIRONMENT, env);
17646
+ };
17647
+ errorBoundary(getGlobalisedValue('setEnvironment', setEnvironmentFn));
17648
+ const setPassportClientIdFn = (passportClientId) => {
17649
+ storeDetail(Detail.PASSPORT_CLIENT_ID, passportClientId);
17650
+ };
17651
+ errorBoundary(getGlobalisedValue('setPassportClientId', setPassportClientIdFn));
17652
+ const setPublishableApiKeyFn = (publishableApiKey) => {
17653
+ storeDetail(Detail.PUBLISHABLE_API_KEY, publishableApiKey);
17654
+ };
17655
+ errorBoundary(getGlobalisedValue('setPublishableApiKey', setPublishableApiKeyFn));
17656
+
17215
17657
  var Environment;
17216
17658
  (function (Environment) {
17217
17659
  Environment["PRODUCTION"] = "production";
@@ -19907,6 +20349,9 @@ const MIN_MAX_HOPS = 1;
19907
20349
  const BASIS_POINT_PRECISION = 10000;
19908
20350
  // 10% maximum secondary fee
19909
20351
  const MAX_SECONDARY_FEE_BASIS_POINTS = 1000;
20352
+ // How much more gas does a swap with secondary fees cost?
20353
+ // This number is the average difference of a number of different swaps with different hop amounts, etc.
20354
+ const AVERAGE_SECONDARY_FEE_EXTRA_GAS = 50876;
19910
20355
 
19911
20356
  const quoteReturnMapping = {
19912
20357
  // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -20327,6 +20772,59 @@ startTokenIn = tokenIn) => {
20327
20772
  return routes;
20328
20773
  };
20329
20774
 
20775
+ const IMMUTABLE_TESTNET_RPC_URL = 'https://rpc.testnet.immutable.com';
20776
+ const IMMUTABLE_TESTNET_CHAIN_ID = 13473;
20777
+
20778
+ const IMMUTABLE_MAINNET_RPC_URL = 'https://rpc.immutable.com';
20779
+ const IMMUTABLE_MAINNET_CHAIN_ID = 13371;
20780
+
20781
+ const NATIVE_IMX_IMMUTABLE_TESTNET = {
20782
+ chainId: IMMUTABLE_TESTNET_CHAIN_ID,
20783
+ decimals: 18,
20784
+ symbol: 'tIMX',
20785
+ name: 'Immutable Testnet Token',
20786
+ type: 'native',
20787
+ };
20788
+ const WIMX_IMMUTABLE_TESTNET = {
20789
+ chainId: IMMUTABLE_TESTNET_CHAIN_ID,
20790
+ address: '0x1CcCa691501174B4A623CeDA58cC8f1a76dc3439',
20791
+ decimals: 18,
20792
+ symbol: 'WIMX',
20793
+ name: 'Wrapped Immutable Testnet Token',
20794
+ type: 'erc20',
20795
+ };
20796
+ const IMMUTABLE_TESTNET_COMMON_ROUTING_TOKENS = [
20797
+ WIMX_IMMUTABLE_TESTNET,
20798
+ ];
20799
+
20800
+ const NATIVE_IMX_IMMUTABLE_MAINNET = {
20801
+ chainId: IMMUTABLE_MAINNET_CHAIN_ID,
20802
+ decimals: 18,
20803
+ symbol: 'IMX',
20804
+ name: 'Immutable Token',
20805
+ type: 'native',
20806
+ };
20807
+ const WIMX_IMMUTABLE_MAINNET = {
20808
+ chainId: IMMUTABLE_MAINNET_CHAIN_ID,
20809
+ address: '0x3a0c2ba54d6cbd3121f01b96dfd20e99d1696c9d',
20810
+ decimals: 18,
20811
+ symbol: 'WIMX',
20812
+ name: 'Wrapped Immutable Token',
20813
+ type: 'erc20',
20814
+ };
20815
+ const ETH_IMMUTABLE_MAINNET = {
20816
+ chainId: IMMUTABLE_MAINNET_CHAIN_ID,
20817
+ address: '0x52a6c53869ce09a731cd772f245b97a4401d3348',
20818
+ decimals: 18,
20819
+ symbol: 'ETH',
20820
+ name: 'Ethereum',
20821
+ type: 'erc20',
20822
+ };
20823
+ const IMMUTABLE_MAINNET_COMMON_ROUTING_TOKENS = [
20824
+ WIMX_IMMUTABLE_MAINNET,
20825
+ ETH_IMMUTABLE_MAINNET,
20826
+ ];
20827
+
20330
20828
  const getFeeData = async (provider) => {
20331
20829
  const [block, maxPriorityFeePerGas] = await Promise.all([
20332
20830
  provider.getBlock('latest'),
@@ -20362,9 +20860,13 @@ const fetchGasPrice = async (provider, nativeToken) => {
20362
20860
  * @param {BigNumber} gasEstimate - The total gas units that will be used for the transaction
20363
20861
  * @returns - The cost of the transaction in the gas token's smallest denomination (e.g. WEI)
20364
20862
  */
20365
- const calculateGasFee = (gasPrice, gasEstimate) =>
20366
- // eslint-disable-next-line implicit-arrow-linebreak
20367
- newAmount(gasEstimate.mul(gasPrice.value), gasPrice.token);
20863
+ const calculateGasFee = (hasSecondaryFees, gasPrice, gasEstimate) => {
20864
+ const baseGasFee = newAmount(gasEstimate.mul(gasPrice.value), gasPrice.token);
20865
+ if (!hasSecondaryFees)
20866
+ return baseGasFee;
20867
+ return newAmount(baseGasFee.value.add(gasPrice.value.mul(AVERAGE_SECONDARY_FEE_EXTRA_GAS)), gasPrice.token);
20868
+ // eslint-disable-next-line implicit-arrow-linebreak
20869
+ };
20368
20870
 
20369
20871
  /**
20370
20872
  * Check if the spender needs approval for the token
@@ -20451,7 +20953,7 @@ const getApproval = async (provider, ownerAddress, preparedApproval, gasPrice) =
20451
20953
  return null;
20452
20954
  }
20453
20955
  const gasEstimate = await getApproveGasEstimate(provider, ownerAddress, preparedApproval.spender, preparedApproval.amount.token.address);
20454
- const gasFeeEstimate = gasPrice ? calculateGasFee(gasPrice, gasEstimate) : null;
20956
+ const gasFeeEstimate = gasPrice ? calculateGasFee(false, gasPrice, gasEstimate) : null;
20455
20957
  return {
20456
20958
  transaction: approveTransaction,
20457
20959
  gasFeeEstimate: gasFeeEstimate ? toPublicAmount(gasFeeEstimate) : null,
@@ -20600,59 +21102,6 @@ const IMX_WRAP_GAS_COST = 45038;
20600
21102
  // to avoid an extra remote call.
20601
21103
  const IMX_UNWRAP_GAS_COST = 35216;
20602
21104
 
20603
- const IMMUTABLE_TESTNET_RPC_URL = 'https://rpc.testnet.immutable.com';
20604
- const IMMUTABLE_TESTNET_CHAIN_ID = 13473;
20605
-
20606
- const IMMUTABLE_MAINNET_RPC_URL = 'https://rpc.immutable.com';
20607
- const IMMUTABLE_MAINNET_CHAIN_ID = 13371;
20608
-
20609
- const NATIVE_IMX_IMMUTABLE_TESTNET = {
20610
- chainId: IMMUTABLE_TESTNET_CHAIN_ID,
20611
- decimals: 18,
20612
- symbol: 'tIMX',
20613
- name: 'Immutable Testnet Token',
20614
- type: 'native',
20615
- };
20616
- const WIMX_IMMUTABLE_TESTNET = {
20617
- chainId: IMMUTABLE_TESTNET_CHAIN_ID,
20618
- address: '0x1CcCa691501174B4A623CeDA58cC8f1a76dc3439',
20619
- decimals: 18,
20620
- symbol: 'WIMX',
20621
- name: 'Wrapped Immutable Testnet Token',
20622
- type: 'erc20',
20623
- };
20624
- const IMMUTABLE_TESTNET_COMMON_ROUTING_TOKENS = [
20625
- WIMX_IMMUTABLE_TESTNET,
20626
- ];
20627
-
20628
- const NATIVE_IMX_IMMUTABLE_MAINNET = {
20629
- chainId: IMMUTABLE_MAINNET_CHAIN_ID,
20630
- decimals: 18,
20631
- symbol: 'IMX',
20632
- name: 'Immutable Token',
20633
- type: 'native',
20634
- };
20635
- const WIMX_IMMUTABLE_MAINNET = {
20636
- chainId: IMMUTABLE_MAINNET_CHAIN_ID,
20637
- address: '0x3a0c2ba54d6cbd3121f01b96dfd20e99d1696c9d',
20638
- decimals: 18,
20639
- symbol: 'WIMX',
20640
- name: 'Wrapped Immutable Token',
20641
- type: 'erc20',
20642
- };
20643
- const ETH_IMMUTABLE_MAINNET = {
20644
- chainId: IMMUTABLE_MAINNET_CHAIN_ID,
20645
- address: '0x52a6c53869ce09a731cd772f245b97a4401d3348',
20646
- decimals: 18,
20647
- symbol: 'ETH',
20648
- name: 'Ethereum',
20649
- type: 'erc20',
20650
- };
20651
- const IMMUTABLE_MAINNET_COMMON_ROUTING_TOKENS = [
20652
- WIMX_IMMUTABLE_MAINNET,
20653
- ETH_IMMUTABLE_MAINNET,
20654
- ];
20655
-
20656
21105
  const zeroNativeCurrencyValue = '0x00';
20657
21106
  const multicallWithDeadlineFunctionSignature = 'multicall(uint256,bytes[])';
20658
21107
  function buildSinglePoolSwap(tokenIn, tokenOut, recipient, trade, route, amountIn, amountOut, routerContract, paymentsContract) {
@@ -20893,8 +21342,8 @@ const getSwapRecipient = (tokenOut, fromAddress, routerContractAddress, secondar
20893
21342
  function getSwap(tokenIn, tokenOut, adjustedQuote, fromAddress, slippage, deadline, routerContractAddress, secondaryFeesContractAddress, gasPrice, secondaryFees) {
20894
21343
  const swapRecipient = getSwapRecipient(tokenOut, fromAddress, routerContractAddress, secondaryFeesContractAddress, secondaryFees);
20895
21344
  const { calldata, maximumAmountIn } = createSwapParameters(tokenIn, tokenOut, adjustedQuote, swapRecipient, slippage, deadline, secondaryFees);
20896
- // TODO: Add additional gas fee estimates for secondary fees
20897
- const gasFeeEstimate = gasPrice ? calculateGasFee(gasPrice, adjustedQuote.gasEstimate) : null;
21345
+ const hasSecondaryFees = secondaryFees.length > 0;
21346
+ const gasFeeEstimate = gasPrice ? calculateGasFee(hasSecondaryFees, gasPrice, adjustedQuote.gasEstimate) : null;
20898
21347
  const transactionValue = getTransactionValue(tokenIn, maximumAmountIn);
20899
21348
  return {
20900
21349
  transaction: {
@@ -21116,7 +21565,7 @@ class Exchange {
21116
21565
  async getUnwrapTransaction(fromAddress, tokenAmount, wimxInterface, gasPrice) {
21117
21566
  const calldata = wimxInterface.encodeFunctionData('withdraw', [tokenAmount]);
21118
21567
  const gasEstimate = ethers.BigNumber.from(IMX_UNWRAP_GAS_COST);
21119
- const gasFeeEstimate = gasPrice ? toPublicAmount(calculateGasFee(gasPrice, gasEstimate)) : null;
21568
+ const gasFeeEstimate = gasPrice ? toPublicAmount(calculateGasFee(false, gasPrice, gasEstimate)) : null;
21120
21569
  // This transaction is for calling calling `withdraw` on the WETH/WIMX contract.
21121
21570
  const transactionDetails = {
21122
21571
  transaction: {
@@ -21134,7 +21583,7 @@ class Exchange {
21134
21583
  getWrapTransaction(fromAddress, tokenAmount, wimxInterface, gasPrice) {
21135
21584
  const calldata = wimxInterface.encodeFunctionData('deposit');
21136
21585
  const gasEstimate = ethers.BigNumber.from(IMX_WRAP_GAS_COST);
21137
- const gasFeeEstimate = gasPrice ? toPublicAmount(calculateGasFee(gasPrice, gasEstimate)) : null;
21586
+ const gasFeeEstimate = gasPrice ? toPublicAmount(calculateGasFee(false, gasPrice, gasEstimate)) : null;
21138
21587
  // This transaction is for calling calling `deposit` on the WETH/WIMX contract.
21139
21588
  const transactionDetails = {
21140
21589
  transaction: {
@@ -27266,7 +27715,7 @@ class APIError extends Error {
27266
27715
 
27267
27716
  /* eslint-disable implicit-arrow-linebreak */
27268
27717
  const defaultHeaders = {
27269
- sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-1.5.1',
27718
+ sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-1.5.3-alpha',
27270
27719
  };
27271
27720
  /**
27272
27721
  * createAPIConfiguration to create a custom Configuration
@@ -28633,7 +29082,7 @@ const IMMUTABLE_ZKVEM_GAS_OVERRIDES = {
28633
29082
  maxPriorityFeePerGas: BigNumber$1.from(10e9),
28634
29083
  };
28635
29084
 
28636
- const SDK_VERSION_MARKER = '1.5.1';
29085
+ const SDK_VERSION_MARKER = '1.5.3-alpha';
28637
29086
  // This SDK version is replaced by the `yarn build` command ran on the root level
28638
29087
  const globalPackageVersion = () => SDK_VERSION_MARKER;
28639
29088