@scallop-io/sui-scallop-sdk 2.2.3-pyth-sponsored-transaction-alpha.1 → 2.3.0-lst-x-oracle-alpha.2

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 (74) hide show
  1. package/dist/address-CW2IpaLn.d.ts +165 -0
  2. package/dist/chunk-E3URAUAC.js +25 -0
  3. package/dist/chunk-YA77R5GT.mjs +5 -0
  4. package/dist/constants/index.d.mts +262 -0
  5. package/dist/constants/index.d.ts +262 -0
  6. package/dist/constants/index.js +86 -0
  7. package/dist/constants/index.mjs +5 -0
  8. package/dist/index.d.mts +11 -3242
  9. package/dist/index.d.ts +11 -3242
  10. package/dist/index.js +0 -47
  11. package/dist/index.mjs +0 -14
  12. package/dist/models/index.d.mts +393 -0
  13. package/dist/models/index.d.ts +393 -0
  14. package/dist/models/index.js +34 -0
  15. package/dist/models/index.mjs +16 -0
  16. package/dist/queryKeys-AaZqDYle.d.ts +43 -0
  17. package/dist/scallopUtils-BZLZd2z8.d.ts +2404 -0
  18. package/dist/types/index.d.mts +9 -0
  19. package/dist/types/index.d.ts +9 -0
  20. package/dist/types/index.js +2 -0
  21. package/dist/types/index.mjs +1 -0
  22. package/package.json +31 -4
  23. package/src/builders/borrowIncentiveBuilder.ts +2 -2
  24. package/src/builders/coreBuilder.ts +5 -14
  25. package/src/builders/index.ts +2 -2
  26. package/src/builders/oracles/error.ts +29 -0
  27. package/src/builders/oracles/index.ts +114 -365
  28. package/src/builders/oracles/oraclePackageRegistry.ts +323 -0
  29. package/src/builders/oracles/priceFeedUpdater.ts +112 -0
  30. package/src/builders/oracles/priceUpdateRequester.ts +50 -0
  31. package/src/builders/oracles/xOracleUpdateStrategy.ts +178 -0
  32. package/src/builders/oracles/xOracleUpdater.ts +160 -0
  33. package/src/builders/vescaBuilder.ts +2 -2
  34. package/src/constants/api.ts +2 -0
  35. package/src/constants/common.ts +0 -3
  36. package/src/constants/index.ts +2 -1
  37. package/src/constants/package.ts +14 -0
  38. package/src/constants/testAddress.ts +190 -105
  39. package/src/constants/xoracle.ts +8 -2
  40. package/src/index.ts +2 -12
  41. package/src/models/index.ts +2 -1
  42. package/src/models/interface.ts +6 -6
  43. package/src/models/scallopAddress.ts +24 -4
  44. package/src/models/scallopBuilder.ts +4 -4
  45. package/src/models/scallopClient.ts +1 -1
  46. package/src/models/scallopConstants.ts +2 -2
  47. package/src/models/scallopIndexer.ts +3 -2
  48. package/src/models/scallopQuery.ts +52 -36
  49. package/src/models/scallopQueryClient.ts +1 -1
  50. package/src/models/scallopSuiKit.ts +1 -1
  51. package/src/models/scallopUtils.ts +5 -8
  52. package/src/queries/borrowIncentiveQuery.ts +2 -3
  53. package/src/queries/coreQuery.ts +2 -3
  54. package/src/queries/index.ts +4 -3
  55. package/src/queries/loyaltyProgramQuery.ts +1 -1
  56. package/src/queries/portfolioQuery.ts +13 -13
  57. package/src/queries/sCoinQuery.ts +2 -2
  58. package/src/queries/spoolQuery.ts +8 -8
  59. package/src/queries/vescaQuery.ts +2 -2
  60. package/src/queries/xOracleQuery.ts +20 -3
  61. package/src/types/address.ts +85 -47
  62. package/src/types/builder/core.ts +2 -4
  63. package/src/types/constant/index.ts +2 -1
  64. package/src/types/constant/package.ts +3 -0
  65. package/src/types/constant/xOracle.ts +10 -7
  66. package/src/types/query/index.ts +0 -1
  67. package/src/utils/builder.ts +1 -1
  68. package/src/utils/index.ts +2 -2
  69. package/src/utils/query.ts +6 -6
  70. package/src/utils/util.ts +1 -1
  71. package/src/builders/oracles/pyth.ts +0 -52
  72. package/src/queries/ownerQuery.ts +0 -32
  73. package/src/types/constant/enum.ts +0 -64
  74. package/src/types/query/sCoin.ts +0 -1
