@scallop-io/sui-scallop-sdk 0.47.5 → 0.47.7

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.
@@ -3,6 +3,7 @@ import type { TransactionBlock, TransactionResult } from '@mysten/sui.js/transac
3
3
  import type { SuiKit, SuiKitParams, NetworkType } from '@scallop-io/sui-kit';
4
4
  import type { ScallopAddress, ScallopQuery, ScallopUtils, ScallopBuilder, ScallopIndexer } from '../models';
5
5
  import { ScallopCache } from 'src/models/scallopCache';
6
+ import { AddressesInterface } from './address';
6
7
  export type ScallopClientFnReturnType<T extends boolean> = T extends true ? SuiTransactionBlockResponse : TransactionBlock;
7
8
  export type ScallopClientVeScaReturnType<T extends boolean> = T extends true ? SuiTransactionBlockResponse : {
8
9
  tx: TransactionBlock;
@@ -35,9 +36,11 @@ export type ScallopAddressParams = {
35
36
  id: string;
36
37
  auth?: string;
37
38
  network?: NetworkType;
39
+ forceInterface?: Partial<Record<NetworkType, AddressesInterface>>;
38
40
  };
39
41
  export type ScallopParams = {
40
42
  addressesId?: string;
43
+ forceAddressesInterface?: Partial<Record<NetworkType, AddressesInterface>>;
41
44
  walletAddress?: string;
42
45
  } & SuiKitParams;
43
46
  export type ScallopClientParams = ScallopParams & ScallopBuilderParams & ScallopQueryParams & ScallopUtilsParams;
@@ -2,3 +2,4 @@ export * from './builder';
2
2
  export * from './query';
3
3
  export * from './util';
4
4
  export * from './tokenBucket';
5
+ export * from './indexer';
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Generic wrapper for methods with indexer fallback.
3
+ *
4
+ * @param method - The method to call with fallback behavior.
5
+ * @param context - The context (`this`) of the class instance.
6
+ * @param args - The arguments to pass to the method.
7
+ * @returns The result of the method call.
8
+ */
9
+ export declare function callMethodWithIndexerFallback(method: Function, context: any, ...args: any[]): Promise<any>;
10
+ /**
11
+ * This function creates a wrapper for methods that have an indexer parameter.
12
+ * It ensures fallback behavior if indexer fails.
13
+ *
14
+ * @param method - The method to wrap.
15
+ * @returns A function that applies indexer fallback.
16
+ */
17
+ export declare function withIndexerFallback(method: Function): (...args: any[]) => Promise<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scallop-io/sui-scallop-sdk",
3
- "version": "0.47.5",
3
+ "version": "0.47.7",
4
4
  "description": "Typescript sdk for interacting with Scallop contract on SUI",
5
5
  "keywords": [
6
6
  "sui",
@@ -64,6 +64,7 @@ export class Scallop {
64
64
  {
65
65
  id: params?.addressesId || ADDRESSES_ID,
66
66
  network: params?.networkType,
67
+ forceInterface: params?.forceAddressesInterface,
67
68
  },
68
69
  { cache: this.cache }
69
70
  );
@@ -377,7 +377,7 @@ export class ScallopAddress {
377
377
  params: ScallopAddressParams,
378
378
  instance?: ScallopAddressInstanceParams
379
379
  ) {
380
- const { id, auth, network } = params;
380
+ const { id, auth, network, forceInterface } = params;
381
381
  this.cache =
382
382
  instance?.cache ??
383
383
  new ScallopCache(
@@ -401,6 +401,18 @@ export class ScallopAddress {
401
401
  ? new Map([['mainnet', TEST_ADDRESSES]])
402
402
  : new Map();
403
403
  if (USE_TEST_ADDRESS) this._currentAddresses = TEST_ADDRESSES;
404
+
405
+ // Set the addresses from the forceInterface if it is provided.
406
+ if (forceInterface) {
407
+ for (const [network, addresses] of Object.entries<AddressesInterface>(
408
+ forceInterface
409
+ )) {
410
+ if (['localnet', 'devnet', 'testnet', 'mainnet'].includes(network)) {
411
+ if (network === this._network) this._currentAddresses = addresses;
412
+ this._addressesMap.set(network as NetworkType, addresses);
413
+ }
414
+ }
415
+ }
404
416
  }
405
417
 
406
418
  /**
@@ -68,6 +68,7 @@ export class ScallopBuilder {
68
68
  {
69
69
  id: params?.addressesId || ADDRESSES_ID,
70
70
  network: params?.networkType,
71
+ forceInterface: params?.forceAddressesInterface,
71
72
  },
72
73
  {
73
74
  cache: this.cache,
@@ -85,6 +85,7 @@ export class ScallopClient {
85
85
  {
86
86
  id: params?.addressesId || ADDRESSES_ID,
87
87
  network: params?.networkType,
88
+ forceInterface: params?.forceAddressesInterface,
88
89
  },
89
90
  {
90
91
  cache: this.cache,
@@ -62,6 +62,7 @@ import {
62
62
  } from 'src/queries/sCoinQuery';
63
63
  import { normalizeSuiAddress } from '@mysten/sui.js/utils';
64
64
  import { getSupplyLimit } from 'src/queries/supplyLimit';
65
+ import { withIndexerFallback } from 'src/utils/indexer';
65
66
 
66
67
  /**
67
68
  * @description
@@ -111,6 +112,7 @@ export class ScallopQuery {
111
112
  {
112
113
  id: params?.addressesId || ADDRESSES_ID,
113
114
  network: params?.networkType,
115
+ forceInterface: params?.forceAddressesInterface,
114
116
  },
115
117
  {
116
118
  cache: this.cache,
@@ -123,8 +125,40 @@ export class ScallopQuery {
123
125
  this.indexer =
124
126
  instance?.indexer ??
125
127
  new ScallopIndexer(this.params, { cache: this.cache });
128
+
129
+ // Wrap any method that has an indexer parameter as the last parameter
130
+ this.queryMarket = withIndexerFallback.call(this, this.queryMarket);
131
+ this.getMarketPools = withIndexerFallback.call(this, this.getMarketPools);
132
+ this.getMarketPool = withIndexerFallback.call(this, this.getMarketPool);
133
+ this.getMarketCollaterals = withIndexerFallback.call(
134
+ this,
135
+ this.getMarketCollaterals
136
+ );
137
+ this.getMarketCollateral = withIndexerFallback.call(
138
+ this,
139
+ this.getMarketCollateral
140
+ );
141
+ this.getSpools = withIndexerFallback.call(this, this.getSpools);
142
+ this.getSpool = withIndexerFallback.call(this, this.getSpool);
143
+ this.getBorrowIncentivePools = withIndexerFallback.call(
144
+ this,
145
+ this.getBorrowIncentivePools
146
+ );
147
+ this.getLendings = withIndexerFallback.call(this, this.getLendings);
148
+ this.getLending = withIndexerFallback.call(this, this.getLending);
149
+ this.getObligationAccounts = withIndexerFallback.call(
150
+ this,
151
+ this.getObligationAccounts
152
+ );
153
+ this.getObligationAccount = withIndexerFallback.call(
154
+ this,
155
+ this.getObligationAccount
156
+ );
157
+ this.getTvl = withIndexerFallback.call(this, this.getTvl);
126
158
  }
127
159
 
160
+ /* ========================================================== */
161
+
128
162
  /**
129
163
  * Request the scallop API to initialize data.
130
164
  *
@@ -101,6 +101,7 @@ export class ScallopUtils {
101
101
  {
102
102
  id: params?.addressesId || ADDRESSES_ID,
103
103
  network: params?.networkType,
104
+ forceInterface: params?.forceAddressesInterface,
104
105
  },
105
106
  {
106
107
  cache: this.cache,
@@ -12,6 +12,7 @@ import type {
12
12
  ScallopIndexer,
13
13
  } from '../models';
14
14
  import { ScallopCache } from 'src/models/scallopCache';
15
+ import { AddressesInterface } from './address';
15
16
 
16
17
  export type ScallopClientFnReturnType<T extends boolean> = T extends true
17
18
  ? SuiTransactionBlockResponse
@@ -59,10 +60,12 @@ export type ScallopAddressParams = {
59
60
  id: string;
60
61
  auth?: string;
61
62
  network?: NetworkType;
63
+ forceInterface?: Partial<Record<NetworkType, AddressesInterface>>;
62
64
  };
63
65
 
64
66
  export type ScallopParams = {
65
67
  addressesId?: string;
68
+ forceAddressesInterface?: Partial<Record<NetworkType, AddressesInterface>>;
66
69
  walletAddress?: string;
67
70
  } & SuiKitParams;
68
71
 
@@ -2,3 +2,4 @@ export * from './builder';
2
2
  export * from './query';
3
3
  export * from './util';
4
4
  export * from './tokenBucket';
5
+ export * from './indexer';
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Generic wrapper for methods with indexer fallback.
3
+ *
4
+ * @param method - The method to call with fallback behavior.
5
+ * @param context - The context (`this`) of the class instance.
6
+ * @param args - The arguments to pass to the method.
7
+ * @returns The result of the method call.
8
+ */
9
+ export async function callMethodWithIndexerFallback(
10
+ method: Function,
11
+ context: any,
12
+ ...args: any[]
13
+ ) {
14
+ const indexer = args[args.length - 1]; // Assume last argument is always `indexer`
15
+
16
+ if (indexer) {
17
+ try {
18
+ return await method.apply(context, args);
19
+ } catch (_e) {
20
+ console.warn('Indexer requests failed. Retrying without indexer..');
21
+ return await method.apply(context, [...args.slice(0, -1), false]);
22
+ }
23
+ }
24
+ return await method.apply(context, args);
25
+ }
26
+
27
+ /**
28
+ * This function creates a wrapper for methods that have an indexer parameter.
29
+ * It ensures fallback behavior if indexer fails.
30
+ *
31
+ * @param method - The method to wrap.
32
+ * @returns A function that applies indexer fallback.
33
+ */
34
+ export function withIndexerFallback(method: Function) {
35
+ return (...args: any[]) => {
36
+ // @ts-ignore
37
+ return callMethodWithIndexerFallback(method, this, ...args); // Preserve `this` with arrow function
38
+ };
39
+ }