@scallop-io/sui-scallop-sdk 2.3.0-lst-x-oracle-alpha.8 → 2.3.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 (73) hide show
  1. package/dist/index.d.mts +1811 -1750
  2. package/dist/index.d.ts +1811 -1750
  3. package/dist/index.js +49 -2
  4. package/dist/index.mjs +15 -2
  5. package/package.json +8 -5
  6. package/src/builders/borrowIncentiveBuilder.ts +4 -4
  7. package/src/builders/coreBuilder.ts +86 -59
  8. package/src/builders/index.ts +2 -2
  9. package/src/builders/loyaltyProgramBuilder.ts +2 -2
  10. package/src/builders/oracles/index.ts +365 -114
  11. package/src/builders/oracles/pyth.ts +135 -0
  12. package/src/builders/referralBuilder.ts +4 -10
  13. package/src/builders/sCoinBuilder.ts +2 -6
  14. package/src/builders/spoolBuilder.ts +2 -2
  15. package/src/builders/vescaBuilder.ts +5 -5
  16. package/src/constants/common.ts +3 -0
  17. package/src/constants/index.ts +1 -1
  18. package/src/constants/queryKeys.ts +1 -1
  19. package/src/constants/testAddress.ts +95 -183
  20. package/src/constants/xoracle.ts +2 -8
  21. package/src/index.ts +1 -1
  22. package/src/models/index.ts +1 -2
  23. package/src/models/interface.ts +6 -6
  24. package/src/models/rateLimiter.ts +55 -0
  25. package/src/models/scallop.ts +1 -1
  26. package/src/models/scallopAddress.ts +5 -25
  27. package/src/models/scallopBuilder.ts +14 -11
  28. package/src/models/scallopClient.ts +31 -14
  29. package/src/models/scallopConstants.ts +3 -3
  30. package/src/models/scallopIndexer.ts +3 -4
  31. package/src/models/scallopQuery.ts +112 -56
  32. package/src/models/scallopQueryClient.ts +1 -1
  33. package/src/models/scallopSuiKit.ts +1 -1
  34. package/src/models/scallopUtils.ts +12 -7
  35. package/src/queries/borrowIncentiveQuery.ts +4 -3
  36. package/src/queries/coreQuery.ts +114 -186
  37. package/src/queries/index.ts +3 -4
  38. package/src/queries/loyaltyProgramQuery.ts +2 -2
  39. package/src/queries/ownerQuery.ts +32 -0
  40. package/src/queries/poolAddressesQuery.ts +1 -3
  41. package/src/queries/portfolioQuery.ts +68 -16
  42. package/src/queries/priceQuery.ts +2 -3
  43. package/src/queries/sCoinQuery.ts +2 -2
  44. package/src/queries/spoolQuery.ts +57 -74
  45. package/src/queries/vescaQuery.ts +3 -3
  46. package/src/queries/xOracleQuery.ts +4 -21
  47. package/src/types/address.ts +47 -85
  48. package/src/types/builder/core.ts +40 -15
  49. package/src/types/builder/index.ts +17 -1
  50. package/src/types/constant/enum.ts +64 -0
  51. package/src/types/constant/index.ts +1 -2
  52. package/src/types/constant/xOracle.ts +7 -10
  53. package/src/types/index.ts +1 -1
  54. package/src/types/query/core.ts +3 -0
  55. package/src/types/query/index.ts +1 -0
  56. package/src/types/query/sCoin.ts +1 -0
  57. package/src/{builders/utils.ts → utils/builder.ts} +1 -1
  58. package/src/utils/core.ts +18 -0
  59. package/src/utils/index.ts +5 -0
  60. package/src/utils/indexer.ts +47 -0
  61. package/src/{queries/utils.ts → utils/query.ts} +7 -25
  62. package/src/utils/util.ts +42 -0
  63. package/src/builders/oracles/error.ts +0 -29
  64. package/src/builders/oracles/oraclePackageRegistry.ts +0 -322
  65. package/src/builders/oracles/priceFeedUpdater.ts +0 -112
  66. package/src/builders/oracles/priceUpdateRequester.ts +0 -50
  67. package/src/builders/oracles/xOracleUpdateStrategy.ts +0 -178
  68. package/src/builders/oracles/xOracleUpdater.ts +0 -160
  69. package/src/constants/api.ts +0 -2
  70. package/src/models/utils.ts +0 -97
  71. package/src/types/builder/type.ts +0 -25
  72. package/src/types/constant/package.ts +0 -16
  73. /package/src/types/{util.ts → utils.ts} +0 -0
