@scallop-io/sui-scallop-sdk 0.44.28 → 0.46.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.
Files changed (60) hide show
  1. package/dist/builders/borrowIncentiveBuilder.d.ts +1 -1
  2. package/dist/builders/referralBuilder.d.ts +12 -0
  3. package/dist/constants/cache.d.ts +8 -0
  4. package/dist/constants/common.d.ts +1 -1
  5. package/dist/index.js +890 -419
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +967 -493
  8. package/dist/index.mjs.map +1 -1
  9. package/dist/models/index.d.ts +1 -0
  10. package/dist/models/scallop.d.ts +7 -1
  11. package/dist/models/scallopAddress.d.ts +4 -2
  12. package/dist/models/scallopBuilder.d.ts +2 -0
  13. package/dist/models/scallopCache.d.ts +75 -0
  14. package/dist/models/scallopClient.d.ts +2 -0
  15. package/dist/models/scallopIndexer.d.ts +5 -3
  16. package/dist/models/scallopQuery.d.ts +28 -28
  17. package/dist/models/scallopUtils.d.ts +1 -0
  18. package/dist/queries/coreQuery.d.ts +3 -29
  19. package/dist/queries/index.d.ts +1 -0
  20. package/dist/queries/priceQuery.d.ts +3 -1
  21. package/dist/queries/referralQuery.d.ts +7 -0
  22. package/dist/queries/vescaQuery.d.ts +6 -2
  23. package/dist/types/address.d.ts +15 -0
  24. package/dist/types/builder/core.d.ts +2 -0
  25. package/dist/types/builder/index.d.ts +2 -1
  26. package/dist/types/builder/referral.d.ts +30 -0
  27. package/dist/types/builder/vesca.d.ts +1 -0
  28. package/dist/types/model.d.ts +2 -0
  29. package/package.json +8 -6
  30. package/src/builders/borrowIncentiveBuilder.ts +12 -21
  31. package/src/builders/coreBuilder.ts +54 -0
  32. package/src/builders/index.ts +5 -2
  33. package/src/builders/referralBuilder.ts +178 -0
  34. package/src/builders/vescaBuilder.ts +8 -6
  35. package/src/constants/cache.ts +15 -0
  36. package/src/constants/common.ts +9 -2
  37. package/src/constants/vesca.ts +1 -3
  38. package/src/models/index.ts +1 -0
  39. package/src/models/scallop.ts +26 -7
  40. package/src/models/scallopAddress.ts +87 -38
  41. package/src/models/scallopBuilder.ts +14 -4
  42. package/src/models/scallopCache.ts +285 -0
  43. package/src/models/scallopClient.ts +15 -4
  44. package/src/models/scallopIndexer.ts +58 -84
  45. package/src/models/scallopQuery.ts +54 -5
  46. package/src/models/scallopUtils.ts +66 -37
  47. package/src/queries/borrowIncentiveQuery.ts +6 -12
  48. package/src/queries/coreQuery.ts +83 -260
  49. package/src/queries/index.ts +1 -0
  50. package/src/queries/priceQuery.ts +48 -9
  51. package/src/queries/referralQuery.ts +27 -0
  52. package/src/queries/spoolQuery.ts +20 -27
  53. package/src/queries/vescaQuery.ts +95 -17
  54. package/src/types/address.ts +15 -0
  55. package/src/types/builder/core.ts +14 -0
  56. package/src/types/builder/index.ts +2 -0
  57. package/src/types/builder/referral.ts +51 -0
  58. package/src/types/builder/vesca.ts +1 -0
  59. package/src/types/model.ts +2 -0
  60. package/src/types/query/borrowIncentive.ts +0 -107
