@scallop-io/sui-scallop-sdk 2.3.0-lst-x-oracle-alpha.9 → 2.3.1

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/index.d.mts +1841 -1809
  2. package/dist/index.d.ts +1841 -1809
  3. package/dist/index.js +49 -2
  4. package/dist/index.mjs +15 -2
  5. package/package.json +8 -7
  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 -2
  14. package/src/builders/spoolBuilder.ts +5 -8
  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/rpc.ts +0 -1
  20. package/src/constants/testAddress.ts +99 -271
  21. package/src/constants/xoracle.ts +2 -8
  22. package/src/index.ts +1 -1
  23. package/src/models/index.ts +1 -2
  24. package/src/models/interface.ts +7 -7
  25. package/src/models/rateLimiter.ts +55 -0
  26. package/src/models/scallop.ts +2 -2
  27. package/src/models/scallopAddress.ts +7 -35
  28. package/src/models/scallopBuilder.ts +19 -12
  29. package/src/models/scallopClient.ts +47 -33
  30. package/src/models/scallopConstants.ts +3 -3
  31. package/src/models/scallopIndexer.ts +3 -4
  32. package/src/models/scallopQuery.ts +113 -57
  33. package/src/models/scallopQueryClient.ts +1 -1
  34. package/src/models/scallopSuiKit.ts +1 -1
  35. package/src/models/scallopUtils.ts +12 -7
  36. package/src/queries/borrowIncentiveQuery.ts +4 -3
  37. package/src/queries/coreQuery.ts +114 -186
  38. package/src/queries/index.ts +3 -4
  39. package/src/queries/loyaltyProgramQuery.ts +2 -2
  40. package/src/queries/ownerQuery.ts +32 -0
  41. package/src/queries/poolAddressesQuery.ts +1 -4
  42. package/src/queries/portfolioQuery.ts +68 -16
  43. package/src/queries/priceQuery.ts +2 -3
  44. package/src/queries/sCoinQuery.ts +2 -2
  45. package/src/queries/spoolQuery.ts +57 -74
  46. package/src/queries/vescaQuery.ts +3 -3
  47. package/src/queries/xOracleQuery.ts +4 -21
  48. package/src/types/address.ts +47 -98
  49. package/src/types/builder/core.ts +40 -15
  50. package/src/types/builder/index.ts +17 -1
  51. package/src/types/constant/enum.ts +64 -0
  52. package/src/types/constant/index.ts +1 -2
  53. package/src/types/constant/xOracle.ts +7 -10
  54. package/src/types/index.ts +1 -1
  55. package/src/types/query/core.ts +3 -0
  56. package/src/types/query/index.ts +1 -0
  57. package/src/types/query/sCoin.ts +1 -0
  58. package/src/{builders/utils.ts → utils/builder.ts} +1 -1
  59. package/src/utils/core.ts +18 -0
  60. package/src/utils/index.ts +5 -0
  61. package/src/utils/indexer.ts +47 -0
  62. package/src/{queries/utils.ts → utils/query.ts} +7 -25
  63. package/src/utils/util.ts +42 -0
  64. package/src/builders/oracles/error.ts +0 -18
  65. package/src/builders/oracles/oraclePackageRegistry.ts +0 -336
  66. package/src/builders/oracles/priceFeedUpdater.ts +0 -112
  67. package/src/builders/oracles/priceUpdateRequester.ts +0 -50
  68. package/src/builders/oracles/xOracleUpdateStrategy.ts +0 -214
  69. package/src/builders/oracles/xOracleUpdater.ts +0 -153
  70. package/src/constants/api.ts +0 -2
  71. package/src/models/utils.ts +0 -97
  72. package/src/types/builder/type.ts +0 -25
  73. package/src/types/constant/package.ts +0 -16
  74. /package/src/types/{util.ts → utils.ts} +0 -0