@@ -1,178 +0,0 @@
1
- import {
2
- SUI_CLOCK_OBJECT_ID,
3
- SuiTxBlock,
4
- TransactionArgument,
5
- } from '@scallop-io/sui-kit';
6
- import { IOraclePackageRegistry } from './oraclePackageRegistry';
7
- import {
8
- SupportedOracleSuiLst,
9
- SupportOracleType,
10
- xOracleRuleType,
11
- } from 'src/types/constant';
12
- import { UnsupportedLstOracleError } from './error';
13
-
14
- export interface IXOracleUpdateStrategy<
15
- T extends SupportOracleType,
16
- U extends string = string,
17
- > {
18
- oracleType: T;
19
- updatePrice(
20
- tx: SuiTxBlock,
21
- request: TransactionArgument,
22
- coinName: U,
23
- rule: xOracleRuleType
24
- ): void;
25
- }
26
-
27
- abstract class BaseUpdateStrategy<
28
- T extends SupportOracleType,
29
- U extends string = string,
30
- > implements IXOracleUpdateStrategy<T, U>
31
- {
32
- constructor(protected readonly registry: IOraclePackageRegistry<T>) {}
33
- abstract readonly oracleType: T;
34
-
35
- protected get clockObject() {
36
- return {
37
- objectId: SUI_CLOCK_OBJECT_ID,
38
- mutable: false,
39
- initialSharedVersion: '1',
40
- };
41
- }
42
-
43
- protected get utils() {
44
- return this.registry.utils;
45
- }
46
-
47
- abstract updatePrice(
48
- tx: SuiTxBlock,
49
- request: TransactionArgument,
50
- coinName: string,
51
- rule: xOracleRuleType
52
- ): void;
53
- }
54
-
55
- export class PythDefaultUpdateStrategy extends BaseUpdateStrategy<'pyth'> {
56
- readonly oracleType = 'pyth';
57
- constructor(protected readonly registry: IOraclePackageRegistry<'pyth'>) {
58
- super(registry);
59
- }
60
-
61
- updatePrice(
62
- tx: SuiTxBlock,
63
- request: TransactionArgument,
64
- coinName: string,
65
- rule: xOracleRuleType
66
- ): void {
67
- const { pythFeedObjectId, pythRegistryId, pythStateId, pythPackageId } =
68
- this.registry.getPackages(coinName);
69
- tx.moveCall(
70
- `${pythPackageId}::rule::set_price_as_${rule}`,
71
- [
72
- request,
73
- pythStateId,
74
- pythFeedObjectId,
75
- pythRegistryId,
76
- tx.sharedObjectRef({
77
- objectId: SUI_CLOCK_OBJECT_ID,
78
- mutable: false,
79
- initialSharedVersion: '1',
80
- }),
81
- ],
82
- [this.utils.parseCoinType(coinName)]
83
- );
84
- }
85
- }
86
- export class PythSuiLstUpdateStrategy extends BaseUpdateStrategy<
87
- 'pyth',
88
- SupportedOracleSuiLst
89
- > {
90
- readonly oracleType = 'pyth';
91
- constructor(protected readonly registry: IOraclePackageRegistry<'pyth'>) {
92
- super(registry);
93
- }
94
-
95
- updatePrice(
96
- tx: SuiTxBlock,
97
- request: TransactionArgument,
98
- coinName: SupportedOracleSuiLst,
99
- rule: xOracleRuleType
100
- ): void {
101
- const { lst, pythFeedObjectId, pythStateId } =
102
- this.registry.getPackages(coinName);
103
- if (!lst[coinName]) {
104
- throw new UnsupportedLstOracleError(coinName, this.oracleType);
105
- }
106
- const { id, configId, stakedSuiVaultId, safeId } = lst[coinName];
107
- tx.moveCall(`${id}::rule::set_price_as_${rule}`, [
108
- request,
109
- pythStateId,
110
- pythFeedObjectId,
111
- configId,
112
- stakedSuiVaultId,
113
- safeId,
114
- tx.sharedObjectRef(this.clockObject),
115
- ]);
116
- }
117
- }
118
-
119
- export class SupraDefaultUpdateStrategy extends BaseUpdateStrategy<'supra'> {
120
- readonly oracleType = 'supra';
121
- constructor(protected readonly registry: IOraclePackageRegistry<'supra'>) {
122
- super(registry);
123
- }
124
-
125
- updatePrice(
126
- tx: SuiTxBlock,
127
- request: TransactionArgument,
128
- coinName: string,
129
- rule: xOracleRuleType
130
- ): void {
131
- const { supraPackageId, supraHolderId, supraRegistryId } =
132
- this.registry.getPackages(coinName);
133
-
134
- tx.moveCall(
135
- `${supraPackageId}::rule::set_price_as_${rule}`,
136
- [
137
- request,
138
- supraHolderId,
139
- supraRegistryId,
140
- tx.sharedObjectRef(this.clockObject),
141
- ],
142
- [this.utils.parseCoinType(coinName)]
143
- );
144
- }
145
- }
146
-
147
- export class SwitchboardDefaultUpdateStrategy extends BaseUpdateStrategy<'switchboard'> {
148
- readonly oracleType = 'switchboard';
149
- constructor(
150
- protected readonly registry: IOraclePackageRegistry<'switchboard'>
151
- ) {
152
- super(registry);
153
- }
154
-
155
- updatePrice(
156
- tx: SuiTxBlock,
157
- request: TransactionArgument,
158
- coinName: string,
159
- rule: xOracleRuleType
160
- ): void {
161
- const {
162
- switchboardPackageId,
163
- switchboardAggregatorId,
164
- switchboardRegistryId,
165
- } = this.registry.getPackages(coinName);
166
-
167
- tx.moveCall(
168
- `${switchboardPackageId}::rule::set_price_as_${rule}`,
169
- [
170
- request,
171
- switchboardAggregatorId,
172
- switchboardRegistryId,
173
- tx.sharedObjectRef(this.clockObject),
174
- ],
175
- [this.utils.parseCoinType(coinName)]
176
- );
177
- }
178
- }
@@ -1,160 +0,0 @@
1
- import { SuiTxBlock, TransactionArgument } from '@scallop-io/sui-kit';
2
- import { SupportOracleType, xOracleRuleType } from 'src/types/constant';
3
- import { SUPPORT_SUI_LST } from 'src/constants/xoracle';
4
- import { IOraclePackageRegistry } from './oraclePackageRegistry';
5
- import {
6
- IXOracleUpdateStrategy,
7
- PythDefaultUpdateStrategy,
8
- PythSuiLstUpdateStrategy,
9
- SupraDefaultUpdateStrategy,
10
- SwitchboardDefaultUpdateStrategy,
11
- } from './xOracleUpdateStrategy';
12
- import { UnsupportedLstOracleError } from './error';
13
-
14
- export interface IXOracleUpdater<
15
- T extends SupportOracleType = SupportOracleType,
16
- > {
17
- oracleName: T; // e.g. 'pyth', 'supra', 'switchboard'
18
- updateXOracle(
19
- coinName: string,
20
- rule: xOracleRuleType,
21
- request: TransactionArgument
22
- ): void;
23
- }
24
-
25
- abstract class BaseXOracleUpdater<
26
- T extends SupportOracleType = SupportOracleType,
27
- > implements IXOracleUpdater
28
- {
29
- protected strategies: Map<string, IXOracleUpdateStrategy<T>>;
30
- abstract readonly oracleName: T;
31
-
32
- constructor(
33
- protected readonly tx: SuiTxBlock,
34
- protected readonly registry: IOraclePackageRegistry<T>,
35
- listStrategies: Record<string, IXOracleUpdateStrategy<T>>
36
- ) {
37
- this.strategies = new Map(Object.entries(listStrategies));
38
- }
39
-
40
- abstract updateXOracle(
41
- coinName: string,
42
- rule: xOracleRuleType,
43
- request: TransactionArgument
44
- ): void;
45
- }
46
-
47
- class PythXOracleUpdater extends BaseXOracleUpdater<'pyth'> {
48
- readonly oracleName = 'pyth';
49
- protected readonly SUPPORT_SUI_LST_SET: Set<string> = new Set(
50
- SUPPORT_SUI_LST
51
- );
52
-
53
- constructor(tx: SuiTxBlock, registry: IOraclePackageRegistry<'pyth'>) {
54
- super(tx, registry, {
55
- default: new PythDefaultUpdateStrategy(registry),
56
- sui_lst: new PythSuiLstUpdateStrategy(registry),
57
- });
58
- }
59
-
60
- private resolveStrategy(
61
- coinName: string
62
- ): IXOracleUpdateStrategy<typeof this.oracleName> {
63
- // decide which key to look up in this.strategies
64
- let key: string;
65
-
66
- if (this.SUPPORT_SUI_LST_SET.has(coinName)) {
67
- key = 'sui_lst';
68
- }
69
- // else if (this.WALRUS_LST_SET.has(coinName)) {
70
- // key = 'walrus_lst';
71
- // }
72
- else {
73
- key = 'default';
74
- }
75
-
76
- const strat = this.strategies.get(key);
77
- if (!strat) {
78
- // guard if you forgot to register default or list strategies
79
- throw new UnsupportedLstOracleError(coinName, this.oracleName);
80
- }
81
-
82
- return strat as IXOracleUpdateStrategy<typeof this.oracleName>;
83
- }
84
-
85
- updateXOracle(
86
- coinName: string,
87
- rule: xOracleRuleType,
88
- request: TransactionArgument
89
- ): void {
90
- const strategy = this.resolveStrategy(coinName);
91
- strategy.updatePrice(this.tx, request, coinName, rule);
92
- }
93
- }
94
-
95
- class SupraXOracleUpdater extends BaseXOracleUpdater<'supra'> {
96
- readonly oracleName = 'supra';
97
-
98
- constructor(tx: SuiTxBlock, registry: IOraclePackageRegistry<'supra'>) {
99
- super(tx, registry, {
100
- default: new SupraDefaultUpdateStrategy(registry), // Placeholder, implement Supra strategy
101
- });
102
- }
103
-
104
- updateXOracle(
105
- coinName: string,
106
- rule: xOracleRuleType,
107
- request: TransactionArgument
108
- ): void {
109
- const stragtegy = this.strategies.get('default');
110
- stragtegy?.updatePrice(this.tx, request, coinName, rule);
111
- }
112
- }
113
-
114
- class SwitchboardXOracleUpdater extends BaseXOracleUpdater<'switchboard'> {
115
- readonly oracleName = 'switchboard';
116
- constructor(tx: SuiTxBlock, registry: IOraclePackageRegistry<'switchboard'>) {
117
- super(tx, registry, {
118
- // Placeholder, implement Switchboard strategy
119
- default: new SwitchboardDefaultUpdateStrategy(registry),
120
- });
121
- }
122
-
123
- updateXOracle(
124
- coinName: string,
125
- rule: xOracleRuleType,
126
- request: TransactionArgument
127
- ): void {
128
- const stragtegy = this.strategies.get('default');
129
- stragtegy?.updatePrice(this.tx, request, coinName, rule);
130
- }
131
- }
132
-
133
- export const createXOracleUpdater = (
134
- tx: SuiTxBlock,
135
- registry: IOraclePackageRegistry
136
- ) => {
137
- const oracleType = registry.oracleName;
138
- switch (oracleType) {
139
- case 'pyth': {
140
- return new PythXOracleUpdater(
141
- tx,
142
- registry as IOraclePackageRegistry<'pyth'>
143
- );
144
- }
145
- case 'supra': {
146
- return new SupraXOracleUpdater(
147
- tx,
148
- registry as IOraclePackageRegistry<'supra'>
149
- );
150
- }
151
- case 'switchboard': {
152
- return new SwitchboardXOracleUpdater(
153
- tx,
154
- registry as IOraclePackageRegistry<'switchboard'>
155
- );
156
- }
157
- default:
158
- throw new Error(`Unsupported oracle type: ${oracleType}`);
159
- }
160
- };
@@ -1,2 +0,0 @@
1
- export const API_BASE_URL = 'https://sui.apis.scallop.io' as const;
2
- export const SDK_API_BASE_URL = 'https://sdk.api.scallop.io' as const;
@@ -1,97 +0,0 @@
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 lastArgs = args[args.length - 1]; // Assume last argument is always `indexer`
15
-
16
- if (typeof lastArgs === 'object' && lastArgs.indexer) {
17
- try {
18
- return await method.apply(context, args);
19
- } catch (e: any) {
20
- console.warn(
21
- `Indexer requests failed: ${e.message}. Retrying without indexer..`
22
- );
23
- return await method.apply(context, [
24
- ...args.slice(0, -1),
25
- {
26
- ...lastArgs,
27
- indexer: false,
28
- },
29
- ]);
30
- }
31
- }
32
- return await method.apply(context, args);
33
- }
34
-
35
- /**
36
- * This function creates a wrapper for methods that have an indexer parameter.
37
- * It ensures fallback behavior if indexer fails.
38
- *
39
- * @param method - The method to wrap.
40
- * @returns A function that applies indexer fallback.
41
- */
42
- export function withIndexerFallback(method: Function) {
43
- return (...args: any[]) => {
44
- // @ts-ignore
45
- return callMethodWithIndexerFallback(method, this, ...args); // Preserve `this` with arrow function
46
- };
47
- }
48
-
49
- import { SuiTxBlock } from '@scallop-io/sui-kit';
50
- import { MAX_LOCK_DURATION } from 'src/constants/vesca';
51
- // import { ScallopConstants } from 'src/models/scallopConstants';
52
-
53
- /**
54
- * Find the closest unlock round timestamp (12AM) to the given unlock at timestamp in seconds.
55
- *
56
- * @param unlockAtInSecondTimestamp - Unlock at in seconds timestamp to find the closest round.
57
- * @returns Closest round (12AM) in seconds timestamp.
58
- */
59
- export const findClosestUnlockRound = (unlockAtInSecondTimestamp: number) => {
60
- const unlockDate = new Date(unlockAtInSecondTimestamp * 1000);
61
- const closestTwelveAM = new Date(unlockAtInSecondTimestamp * 1000);
62
-
63
- closestTwelveAM.setUTCHours(0, 0, 0, 0); // Set the time to the next 12 AM UTC
64
-
65
- // If the current time is past 12 AM, set the date to the next day
66
- if (unlockDate.getUTCHours() >= 0) {
67
- closestTwelveAM.setUTCDate(closestTwelveAM.getUTCDate() + 1);
68
- }
69
-
70
- const now = new Date().getTime();
71
- // check if unlock period > 4 years
72
- if (closestTwelveAM.getTime() - now > MAX_LOCK_DURATION * 1000) {
73
- closestTwelveAM.setUTCDate(closestTwelveAM.getUTCDate() - 1);
74
- }
75
- return Math.floor(closestTwelveAM.getTime() / 1000);
76
- };
77
-
78
- export const parseUrl = (url: string) => {
79
- if (url.endsWith('/')) {
80
- url = url.slice(0, -1);
81
- }
82
- return url;
83
- };
84
-
85
- /**
86
- * Check and get the sender from the transaction block.
87
- *
88
- * @param txBlock - TxBlock created by SuiKit.
89
- * @return Sender of transaction.
90
- */
91
- export const requireSender = (txBlock: SuiTxBlock) => {
92
- const sender = txBlock.blockData.sender;
93
- if (!sender) {
94
- throw new Error('Sender is required');
95
- }
96
- return sender;
97
- };
@@ -1,25 +0,0 @@
1
- import type { CoreTxBlock, NestedResult } from './core';
2
- import type { SpoolTxBlock } from './spool';
3
- import type { BorrowIncentiveTxBlock } from './borrowIncentive';
4
- import type { VeScaTxBlock } from './vesca';
5
- import type { ReferralTxBlock } from './referral';
6
- import type { LoyaltyProgramTxBlock } from './loyaltyProgram';
7
- import type { SCoinTxBlock } from './sCoin';
8
-
9
- export type BaseScallopTxBlock = ReferralTxBlock &
10
- LoyaltyProgramTxBlock &
11
- BorrowIncentiveTxBlock &
12
- VeScaTxBlock;
13
-
14
- export type SuiTxBlockWithSCoin = BaseScallopTxBlock & SCoinTxBlock;
15
- export type SuiTxBlockWithSpool = SuiTxBlockWithSCoin & SpoolTxBlock;
16
- export type ScallopTxBlock = SuiTxBlockWithSpool & CoreTxBlock;
17
-
18
- export type SelectCoinReturnType<T extends string> = T extends 'sui'
19
- ? {
20
- takeCoin: NestedResult;
21
- }
22
- : {
23
- takeCoin: NestedResult;
24
- leftCoin: NestedResult;
25
- };
@@ -1,16 +0,0 @@
1
- const _PACKAGE_NAMES = [
2
- 'coinDecimalsRegistry',
3
- 'math',
4
- 'whitelist',
5
- 'x',
6
- 'protocol',
7
- 'protocolWhitelist',
8
- 'query',
9
- 'supra',
10
- 'pyth',
11
- 'switchboard',
12
- 'xOracle',
13
- 'testCoin',
14
- ] as const;
15
-
16
- export type PackageName = (typeof _PACKAGE_NAMES)[number];
File without changes