@@ -70,6 +70,10 @@ const generateCoreNormalMethod: GenerateCoreNormalMethod = ({
70
70
  coinDecimalsRegistry: builder.address.get('core.coinDecimalsRegistry'),
71
71
  xOracle: builder.address.get('core.oracles.xOracle'),
72
72
  };
73
+
74
+ const referralPkgId = builder.address.get('referral.id');
75
+ const referralWitnessType = `${referralPkgId}::scallop_referral_program::REFERRAL_WITNESS`;
76
+
73
77
  return {
74
78
  openObligation: () =>
75
79
  txBlock.moveCall(
@@ -160,6 +164,30 @@ const generateCoreNormalMethod: GenerateCoreNormalMethod = ({
160
164
  [coinType]
161
165
  );
162
166
  },
167
+ borrowWithReferral: (
168
+ obligation,
169
+ obligationKey,
170
+ borrowReferral,
171
+ amount,
172
+ poolCoinName
173
+ ) => {
174
+ const coinType = builder.utils.parseCoinType(poolCoinName);
175
+ return txBlock.moveCall(
176
+ `${coreIds.protocolPkg}::borrow::borrow_with_referral`,
177
+ [
178
+ coreIds.version,
179
+ obligation,
180
+ obligationKey,
181
+ coreIds.market,
182
+ coreIds.coinDecimalsRegistry,
183
+ borrowReferral,
184
+ amount,
185
+ coreIds.xOracle,
186
+ SUI_CLOCK_OBJECT_ID,
187
+ ],
188
+ [coinType, referralWitnessType]
189
+ );
190
+ },
163
191
  borrowEntry: (obligation, obligationKey, amount, poolCoinName) => {
164
192
  const coinType = builder.utils.parseCoinType(poolCoinName);
165
193
  return txBlock.moveCall(
@@ -319,6 +347,32 @@ const generateCoreQuickMethod: GenerateCoreQuickMethod = ({
319
347
  poolCoinName
320
348
  );
321
349
  },
350
+ borrowWithReferralQuick: async (
351
+ amount,
352
+ poolCoinName,
353
+ borrowReferral,
354
+ obligationId,
355
+ obligationKey
356
+ ) => {
357
+ const obligationInfo = await requireObligationInfo(
358
+ builder,
359
+ txBlock,
360
+ obligationId,
361
+ obligationKey
362
+ );
363
+ const obligationCoinNames = await builder.utils.getObligationCoinNames(
364
+ obligationInfo.obligationId
365
+ );
366
+ const updateCoinNames = [...obligationCoinNames, poolCoinName];
367
+ await updateOracles(builder, txBlock, updateCoinNames);
368
+ return txBlock.borrowWithReferral(
369
+ obligationInfo.obligationId,
370
+ obligationInfo.obligationKey as SuiAddressArg,
371
+ borrowReferral,
372
+ amount,
373
+ poolCoinName
374
+ );
375
+ },
322
376
  repayQuick: async (amount, poolCoinName, obligationId) => {
323
377
  const sender = requireSender(txBlock);
324
378
  const obligationInfo = await requireObligationInfo(
@@ -6,6 +6,7 @@ import { newBorrowIncentiveTxBlock } from './borrowIncentiveBuilder';
6
6
  import { newVeScaTxBlock } from './vescaBuilder';
7
7
  import type { ScallopBuilder } from '../models';
8
8
  import type { ScallopTxBlock } from '../types';
9
+ import { newReferralTxBlock } from './referralBuilder';
9
10
 
10
11
  /**
11
12
  * Create a new ScallopTxBlock instance.
@@ -23,15 +24,17 @@ export const newScallopTxBlock = (
23
24
  builder,
24
25
  vescaTxBlock
25
26
  );
26
- const spoolTxBlock = newSpoolTxBlock(builder, borrowIncentiveTxBlock);
27
+ const referralTxBlock = newReferralTxBlock(builder, borrowIncentiveTxBlock);
28
+ const spoolTxBlock = newSpoolTxBlock(builder, referralTxBlock);
27
29
  const coreTxBlock = newCoreTxBlock(builder, spoolTxBlock);
28
-
29
30
  return new Proxy(coreTxBlock, {
30
31
  get: (target, prop) => {
31
32
  if (prop in vescaTxBlock) {
32
33
  return Reflect.get(vescaTxBlock, prop);
33
34
  } else if (prop in borrowIncentiveTxBlock) {
34
35
  return Reflect.get(borrowIncentiveTxBlock, prop);
36
+ } else if (prop in referralTxBlock) {
37
+ return Reflect.get(referralTxBlock, prop);
35
38
  } else if (prop in spoolTxBlock) {
36
39
  return Reflect.get(spoolTxBlock, prop);
37
40
  }
@@ -0,0 +1,178 @@
1
+ import { ScallopBuilder } from 'src/models';
2
+ import { ScallopTxBlock, SupportCoins, SupportPoolCoins } from 'src/types';
3
+ import {
4
+ SUI_CLOCK_OBJECT_ID,
5
+ SuiTxBlock as SuiKitTxBlock,
6
+ SuiObjectArg,
7
+ TransactionBlock,
8
+ } from '@scallop-io/sui-kit';
9
+ import {
10
+ GenerateReferralNormalMethod,
11
+ GenerateReferralQuickMethod,
12
+ ReferralIds,
13
+ ReferralTxBlock,
14
+ SuiTxBlockWithReferralNormalMethods,
15
+ } from 'src/types/builder/referral';
16
+ import { SUPPORT_POOLS } from 'src/constants';
17
+ import { requireSender } from 'src/utils';
18
+
19
+ const generateReferralNormalMethod: GenerateReferralNormalMethod = ({
20
+ builder,
21
+ txBlock,
22
+ }) => {
23
+ const referralIds: ReferralIds = {
24
+ referralPgkId: builder.address.get('referral.id'),
25
+ referralBindings: builder.address.get('referral.referralBindings'),
26
+ referralRevenuePool: builder.address.get('referral.referralRevenuePool'),
27
+ authorizedWitnessList: builder.address.get(
28
+ 'referral.authorizedWitnessList'
29
+ ),
30
+ referralTiers: builder.address.get('referral.referralTiers'),
31
+ version: builder.address.get('referral.version'),
32
+ };
33
+
34
+ const veScaTable = builder.address.get('vesca.table');
35
+
36
+ return {
37
+ bindToReferral: (veScaKeyId: string) => {
38
+ return txBlock.moveCall(
39
+ `${referralIds.referralPgkId}::referral_bindings::bind_ve_sca_referrer`,
40
+ [
41
+ referralIds.referralBindings,
42
+ txBlock.pure(veScaKeyId),
43
+ veScaTable,
44
+ SUI_CLOCK_OBJECT_ID,
45
+ ],
46
+ []
47
+ );
48
+ },
49
+ claimReferralTicket: (poolCoinName: SupportCoins) => {
50
+ const coinType = builder.utils.parseCoinType(poolCoinName);
51
+ return txBlock.moveCall(
52
+ `${referralIds.referralPgkId}::scallop_referral_program::claim_ve_sca_referral_ticket`,
53
+ [
54
+ referralIds.version,
55
+ veScaTable,
56
+ referralIds.referralBindings,
57
+ referralIds.authorizedWitnessList,
58
+ referralIds.referralTiers,
59
+ SUI_CLOCK_OBJECT_ID,
60
+ ],
61
+ [coinType]
62
+ );
63
+ },
64
+ burnReferralTicket: (ticket: SuiObjectArg, poolCoinName: SupportCoins) => {
65
+ const coinType = builder.utils.parseCoinType(poolCoinName);
66
+ return txBlock.moveCall(
67
+ `${referralIds.referralPgkId}::scallop_referral_program::burn_ve_sca_referral_ticket`,
68
+ [
69
+ referralIds.version,
70
+ ticket,
71
+ referralIds.referralRevenuePool,
72
+ SUI_CLOCK_OBJECT_ID,
73
+ ],
74
+ [coinType]
75
+ );
76
+ },
77
+ claimReferralRevenue: (
78
+ veScaKey: SuiObjectArg,
79
+ poolCoinName: SupportCoins
80
+ ) => {
81
+ const coinType = builder.utils.parseCoinType(poolCoinName);
82
+ return txBlock.moveCall(
83
+ `${referralIds.referralPgkId}::referral_revenue_pool::claim_revenue_with_ve_sca_key`,
84
+ [
85
+ referralIds.version,
86
+ referralIds.referralRevenuePool,
87
+ veScaKey,
88
+ SUI_CLOCK_OBJECT_ID,
89
+ ],
90
+ [coinType]
91
+ );
92
+ },
93
+ };
94
+ };
95
+
96
+ const generateReferralQuickMethod: GenerateReferralQuickMethod = ({
97
+ builder,
98
+ txBlock,
99
+ }) => {
100
+ return {
101
+ claimReferralRevenueQuick: async (
102
+ veScaKey: SuiObjectArg,
103
+ coinNames: SupportPoolCoins[] = [...SUPPORT_POOLS]
104
+ ) => {
105
+ const sender = requireSender(txBlock);
106
+ const objToTransfer: SuiObjectArg[] = [];
107
+ for (const coinName of coinNames) {
108
+ if (coinName === 'sui') {
109
+ const rewardCoin = txBlock.claimReferralRevenue(veScaKey, coinName);
110
+ objToTransfer.push(rewardCoin);
111
+ } else {
112
+ // get the matching user coin if exists
113
+ const coins = await builder.suiKit.suiInteractor.selectCoins(
114
+ sender,
115
+ Infinity,
116
+ builder.utils.parseCoinType(coinName)
117
+ );
118
+
119
+ const rewardCoin = txBlock.claimReferralRevenue(veScaKey, coinName);
120
+ if (coins.length > 0) {
121
+ txBlock.mergeCoins(rewardCoin, coins);
122
+ }
123
+ objToTransfer.push(rewardCoin);
124
+ }
125
+ }
126
+ if (objToTransfer.length > 0) {
127
+ txBlock.transferObjects(objToTransfer, sender);
128
+ }
129
+ },
130
+ };
131
+ };
132
+
133
+ /**
134
+ * Create an enhanced transaction block instance for interaction with borrow incentive modules of the Scallop contract.
135
+ *
136
+ * @param builder - Scallop builder instance.
137
+ * @param initTxBlock - Scallop txBlock, txBlock created by SuiKit, or original transaction block.
138
+ * @return Scallop borrow incentive txBlock.
139
+ */
140
+ export const newReferralTxBlock = (
141
+ builder: ScallopBuilder,
142
+ initTxBlock?: ScallopTxBlock | SuiKitTxBlock | TransactionBlock
143
+ ) => {
144
+ const txBlock =
145
+ initTxBlock instanceof TransactionBlock
146
+ ? new SuiKitTxBlock(initTxBlock)
147
+ : initTxBlock
148
+ ? initTxBlock
149
+ : new SuiKitTxBlock();
150
+
151
+ const normalMethod = generateReferralNormalMethod({
152
+ builder,
153
+ txBlock,
154
+ });
155
+
156
+ const normalTxBlock = new Proxy(txBlock, {
157
+ get: (target, prop) => {
158
+ if (prop in normalMethod) {
159
+ return Reflect.get(normalMethod, prop);
160
+ }
161
+ return Reflect.get(target, prop);
162
+ },
163
+ }) as SuiTxBlockWithReferralNormalMethods;
164
+
165
+ const quickMethod = generateReferralQuickMethod({
166
+ builder,
167
+ txBlock: normalTxBlock,
168
+ });
169
+
170
+ return new Proxy(normalTxBlock, {
171
+ get: (target, prop) => {
172
+ if (prop in quickMethod) {
173
+ return Reflect.get(quickMethod, prop);
174
+ }
175
+ return Reflect.get(target, prop);
176
+ },
177
+ }) as ReferralTxBlock;
178
+ };
@@ -65,11 +65,7 @@ export const requireVeSca = async (
65
65
  return undefined;
66
66
  }
67
67
 
68
- return veScas.reduce(
69
- (prev, acc) =>
70
- acc.currentVeScaBalance > prev.currentVeScaBalance ? acc : prev,
71
- veScas[0]
72
- ); // return veSCA with highest veSCA balance
68
+ return veScas[0]; // return veSCA with highest veSCA balance
73
69
  };
74
70
 
75
71
  /**
@@ -161,6 +157,13 @@ const generateNormalVeScaMethod: GenerateVeScaNormalMethod = ({
161
157
  []
162
158
  );
163
159
  },
160
+ mintEmptyVeSca: () => {
161
+ return txBlock.moveCall(
162
+ `${veScaIds.pkgId}::ve_sca::mint_ve_sca_placeholder_key`,
163
+ [veScaIds.config, veScaIds.table],
164
+ []
165
+ );
166
+ },
164
167
  };
165
168
  };
166
169
 
@@ -378,7 +381,6 @@ export const newVeScaTxBlock = (
378
381
  },
379
382
  }) as SuiTxBlockWithVeScaNormalMethods;
380
383
 
381
- // TODO: Add quickMethod for veSCA
382
384
  const quickMethod = generateQuickVeScaMethod({
383
385
  builder,
384
386
  txBlock: normalTxBlock,
@@ -0,0 +1,15 @@
1
+ import { QueryClientConfig } from '@tanstack/query-core';
2
+
3
+ /**
4
+ * Default cache options for the QueryClient.
5
+ * @type {QueryClientConfig}
6
+ * @description Default cache options for the QueryClient
7
+ * We set the default to 5s to prevent duplicate requests from being requested (e.g. query MarketObject, etc.)
8
+ */
9
+ export const DEFAULT_CACHE_OPTIONS: QueryClientConfig = {
10
+ defaultOptions: {
11
+ queries: {
12
+ staleTime: 3000,
13
+ },
14
+ },
15
+ };
@@ -3,18 +3,25 @@ export const SDK_API_BASE_URL = 'https://sdk.api.scallop.io';
3
3
 
4
4
  export const IS_VE_SCA_TEST = false;
5
5
 
6
+ // export const ADDRESSES_ID = '';
6
7
  export const ADDRESSES_ID = IS_VE_SCA_TEST
7
- ? ('65fb07c39c845425d71d7b18' as const)
8
- : ('6601955b8b0024600a917079' as const);
8
+ ? // ? ('65fb07c39c845425d71d7b18' as const)
9
+ ('65fb07c39c845425d71d7b18' as const)
10
+ : ('664dfe22898c36c159e28bc8' as const);
11
+ // : ('6601955b8b0024600a917079' as const);
9
12
  // : ('6462a088a7ace142bb6d7e9b' as const);
10
13
 
11
14
  export const PROTOCOL_OBJECT_ID = IS_VE_SCA_TEST
12
15
  ? ('0xc9f859f98ca352a11b97a038c4b4162bee437b8df8caa047990fe9cb03d4f778' as const)
13
16
  : ('0xefe8b36d5b2e43728cc323298626b83177803521d195cfb11e15b910e892fddf' as const);
17
+ // export const PROTOCOL_OBJECT_ID =
18
+ // '0x87ddec2984645dbbe2403a509cc6edf393a43acdba9b77d45da2bcbefcf733c1' as const;
14
19
 
15
20
  export const BORROW_FEE_PROTOCOL_ID = IS_VE_SCA_TEST
16
21
  ? ('0xc9f859f98ca352a11b97a038c4b4162bee437b8df8caa047990fe9cb03d4f778' as const)
17
22
  : ('0xc38f849e81cfe46d4e4320f508ea7dda42934a329d5a6571bb4c3cb6ea63f5da' as const); // test environment
23
+ // export const BORROW_FEE_PROTOCOL_ID =
24
+ // '0x87ddec2984645dbbe2403a509cc6edf393a43acdba9b77d45da2bcbefcf733c1' as const;
18
25
 
19
26
  export const SCA_COIN_TYPE = IS_VE_SCA_TEST
20
27
  ? (`0x6cd813061a3adf3602b76545f076205f0c8e7ec1d3b1eab9a1da7992c18c0524::sca::SCA` as const)
@@ -1,7 +1,5 @@
1
- import { IS_VE_SCA_TEST } from './common';
2
-
3
1
  export const UNLOCK_ROUND_DURATION = 60 * 60 * 24; // 1 days in seconds
4
- export const MAX_LOCK_ROUNDS: number = IS_VE_SCA_TEST ? 9 : 1460; // 4 years in days (or 9 days for testing)
2
+ export const MAX_LOCK_ROUNDS: number = 1460; // 4 years in days (or 9 days for testing)
5
3
  export const MAX_LOCK_DURATION: number =
6
4
  MAX_LOCK_ROUNDS * UNLOCK_ROUND_DURATION; // 4 years in seconds
7
5
 
@@ -5,3 +5,4 @@ export * from './scallopBuilder';
5
5
  export * from './scallopQuery';
6
6
  export * from './scallopUtils';
7
7
  export * from './scallopIndexer';
8
+ export * from './scallopCache';
@@ -7,8 +7,14 @@ import { ScallopUtils } from './scallopUtils';
7
7
  import { ADDRESSES_ID } from '../constants';
8
8
  import type { ScallopParams } from '../types/';
9
9
  import { ScallopIndexer } from './scallopIndexer';
10
+ import { ScallopCache } from './scallopCache';
11
+ import { QueryClientConfig } from '@tanstack/query-core';
12
+ import { DEFAULT_CACHE_OPTIONS } from 'src/constants/cache';
10
13
 
11
14
  /**
15
+ * @argument params - The parameters for the Scallop instance.
16
+ * @argument cacheOptions - The cache options for the QueryClient.
17
+ *
12
18
  * @description
13
19
  * The main instance that controls interaction with the Scallop contract.
14
20
  *
@@ -25,16 +31,24 @@ import { ScallopIndexer } from './scallopIndexer';
25
31
  export class Scallop {
26
32
  public params: ScallopParams;
27
33
  public suiKit: SuiKit;
34
+ public cache: ScallopCache;
28
35
 
29
36
  private _address: ScallopAddress;
30
37
 
31
- public constructor(params: ScallopParams) {
38
+ public constructor(params: ScallopParams, cacheOptions?: QueryClientConfig) {
32
39
  this.params = params;
33
40
  this.suiKit = new SuiKit(params);
34
- this._address = new ScallopAddress({
35
- id: params?.addressesId || ADDRESSES_ID,
36
- network: params?.networkType,
37
- });
41
+ this.cache = new ScallopCache(
42
+ cacheOptions ?? DEFAULT_CACHE_OPTIONS,
43
+ this.suiKit
44
+ );
45
+ this._address = new ScallopAddress(
46
+ {
47
+ id: params?.addressesId || ADDRESSES_ID,
48
+ network: params?.networkType,
49
+ },
50
+ this.cache
51
+ );
38
52
  }
39
53
 
40
54
  /**
@@ -59,6 +73,7 @@ export class Scallop {
59
73
  const scallopBuilder = new ScallopBuilder(this.params, {
60
74
  suiKit: this.suiKit,
61
75
  address: this._address,
76
+ cache: this.cache,
62
77
  });
63
78
 
64
79
  return scallopBuilder;
@@ -74,7 +89,7 @@ export class Scallop {
74
89
  if (!this._address.getAddresses()) await this._address.read();
75
90
  const scallopClient = new ScallopClient(
76
91
  { ...this.params, walletAddress },
77
- { suiKit: this.suiKit, address: this._address }
92
+ { suiKit: this.suiKit, address: this._address, cache: this.cache }
78
93
  );
79
94
 
80
95
  return scallopClient;
@@ -90,6 +105,7 @@ export class Scallop {
90
105
  const scallopQuery = new ScallopQuery(this.params, {
91
106
  suiKit: this.suiKit,
92
107
  address: this._address,
108
+ cache: this.cache,
93
109
  });
94
110
 
95
111
  return scallopQuery;
@@ -101,7 +117,9 @@ export class Scallop {
101
117
  * @return Scallop Indexer.
102
118
  */
103
119
  public async createScallopIndexer() {
104
- const scallopIndexer = new ScallopIndexer();
120
+ const scallopIndexer = new ScallopIndexer(this.params, {
121
+ cache: this.cache,
122
+ });
105
123
 
106
124
  return scallopIndexer;
107
125
  }
@@ -116,6 +134,7 @@ export class Scallop {
116
134
  const scallopUtils = new ScallopUtils(this.params, {
117
135
  suiKit: this.suiKit,
118
136
  address: this._address,
137
+ cache: this.cache,
119
138
  });
120
139
 
121
140
  return scallopUtils;