@scallop-io/sui-scallop-sdk 2.1.8 → 2.1.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scallop-io/sui-scallop-sdk",
3
- "version": "2.1.8",
3
+ "version": "2.1.9",
4
4
  "description": "Typescript sdk for interacting with Scallop contract on SUI",
5
5
  "keywords": [
6
6
  "sui",
@@ -3,10 +3,14 @@ import { API_BASE_URL, queryKeys } from 'src/constants';
3
3
  import { AddressesInterface, AddressStringPath } from 'src/types';
4
4
  import ScallopAxios, { ScallopAxiosParams } from './scallopAxios';
5
5
  import { QueryKey } from '@tanstack/query-core';
6
+ import { AxiosRequestConfig } from 'axios';
7
+ import { parseUrl } from 'src/utils';
6
8
 
7
9
  export type ScallopAddressParams = {
8
10
  addressId?: string;
9
- addressApiUrl?: string;
11
+ urls?: {
12
+ addresses?: string[];
13
+ };
10
14
  auth?: string;
11
15
  network?: NetworkType;
12
16
  forceAddressesInterface?: Partial<Record<NetworkType, AddressesInterface>>;
@@ -445,7 +449,6 @@ class ScallopAddress {
445
449
  constructor(public readonly params: ScallopAddressParams = {}) {
446
450
  this.scallopAxios = new ScallopAxios({
447
451
  ...this.defaultParamValues,
448
- baseUrl: params.addressApiUrl || API_BASE_URL,
449
452
  ...params,
450
453
  });
451
454
 
@@ -662,11 +665,13 @@ class ScallopAddress {
662
665
  protected async readApi<T>({
663
666
  url,
664
667
  queryKey,
668
+ config,
665
669
  }: {
666
670
  url: string;
667
671
  queryKey: QueryKey;
672
+ config?: AxiosRequestConfig;
668
673
  }) {
669
- const resp = await this.axiosClient.get<T>(url, queryKey);
674
+ const resp = await this.axiosClient.get<T>(url, queryKey, config);
670
675
  if (resp.status === 200) {
671
676
  return resp.data as T;
672
677
  } else {
@@ -686,20 +691,26 @@ class ScallopAddress {
686
691
  const addressId = id || this.addressId || undefined;
687
692
  if (addressId !== undefined) {
688
693
  const response = await (async () => {
689
- try {
690
- return await this.readApi<
691
- Record<NetworkType, AddressesInterface> & { id?: string }
692
- >({
693
- url: `/addresses/${addressId}`,
694
- queryKey: queryKeys.api.getAddresses({ addressId }) as string[],
695
- });
696
- } catch (e) {
697
- console.error(e);
698
- return {
699
- id: '',
700
- mainnet: this.defaultValues?.addresses?.mainnet ?? EMPTY_ADDRESSES,
701
- };
694
+ const urls = (this.params.urls?.addresses ?? [API_BASE_URL]).map(
695
+ (url) => `${parseUrl(url)}/addresses/${addressId}`
696
+ );
697
+ for (const url of urls) {
698
+ try {
699
+ return await this.readApi<
700
+ Record<NetworkType, AddressesInterface> & { id?: string }
701
+ >({
702
+ url,
703
+ queryKey: queryKeys.api.getAddresses({ addressId }) as string[],
704
+ });
705
+ } catch (e) {
706
+ console.error(`${e}`); // Trying next url in the list
707
+ }
702
708
  }
709
+
710
+ return {
711
+ id: '',
712
+ mainnet: this.defaultValues?.addresses?.mainnet ?? EMPTY_ADDRESSES,
713
+ };
703
714
  })();
704
715
 
705
716
  const isNetworkValid = (network: string): network is NetworkType =>
@@ -2,6 +2,7 @@ import { PoolAddress, Whitelist } from 'src/types';
2
2
  import ScallopAddress, { ScallopAddressParams } from './scallopAddress';
3
3
  import { NetworkType, parseStructTag } from '@scallop-io/sui-kit';
4
4
  import { queryKeys } from 'src/constants';
5
+ import { parseUrl } from 'src/utils';
5
6
 
6
7
  const isEmptyObject = (obj: object) => {
7
8
  return Object.keys(obj).length === 0;
@@ -23,8 +24,10 @@ type SCoinRawName = string;
23
24
  type SCoinName = string;
24
25
 
25
26
  export type ScallopConstantsParams = {
26
- poolAddressesApiUrl?: string;
27
- whitelistApiUrl?: string;
27
+ urls?: {
28
+ poolAddresses?: string[];
29
+ whitelist?: string[];
30
+ };
28
31
  forcePoolAddressInterface?: Record<string, PoolAddress>;
29
32
  forceWhitelistInterface?: Whitelist | Record<string, any>;
30
33
  defaultValues?: {
@@ -325,17 +328,22 @@ class ScallopConstants extends ScallopAddress {
325
328
 
326
329
  async readWhiteList() {
327
330
  const response = await (async () => {
328
- try {
329
- return await this.readApi<Record<keyof Whitelist, string[]>>({
330
- url:
331
- this.params.whitelistApiUrl ??
332
- `https://sui.apis.scallop.io/pool/whitelist`,
333
- queryKey: queryKeys.api.getWhiteList(),
334
- });
335
- } catch (e) {
336
- console.error(e);
337
- return this.defaultValues?.whitelist ?? DEFAULT_WHITELIST;
331
+ const urls = (
332
+ this.params.urls?.whitelist ?? [
333
+ `https://sui.apis.scallop.io/pool/whitelist`,
334
+ ]
335
+ ).map(parseUrl);
336
+ for (const url of urls) {
337
+ try {
338
+ return await this.readApi<Record<keyof Whitelist, string[]>>({
339
+ url,
340
+ queryKey: queryKeys.api.getWhiteList(),
341
+ });
342
+ } catch (e) {
343
+ console.error(`${e}`); // Trying next url in the list
344
+ }
338
345
  }
346
+ return this.defaultValues?.whitelist ?? DEFAULT_WHITELIST;
339
347
  })();
340
348
 
341
349
  return Object.fromEntries(
@@ -349,17 +357,22 @@ class ScallopConstants extends ScallopAddress {
349
357
  }
350
358
 
351
359
  async readPoolAddresses() {
352
- try {
353
- return await this.readApi<Record<string, PoolAddress>>({
354
- url:
355
- this.params.poolAddressesApiUrl ??
356
- `https://sui.apis.scallop.io/pool/addresses`,
357
- queryKey: queryKeys.api.getPoolAddresses(),
358
- });
359
- } catch (e) {
360
- console.error(e);
361
- return this.defaultValues?.poolAddresses ?? {};
360
+ const urls = (
361
+ this.params.urls?.poolAddresses ?? [
362
+ `https://sui.apis.scallop.io/pool/addresses`,
363
+ ]
364
+ ).map(parseUrl);
365
+ for (const url of urls) {
366
+ try {
367
+ return await this.readApi<Record<string, PoolAddress>>({
368
+ url,
369
+ queryKey: queryKeys.api.getPoolAddresses(),
370
+ });
371
+ } catch (e) {
372
+ console.error(`${e}`); // Trying next url in the list
373
+ }
362
374
  }
375
+ return this.defaultValues?.poolAddresses ?? {};
363
376
  }
364
377
  }
365
378
 
@@ -163,7 +163,13 @@ class ScallopSuiKit extends ScallopQueryClient {
163
163
  }
164
164
 
165
165
  get currentFullNode() {
166
- return this.suiKit.suiInteractor.currentFullNode;
166
+ try {
167
+ // return current fullnode from SuiKit
168
+ return this.suiKit.suiInteractor.currentFullNode;
169
+ } catch (_) {
170
+ // SuiKit is initialized with custom sui clients, so no fullnodes can be read
171
+ return '';
172
+ }
167
173
  }
168
174
 
169
175
  signAndSendTxn(
package/src/utils/util.ts CHANGED
@@ -33,3 +33,10 @@ export const partitionArray = <T>(array: T[], chunkSize: number) => {
33
33
  }
34
34
  return result;
35
35
  };
36
+
37
+ export const parseUrl = (url: string) => {
38
+ if (url.endsWith('/')) {
39
+ url = url.slice(0, -1);
40
+ }
41
+ return url;
42
+ };