@scallop-io/scallop-swap-sdk 1.3.2 → 2.1.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.
package/README.md CHANGED
@@ -1,3 +1,34 @@
1
1
  ### Scallop Swap Meta Aggregator SDK
2
2
 
3
3
  A unified SDK that aggregates existing swap aggregators on Sui, including Aftermath, Cetus, 7K, and FlowX and provides a generic `SwapSdkBase` class for seamless integration and extension with future swap aggregators.
4
+
5
+ #### Adding a Custom SDK Integration
6
+
7
+ Extend `SwapSdkBase` with your own SDK-specific generic types:
8
+
9
+ ```typescript
10
+ import { SwapSdkBase, SwapSdkConstructorParams } from '@scallop-io/scallop-swap-sdk';
11
+
12
+ class MyDexSwap extends SwapSdkBase<MyPoolType, MyRawRoute, MyFetchSettings, MyClient> {
13
+ constructor(params: SwapSdkConstructorParams<MyFetchSettings>) {
14
+ super('mydex', params);
15
+ this.client = new MyClient(/* ... */);
16
+ }
17
+
18
+ // Implement abstract methods: fetchRoute, buildSwapTransaction, setFetchRouteSettings, calcSlippage
19
+ }
20
+ ```
21
+
22
+ To get type-safe access via `aggregator.getAggregator('mydex')`, extend the `SdkRegistry` interface using declaration merging:
23
+
24
+ ```typescript
25
+ declare module '@scallop-io/scallop-swap-sdk' {
26
+ interface SdkRegistry {
27
+ mydex: MyDexSwap;
28
+ }
29
+ }
30
+
31
+ // Now fully typed:
32
+ const mydex = aggregator.getAggregator('mydex'); // MyDexSwap | undefined
33
+ mydex?.client; // MyClient | undefined
34
+ ```
package/dist/index.d.mts CHANGED
@@ -3,13 +3,17 @@ import { SuiClient, CoinMetadata, SuiTransactionBlockResponse, DryRunTransaction
3
3
  import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
4
4
  import { EventEmitter } from 'events';
5
5
  import * as _scallop_io_sui_kit from '@scallop-io/sui-kit';
6
- import { SuiKitParams, AccountManagerParams, SuiKit, Transaction as Transaction$1, TransactionObjectArgument as TransactionObjectArgument$1 } from '@scallop-io/sui-kit';
6
+ import { SuiKitParams, AccountManagerParams, Transaction as Transaction$1, TransactionObjectArgument as TransactionObjectArgument$1, SuiKit } from '@scallop-io/sui-kit';
7
7
  import * as _mysten_sui_dist_cjs_transactions from '@mysten/sui/dist/cjs/transactions';
8
8
  import { RouterDataV3, FindRouterParams, AggregatorClient, AggregatorClientParams } from '@cetusprotocol/aggregator-sdk';
9
+ export { AggregatorClient, RouterDataV3 } from '@cetusprotocol/aggregator-sdk';
9
10
  import * as aftermath_ts_sdk from 'aftermath-ts-sdk';
10
11
  import { RouterProtocolName, RouterCompleteTradeRoute, ApiRouterPartialCompleteTradeRouteBody, Aftermath, AftermathApi, ConfigAddresses } from 'aftermath-ts-sdk';
12
+ export { Aftermath, RouterCompleteTradeRoute } from 'aftermath-ts-sdk';
11
13
  import { SourceDex, QuoteResponse, Commission } from '@7kprotocol/sdk-ts';
12
- import { Protocol, GetRoutesResult, Coin as Coin$1, SingleQuoteQueryParams, Commission as Commission$1 } from '@flowx-finance/sdk';
14
+ export { QuoteResponse } from '@7kprotocol/sdk-ts';
15
+ import { Protocol, GetRoutesResult, Coin as Coin$1, SingleQuoteQueryParams, AggregatorQuoter, Commission as Commission$1 } from '@flowx-finance/sdk';
16
+ export { AggregatorQuoter, GetRoutesResult } from '@flowx-finance/sdk';
13
17
 
14
18
  type SecretKeyOrMnemonicsOrWalletAddress = {
15
19
  mnemonics: string;
@@ -96,7 +100,7 @@ type SwapBuildResult<MergeResult extends boolean> = MergeResult extends true ? {
96
100
  coinOut: TransactionObjectArgument;
97
101
  };
98
102
  interface SwapSdkConstructorParams<T> {
99
- client?: SuiClient;
103
+ suiClient?: SuiClient;
100
104
  fetchTimeoutInMs?: number;
101
105
  walletAddress: string;
102
106
  fetchSettings?: T;
@@ -104,7 +108,11 @@ interface SwapSdkConstructorParams<T> {
104
108
  /**
105
109
  * Base interface for Swap SDK
106
110
  */
107
- interface SwapSdkBaseInterface<PoolType = string, RawRouteResultType = any, FetchRouteSettings = any> {
111
+ interface SwapSdkBaseInterface<PoolType = string, RawRouteResultType = any, FetchRouteSettings = any, SdkClient = SuiClient> {
112
+ /**
113
+ * SDK specific client
114
+ */
115
+ client?: SdkClient;
108
116
  /**
109
117
  * How long the route fetch request should wait before timing out (in milliseconds)
110
118
  */
@@ -197,11 +205,12 @@ declare class TypedEventEmitter<Events extends Record<string, any>> extends Even
197
205
  emit<K extends keyof Events & (string | symbol)>(event: K, payload: Events[K]): boolean;
198
206
  }
199
207
 
200
- declare abstract class SwapSdkBase<PoolType = string, RawRouteResultType = any, FetchRouteSettings = any> implements SwapSdkBaseInterface<PoolType, RawRouteResultType, FetchRouteSettings> {
208
+ declare abstract class SwapSdkBase<PoolType = string, RawRouteResultType = any, FetchRouteSettings = any, SdkClient = any> implements SwapSdkBaseInterface<PoolType, RawRouteResultType, FetchRouteSettings, SdkClient> {
201
209
  protected rawRouteResult?: RawRouteResultType;
202
210
  readonly name: string;
203
211
  readonly events: TypedEventEmitter<SwapSdkEvents<PoolType>>;
204
- client: SuiClient;
212
+ client?: SdkClient;
213
+ suiClient: SuiClient;
205
214
  fetchTimeoutInMs?: number;
206
215
  walletAddress: string;
207
216
  fetchSettings?: FetchRouteSettings;
@@ -211,8 +220,8 @@ declare abstract class SwapSdkBase<PoolType = string, RawRouteResultType = any,
211
220
  protected validateWalletAddress(): asserts this is typeof SwapSdkBase & {
212
221
  walletAddress: string;
213
222
  };
214
- protected validateRawRouteResult(): asserts this is typeof SwapSdkBase & {
215
- rawRouteResult: RawRouteResultType;
223
+ protected validateClient(): asserts this is typeof SwapSdkBase & {
224
+ client: SdkClient;
216
225
  };
217
226
  setWalletAddress(address: string): void;
218
227
  setSuiClient(client: SuiClient): void;
@@ -239,111 +248,28 @@ declare abstract class SwapSdkBase<PoolType = string, RawRouteResultType = any,
239
248
  }>;
240
249
  }
241
250
 
242
- declare class CoinMetadataRegistry {
243
- client: SuiClient;
244
- private cache;
245
- constructor(client: SuiClient, initialData?: (CoinMetadata & {
246
- coinType: string;
247
- })[]);
248
- setClient(client: SuiClient): void;
249
- getCoinMetadata(coinType: string): Promise<CoinMetadata>;
250
- }
251
-
252
- type TxExt = {
253
- initTx: Transaction;
254
- coinIn: TransactionObjectArgument;
255
- };
256
- type BuildOptionsBase = {
257
- slippage?: number;
258
- txExtensionParams?: TxExt;
259
- };
260
- declare class Aggregator {
261
- #private;
262
- readonly coinMetadata: CoinMetadataRegistry;
263
- readonly suiKit: SuiKit | undefined;
264
- readonly events: TypedEventEmitter<AggregatorEvents<string>>;
265
- protected _slippage: number;
266
- private signerMode;
267
- private aggregatorMap;
268
- private activeAggregator;
269
- private _fetchingStatus;
270
- private poolStatusForwarders;
271
- walletAddress: string;
272
- client: SuiClient;
273
- constructor(params: AggregatorConstructorParams);
274
- private forwardPoolStatusChangeEvent;
275
- addAggregator(aggregator: SwapSdkBase[]): void;
276
- getActiveAggregators(): string[];
277
- getAggregator(name: string): SwapSdkBase<string, any, any> | undefined;
278
- toggleAggregator(name: string, enabled: boolean): void;
279
- removeAggregator(name: string): void;
280
- get aggregators(): SwapSdkBase<string, any, any>[];
281
- /**
282
- * Per-aggregator fetching status. True while that aggregator's fetchRoute is in-flight.
283
- */
284
- get fetchingStatus(): Record<string, boolean>;
285
- /**
286
- * Get slippage (0.01 = 1%)
287
- */
288
- get slippage(): number;
289
- /**
290
- * Set slippage (0.01 = 1%)
291
- * @param slippageInDecimal
292
- */
293
- setSlippage(slippageInDecimal: number): void;
294
- /**
295
- * Set fetch timeout in milliseconds for all aggregators
296
- * @param timeoutInMs
297
- */
298
- setFetchTimeoutInMs(timeoutInMs: number): void;
299
- setSuiClient(client: SuiClient): void;
300
- setWalletAddress(address: string): void;
301
- getCoinMetadata(coinType: string): Promise<_mysten_sui_dist_cjs_client.CoinMetadata>;
302
- /**
303
- * Fetch swap routes from all aggregators, and sort them by best output amount
304
- * @param params FetchRouteParams
305
- * @param options FetchRoutesOptions
306
- */
307
- fetchRoutes(params: FetchRouteParams, options?: FetchRoutesOptions): Promise<{
308
- routes: FetchRouteResult<any>[];
309
- errors: {
310
- name: string;
311
- reason: unknown;
312
- }[];
313
- }>;
314
- /**
315
- * Build transaction with the selected aggregator's route
316
- * @param options Transaction building options
317
- */
318
- buildRouteTransaction(route: FetchRouteResult<any>, options?: BuildOptionsBase & {
319
- mergeResult?: true;
320
- }): Promise<SwapBuildResult<true>>;
321
- buildRouteTransaction(route: FetchRouteResult<any>, options: BuildOptionsBase & {
322
- mergeResult: false;
323
- }): Promise<SwapBuildResult<false>>;
324
- executeSwapTransaction(tx: Transaction): Promise<SuiTransactionBlockResponse>;
325
- executeSwapTransaction(tx: Transaction, dryRun: true): Promise<DryRunTransactionBlockResponse>;
326
- executeSwapTransaction(tx: Transaction, dryRun: false): Promise<SuiTransactionBlockResponse>;
327
- }
328
-
329
251
  type FetchRouteSettings$3 = Omit<FindRouterParams, 'from' | 'target' | 'amount' | 'byAmountIn' | 'providers'>;
330
- declare class CetusSwap extends SwapSdkBase<string, RouterDataV3, FetchRouteSettings$3> {
331
- readonly cetusClient: AggregatorClient;
332
- constructor(params: SwapSdkConstructorParams<FetchRouteSettings$3> & AggregatorClientParams);
252
+
253
+ declare class CetusSwap extends SwapSdkBase<string, RouterDataV3, FetchRouteSettings$3, AggregatorClient> {
254
+ constructor(params: SwapSdkConstructorParams<FetchRouteSettings$3> & Omit<AggregatorClientParams, 'client'>);
333
255
  private createRoute;
334
256
  /**
335
257
  * Parse the raw route data from Cetus to the standardized FetchRouteResult format.
336
258
  * @returns FormattedRouteResult
337
259
  */
338
- private parseRawToFormattedResult;
260
+ parseRawToFormattedResult({ routerData, coinInType, coinOutType, }: {
261
+ routerData: RouterDataV3;
262
+ coinInType: string;
263
+ coinOutType: string;
264
+ }): Omit<FormattedRouteResult, 'name'>;
339
265
  setFetchRouteSettings(settings: FindRouterParams): void;
340
266
  calcSlippage(slippageInDecimal: number): number;
341
267
  fetchRoute(params: FetchRouteParams, abortSignal?: AbortSignal): Promise<{
342
268
  formattedResult: {
343
269
  name: string;
344
- coinOut: Coin;
345
270
  routes: SwapRoute[];
346
271
  coinIn: Coin;
272
+ coinOut: Coin;
347
273
  };
348
274
  rawRouteResult: RouterDataV3;
349
275
  }>;
@@ -354,8 +280,8 @@ declare class CetusSwap extends SwapSdkBase<string, RouterDataV3, FetchRouteSett
354
280
  }
355
281
 
356
282
  type FetchRouteSettings$2 = Omit<ApiRouterPartialCompleteTradeRouteBody, 'coinInType' | 'coinOutType' | 'coinInAmount' | 'protocolWhitelist' | 'protocolBlacklist'>;
357
- declare class AftermathSwap extends SwapSdkBase<RouterProtocolName, RouterCompleteTradeRoute, FetchRouteSettings$2> {
358
- readonly afClient: Aftermath;
283
+
284
+ declare class AftermathSwap extends SwapSdkBase<RouterProtocolName, RouterCompleteTradeRoute, FetchRouteSettings$2, Aftermath> {
359
285
  readonly afApi: AftermathApi;
360
286
  constructor(params: SwapSdkConstructorParams<FetchRouteSettings$2> & {
361
287
  addresses?: ConfigAddresses;
@@ -391,6 +317,7 @@ interface Params {
391
317
  isSponsored?: boolean;
392
318
  }
393
319
  type FetchRouteSettings$1 = Omit<Params, 'tokenIn' | 'tokenOut' | 'amountIn' | 'sources'>;
320
+
394
321
  declare class _7kSwap extends SwapSdkBase<SourceDex, QuoteResponse, FetchRouteSettings$1> {
395
322
  commission: Commission;
396
323
  constructor(params: SwapSdkConstructorParams<FetchRouteSettings$1> & {
@@ -409,8 +336,9 @@ declare class _7kSwap extends SwapSdkBase<SourceDex, QuoteResponse, FetchRouteSe
409
336
  }
410
337
 
411
338
  type FetchRouteSettings = Omit<SingleQuoteQueryParams, 'amountIn' | 'tokenIn' | 'tokenOut' | 'excludeSources' | 'includeSources'>;
412
- declare class FlowXSwap extends SwapSdkBase<Protocol, GetRoutesResult<Coin$1, Coin$1>, FetchRouteSettings> {
413
- private quoter;
339
+
340
+ declare class FlowXSwap extends SwapSdkBase<Protocol, GetRoutesResult<Coin$1, Coin$1>, FetchRouteSettings, AggregatorQuoter> {
341
+ client: AggregatorQuoter;
414
342
  commission?: Commission$1;
415
343
  constructor(params: SwapSdkConstructorParams<FetchRouteSettings> & Omit<SingleQuoteQueryParams, 'amountIn' | 'tokenIn' | 'tokenOut'> & {
416
344
  commission?: Commission$1;
@@ -430,9 +358,105 @@ declare class FlowXSwap extends SwapSdkBase<Protocol, GetRoutesResult<Coin$1, Co
430
358
  }>;
431
359
  }
432
360
 
361
+ interface SdkRegistry {
362
+ cetus: CetusSwap;
363
+ aftermath: AftermathSwap;
364
+ '7k': _7kSwap;
365
+ flowx: FlowXSwap;
366
+ }
367
+ type SdkName = keyof SdkRegistry;
368
+
369
+ declare class CoinMetadataRegistry {
370
+ client: SuiClient;
371
+ private cache;
372
+ constructor(client: SuiClient, initialData?: (CoinMetadata & {
373
+ coinType: string;
374
+ })[]);
375
+ setClient(client: SuiClient): void;
376
+ getCoinMetadata(coinType: string): Promise<CoinMetadata>;
377
+ }
378
+
379
+ type TxExt = {
380
+ initTx: Transaction;
381
+ coinIn: TransactionObjectArgument;
382
+ };
383
+ type BuildOptionsBase = {
384
+ slippage?: number;
385
+ txExtensionParams?: TxExt;
386
+ };
387
+ declare class Aggregator {
388
+ #private;
389
+ readonly coinMetadata: CoinMetadataRegistry;
390
+ readonly suiKit: SuiKit | undefined;
391
+ readonly events: TypedEventEmitter<AggregatorEvents<string>>;
392
+ protected _slippage: number;
393
+ private signerMode;
394
+ private aggregatorMap;
395
+ private activeAggregator;
396
+ private _fetchingStatus;
397
+ private poolStatusForwarders;
398
+ walletAddress: string;
399
+ client: SuiClient;
400
+ constructor(params: AggregatorConstructorParams);
401
+ private forwardPoolStatusChangeEvent;
402
+ addAggregator(aggregator: SwapSdkBase[]): void;
403
+ getActiveAggregators(): string[];
404
+ getAggregator<T extends SdkName>(name: T): SdkRegistry[T] | undefined;
405
+ getAggregator(name: string): SwapSdkBase | undefined;
406
+ toggleAggregator(name: string, enabled: boolean): void;
407
+ removeAggregator(name: string): void;
408
+ get aggregators(): SwapSdkBase<string, any, any, any>[];
409
+ /**
410
+ * Per-aggregator fetching status. True while that aggregator's fetchRoute is in-flight.
411
+ */
412
+ get fetchingStatus(): Record<string, boolean>;
413
+ /**
414
+ * Get slippage (0.01 = 1%)
415
+ */
416
+ get slippage(): number;
417
+ /**
418
+ * Set slippage (0.01 = 1%)
419
+ * @param slippageInDecimal
420
+ */
421
+ setSlippage(slippageInDecimal: number): void;
422
+ /**
423
+ * Set fetch timeout in milliseconds for all aggregators
424
+ * @param timeoutInMs
425
+ */
426
+ setFetchTimeoutInMs(timeoutInMs: number): void;
427
+ setSuiClient(client: SuiClient): void;
428
+ setWalletAddress(address: string): void;
429
+ getCoinMetadata(coinType: string): Promise<_mysten_sui_dist_cjs_client.CoinMetadata>;
430
+ /**
431
+ * Fetch swap routes from all aggregators, and sort them by best output amount
432
+ * @param params FetchRouteParams
433
+ * @param options FetchRoutesOptions
434
+ */
435
+ fetchRoutes(params: FetchRouteParams, options?: FetchRoutesOptions): Promise<{
436
+ routes: FetchRouteResult<any>[];
437
+ errors: {
438
+ name: string;
439
+ reason: unknown;
440
+ }[];
441
+ }>;
442
+ /**
443
+ * Build transaction with the selected aggregator's route
444
+ * @param options Transaction building options
445
+ */
446
+ buildRouteTransaction(route: FetchRouteResult<any>, options?: BuildOptionsBase & {
447
+ mergeResult?: true;
448
+ }): Promise<SwapBuildResult<true>>;
449
+ buildRouteTransaction(route: FetchRouteResult<any>, options: BuildOptionsBase & {
450
+ mergeResult: false;
451
+ }): Promise<SwapBuildResult<false>>;
452
+ executeSwapTransaction(tx: Transaction): Promise<SuiTransactionBlockResponse>;
453
+ executeSwapTransaction(tx: Transaction, dryRun: true): Promise<DryRunTransactionBlockResponse>;
454
+ executeSwapTransaction(tx: Transaction, dryRun: false): Promise<SuiTransactionBlockResponse>;
455
+ }
456
+
433
457
  declare const isSuiType: (coinType: string) => boolean;
434
458
  declare const selectCoins: (client: SuiClient, coinType: string, amount: string, owner: string) => Promise<CoinStruct[]>;
435
459
  declare const mergeWithExistingOrTransfer: (tx: Transaction, coin: TransactionObjectArgument, client: SuiClient, coinType: string, sender: string) => Promise<void>;
436
460
  declare const transformProperCase: (provider: string) => string;
437
461
 
438
- export { AftermathSwap, Aggregator, type AggregatorConstructorParams, type AggregatorEvents, type AggregatorStatusEvent, CetusSwap, type Coin, CoinMetadataRegistry, type FetchRouteParams, type FetchRouteResult, type FetchRoutesOptions, FlowXSwap, type FormattedRouteResult, type SecretKeyOrMnemonicsOrWalletAddress, type SuiClientOrFullnodeUrl, type SwapBuildResult, type SwapCoinInfo, type SwapParams, type SwapPath, type SwapRoute, SwapSdkBase, type SwapSdkBaseInterface, type SwapSdkConstructorParams, type SwapSdkEvents, type SwapSdkPoolStatusEvent, TypedEventEmitter, _7kSwap, isSuiType, mergeWithExistingOrTransfer, selectCoins, transformProperCase };
462
+ export { type FetchRouteSettings$2 as AftermathFetchRouteSettings, AftermathSwap, Aggregator, type AggregatorConstructorParams, type AggregatorEvents, type AggregatorStatusEvent, type FetchRouteSettings$3 as CetusFetchRouteSettings, CetusSwap, type Coin, CoinMetadataRegistry, type FetchRouteParams, type FetchRouteResult, type FetchRoutesOptions, type FetchRouteSettings as FlowXFetchRouteSettings, FlowXSwap, type FormattedRouteResult, type SdkName, type SdkRegistry, type SecretKeyOrMnemonicsOrWalletAddress, type SuiClientOrFullnodeUrl, type SwapBuildResult, type SwapCoinInfo, type SwapParams, type SwapPath, type SwapRoute, SwapSdkBase, type SwapSdkBaseInterface, type SwapSdkConstructorParams, type SwapSdkEvents, type SwapSdkPoolStatusEvent, TypedEventEmitter, type FetchRouteSettings$1 as _7kFetchRouteSettings, _7kSwap, isSuiType, mergeWithExistingOrTransfer, selectCoins, transformProperCase };