@@ -1,14 +1,18 @@
1
- import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils';
2
- import type { TransactionArgument } from '@mysten/sui/transactions';
1
+ import { X_ORACLE_LIST } from 'src/constants/xoracle';
2
+ import { createXOracleUpdater, IXOracleUpdater } from './xOracleUpdater';
3
+ import {
4
+ createPackageRegistry,
5
+ XOraclePackageRegistry,
6
+ } from './oraclePackageRegistry';
7
+ import {
8
+ createPriceFeedUpdater,
9
+ PriceFeedUpdateOptions,
10
+ } from './priceFeedUpdater';
3
11
  import type { SuiTxBlock as SuiKitTxBlock } from '@scallop-io/sui-kit';
4
12
  import type { ScallopBuilder } from 'src/models';
5
- import type {
6
- SupportOracleType,
7
- xOracleRules,
8
- xOracleRuleType,
9
- } from 'src/types';
10
- import { xOracleList as X_ORACLE_LIST } from 'src/constants';
11
- import { updatePythPriceFeeds } from './pyth';
13
+ import type { SupportOracleType, xOracleRuleType } from 'src/types';
14
+ import { PriceUpdateRequester } from './priceUpdateRequester';
15
+ import { UnsupportedOracleError } from './error';
12
16
 
