@imtbl/sdk 0.37.0 → 0.39.0

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
@@ -26051,7 +26051,7 @@ class APIError extends Error {
26051
26051
 
26052
26052
  /* eslint-disable implicit-arrow-linebreak */
26053
26053
  const defaultHeaders = {
26054
- sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-0.37.0',
26054
+ sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-0.39.0',
26055
26055
  };
26056
26056
  /**
26057
26057
  * createAPIConfiguration to create a custom Configuration
@@ -26497,6 +26497,7 @@ var ConnectEventType;
26497
26497
  ConnectEventType["CLOSE_WIDGET"] = "close-widget";
26498
26498
  ConnectEventType["SUCCESS"] = "success";
26499
26499
  ConnectEventType["FAILURE"] = "failure";
26500
+ ConnectEventType["LANGUAGE_CHANGED"] = "language-changed";
26500
26501
  })(ConnectEventType || (ConnectEventType = {}));
26501
26502
 
26502
26503
  /**
@@ -26507,6 +26508,7 @@ var WalletEventType;
26507
26508
  WalletEventType["CLOSE_WIDGET"] = "close-widget";
26508
26509
  WalletEventType["NETWORK_SWITCH"] = "network-switch";
26509
26510
  WalletEventType["DISCONNECT_WALLET"] = "disconnect-wallet";
26511
+ WalletEventType["LANGUAGE_CHANGED"] = "language-changed";
26510
26512
  })(WalletEventType || (WalletEventType = {}));
26511
26513
 
26512
26514
  /**
@@ -26518,6 +26520,7 @@ var SwapEventType;
26518
26520
  SwapEventType["SUCCESS"] = "success";
26519
26521
  SwapEventType["FAILURE"] = "failure";
26520
26522
  SwapEventType["REJECTED"] = "rejected";
26523
+ SwapEventType["LANGUAGE_CHANGED"] = "language-changed";
26521
26524
  })(SwapEventType || (SwapEventType = {}));
26522
26525
 
26523
26526
  /**
@@ -26530,6 +26533,7 @@ var SaleEventType;
26530
26533
  SaleEventType["FAILURE"] = "failure";
26531
26534
  SaleEventType["REJECTED"] = "rejected";
26532
26535
  SaleEventType["TRANSACTION_SUCCESS"] = "transaction-success";
26536
+ SaleEventType["LANGUAGE_CHANGED"] = "language-changed";
26533
26537
  })(SaleEventType || (SaleEventType = {}));
26534
26538
 
26535
26539
  /**
@@ -26540,6 +26544,7 @@ var BridgeEventType;
26540
26544
  BridgeEventType["CLOSE_WIDGET"] = "close-widget";
26541
26545
  BridgeEventType["FAILURE"] = "failure";
26542
26546
  BridgeEventType["TRANSACTION_SENT"] = "transaction-sent";
26547
+ BridgeEventType["LANGUAGE_CHANGED"] = "language-changed";
26543
26548
  })(BridgeEventType || (BridgeEventType = {}));
26544
26549
 
26545
26550
  /**
@@ -26562,6 +26567,7 @@ var OnRampEventType;
26562
26567
  OnRampEventType["CLOSE_WIDGET"] = "close-widget";
26563
26568
  OnRampEventType["SUCCESS"] = "success";
26564
26569
  OnRampEventType["FAILURE"] = "failure";
26570
+ OnRampEventType["LANGUAGE_CHANGED"] = "language-changed";
26565
26571
  })(OnRampEventType || (OnRampEventType = {}));
26566
26572
 
26567
26573
  /**
@@ -26950,7 +26956,11 @@ async function addNetworkToWallet(networkMap, web3Provider, chainId) {
26950
26956
  async function getNetworkAllowList(config, { type = NetworkFilterTypes.ALL, exclude }) {
26951
26957
  const { networkMap } = config;
26952
26958
  const allowedNetworkConfig = (await config.remote.getConfig('allowedNetworks'));
26953
- const list = allowedNetworkConfig.filter((network) => {
26959
+ if (!allowedNetworkConfig) {
26960
+ // eslint-disable-next-line no-console
26961
+ console.warn('No allowed networks configured');
26962
+ }
26963
+ const list = (allowedNetworkConfig || []).filter((network) => {
26954
26964
  const allowAllTokens = type === NetworkFilterTypes.ALL;
26955
26965
  const networkNotExcluded = !(exclude || [])
26956
26966
  .map((exc) => exc.chainId)
@@ -27390,32 +27400,21 @@ const GAS_OVERRIDES = {
27390
27400
  maxPriorityFeePerGas: BigNumber$1.from(101e9),
27391
27401
  };
27392
27402
 
27393
- const SDK_VERSION_MARKER = '0.37.0';
27403
+ const SDK_VERSION_MARKER = '0.39.0';
27394
27404
  // This SDK version is replaced by the `yarn build` command ran on the root level
27395
27405
  const globalPackageVersion = () => SDK_VERSION_MARKER;
27396
27406
 
27397
27407
  class RemoteConfigFetcher {
27408
+ httpClient;
27398
27409
  isDevelopment;
27399
27410
  isProduction;
27400
27411
  configCache;
27401
27412
  tokensCache;
27402
27413
  version = 'v1';
27403
- constructor(params) {
27414
+ constructor(httpClient, params) {
27404
27415
  this.isDevelopment = params.isDevelopment;
27405
27416
  this.isProduction = params.isProduction;
27406
- }
27407
- static async makeHttpRequest(url) {
27408
- let response;
27409
- try {
27410
- response = await globalAxios.get(url);
27411
- }
27412
- catch (error) {
27413
- throw new CheckoutError(`Error fetching from api: ${error.message}`, CheckoutErrorType.API_ERROR);
27414
- }
27415
- if (response.status !== 200) {
27416
- throw new CheckoutError(`Error fetching from api: ${response.status} ${response.statusText}`, CheckoutErrorType.API_ERROR);
27417
- }
27418
- return response;
27417
+ this.httpClient = httpClient;
27419
27418
  }
27420
27419
  getEndpoint = () => {
27421
27420
  if (this.isDevelopment)
@@ -27424,18 +27423,33 @@ class RemoteConfigFetcher {
27424
27423
  return CHECKOUT_CDN_BASE_URL[Environment$1.PRODUCTION];
27425
27424
  return CHECKOUT_CDN_BASE_URL[Environment$1.SANDBOX];
27426
27425
  };
27426
+ // eslint-disable-next-line class-methods-use-this
27427
+ parseResponse(response) {
27428
+ let responseData = response.data;
27429
+ if (response.data && typeof response.data !== 'object') {
27430
+ try {
27431
+ responseData = JSON.parse(response.data);
27432
+ }
27433
+ catch (jsonError) {
27434
+ throw new CheckoutError('Invalid configuration', CheckoutErrorType.API_ERROR);
27435
+ }
27436
+ }
27437
+ return responseData;
27438
+ }
27427
27439
  async loadConfig() {
27428
27440
  if (this.configCache)
27429
27441
  return this.configCache;
27430
- const response = await RemoteConfigFetcher.makeHttpRequest(`${this.getEndpoint()}/${this.version}/config`);
27431
- this.configCache = response.data;
27442
+ const response = await this.httpClient.get(`${this.getEndpoint()}/${this.version}/config`);
27443
+ // Ensure that the configuration is valid
27444
+ this.configCache = this.parseResponse(response);
27432
27445
  return this.configCache;
27433
27446
  }
27434
27447
  async loadConfigTokens() {
27435
27448
  if (this.tokensCache)
27436
27449
  return this.tokensCache;
27437
- const response = await RemoteConfigFetcher.makeHttpRequest(`${this.getEndpoint()}/${this.version}/config/tokens`);
27438
- this.tokensCache = response.data;
27450
+ const response = await this.httpClient.get(`${this.getEndpoint()}/${this.version}/config/tokens`);
27451
+ // Ensure that the configuration is valid
27452
+ this.tokensCache = this.parseResponse(response);
27439
27453
  return this.tokensCache;
27440
27454
  }
27441
27455
  async getConfig(key) {
@@ -27452,6 +27466,7 @@ class RemoteConfigFetcher {
27452
27466
  return {};
27453
27467
  return config[chainId] ?? [];
27454
27468
  }
27469
+ getHttpClient = () => this.httpClient;
27455
27470
  }
27456
27471
 
27457
27472
  class CheckoutConfigurationError extends Error {
@@ -27502,7 +27517,7 @@ class CheckoutConfiguration {
27502
27517
  remote;
27503
27518
  environment;
27504
27519
  networkMap;
27505
- constructor(config) {
27520
+ constructor(config, httpClient) {
27506
27521
  if (!Object.values(Environment$1).includes(config.baseConfig.environment)) {
27507
27522
  throw new CheckoutConfigurationError('Invalid checkout configuration of environment');
27508
27523
  }
@@ -27513,7 +27528,7 @@ class CheckoutConfiguration {
27513
27528
  this.isSwapEnabled = config.swap?.enable ?? DEFAULT_SWAP_ENABLED;
27514
27529
  this.isBridgeEnabled = config.bridge?.enable ?? DEFAULT_BRIDGE_ENABLED;
27515
27530
  this.networkMap = networkMap(this.isProduction, this.isDevelopment);
27516
- this.remote = new RemoteConfigFetcher({
27531
+ this.remote = new RemoteConfigFetcher(httpClient, {
27517
27532
  isDevelopment: this.isDevelopment,
27518
27533
  isProduction: this.isProduction,
27519
27534
  });
@@ -27578,9 +27593,7 @@ class Blockscout {
27578
27593
  ttl;
27579
27594
  chainId;
27580
27595
  cacheMap;
27581
- static async makeHttpRequest(url) {
27582
- return globalAxios.get(url);
27583
- }
27596
+ httpClient;
27584
27597
  setCache(key, data) {
27585
27598
  this.cacheMap[key] = { data, ttl: new Date().getTime() + this.ttl * 1000 };
27586
27599
  }
@@ -27592,11 +27605,13 @@ class Blockscout {
27592
27605
  }
27593
27606
  /**
27594
27607
  * Blockscout constructor
27608
+ * @param httpClient Client used for http requests
27595
27609
  * @param chainId target chain
27596
27610
  * @param ttl cache TTL
27597
27611
  */
27598
- constructor(params) {
27599
- this.chainId = params.chainId;
27612
+ constructor(httpClient, chainId, ttl) {
27613
+ this.httpClient = httpClient;
27614
+ this.chainId = chainId;
27600
27615
  this.url = BLOCKSCOUT_CHAIN_URL_MAP[this.chainId].url;
27601
27616
  const native = BLOCKSCOUT_CHAIN_URL_MAP[this.chainId].nativeToken;
27602
27617
  this.nativeToken = {
@@ -27606,7 +27621,7 @@ class Blockscout {
27606
27621
  symbol: native.symbol,
27607
27622
  };
27608
27623
  this.cacheMap = {};
27609
- this.ttl = params.ttl !== undefined ? params.ttl : CACHE_DATA_TTL;
27624
+ this.ttl = ttl !== undefined ? ttl : CACHE_DATA_TTL;
27610
27625
  }
27611
27626
  /**
27612
27627
  * isChainSupported verifies if the chain is supported by Blockscout
@@ -27634,7 +27649,7 @@ class Blockscout {
27634
27649
  const cached = this.getCache(url);
27635
27650
  if (cached)
27636
27651
  return Promise.resolve(cached);
27637
- const response = await Blockscout.makeHttpRequest(url);
27652
+ const response = await this.httpClient.get(url);
27638
27653
  if (response.status >= 400) {
27639
27654
  return Promise.reject({
27640
27655
  code: response.status,
@@ -27675,7 +27690,7 @@ class Blockscout {
27675
27690
  const cached = this.getCache(url);
27676
27691
  if (cached)
27677
27692
  return Promise.resolve(cached);
27678
- const response = await Blockscout.makeHttpRequest(url);
27693
+ const response = await this.httpClient.get(url);
27679
27694
  if (response.status >= 400) {
27680
27695
  return Promise.reject({
27681
27696
  code: response.status,
@@ -27760,7 +27775,7 @@ const blockscoutClientMap = new Map();
27760
27775
  // This function is a utility function that can be used to reset the
27761
27776
  // blockscout map and therefore clear all the cache.
27762
27777
  const resetBlockscoutClientMap = () => blockscoutClientMap.clear();
27763
- const getIndexerBalance = async (walletAddress, chainId, filterTokens) => {
27778
+ const getIndexerBalance = async (config, walletAddress, chainId, filterTokens) => {
27764
27779
  // Shuffle the mapping of the tokens configuration so it is a hashmap
27765
27780
  // for faster access to tokens config objects.
27766
27781
  const shouldFilter = filterTokens !== undefined;
@@ -27768,7 +27783,8 @@ const getIndexerBalance = async (walletAddress, chainId, filterTokens) => {
27768
27783
  // Get blockscout client for the given chain
27769
27784
  let blockscoutClient = blockscoutClientMap.get(chainId);
27770
27785
  if (!blockscoutClient) {
27771
- blockscoutClient = new Blockscout({ chainId });
27786
+ const httpClient = config.remote.getHttpClient();
27787
+ blockscoutClient = new Blockscout(httpClient, chainId);
27772
27788
  blockscoutClientMap.set(chainId, blockscoutClient);
27773
27789
  }
27774
27790
  // Hold the items in an array for post-fetching processing
@@ -27903,7 +27919,7 @@ const getAllBalances = async (config, web3Provider, walletAddress, chainId) => {
27903
27919
  const isL1Chain = getL1ChainId(config) === chainId;
27904
27920
  if (!address)
27905
27921
  address = await web3Provider?.getSigner().getAddress();
27906
- return await measureAsyncExecution(config, `Time to fetch balances using blockscout for ${chainId}`, getIndexerBalance(address, chainId, isL1Chain ? tokens : undefined));
27922
+ return await measureAsyncExecution(config, `Time to fetch balances using blockscout for ${chainId}`, getIndexerBalance(config, address, chainId, isL1Chain ? tokens : undefined));
27907
27923
  }
27908
27924
  if (!web3Provider) {
27909
27925
  throw new CheckoutError('indexer is disabled for this chain, you must provide a provider.', CheckoutErrorType.MISSING_PARAMS);
@@ -29587,8 +29603,8 @@ const bridgeRoute = async (config, readOnlyProviders, availableRoutingOptions, b
29587
29603
  if (tokenBalanceResult === undefined || tokenBalanceResult.success === false)
29588
29604
  return undefined;
29589
29605
  // todo: revert this back to using the allowlist
29590
- const allowedTokenList = await allowListCheckForBridge(config, tokenBalanceResults, availableRoutingOptions);
29591
- if (allowedTokenList.length === 0)
29606
+ const allowedL1TokenList = await allowListCheckForBridge(config, tokenBalanceResults, availableRoutingOptions);
29607
+ if (allowedL1TokenList.length === 0)
29592
29608
  return undefined;
29593
29609
  const l1RepresentationResult = await fetchL1Representation(config, bridgeRequirement.l2address);
29594
29610
  if (!l1RepresentationResult)
@@ -29596,10 +29612,10 @@ const bridgeRoute = async (config, readOnlyProviders, availableRoutingOptions, b
29596
29612
  // Ensure l1address is in the allowed token list
29597
29613
  const { l1address } = l1RepresentationResult;
29598
29614
  if (isNativeToken(l1address)) {
29599
- if (!allowedTokenList.find((token) => !('address' in token)))
29615
+ if (!allowedL1TokenList.find((token) => isNativeToken(token.address)))
29600
29616
  return undefined;
29601
29617
  }
29602
- else if (!allowedTokenList.find((token) => token.address === l1address)) {
29618
+ else if (!allowedL1TokenList.find((token) => token.address === l1address)) {
29603
29619
  return undefined;
29604
29620
  }
29605
29621
  const feesFromBridge = await getBridgeFeeEstimate(config, readOnlyProviders, l1ChainId, l2ChainId);
@@ -30896,6 +30912,90 @@ function loadUnresolved(version) {
30896
30912
  return { loaded: false, element: tag };
30897
30913
  }
30898
30914
 
30915
+ const PUBLISHABLE_KEY_PREFIX = 'pk_imapik-';
30916
+ const publishableKeyDomainAllowlist = [
30917
+ 'https://checkout-api.dev.immutable.com',
30918
+ 'https://checkout-api.sandbox.immutable.com',
30919
+ 'https://checkout-api.immutable.com',
30920
+ ];
30921
+ class HttpClient {
30922
+ config;
30923
+ axiosInstance;
30924
+ constructor(config) {
30925
+ this.config = config;
30926
+ this.axiosInstance = globalAxios.create();
30927
+ this.setupInterceptors();
30928
+ }
30929
+ // eslint-disable-next-line class-methods-use-this
30930
+ shouldAddPublishableKey(url) {
30931
+ // Check if the request is going to a whitelisted domain
30932
+ return publishableKeyDomainAllowlist.some((domain) => url.startsWith(domain));
30933
+ }
30934
+ setupInterceptors() {
30935
+ this.axiosInstance.interceptors.request.use((config) => {
30936
+ const publishableKey = this.config?.baseConfig?.publishableKey;
30937
+ if (publishableKey && this.shouldAddPublishableKey(config.url || '')) {
30938
+ if (!publishableKey.startsWith(PUBLISHABLE_KEY_PREFIX)) {
30939
+ throw new Error('Invalid Publishable key. Create your Publishable key in Immutable developer hub.'
30940
+ + ' https://hub.immutable.com');
30941
+ }
30942
+ // Add publishable key header for whitelisted domains
30943
+ // eslint-disable-next-line no-param-reassign
30944
+ config.headers['x-immutable-publishable-key'] = publishableKey;
30945
+ }
30946
+ return config;
30947
+ }, (error) => Promise.reject(error));
30948
+ }
30949
+ // eslint-disable-next-line class-methods-use-this
30950
+ processResponse(response) {
30951
+ if (response && response?.status !== 200) {
30952
+ throw new CheckoutError(`Error: ${response.status} ${response.statusText}`, CheckoutErrorType.API_ERROR);
30953
+ }
30954
+ }
30955
+ async request(config) {
30956
+ let response;
30957
+ try {
30958
+ response = await this.axiosInstance.request(config);
30959
+ }
30960
+ catch (error) {
30961
+ throw new CheckoutError(error.message, CheckoutErrorType.API_ERROR);
30962
+ }
30963
+ this.processResponse(response);
30964
+ return response;
30965
+ }
30966
+ async get(url, config) {
30967
+ const requestConfig = {
30968
+ ...config,
30969
+ url,
30970
+ };
30971
+ return await this.request(requestConfig);
30972
+ }
30973
+ async post(url, data, config) {
30974
+ const requestConfig = {
30975
+ ...config,
30976
+ data,
30977
+ url,
30978
+ };
30979
+ return await this.request(requestConfig);
30980
+ }
30981
+ async put(url, data, config) {
30982
+ const requestConfig = {
30983
+ ...config,
30984
+ data,
30985
+ url,
30986
+ };
30987
+ return await this.request(requestConfig);
30988
+ }
30989
+ async delete(url, data, config) {
30990
+ const requestConfig = {
30991
+ ...config,
30992
+ data,
30993
+ url,
30994
+ };
30995
+ return await this.request(requestConfig);
30996
+ }
30997
+ }
30998
+
30899
30999
  const SANDBOX_CONFIGURATION = {
30900
31000
  baseConfig: {
30901
31001
  environment: Environment$1.SANDBOX,
@@ -30906,6 +31006,7 @@ const WIDGETS_SCRIPT_TIMEOUT = 100;
30906
31006
  // Checkout SDK
30907
31007
  class Checkout {
30908
31008
  readOnlyProviders;
31009
+ httpClient;
30909
31010
  config;
30910
31011
  fiatRampService;
30911
31012
  availability;
@@ -30915,7 +31016,8 @@ class Checkout {
30915
31016
  * @param {CheckoutModuleConfiguration} [config=SANDBOX_CONFIGURATION] - The configuration object for the CheckoutModule.
30916
31017
  */
30917
31018
  constructor(config = SANDBOX_CONFIGURATION) {
30918
- this.config = new CheckoutConfiguration(config);
31019
+ this.httpClient = new HttpClient(config);
31020
+ this.config = new CheckoutConfiguration(config, this.httpClient);
30919
31021
  this.fiatRampService = new FiatRampService(this.config);
30920
31022
  this.readOnlyProviders = new Map();
30921
31023
  this.availability = availabilityService(this.config.isDevelopment, this.config.isProduction);
@@ -17,7 +17,7 @@ const createImmutableXConfiguration = ({ basePath, chainID, coreContractAddress,
17
17
  chainID,
18
18
  coreContractAddress,
19
19
  registrationContractAddress,
20
- sdkVersion: 'ts-immutable-sdk-0.37.0',
20
+ sdkVersion: 'ts-immutable-sdk-0.39.0',
21
21
  });
22
22
  /**
23
23
  * Sets `sdkVersion` at the time of build