@@ -1,214 +0,0 @@
1
- import {
2
- SUI_CLOCK_OBJECT_ID,
3
- SuiTxBlock,
4
- TransactionArgument,
5
- } from '@scallop-io/sui-kit';
6
- import {
7
- IOraclePackageRegistry,
8
- PythLstPackages,
9
- } from './oraclePackageRegistry';
10
- import { SupportOracleType, xOracleRuleType } from 'src/types/constant';
11
- import { UnsupportedLstOracleError } from './error';
12
-
13
- export interface IXOracleUpdateStrategy<
14
- T extends SupportOracleType,
15
- U extends string = string,
16
- > {
17
- oracleType: T;
18
- updatePrice(
19
- tx: SuiTxBlock,
20
- request: TransactionArgument,
21
- coinName: U,
22
- rule: xOracleRuleType
23
- ): void;
24
- }
25
-
26
- abstract class BaseUpdateStrategy<
27
- T extends SupportOracleType,
28
- U extends string = string,
29
- > implements IXOracleUpdateStrategy<T, U>
30
- {
31
- constructor(protected readonly registry: IOraclePackageRegistry<T>) {}
32
- abstract readonly oracleType: T;
33
-
34
- protected get clockObject() {
35
- return {
36
- objectId: SUI_CLOCK_OBJECT_ID,
37
- mutable: false,
38
- initialSharedVersion: '1',
39
- };
40
- }
41
-
42
- protected get utils() {
43
- return this.registry.utils;
44
- }
45
-
46
- abstract updatePrice(
47
- tx: SuiTxBlock,
48
- request: TransactionArgument,
49
- coinName: string,
50
- rule: xOracleRuleType
51
- ): void;
52
- }
53
-
54
- export class PythDefaultUpdateStrategy extends BaseUpdateStrategy<'pyth'> {
55
- readonly oracleType = 'pyth';
56
- constructor(protected readonly registry: IOraclePackageRegistry<'pyth'>) {
57
- super(registry);
58
- }
59
-
60
- updatePrice(
61
- tx: SuiTxBlock,
62
- request: TransactionArgument,
63
- coinName: string,
64
- rule: xOracleRuleType
65
- ): void {
66
- const { pythFeedObjectId, pythRegistryId, pythStateId, pythPackageId } =
67
- this.registry.getPackages(coinName);
68
- tx.moveCall(
69
- `${pythPackageId}::rule::set_price_as_${rule}`,
70
- [
71
- request,
72
- pythStateId,
73
- pythFeedObjectId,
74
- pythRegistryId,
75
- tx.sharedObjectRef({
76
- objectId: SUI_CLOCK_OBJECT_ID,
77
- mutable: false,
78
- initialSharedVersion: '1',
79
- }),
80
- ],
81
- [this.utils.parseCoinType(coinName)]
82
- );
83
- }
84
- }
85
-
86
- export class PythAfSuiUpdateStrategy extends BaseUpdateStrategy<
87
- 'pyth',
88
- 'afsui'
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: 'afsui',
99
- rule: xOracleRuleType
100
- ): void {
101
- const { lst, pythFeedObjectId, pythStateId } =
102
- this.registry.getPackages(coinName);
103
- if (!lst) {
104
- throw new UnsupportedLstOracleError(coinName, this.oracleType);
105
- }
106
- const { id, configId, stakedSuiVaultId, safeId } = lst as PythLstPackages<
107
- typeof coinName
108
- >;
109
-
110
- tx.moveCall(`${id}::rule::set_price_as_${rule}`, [
111
- request,
112
- pythStateId,
113
- pythFeedObjectId,
114
- configId,
115
- stakedSuiVaultId,
116
- safeId,
117
- tx.sharedObjectRef(this.clockObject),
118
- ]);
119
- }
120
- }
121
-
122
- export class PythHaSuiUpdateStrategy extends BaseUpdateStrategy<
123
- 'pyth',
124
- 'hasui'
125
- > {
126
- readonly oracleType = 'pyth';
127
- constructor(protected readonly registry: IOraclePackageRegistry<'pyth'>) {
128
- super(registry);
129
- }
130
-
131
- updatePrice(
132
- tx: SuiTxBlock,
133
- request: TransactionArgument,
134
- coinName: 'hasui',
135
- rule: xOracleRuleType
136
- ): void {
137
- const { lst, pythFeedObjectId, pythStateId } =
138
- this.registry.getPackages(coinName);
139
- if (!lst) {
140
- throw new UnsupportedLstOracleError(coinName, this.oracleType);
141
- }
142
- const { id, staking, configId } = lst as PythLstPackages<typeof coinName>;
143
-
144
- tx.moveCall(`${id}::rule::set_price_as_${rule}`, [
145
- request,
146
- pythStateId,
147
- pythFeedObjectId,
148
- configId,
149
- staking,
150
- tx.sharedObjectRef(this.clockObject),
151
- ]);
152
- }
153
- }
154
-
155
- export class SupraDefaultUpdateStrategy extends BaseUpdateStrategy<'supra'> {
156
- readonly oracleType = 'supra';
157
- constructor(protected readonly registry: IOraclePackageRegistry<'supra'>) {
158
- super(registry);
159
- }
160
-
161
- updatePrice(
162
- tx: SuiTxBlock,
163
- request: TransactionArgument,
164
- coinName: string,
165
- rule: xOracleRuleType
166
- ): void {
167
- const { supraPackageId, supraHolderId, supraRegistryId } =
168
- this.registry.getPackages(coinName);
169
-
170
- tx.moveCall(
171
- `${supraPackageId}::rule::set_price_as_${rule}`,
172
- [
173
- request,
174
- supraHolderId,
175
- supraRegistryId,
176
- tx.sharedObjectRef(this.clockObject),
177
- ],
178
- [this.utils.parseCoinType(coinName)]
179
- );
180
- }
181
- }
182
-
183
- export class SwitchboardDefaultUpdateStrategy extends BaseUpdateStrategy<'switchboard'> {
184
- readonly oracleType = 'switchboard';
185
- constructor(
186
- protected readonly registry: IOraclePackageRegistry<'switchboard'>
187
- ) {
188
- super(registry);
189
- }
190
-
191
- updatePrice(
192
- tx: SuiTxBlock,
193
- request: TransactionArgument,
194
- coinName: string,
195
- rule: xOracleRuleType
196
- ): void {
197
- const {
198
- switchboardPackageId,
199
- switchboardAggregatorId,
200
- switchboardRegistryId,
201
- } = this.registry.getPackages(coinName);
202
-
203
- tx.moveCall(
204
- `${switchboardPackageId}::rule::set_price_as_${rule}`,
205
- [
206
- request,
207
- switchboardAggregatorId,
208
- switchboardRegistryId,
209
- tx.sharedObjectRef(this.clockObject),
210
- ],
211
- [this.utils.parseCoinType(coinName)]
212
- );
213
- }
214
- }
@@ -1,153 +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
- PythAfSuiUpdateStrategy,
8
- PythDefaultUpdateStrategy,
9
- PythHaSuiUpdateStrategy,
10
- SupraDefaultUpdateStrategy,
11
- SwitchboardDefaultUpdateStrategy,
12
- } from './xOracleUpdateStrategy';
13
- import { UnsupportedOracleError } from './error';
14
-
15
- export interface IXOracleUpdater<
16
- T extends SupportOracleType = SupportOracleType,
17
- > {
18
- oracleName: T; // e.g. 'pyth', 'supra', 'switchboard'
19
- updateXOracle(
20
- coinName: string,
21
- rule: xOracleRuleType,
22
- request: TransactionArgument
23
- ): void;
24
- }
25
-
26
- abstract class BaseXOracleUpdater<
27
- T extends SupportOracleType = SupportOracleType,
28
- > implements IXOracleUpdater
29
- {
30
- protected strategies: Map<string, IXOracleUpdateStrategy<T>>;
31
- abstract readonly oracleName: T;
32
-
33
- constructor(
34
- protected readonly tx: SuiTxBlock,
35
- protected readonly registry: IOraclePackageRegistry<T>,
36
- listStrategies: Record<string, IXOracleUpdateStrategy<T>>
37
- ) {
38
- this.strategies = new Map(Object.entries(listStrategies));
39
- }
40
-
41
- abstract updateXOracle(
42
- coinName: string,
43
- rule: xOracleRuleType,
44
- request: TransactionArgument
45
- ): void;
46
- }
47
-
48
- class PythXOracleUpdater extends BaseXOracleUpdater<'pyth'> {
49
- readonly oracleName = 'pyth';
50
- protected readonly SUPPORT_SUI_LST_SET: Set<string> = new Set(
51
- SUPPORT_SUI_LST
52
- );
53
-
54
- constructor(tx: SuiTxBlock, registry: IOraclePackageRegistry<'pyth'>) {
55
- super(tx, registry, {
56
- default: new PythDefaultUpdateStrategy(registry),
57
- afsui: new PythAfSuiUpdateStrategy(registry),
58
- hasui: new PythHaSuiUpdateStrategy(registry),
59
- });
60
- }
61
-
62
- private resolveStrategy(
63
- coinName: string
64
- ): IXOracleUpdateStrategy<typeof this.oracleName> {
65
- // decide which key to look up in this.strategies
66
- const strat =
67
- this.strategies.get(coinName) ?? this.strategies.get('default');
68
-
69
- return strat as IXOracleUpdateStrategy<typeof this.oracleName>;
70
- }
71
-
72
- updateXOracle(
73
- coinName: string,
74
- rule: xOracleRuleType,
75
- request: TransactionArgument
76
- ): void {
77
- const strategy = this.resolveStrategy(coinName);
78
- strategy.updatePrice(this.tx, request, coinName, rule);
79
- }
80
- }
81
-
82
- class SupraXOracleUpdater extends BaseXOracleUpdater<'supra'> {
83
- readonly oracleName = 'supra';
84
-
85
- constructor(tx: SuiTxBlock, registry: IOraclePackageRegistry<'supra'>) {
86
- super(tx, registry, {
87
- default: new SupraDefaultUpdateStrategy(registry), // Placeholder, implement Supra strategy
88
- });
89
- }
90
-
91
- updateXOracle(
92
- coinName: string,
93
- rule: xOracleRuleType,
94
- request: TransactionArgument
95
- ): void {
96
- const stragtegy = this.strategies.get('default');
97
- if (!stragtegy) {
98
- throw new UnsupportedOracleError(this.oracleName);
99
- }
100
- stragtegy.updatePrice(this.tx, request, coinName, rule);
101
- }
102
- }
103
-
104
- class SwitchboardXOracleUpdater extends BaseXOracleUpdater<'switchboard'> {
105
- readonly oracleName = 'switchboard';
106
- constructor(tx: SuiTxBlock, registry: IOraclePackageRegistry<'switchboard'>) {
107
- super(tx, registry, {
108
- // Placeholder, implement Switchboard strategy
109
- default: new SwitchboardDefaultUpdateStrategy(registry),
110
- });
111
- }
112
-
113
- updateXOracle(
114
- coinName: string,
115
- rule: xOracleRuleType,
116
- request: TransactionArgument
117
- ): void {
118
- const stragtegy = this.strategies.get('default');
119
- if (!stragtegy) {
120
- throw new UnsupportedOracleError(this.oracleName);
121
- }
122
- stragtegy.updatePrice(this.tx, request, coinName, rule);
123
- }
124
- }
125
-
126
- export const createXOracleUpdater = (
127
- tx: SuiTxBlock,
128
- registry: IOraclePackageRegistry
129
- ) => {
130
- const oracleType = registry.oracleName;
131
- switch (oracleType) {
132
- case 'pyth': {
133
- return new PythXOracleUpdater(
134
- tx,
135
- registry as IOraclePackageRegistry<'pyth'>
136
- );
137
- }
138
- case 'supra': {
139
- return new SupraXOracleUpdater(
140
- tx,
141
- registry as IOraclePackageRegistry<'supra'>
142
- );
143
- }
144
- case 'switchboard': {
145
- return new SwitchboardXOracleUpdater(
146
- tx,
147
- registry as IOraclePackageRegistry<'switchboard'>
148
- );
149
- }
150
- default:
151
- throw new Error(`Unsupported oracle type: ${oracleType}`);
152
- }
153
- };
@@ -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