13
17
  /**
14
18
  * Update the price of the oracle for multiple coin.
@@ -22,381 +26,126 @@ export const updateOracles = async (
22
26
  builder: ScallopBuilder,
23
27
  txBlock: SuiKitTxBlock,
24
28
  assetCoinNames: string[] = [...builder.constants.whitelist.lending],
25
- options?: {
26
- usePythPullModel?: boolean;
27
- useOnChainXOracleList?: boolean;
28
- sponsoredFeeds?: string[];
29
- isSponsoredTx?: boolean;
30
- }
29
+ options: Partial<PriceFeedUpdateOptions> = {}
31
30
  ) => {
32
- const usePythPullModel =
33
- options?.usePythPullModel ?? builder.usePythPullModel;
34
- const useOnChainXOracleList =
35
- options?.useOnChainXOracleList ?? builder.useOnChainXOracleList;
36
- const sponsoredFeeds = new Set(
37
- options?.sponsoredFeeds ?? builder.sponsoredFeeds
38
- );
39
- const isSponsoredTx = options?.isSponsoredTx ?? false;
40
-
41
- // Validate the sponsoredFeeds content.
42
- sponsoredFeeds.forEach((feed) => {
43
- if (!builder.constants.whitelist.lending.has(feed)) {
44
- throw new Error(`${feed} is not valid feed`);
45
- }
46
- });
47
-
48
- const xOracleList = useOnChainXOracleList
31
+ // Validate the options
32
+ assetCoinNames = [...new Set(assetCoinNames)]; // Remove duplicates
33
+ const mergedOptions = {
34
+ usePythPullModel: builder.usePythPullModel,
35
+ useOnChainXOracleList: builder.useOnChainXOracleList,
36
+ pythSponsoredFeeds: builder.pythSponsoredFeeds,
37
+ ...options,
38
+ } as PriceFeedUpdateOptions;
39
+
40
+ // Retrieve the xOracle list from the builder or use the default one.
41
+ const xOracleList = mergedOptions.useOnChainXOracleList
49
42
  ? await builder.query.getAssetOracles()
50
- : X_ORACLE_LIST;
43
+ : X_ORACLE_LIST; // Record<coinName, Record<xOracleRuleType, SupportOracleType[]>>
44
+
45
+ const xOracleListAsSet = Object.entries(xOracleList).reduce(
46
+ (acc, [coinName, { primary, secondary }]) => {
47
+ acc[coinName] = {
48
+ primary: new Set(primary),
49
+ secondary: new Set(secondary),
50
+ };
51
+ return acc;
52
+ },
53
+ {} as Record<string, Record<xOracleRuleType, Set<SupportOracleType>>>
54
+ );
51
55
 
52
- // const rules: SupportOracleType[] = builder.isTestnet ? ['pyth'] : ['pyth'];
53
- const flattenedRules = new Set(
56
+ // Get all oracle types no duplicates from all rules (primary and secondary).
57
+ const oracleTypesSet = new Set(
54
58
  Object.values(xOracleList).flatMap(({ primary, secondary }) => [
55
59
  ...primary,
56
60
  ...secondary,
57
61
  ])
58
62
  );
59
63
 
60
- const filterAssetCoinNames = (
64
+ const getRuleType = (
61
65
  assetCoinName: string,
62
- rule: SupportOracleType
66
+ oracleType: SupportOracleType
63
67
  ) => {
64
- const assetXOracle = xOracleList[assetCoinName];
65
- return (
66
- assetXOracle &&
67
- (assetXOracle.primary.includes(rule) ||
68
- assetXOracle.secondary.includes(rule))
69
- );
68
+ const ruleSet = xOracleListAsSet[assetCoinName];
69
+ if (!ruleSet)
70
+ return {
71
+ isPrimary: false,
72
+ isSecondary: false,
73
+ };
74
+
75
+ const { primary, secondary } = ruleSet;
76
+ const isPrimary = primary.has(oracleType);
77
+ const isSecondary = secondary.has(oracleType);
78
+
79
+ return {
80
+ isPrimary,
81
+ isSecondary,
82
+ };
70
83
  };
71
84
 
72
- const updateAssetCoinNames = [...new Set(assetCoinNames)];
73
- const pythAssetCoinNames = updateAssetCoinNames.filter((assetCoinName) =>
74
- filterAssetCoinNames(assetCoinName, 'pyth')
75
- );
76
-
77
- if (flattenedRules.has('pyth')) {
78
- const needToUpdatePythPriceFeeds: string[] = [];
79
- for (const pythAssetCoinName of pythAssetCoinNames) {
80
- /**
81
- * Check if the Pyth pull model is not used but the feed is not sponsored.
82
- * This is used to determine if we should update the Pyth price feeds.
83
- */
84
- const notUsingPullAndNotSponsored =
85
- !usePythPullModel && !sponsoredFeeds.has(pythAssetCoinName);
86
-
87
- if (usePythPullModel || notUsingPullAndNotSponsored) {
88
- needToUpdatePythPriceFeeds.push(pythAssetCoinName);
89
- }
90
- }
91
-
92
- if (needToUpdatePythPriceFeeds.length > 0) {
93
- await updatePythPriceFeeds(
94
- builder,
95
- needToUpdatePythPriceFeeds,
85
+ // Create a package registry for xOracle
86
+ const xOraclePackageRegistry = new XOraclePackageRegistry(builder.utils);
87
+
88
+ // Create xOracle updater for each oracle type
89
+ const xOracleUpdaters: Partial<Record<SupportOracleType, IXOracleUpdater>> =
90
+ {};
91
+
92
+ // Iterate through oracle set
93
+ for (const oracleType of oracleTypesSet) {
94
+ const filteredAssetCoinNames = assetCoinNames.filter((assetCoinName) => {
95
+ const { isPrimary, isSecondary } = getRuleType(assetCoinName, oracleType);
96
+ if (!isPrimary && !isSecondary) return false;
97
+ return true;
98
+ });
99
+
100
+ if (filteredAssetCoinNames.length > 0) {
101
+ // Update all necessary price feeds
102
+ await createPriceFeedUpdater(
103
+ oracleType,
96
104
  txBlock,
97
- isSponsoredTx
98
- );
105
+ builder,
106
+ filteredAssetCoinNames,
107
+ mergedOptions
108
+ ).updatePriceFeeds();
99
109
  }
100
- }
101
110
 
102
- // Remove duplicate coin names.
103
- for (const assetCoinName of updateAssetCoinNames) {
104
- updateOracle(builder, txBlock, assetCoinName, xOracleList[assetCoinName]);
111
+ // Create the xOracle updater for the current oracle type
112
+ xOracleUpdaters[oracleType] = createXOracleUpdater(
113
+ txBlock,
114
+ createPackageRegistry(oracleType, xOraclePackageRegistry)
115
+ );
105
116
  }
106
- };
107
-
108
- /**
109
- * Update the price of the oracle for specific coin.
110
- *
111
- * @param builder - The scallop builder.
112
- * @param txBlock - TxBlock created by SuiKit.
113
- * @param assetCoinName - Specific support asset coin name.
114
- */
115
- const updateOracle = (
116
- builder: ScallopBuilder,
117
- txBlock: SuiKitTxBlock,
118
- assetCoinName: string,
119
- rules: xOracleRules
120
- ) => {
121
- const coinType = builder.utils.parseCoinType(assetCoinName);
122
117
 
123
- updatePrice(
118
+ // Create a price update requester
119
+ const priceUpdateRequester = new PriceUpdateRequester(
124
120
  txBlock,
125
- rules,
126
- builder.address.get('core.packages.xOracle.id'),
127
- builder.address.get('core.oracles.xOracle'),
128
- builder.address.get('core.packages.pyth.id'),
129
- builder.address.get('core.oracles.pyth.registry'),
130
- builder.address.get('core.oracles.pyth.state'),
131
- builder.address.get(`core.coins.${assetCoinName}.oracle.pyth.feedObject`),
132
- builder.address.get('core.packages.switchboard.id'),
133
- builder.address.get('core.oracles.switchboard.registry'),
134
- builder.address.get(`core.coins.${assetCoinName}.oracle.switchboard`),
135
- builder.address.get('core.packages.supra.id'),
136
- builder.address.get('core.oracles.supra.registry'),
137
- builder.address.get(`core.oracles.supra.holder`),
138
- coinType
121
+ xOraclePackageRegistry
139
122
  );
140
- };
141
123
 
142
- /**
143
- * Construct a transaction block for update the price.
144
- *
145
- * @param txBlock - The transaction block.
146
- * @param rules - The oracle rules.
147
- * @param xOraclePackageId - The xOracle package id.
148
- * @param xOracleId - The xOracle Id from xOracle package.
149
- * @param pythPackageId - The pyth package id.
150
- * @param pythRegistryId - The registry id from pyth package.
151
- * @param pythStateId - The price state id from pyth package.
152
- * @param pythFeedObjectId - The feed object id from pyth package.
153
- * @param switchboardPackageId - The switchboard package id.
154
- * @param switchboardRegistryId - The registry id from switchboard package.
155
- * @param switchboardAggregatorId - The aggregator id from switchboard package.
156
- * @param supraPackageId - The supra package id.
157
- * @param supraRegistryId - The registry id from supra package.
158
- * @param supraHolderId - The holder id from supra package.
159
- * @param coinType - The type of coin.
160
- * @return TxBlock created by SuiKit.
161
- */
162
- const updatePrice = (
163
- txBlock: SuiKitTxBlock,
164
- rules: xOracleRules,
165
- xOraclePackageId: string,
166
- xOracleId: TransactionArgument | string,
167
- pythPackageId: string,
168
- pythRegistryId: TransactionArgument | string,
169
- pythStateId: TransactionArgument | string,
170
- pythFeedObjectId: TransactionArgument | string,
171
- switchboardPackageId: string,
172
- switchboardRegistryId: TransactionArgument | string,
173
- switchboardAggregatorId: TransactionArgument | string,
174
- supraPackageId: string,
175
- supraRegistryId: TransactionArgument | string,
176
- supraHolderId: TransactionArgument | string,
177
- coinType: string
178
- ) => {
179
- const request = priceUpdateRequest(
180
- txBlock,
181
- xOraclePackageId,
182
- xOracleId,
183
- coinType
184
- );
185
- Object.entries(rules).forEach(([type, rule]: [any, string[]]) => {
186
- if (rule.includes('pyth')) {
187
- updatePythPrice(
188
- type,
189
- txBlock,
190
- pythPackageId,
191
- request,
192
- pythStateId,
193
- pythFeedObjectId,
194
- pythRegistryId,
195
- coinType
196
- );
197
- }
198
- if (rule.includes('supra')) {
199
- updateSupraPrice(
200
- type,
201
- txBlock,
202
- supraPackageId,
203
- request,
204
- supraHolderId,
205
- supraRegistryId,
206
- coinType
207
- );
208
- }
209
- if (rule.includes('switchboard')) {
210
- updateSwitchboardPrice(
211
- type,
212
- txBlock,
213
- switchboardPackageId,
214
- request,
215
- switchboardAggregatorId,
216
- switchboardRegistryId,
217
- coinType
218
- );
219
- }
220
- });
221
-
222
- confirmPriceUpdateRequest(
223
- txBlock,
224
- xOraclePackageId,
225
- xOracleId,
226
- request,
227
- coinType
228
- );
229
- return txBlock;
230
- };
231
-
232
- /**
233
- * Construct a transaction block for request price update.
234
- *
235
- * @param txBlock - The transaction block.
236
- * @param packageId - The xOracle package id.
237
- * @param xOracleId - The xOracle Id from xOracle package.
238
- * @param coinType - The type of coin.
239
- * @return TxBlock created by SuiKit.
240
- */
241
- const priceUpdateRequest = (
242
- txBlock: SuiKitTxBlock,
243
- packageId: string,
244
- xOracleId: TransactionArgument | string,
245
- coinType: string
246
- ) => {
247
- const target = `${packageId}::x_oracle::price_update_request`;
248
- const typeArgs = [coinType];
249
- return txBlock.moveCall(target, [xOracleId], typeArgs);
250
- };
251
-
252
- /**
253
- * Construct a transaction block for confirm price update request.
254
- *
255
- * @param txBlock - The transaction block.
256
- * @param packageId - The xOracle package id.
257
- * @param xOracleId - The xOracle Id from xOracle package.
258
- * @param request - The result of the request.
259
- * @param coinType - The type of coin.
260
- * @return TxBlock created by SuiKit.
261
- */
262
- const confirmPriceUpdateRequest = (
263
- txBlock: SuiKitTxBlock,
264
- packageId: string,
265
- xOracleId: TransactionArgument | string,
266
- request: TransactionArgument,
267
- coinType: string
268
- ) => {
269
- const target = `${packageId}::x_oracle::confirm_price_update_request`;
270
- const typeArgs = [coinType];
271
- txBlock.moveCall(
272
- target,
273
- [
274
- xOracleId,
275
- request,
276
- txBlock.sharedObjectRef({
277
- objectId: SUI_CLOCK_OBJECT_ID,
278
- mutable: false,
279
- initialSharedVersion: '1',
280
- }),
281
- ],
282
- typeArgs
283
- );
284
- return txBlock;
285
- };
286
-
287
- /**
288
- * Construct a transaction block for update supra price.
289
- *
290
- * @param type - The type of price rule.
291
- * @param txBlock - The transaction block.
292
- * @param packageId - The supra package id.
293
- * @param request - The result of the request.
294
- * @param holderId - The holder id from supra package.
295
- * @param registryId - The registry id from supra package.
296
- * @param coinType - The type of coin.
297
- * @return TxBlock created by SuiKit.
298
- */
299
- const updateSupraPrice = (
300
- type: xOracleRuleType,
301
- txBlock: SuiKitTxBlock,
302
- packageId: string,
303
- request: TransactionArgument,
304
- holderId: TransactionArgument | string,
305
- registryId: TransactionArgument | string,
306
- coinType: string
307
- ) => {
308
- txBlock.moveCall(
309
- `${packageId}::rule::set_price_as_${type}`,
310
- [
311
- request,
312
- holderId,
313
- registryId,
314
- txBlock.sharedObjectRef({
315
- objectId: SUI_CLOCK_OBJECT_ID,
316
- initialSharedVersion: '1',
317
- mutable: false,
318
- }),
319
- ],
320
- [coinType]
321
- );
322
- };
323
-
324
- /**
325
- * Construct a transaction block for update switchboard price.
326
- *
327
- * @param type - The type of price rule.
328
- * @param txBlock - The transaction block.
329
- * @param packageId - The switchboard package id.
330
- * @param request - The result of the request.
331
- * @param aggregatorId - The aggregator id from switchboard package.
332
- * @param registryId - The registry id from switchboard package.
333
- * @param coinType - The type of coin.
334
- * @return TxBlock created by SuiKit.
335
- */
336
- const updateSwitchboardPrice = (
337
- type: xOracleRuleType,
338
- txBlock: SuiKitTxBlock,
339
- packageId: string,
340
- request: TransactionArgument,
341
- aggregatorId: TransactionArgument | string,
342
- registryId: TransactionArgument | string,
343
- coinType: string
344
- ) => {
345
- txBlock.moveCall(
346
- // `${packageId}::rule::set_price_as_${type}`,
347
- `${packageId}::rule::set_as_${type}_price`,
348
- [
349
- request,
350
- aggregatorId,
351
- registryId,
352
- txBlock.sharedObjectRef({
353
- objectId: SUI_CLOCK_OBJECT_ID,
354
- initialSharedVersion: '1',
355
- mutable: false,
356
- }),
357
- ],
358
- [coinType]
359
- );
360
- };
361
-
362
- /**
363
- * Construct a transaction block for update pyth price.
364
- *
365
- * @param type - The type of price rule.
366
- * @param txBlock - The transaction block.
367
- * @param packageId - The pyth package id.
368
- * @param request - The result of the request.
369
- * @param stateId - The price state id from pyth package.
370
- * @param wormholeStateId - The whormhole state id from pyth package.
371
- * @param feedObjectId - The feed object id from pyth package.
372
- * @param vaaFromFeeId - The vaa from pyth api with feed id.
373
- * @param registryId - The registry id from pyth package.
374
- * @param coinType - The type of coin.
375
- * @return TxBlock created by SuiKit.
376
- */
377
- const updatePythPrice = (
378
- type: xOracleRuleType,
379
- txBlock: SuiKitTxBlock,
380
- packageId: string,
381
- request: TransactionArgument,
382
- stateId: TransactionArgument | string,
383
- feedObjectId: TransactionArgument | string,
384
- registryId: TransactionArgument | string,
385
- coinType: string
386
- ) => {
387
- txBlock.moveCall(
388
- `${packageId}::rule::set_price_as_${type}`,
389
- [
390
- request,
391
- stateId,
392
- feedObjectId,
393
- registryId,
394
- txBlock.sharedObjectRef({
395
- objectId: SUI_CLOCK_OBJECT_ID,
396
- initialSharedVersion: '1',
397
- mutable: false,
398
- }),
399
- ],
400
- [coinType]
401
- );
124
+ for (const assetCoinName of assetCoinNames) {
125
+ const rules = xOracleList[assetCoinName];
126
+
127
+ // build the price update request for the asset coin
128
+ // Each coin name has its own price update request
129
+ const updateRequest = priceUpdateRequester.buildRequest(assetCoinName);
130
+
131
+ // Iterate through each rule and update the xOracle for each oracle type
132
+ Object.keys(rules).forEach((rule) => {
133
+ const oracles = rules[rule as xOracleRuleType];
134
+ oracles.forEach((oracleType) => {
135
+ const updater = xOracleUpdaters[oracleType];
136
+ if (!updater) {
137
+ // Should never happen if ORACLE_TYPES is the source of truth
138
+ throw new UnsupportedOracleError(oracleType);
139
+ }
140
+ updater.updateXOracle(
141
+ assetCoinName,
142
+ rule as xOracleRuleType,
143
+ updateRequest
144
+ );
145
+ });
146
+ });
147
+
148
+ // Confirm the price update request for all oracles
149
+ priceUpdateRequester.confirmRequest(assetCoinName, updateRequest);
150
+ }
402
151
  };