@scallop-io/sui-scallop-sdk 2.0.0-alpha.1 → 2.0.0-alpha.3

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.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.3",
4
4
  "description": "Typescript sdk for interacting with Scallop contract on SUI",
5
5
  "keywords": [
6
6
  "sui",
@@ -98,13 +98,11 @@ export class ScallopBuilder {
98
98
  * @param force - Whether to force initialization.
99
99
  * @param address - ScallopAddress instance.
100
100
  */
101
- public async init(force: boolean = false, address?: ScallopAddress) {
102
- if (address && !this.address) this.address = address;
103
- if (force || !this.address.getAddresses()) {
104
- await this.address.read();
101
+ public async init(force: boolean = false) {
102
+ if (force || !this.constants.isInitialized) {
103
+ await this.constants.init();
105
104
  }
106
- await this.query.init(force, this.address);
107
- // await this.utils.init(force, this.address);
105
+ await this.query.init(force);
108
106
  }
109
107
 
110
108
  /**
@@ -59,6 +59,7 @@ export class ScallopClient {
59
59
 
60
60
  this.cache =
61
61
  instance?.builder?.cache ??
62
+ instance?.cache ??
62
63
  new ScallopCache(this.params, {
63
64
  suiKit: this.suiKit,
64
65
  });
@@ -100,13 +101,13 @@ export class ScallopClient {
100
101
  * @param force - Whether to force initialization.
101
102
  */
102
103
  public async init(force: boolean = false) {
103
- if (force || !this.address.getAddresses()) {
104
- await this.address.read();
104
+ if (force || !this.constants.isInitialized) {
105
+ await this.constants.init();
105
106
  }
106
107
 
107
- await this.builder.init(force, this.address);
108
- await this.query.init(force, this.address);
109
- await this.utils.init(force, this.address);
108
+ await this.builder.init(force);
109
+ await this.query.init(force);
110
+ await this.utils.init(force);
110
111
  }
111
112
 
112
113
  /* ==================== Query Method ==================== */
@@ -44,7 +44,6 @@ export class ScallopConstants {
44
44
  };
45
45
 
46
46
  private _coinDecimals: Record<string, number | undefined> = {};
47
- private _coinNameToCoinTypeMap: Record<string, string | undefined> = {};
48
47
  private _coinNameToOldMarketCoinTypeMap: Record<string, string | undefined> =
49
48
  {};
50
49
  private _scoinRawNameToSCoinNameMap: Record<string, string | undefined> = {};
@@ -73,6 +72,7 @@ export class ScallopConstants {
73
72
 
74
73
  this.cache =
75
74
  instance?.address?.cache ??
75
+ instance?.cache ??
76
76
  new ScallopCache(this.params, {
77
77
  suiKit: newSuiKit(this.params),
78
78
  });
@@ -98,7 +98,9 @@ export class ScallopConstants {
98
98
 
99
99
  get isInitialized() {
100
100
  return (
101
- !!this._poolAddresses && !!this._whitelist && this.isAddressInitialized
101
+ !this.isEmptyObject(this._poolAddresses) &&
102
+ Object.values(this._whitelist).every((t) => t.size > 0) &&
103
+ this.isAddressInitialized
102
104
  );
103
105
  }
104
106
 
@@ -162,23 +164,10 @@ export class ScallopConstants {
162
164
  return this._wormholeCoinTypeToCoinNameMap;
163
165
  }
164
166
 
165
- get coinNameToCoinTypeMap() {
166
- if (this.isEmptyObject(this._coinNameToCoinTypeMap))
167
- this._coinNameToCoinTypeMap = Object.fromEntries(
168
- Object.entries(this.poolAddresses)
169
- .filter(([_, value]) => !!value)
170
- .map(([_, value]) => [value!.coinName, value!.coinType])
171
- );
172
- return this._coinNameToCoinTypeMap;
173
- }
174
-
175
167
  get coinTypeToCoinNameMap() {
176
168
  if (this.isEmptyObject(this._coinTypeToCoinNameMap))
177
169
  this._coinTypeToCoinNameMap = Object.fromEntries(
178
- Object.entries(this.coinNameToCoinTypeMap).map(([key, val]) => [
179
- val,
180
- key,
181
- ])
170
+ Object.entries(this.coinTypes).map(([key, val]) => [val, key])
182
171
  );
183
172
  return this._coinTypeToCoinNameMap;
184
173
  }
@@ -291,7 +280,8 @@ export class ScallopConstants {
291
280
  async readWhiteList() {
292
281
  const response = await this.readApi<Record<keyof Whitelist, string[]>>({
293
282
  url:
294
- this.params.whitelistApiUrl ?? 'https://sui.apis.scallop.io/whitelist',
283
+ this.params.whitelistApiUrl ??
284
+ `https://sui.apis.scallop.io/pool/whitelist/${this.params.whitelistId}`,
295
285
  queryKey: queryKeys.api.getWhiteList(),
296
286
  });
297
287
 
@@ -304,7 +294,7 @@ export class ScallopConstants {
304
294
  return await this.readApi<Record<string, PoolAddress>>({
305
295
  url:
306
296
  this.params.poolAddressesApiUrl ??
307
- `https://sui.apis.scallop.io/poolAddresses`,
297
+ `https://sui.apis.scallop.io/pool/addresses`,
308
298
  queryKey: queryKeys.api.getPoolAddresses(),
309
299
  });
310
300
  }
@@ -322,20 +312,42 @@ export class ScallopConstants {
322
312
  this._whitelist = params?.forceWhitelistInterface;
323
313
  }
324
314
 
315
+ console.log({
316
+ isAddressInitialized: this.isAddressInitialized,
317
+ a: params?.forcePoolAddressInterface,
318
+ b: params?.forceWhitelistInterface,
319
+ isInitialized: this.isInitialized,
320
+ });
325
321
  if (this.isInitialized) return;
326
322
 
327
323
  const [whitelistResponse, poolAddressesResponse] = await Promise.all([
328
324
  this.readWhiteList(),
329
325
  this.readPoolAddresses(),
330
326
  ]);
327
+
331
328
  if (!this.params.forceWhitelistInterface) {
332
- this._whitelist = whitelistResponse;
329
+ this._whitelist = Object.fromEntries(
330
+ Object.entries(whitelistResponse)
331
+ .filter(([key]) => key !== 'id')
332
+ .map(([key, value]) => [
333
+ key as keyof Whitelist,
334
+ key !== 'id' ? new Set(value) : value,
335
+ ])
336
+ ) as Whitelist;
333
337
  }
334
338
  if (!this.params.forcePoolAddressInterface)
335
339
  this._poolAddresses = Object.fromEntries(
336
- Object.entries(poolAddressesResponse).filter(([key]) =>
337
- Object.values(this.whitelist).some((set) => set.has(key))
338
- )
340
+ Object.entries(poolAddressesResponse)
341
+ .filter(([key]) =>
342
+ Object.values(this.whitelist).some((set) => set.has(key))
343
+ )
344
+ .map(([key, value]) => {
345
+ const parsedValue = Object.fromEntries(
346
+ Object.entries(value).map(([k, v]) => [k, v || undefined])
347
+ );
348
+ return [key, parsedValue as PoolAddress];
349
+ })
339
350
  );
351
+ console.log({ p: this._poolAddresses });
340
352
  }
341
353
  }
@@ -109,6 +109,7 @@ export class ScallopQuery {
109
109
 
110
110
  this.cache =
111
111
  instance?.utils?.cache ??
112
+ instance?.cache ??
112
113
  new ScallopCache(this.params, {
113
114
  suiKit: this.suiKit,
114
115
  });
@@ -173,15 +174,12 @@ export class ScallopQuery {
173
174
  * @param force - Whether to force initialization.
174
175
  * @param address - ScallopAddress instance.
175
176
  */
176
- public async init(force: boolean = false, address?: ScallopAddress) {
177
- if (address && !this.address) {
178
- this.address = address;
179
- }
180
- if (force || !this.address.getAddresses()) {
181
- await this.address.read();
177
+ public async init(force: boolean = false) {
178
+ if (force || !this.constants.isInitialized) {
179
+ await this.constants.init();
182
180
  }
183
181
 
184
- await this.utils.init(force, this.address);
182
+ await this.utils.init(force);
185
183
  }
186
184
 
187
185
  /* ==================== Core Query Methods ==================== */
@@ -67,6 +67,7 @@ export class ScallopUtils {
67
67
 
68
68
  this.cache =
69
69
  instance?.constants?.cache ??
70
+ instance?.cache ??
70
71
  new ScallopCache(this.params, {
71
72
  suiKit: this.suiKit,
72
73
  });
@@ -111,10 +112,9 @@ export class ScallopUtils {
111
112
  * @param force - Whether to force initialization.
112
113
  * @param address - ScallopAddress instance.
113
114
  */
114
- public async init(force: boolean = false, address?: ScallopAddress) {
115
- if (address && !this.address) this.address = address;
116
- if (force || !this.address.getAddresses()) {
117
- await this.address.read();
115
+ public async init(force: boolean = false) {
116
+ if (force || !this.constants.isInitialized) {
117
+ await this.constants.init();
118
118
  }
119
119
  }
120
120
 
@@ -125,7 +125,10 @@ export class ScallopUtils {
125
125
  * @return Symbol string.
126
126
  */
127
127
  public parseSymbol(coinName: string) {
128
- return this.constants.poolAddresses[coinName]?.symbol ?? '';
128
+ return this.isMarketCoin(coinName)
129
+ ? (this.constants.poolAddresses[this.parseCoinName(coinName)]
130
+ ?.sCoinSymbol ?? '')
131
+ : (this.constants.poolAddresses[coinName]?.symbol ?? '');
129
132
  }
130
133
 
131
134
  /**
@@ -143,7 +146,7 @@ export class ScallopUtils {
143
146
  if (useOldMarketCoin) {
144
147
  return this.constants.coinNameToOldMarketCoinTypeMap[coinName] ?? '';
145
148
  }
146
- return this.constants.coinNameToCoinTypeMap[coinName] ?? '';
149
+ return this.constants.coinTypes[coinName] ?? '';
147
150
  }
148
151
 
149
152
  /**
@@ -174,8 +174,10 @@ export const queryBorrowIncentiveAccounts = async (
174
174
  const borrowIncentiveAccounts: BorrowIncentiveAccounts = Object.values(
175
175
  borrowIncentiveAccountsQueryData?.pool_records ?? []
176
176
  ).reduce((accounts, accountData) => {
177
- const parsedBorrowIncentiveAccount =
178
- parseOriginBorrowIncentiveAccountData(accountData);
177
+ const parsedBorrowIncentiveAccount = parseOriginBorrowIncentiveAccountData(
178
+ utils,
179
+ accountData
180
+ );
179
181
  const poolType = parsedBorrowIncentiveAccount.poolType;
180
182
  const coinName = utils.parseCoinNameFromType(poolType);
181
183
 
@@ -188,7 +188,10 @@ export const getPoolAddresses = async (
188
188
  const { symbol, metaData: coinMetadataId } =
189
189
  addressApiResponse.core.coins[coinName];
190
190
 
191
- let spoolData = undefined;
191
+ let spoolData = {
192
+ spool: '',
193
+ spoolReward: '',
194
+ };
192
195
  const _spoolData = addressApiResponse.spool.pools[`s${coinName}`];
193
196
  // @ts-ignore
194
197
  if (_spoolData) {
@@ -197,11 +200,15 @@ export const getPoolAddresses = async (
197
200
  spoolData = {
198
201
  spool,
199
202
  spoolReward,
200
- coinMetadataId,
201
203
  };
202
204
  }
203
205
 
204
- let sCoinData = undefined;
206
+ let sCoinData = {
207
+ sCoinType: '',
208
+ sCoinTreasury: '',
209
+ sCoinMetadataId: '',
210
+ sCoinSymbol: '',
211
+ };
205
212
  const sCoinName = `s${coinName}`;
206
213
  const _sCoinData = addressApiResponse.scoin.coins[sCoinName];
207
214
  if (_sCoinData) {
@@ -219,7 +226,21 @@ export const getPoolAddresses = async (
219
226
  };
220
227
  }
221
228
 
222
- const { feed: pythFeed, feedObject: pythFeedObjectId } =
229
+ let pythData = {
230
+ pythFeed: '',
231
+ pythFeedObjectId: '',
232
+ };
233
+
234
+ if (addressApiResponse.core.coins[coinName]?.oracle.pyth) {
235
+ const { feed: pythFeed, feedObject: pythFeedObjectId } =
236
+ //@ts-ignore
237
+ addressApiResponse.core.coins[coinName].oracle.pyth;
238
+ pythData = {
239
+ pythFeed,
240
+ pythFeedObjectId,
241
+ };
242
+ }
243
+ const { feed: _pythFeed, feedObject: _pythFeedObjectId } =
223
244
  //@ts-ignore
224
245
  addressApiResponse.core.coins[coinName].oracle.pyth;
225
246
 
@@ -243,18 +264,17 @@ export const getPoolAddresses = async (
243
264
  interestModel: addresses[3] ?? '',
244
265
  riskModel: addresses[4],
245
266
  borrowFeeKey: addresses[5] ?? '',
246
- supplyLimitKey: addresses[6],
247
- borrowLimitKey: addresses[7],
248
- isolatedAssetKey: addresses[8],
249
- ...(spoolData ?? {}),
250
- ...(sCoinData ?? {}),
267
+ supplyLimitKey: addresses[6] ?? '',
268
+ borrowLimitKey: addresses[7] ?? '',
269
+ isolatedAssetKey: addresses[8] ?? '',
270
+ ...spoolData,
271
+ ...sCoinData,
251
272
  sCoinName,
252
273
  coinMetadataId,
253
274
  coinType,
254
275
  spoolName,
255
276
  decimals,
256
- pythFeed,
257
- pythFeedObjectId,
277
+ ...pythData,
258
278
  flashloanFeeObject: flashloanFeeObjectIds[coinType] ?? '',
259
279
  };
260
280
 
@@ -549,7 +549,6 @@ export const getObligationAccount = async (
549
549
  borrowIncentivePool.points[
550
550
  query.utils.parseSCoinTypeNameToMarketCoinName(key)
551
551
  ];
552
-
553
552
  if (accountPoint && poolPoint) {
554
553
  let availableClaimAmount = BigNumber(0);
555
554
  let availableClaimCoin = BigNumber(0);
@@ -28,6 +28,7 @@ export type ScallopClientVeScaReturnType<T extends boolean> = T extends true
28
28
 
29
29
  export type ScallopBaseInstanceParams = {
30
30
  suiKit?: SuiKit;
31
+ cache?: ScallopCache;
31
32
  };
32
33
 
33
34
  export type ScallopCacheInstanceParams = ScallopBaseInstanceParams & {
@@ -40,6 +41,7 @@ export type ScallopAddressInstanceParams = ScallopBaseInstanceParams & {
40
41
 
41
42
  export type ScallopConstantsInstanceParams = {
42
43
  address?: ScallopAddress;
44
+ cache?: ScallopCache;
43
45
  };
44
46
 
45
47
  export type ScallopIndexerInstanceParams = {
@@ -82,6 +84,7 @@ export type ScallopIndexerParams = ScallopCacheParams & {
82
84
  export type ScallopAddressParams = ScallopCacheParams & {
83
85
  addressApiUrl?: string;
84
86
  addressId: string;
87
+ whitelistId: string;
85
88
  auth?: string;
86
89
  network?: NetworkType;
87
90
  forceAddressesInterface?: Partial<Record<NetworkType, AddressesInterface>>;
@@ -1,5 +1,5 @@
1
1
  import BigNumber from 'bignumber.js';
2
- import { normalizeStructTag, parseStructTag } from '@mysten/sui/utils';
2
+ import { normalizeStructTag } from '@mysten/sui/utils';
3
3
  import type { ScallopUtils } from '../models';
4
4
  import type {
5
5
  OriginMarketPoolData,
@@ -581,6 +581,7 @@ export const parseOriginBorrowIncentiveAccountPoolPointData = (
581
581
  * @return Parsed borrow incentive account data
582
582
  */
583
583
  export const parseOriginBorrowIncentiveAccountData = (
584
+ utils: ScallopUtils,
584
585
  originBorrowIncentiveAccountData: OriginBorrowIncentiveAccountData
585
586
  ): ParsedBorrowIncentiveAccountData => {
586
587
  return {
@@ -591,9 +592,7 @@ export const parseOriginBorrowIncentiveAccountData = (
591
592
  pointList: originBorrowIncentiveAccountData.points_list.reduce(
592
593
  (acc, point) => {
593
594
  const parsed = parseOriginBorrowIncentiveAccountPoolPointData(point);
594
- const name = parseStructTag(
595
- parsed.pointType
596
- ).name.toLowerCase() as string;
595
+ const name = utils.parseCoinNameFromType(parsed.pointType);
597
596
  acc[name] = parsed;
598
597
  return acc;
599
598
  },
File without changes