@cetusprotocol/aggregator-sdk 0.4.0 → 0.4.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 (102) hide show
  1. package/README.md +19 -8
  2. package/dist/index.d.mts +14 -3
  3. package/dist/index.d.ts +14 -3
  4. package/dist/index.js +25 -27
  5. package/dist/index.mjs +26 -28
  6. package/package.json +1 -1
  7. package/dist/src/api.d.ts +0 -104
  8. package/dist/src/client.d.ts +0 -107
  9. package/dist/src/const.d.ts +0 -8
  10. package/dist/src/errors.d.ts +0 -40
  11. package/dist/src/index.d.ts +0 -9
  12. package/dist/src/math.d.ts +0 -6
  13. package/dist/src/transaction/afsui.d.ts +0 -10
  14. package/dist/src/transaction/aftermath.d.ts +0 -13
  15. package/dist/src/transaction/alphafi.d.ts +0 -7
  16. package/dist/src/transaction/bluefin.d.ts +0 -7
  17. package/dist/src/transaction/bluemove.d.ts +0 -7
  18. package/dist/src/transaction/cetus.d.ts +0 -15
  19. package/dist/src/transaction/deepbook_v2.d.ts +0 -14
  20. package/dist/src/transaction/deepbook_v3.d.ts +0 -12
  21. package/dist/src/transaction/flowx_v2.d.ts +0 -7
  22. package/dist/src/transaction/flowx_v3.d.ts +0 -8
  23. package/dist/src/transaction/haedal.d.ts +0 -6
  24. package/dist/src/transaction/haedal_pmm.d.ts +0 -7
  25. package/dist/src/transaction/index.d.ts +0 -10
  26. package/dist/src/transaction/kriya_v2.d.ts +0 -6
  27. package/dist/src/transaction/kriya_v3.d.ts +0 -7
  28. package/dist/src/transaction/metastable.d.ts +0 -8
  29. package/dist/src/transaction/obric.d.ts +0 -8
  30. package/dist/src/transaction/scallop.d.ts +0 -8
  31. package/dist/src/transaction/steamm.d.ts +0 -6
  32. package/dist/src/transaction/suilend.d.ts +0 -7
  33. package/dist/src/transaction/swap.d.ts +0 -4
  34. package/dist/src/transaction/turbos.d.ts +0 -7
  35. package/dist/src/transaction/volo.d.ts +0 -8
  36. package/dist/src/types/CoinAssist.d.ts +0 -122
  37. package/dist/src/types/sui.d.ts +0 -112
  38. package/dist/src/utils/api.d.ts +0 -1
  39. package/dist/src/utils/coin.d.ts +0 -11
  40. package/dist/src/utils/contracts.d.ts +0 -16
  41. package/dist/src/utils/index.d.ts +0 -5
  42. package/dist/src/utils/msafe.d.ts +0 -2
  43. package/dist/src/utils/transaction.d.ts +0 -3
  44. package/dist/tests/math.test.d.ts +0 -1
  45. package/dist/tests/router/metastable.test.d.ts +0 -2
  46. package/dist/tests/router/obric.test.d.ts +0 -2
  47. package/dist/tests/router/scallop.test.d.ts +0 -2
  48. package/dist/tests/router/steamm.test.d.ts +0 -2
  49. package/dist/tests/router.test.d.ts +0 -2
  50. package/dist/tests/test_data.test.d.ts +0 -18
  51. package/dist/tests/wallet.test.d.ts +0 -1
  52. package/example/package.json +0 -17
  53. package/example/swap.ts +0 -21
  54. package/example/tsconfig.json +0 -12
  55. package/jest.config.mjs +0 -13
  56. package/src/api.ts +0 -320
  57. package/src/client.ts +0 -1081
  58. package/src/const.ts +0 -15
  59. package/src/errors.ts +0 -77
  60. package/src/index.ts +0 -10
  61. package/src/math.ts +0 -66
  62. package/src/transaction/afsui.ts +0 -62
  63. package/src/transaction/aftermath.ts +0 -88
  64. package/src/transaction/alphafi.ts +0 -50
  65. package/src/transaction/bluefin.ts +0 -46
  66. package/src/transaction/bluemove.ts +0 -42
  67. package/src/transaction/cetus.ts +0 -118
  68. package/src/transaction/deepbook_v2.ts +0 -122
  69. package/src/transaction/deepbook_v3.ts +0 -60
  70. package/src/transaction/flowx_v2.ts +0 -42
  71. package/src/transaction/flowx_v3.ts +0 -52
  72. package/src/transaction/haedal.ts +0 -33
  73. package/src/transaction/haedal_pmm.ts +0 -80
  74. package/src/transaction/index.ts +0 -42
  75. package/src/transaction/kriya_v2.ts +0 -37
  76. package/src/transaction/kriya_v3.ts +0 -47
  77. package/src/transaction/metastable.ts +0 -141
  78. package/src/transaction/obric.ts +0 -90
  79. package/src/transaction/scallop.ts +0 -64
  80. package/src/transaction/steamm.ts +0 -77
  81. package/src/transaction/suilend.ts +0 -48
  82. package/src/transaction/swap.ts +0 -162
  83. package/src/transaction/turbos.ts +0 -56
  84. package/src/transaction/volo.ts +0 -53
  85. package/src/types/CoinAssist.ts +0 -217
  86. package/src/types/sui.ts +0 -148
  87. package/src/utils/api.ts +0 -6
  88. package/src/utils/coin.ts +0 -136
  89. package/src/utils/contracts.ts +0 -136
  90. package/src/utils/index.ts +0 -5
  91. package/src/utils/msafe.ts +0 -40
  92. package/src/utils/transaction.ts +0 -20
  93. package/tests/math.test.ts +0 -21
  94. package/tests/router/metastable.test.ts +0 -377
  95. package/tests/router/obric.test.ts +0 -197
  96. package/tests/router/scallop.test.ts +0 -127
  97. package/tests/router/steamm.test.ts +0 -124
  98. package/tests/router.test.ts +0 -373
  99. package/tests/test_data.test.ts +0 -28
  100. package/tests/wallet.test.ts +0 -21
  101. package/tsup.config.ts +0 -23
  102. package/version.mjs +0 -28
package/README.md CHANGED
@@ -41,9 +41,22 @@ npm install @cetusprotocol/aggregator-sdk
41
41
 
42
42
  ## 1. Init client with rpc and package config
43
43
 
44
- ```typescript
45
- const client = new AggregatorClient()
46
- ```
44
+ 1. Fast init:
45
+ ```typescript
46
+ const client = new AggregatorClient()
47
+ ```
48
+
49
+ 2. Full init:
50
+ ```typescript
51
+ const client = new AggregatorClient({
52
+ // endpoint, // If you do not have a exclusive aggregator api domain,just use cetus default aggregator endpoints.
53
+ signer: wallet,
54
+ client: suiClient,
55
+ env: Env.Mainnet,
56
+ pythUrls: ["YOUR_PYTH_URL", "ANOTHER_PYTH_URL"],
57
+ })
58
+ ```
59
+ **Notes**: Some providers, such as HaedalHMM and Metastable, rely on Pyth oracle prices when build transactions. Currently, we have a default configuration that connects to Pyth's publicly available node. However, if you frequently build transactions, we recommend setting up a private Pyth node interface as an additional backup. This will ensure uninterrupted access to HaedalHMM and Metastable, even if the public node experiences occasional downtime.
47
60
 
48
61
  ## 2. Get best router swap result from aggregator service
49
62
 
@@ -53,7 +66,7 @@ const from = "0x2::sui::SUI"
53
66
  const target =
54
67
  "0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS"
55
68
 
56
- const routerRes = await client.findRouters({
69
+ const routers = await client.findRouters({
57
70
  from,
58
71
  target,
59
72
  amount,
@@ -68,8 +81,7 @@ const routerTx = new Transaction()
68
81
 
69
82
  if (routerRes != null) {
70
83
  await client.fastRouterSwap({
71
- routers: routerRes.routes,
72
- byAmountIn,
84
+ routers,
73
85
  txb: routerTx,
74
86
  slippage: 0.01,
75
87
  isMergeTragetCoin: true,
@@ -94,8 +106,7 @@ const byAmountIn = true;
94
106
 
95
107
  if (routerRes != null) {
96
108
  const targetCoin = await client.routerSwap({
97
- routers: routerRes.routes,
98
- byAmountIn,
109
+ routers,
99
110
  txb: routerTx,
100
111
  inputCoin,
101
112
  slippage: 0.01,
package/dist/index.d.mts CHANGED
@@ -4,6 +4,7 @@ import { TransactionObjectArgument, Transaction } from '@mysten/sui/transactions
4
4
  import { Signer } from '@mysten/sui/cryptography';
5
5
  import BN from 'bn.js';
6
6
  import Decimal from 'decimal.js';
7
+ import { SuiPriceServiceConnection } from '@pythnetwork/pyth-sui-js';
7
8
 
8
9
  /**
9
10
  * Represents a SUI address, which is a string.
@@ -128,16 +129,26 @@ interface SwapInPoolsResult {
128
129
  isExceed: boolean;
129
130
  routeData?: RouterData;
130
131
  }
132
+ type AggregatorClientParams = {
133
+ endpoint?: string;
134
+ signer?: string;
135
+ client?: SuiClient;
136
+ env?: Env;
137
+ pythUrls?: string[];
138
+ apiKey?: string;
139
+ };
131
140
  declare class AggregatorClient {
132
141
  endpoint: string;
133
142
  signer: string;
134
143
  client: SuiClient;
135
144
  env: Env;
145
+ apiKey: string;
136
146
  private allCoins;
137
147
  private pythConnections;
138
148
  private pythClient;
139
149
  private static readonly CONFIG;
140
- constructor(endpoint?: string, signer?: string, client?: SuiClient, env?: Env);
150
+ constructor(params: AggregatorClientParams);
151
+ newPythClients(pythUrls: string[]): SuiPriceServiceConnection[];
141
152
  getCoins(coinType: string, refresh?: boolean): Promise<CoinAsset[]>;
142
153
  findRouters(params: FindRouterParams): Promise<RouterData | null>;
143
154
  executeFlexibleInputSwap(txb: Transaction, inputCoin: TransactionObjectArgument, routers: Router[], amountOutLimit: BN, pythPriceIDs: Map<string, string>, partner?: string, deepbookv3DeepFee?: TransactionObjectArgument, packages?: Map<string, string>): Promise<TransactionObjectArgument>;
@@ -296,7 +307,7 @@ type AggregatorResponse = {
296
307
  msg: string;
297
308
  data: RouterData;
298
309
  };
299
- declare function getRouterResult(endpoint: string, params: FindRouterParams): Promise<RouterData | null>;
310
+ declare function getRouterResult(endpoint: string, apiKey: string, params: FindRouterParams): Promise<RouterData | null>;
300
311
  type DeepbookV3Config = {
301
312
  id: string;
302
313
  is_alternative_payment: boolean;
@@ -321,4 +332,4 @@ declare enum Env {
321
332
  Testnet = 1
322
333
  }
323
334
 
324
- export { AFSUI, AFTERMATH, AGGREGATOR_V2, AGGREGATOR_V2_EXTEND, ALPHAFI, AggregatorClient, type AggregatorResponse, BLUEFIN, BLUEMOVE, type BuildCoinResult, type BuildFastRouterSwapParams, type BuildFastRouterSwapParamsV2, type BuildRouterSwapParams, type BuildRouterSwapParamsV2, CETUS, CLOCK_ADDRESS, DEEPBOOKV2, DEEPBOOKV3, DEFAULT_ENDPOINT, type DeepbookV3Config, type DeepbookV3ConfigResponse, type Dex, Env, type ExtendedDetails, FLOWXV2, FLOWXV3, type FindRouterParams, HAEDAL, HAEDALPMM, KRIYA, KRIYAV3, METASTABLE, OBRIC, ONE, type Path, type PreSwapLpChangeParams, type Router, type RouterData, type RouterError, SCALLOP, SPRINGSUI, STEAMM, SUILEND, type SwapInPoolsParams, type SwapInPoolsResult, TEN_POW_NINE, TURBOS, TWO, U128, U64_MAX, U64_MAX_BN, VOLO, ZERO, buildInputCoin, checkInvalidSuiAddress, compareCoins, completionCoin, composeType, createTarget, dealWithFastRouterSwapParamsForMsafe, extractAddressFromType, extractStructTagFromType, findPythPriceIDs, fixSuiObjectId, getAggregatorV2ExtendPublishedAt, getAggregatorV2PublishedAt, getDeepbookV3Config, getRouterResult, isSortedSymbols, mintZeroCoin, normalizeCoinType, parseRouterResponse, patchFixSuiObjectId, printTransaction, processEndpoint, restituteMsafeFastRouterSwapParams };
335
+ export { AFSUI, AFTERMATH, AGGREGATOR_V2, AGGREGATOR_V2_EXTEND, ALPHAFI, AggregatorClient, type AggregatorClientParams, type AggregatorResponse, BLUEFIN, BLUEMOVE, type BuildCoinResult, type BuildFastRouterSwapParams, type BuildFastRouterSwapParamsV2, type BuildRouterSwapParams, type BuildRouterSwapParamsV2, CETUS, CLOCK_ADDRESS, DEEPBOOKV2, DEEPBOOKV3, DEFAULT_ENDPOINT, type DeepbookV3Config, type DeepbookV3ConfigResponse, type Dex, Env, type ExtendedDetails, FLOWXV2, FLOWXV3, type FindRouterParams, HAEDAL, HAEDALPMM, KRIYA, KRIYAV3, METASTABLE, OBRIC, ONE, type Path, type PreSwapLpChangeParams, type Router, type RouterData, type RouterError, SCALLOP, SPRINGSUI, STEAMM, SUILEND, type SwapInPoolsParams, type SwapInPoolsResult, TEN_POW_NINE, TURBOS, TWO, U128, U64_MAX, U64_MAX_BN, VOLO, ZERO, buildInputCoin, checkInvalidSuiAddress, compareCoins, completionCoin, composeType, createTarget, dealWithFastRouterSwapParamsForMsafe, extractAddressFromType, extractStructTagFromType, findPythPriceIDs, fixSuiObjectId, getAggregatorV2ExtendPublishedAt, getAggregatorV2PublishedAt, getDeepbookV3Config, getRouterResult, isSortedSymbols, mintZeroCoin, normalizeCoinType, parseRouterResponse, patchFixSuiObjectId, printTransaction, processEndpoint, restituteMsafeFastRouterSwapParams };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ import { TransactionObjectArgument, Transaction } from '@mysten/sui/transactions
4
4
  import { Signer } from '@mysten/sui/cryptography';
5
5
  import BN from 'bn.js';
6
6
  import Decimal from 'decimal.js';
7
+ import { SuiPriceServiceConnection } from '@pythnetwork/pyth-sui-js';
7
8
 
8
9
  /**
9
10
  * Represents a SUI address, which is a string.
@@ -128,16 +129,26 @@ interface SwapInPoolsResult {
128
129
  isExceed: boolean;
129
130
  routeData?: RouterData;
130
131
  }
132
+ type AggregatorClientParams = {
133
+ endpoint?: string;
134
+ signer?: string;
135
+ client?: SuiClient;
136
+ env?: Env;
137
+ pythUrls?: string[];
138
+ apiKey?: string;
139
+ };
131
140
  declare class AggregatorClient {
132
141
  endpoint: string;
133
142
  signer: string;
134
143
  client: SuiClient;
135
144
  env: Env;
145
+ apiKey: string;
136
146
  private allCoins;
137
147
  private pythConnections;
138
148
  private pythClient;
139
149
  private static readonly CONFIG;
140
- constructor(endpoint?: string, signer?: string, client?: SuiClient, env?: Env);
150
+ constructor(params: AggregatorClientParams);
151
+ newPythClients(pythUrls: string[]): SuiPriceServiceConnection[];
141
152
  getCoins(coinType: string, refresh?: boolean): Promise<CoinAsset[]>;
142
153
  findRouters(params: FindRouterParams): Promise<RouterData | null>;
143
154
  executeFlexibleInputSwap(txb: Transaction, inputCoin: TransactionObjectArgument, routers: Router[], amountOutLimit: BN, pythPriceIDs: Map<string, string>, partner?: string, deepbookv3DeepFee?: TransactionObjectArgument, packages?: Map<string, string>): Promise<TransactionObjectArgument>;
@@ -296,7 +307,7 @@ type AggregatorResponse = {
296
307
  msg: string;
297
308
  data: RouterData;
298
309
  };
299
- declare function getRouterResult(endpoint: string, params: FindRouterParams): Promise<RouterData | null>;
310
+ declare function getRouterResult(endpoint: string, apiKey: string, params: FindRouterParams): Promise<RouterData | null>;
300
311
  type DeepbookV3Config = {
301
312
  id: string;
302
313
  is_alternative_payment: boolean;
@@ -321,4 +332,4 @@ declare enum Env {
321
332
  Testnet = 1
322
333
  }
323
334
 
324
- export { AFSUI, AFTERMATH, AGGREGATOR_V2, AGGREGATOR_V2_EXTEND, ALPHAFI, AggregatorClient, type AggregatorResponse, BLUEFIN, BLUEMOVE, type BuildCoinResult, type BuildFastRouterSwapParams, type BuildFastRouterSwapParamsV2, type BuildRouterSwapParams, type BuildRouterSwapParamsV2, CETUS, CLOCK_ADDRESS, DEEPBOOKV2, DEEPBOOKV3, DEFAULT_ENDPOINT, type DeepbookV3Config, type DeepbookV3ConfigResponse, type Dex, Env, type ExtendedDetails, FLOWXV2, FLOWXV3, type FindRouterParams, HAEDAL, HAEDALPMM, KRIYA, KRIYAV3, METASTABLE, OBRIC, ONE, type Path, type PreSwapLpChangeParams, type Router, type RouterData, type RouterError, SCALLOP, SPRINGSUI, STEAMM, SUILEND, type SwapInPoolsParams, type SwapInPoolsResult, TEN_POW_NINE, TURBOS, TWO, U128, U64_MAX, U64_MAX_BN, VOLO, ZERO, buildInputCoin, checkInvalidSuiAddress, compareCoins, completionCoin, composeType, createTarget, dealWithFastRouterSwapParamsForMsafe, extractAddressFromType, extractStructTagFromType, findPythPriceIDs, fixSuiObjectId, getAggregatorV2ExtendPublishedAt, getAggregatorV2PublishedAt, getDeepbookV3Config, getRouterResult, isSortedSymbols, mintZeroCoin, normalizeCoinType, parseRouterResponse, patchFixSuiObjectId, printTransaction, processEndpoint, restituteMsafeFastRouterSwapParams };
335
+ export { AFSUI, AFTERMATH, AGGREGATOR_V2, AGGREGATOR_V2_EXTEND, ALPHAFI, AggregatorClient, type AggregatorClientParams, type AggregatorResponse, BLUEFIN, BLUEMOVE, type BuildCoinResult, type BuildFastRouterSwapParams, type BuildFastRouterSwapParamsV2, type BuildRouterSwapParams, type BuildRouterSwapParamsV2, CETUS, CLOCK_ADDRESS, DEEPBOOKV2, DEEPBOOKV3, DEFAULT_ENDPOINT, type DeepbookV3Config, type DeepbookV3ConfigResponse, type Dex, Env, type ExtendedDetails, FLOWXV2, FLOWXV3, type FindRouterParams, HAEDAL, HAEDALPMM, KRIYA, KRIYAV3, METASTABLE, OBRIC, ONE, type Path, type PreSwapLpChangeParams, type Router, type RouterData, type RouterError, SCALLOP, SPRINGSUI, STEAMM, SUILEND, type SwapInPoolsParams, type SwapInPoolsResult, TEN_POW_NINE, TURBOS, TWO, U128, U64_MAX, U64_MAX_BN, VOLO, ZERO, buildInputCoin, checkInvalidSuiAddress, compareCoins, completionCoin, composeType, createTarget, dealWithFastRouterSwapParamsForMsafe, extractAddressFromType, extractStructTagFromType, findPythPriceIDs, fixSuiObjectId, getAggregatorV2ExtendPublishedAt, getAggregatorV2PublishedAt, getDeepbookV3Config, getRouterResult, isSortedSymbols, mintZeroCoin, normalizeCoinType, parseRouterResponse, patchFixSuiObjectId, printTransaction, processEndpoint, restituteMsafeFastRouterSwapParams };
package/dist/index.js CHANGED
@@ -6514,19 +6514,28 @@ function isBuilderFastRouterSwapParams(params) {
6514
6514
  return Array.isArray(params.routers);
6515
6515
  }
6516
6516
  var _AggregatorClient = class _AggregatorClient {
6517
- constructor(endpoint, signer, client$1, env) {
6518
- this.endpoint = endpoint ? processEndpoint(endpoint) : DEFAULT_ENDPOINT;
6519
- this.client = client$1 || new client.SuiClient({ url: client.getFullnodeUrl("mainnet") });
6520
- this.signer = signer || "";
6521
- this.env = env || 0 /* Mainnet */;
6517
+ constructor(params) {
6518
+ var _a;
6519
+ this.endpoint = params.endpoint ? processEndpoint(params.endpoint) : DEFAULT_ENDPOINT;
6520
+ this.client = params.client || new client.SuiClient({ url: client.getFullnodeUrl("mainnet") });
6521
+ this.signer = params.signer || "";
6522
+ this.env = params.env || 0 /* Mainnet */;
6522
6523
  this.allCoins = /* @__PURE__ */ new Map();
6523
6524
  const config2 = _AggregatorClient.CONFIG[this.env];
6524
- this.pythConnections = config2.connections;
6525
+ this.pythConnections = this.newPythClients((_a = params.pythUrls) != null ? _a : []);
6525
6526
  this.pythClient = new pythSuiJs.SuiPythClient(
6526
6527
  this.client,
6527
6528
  config2.pythStateId,
6528
6529
  config2.wormholeStateId
6529
6530
  );
6531
+ this.apiKey = params.apiKey || "";
6532
+ }
6533
+ newPythClients(pythUrls) {
6534
+ if (!pythUrls.includes("https://hermes.pyth.network")) {
6535
+ pythUrls.push("https://hermes.pyth.network");
6536
+ }
6537
+ const connections = pythUrls.map((url) => new pythSuiJs.SuiPriceServiceConnection(url, { timeout: 3e3 }));
6538
+ return connections;
6530
6539
  }
6531
6540
  getCoins(coinType, refresh = true) {
6532
6541
  return __async(this, null, function* () {
@@ -6572,7 +6581,7 @@ var _AggregatorClient = class _AggregatorClient {
6572
6581
  }
6573
6582
  findRouters(params) {
6574
6583
  return __async(this, null, function* () {
6575
- return getRouterResult(this.endpoint, params);
6584
+ return getRouterResult(this.endpoint, this.apiKey, params);
6576
6585
  });
6577
6586
  }
6578
6587
  executeFlexibleInputSwap(txb, inputCoin, routers, amountOutLimit, pythPriceIDs, partner, deepbookv3DeepFee, packages) {
@@ -7104,7 +7113,7 @@ var _AggregatorClient = class _AggregatorClient {
7104
7113
  }
7105
7114
  if (priceUpdateData == null) {
7106
7115
  throw new Error(
7107
- `No pyth price seeds update data found: ${lastError == null ? void 0 : lastError.message}`
7116
+ `All Pyth price nodes are unavailable. Cannot fetch price data. Please switch to or add new available Pyth nodes. Detailed error: ${lastError == null ? void 0 : lastError.message}`
7108
7117
  );
7109
7118
  }
7110
7119
  let priceInfoObjectIds = [];
@@ -7115,7 +7124,7 @@ var _AggregatorClient = class _AggregatorClient {
7115
7124
  priceIDs
7116
7125
  );
7117
7126
  } catch (e) {
7118
- throw new Error(`Failed to update price feeds: ${e}`);
7127
+ throw new Error(`All Pyth price nodes are unavailable. Cannot fetch price data. Please switch to or add new available Pyth nodes in the pythUrls parameter when initializing AggregatorClient, for example: new AggregatorClient({ pythUrls: ["https://your-pyth-node-url"] }). Detailed error: ${e}`);
7119
7128
  }
7120
7129
  let priceInfoObjectIdsMap = /* @__PURE__ */ new Map();
7121
7130
  for (let i = 0; i < priceIDs.length; i++) {
@@ -7127,24 +7136,10 @@ var _AggregatorClient = class _AggregatorClient {
7127
7136
  };
7128
7137
  _AggregatorClient.CONFIG = {
7129
7138
  [1 /* Testnet */]: {
7130
- connections: [
7131
- new pythSuiJs.SuiPriceServiceConnection("https://hermes-beta.pyth.network")
7132
- ],
7133
7139
  wormholeStateId: "0x31358d198147da50db32eda2562951d53973a0c0ad5ed738e9b17d88b213d790",
7134
7140
  pythStateId: "0x243759059f4c3111179da5878c12f68d612c21a8d54d85edc86164bb18be1c7c"
7135
7141
  },
7136
7142
  [0 /* Mainnet */]: {
7137
- connections: [
7138
- new pythSuiJs.SuiPriceServiceConnection(
7139
- "https://cetus-pythnet-a648.mainnet.pythnet.rpcpool.com/219cf7a8-6d75-432d-a648-d487a6dd5dc3/hermes",
7140
- {
7141
- timeout: 3e3
7142
- }
7143
- ),
7144
- new pythSuiJs.SuiPriceServiceConnection("https://hermes.pyth.network", {
7145
- timeout: 3e3
7146
- })
7147
- ],
7148
7143
  wormholeStateId: "0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c",
7149
7144
  pythStateId: "0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8"
7150
7145
  }
@@ -7324,14 +7319,14 @@ function processEndpoint(endpoint) {
7324
7319
  }
7325
7320
 
7326
7321
  // src/api.ts
7327
- var SDK_VERSION = 1000400;
7328
- function getRouterResult(endpoint, params) {
7322
+ var SDK_VERSION = 1000402;
7323
+ function getRouterResult(endpoint, apiKey, params) {
7329
7324
  return __async(this, null, function* () {
7330
7325
  let response;
7331
7326
  if (params.liquidityChanges && params.liquidityChanges.length > 0) {
7332
7327
  response = yield postRouterWithLiquidityChanges(endpoint, params);
7333
7328
  } else {
7334
- response = yield getRouter(endpoint, params);
7329
+ response = yield getRouter(endpoint, apiKey, params);
7335
7330
  }
7336
7331
  if (!response) {
7337
7332
  return null;
@@ -7389,7 +7384,7 @@ function getRouterResult(endpoint, params) {
7389
7384
  };
7390
7385
  });
7391
7386
  }
7392
- function getRouter(endpoint, params) {
7387
+ function getRouter(endpoint, apiKey, params) {
7393
7388
  return __async(this, null, function* () {
7394
7389
  try {
7395
7390
  const {
@@ -7423,6 +7418,9 @@ function getRouter(endpoint, params) {
7423
7418
  url += `&providers=${providers.join(",")}`;
7424
7419
  }
7425
7420
  }
7421
+ if (apiKey.length > 0) {
7422
+ url += `&apiKey=${apiKey}`;
7423
+ }
7426
7424
  url += `&v=${SDK_VERSION}`;
7427
7425
  const response = yield fetch(url);
7428
7426
  return response;
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
2
2
  import { normalizeSuiObjectId, SUI_CLOCK_OBJECT_ID, SUI_FRAMEWORK_ADDRESS } from '@mysten/sui/utils';
3
3
  import { Transaction } from '@mysten/sui/transactions';
4
- import { SuiPriceServiceConnection, SuiPythClient } from '@pythnetwork/pyth-sui-js';
4
+ import { SuiPythClient, SuiPriceServiceConnection } from '@pythnetwork/pyth-sui-js';
5
5
 
6
6
  var __create = Object.create;
7
7
  var __defProp = Object.defineProperty;
@@ -6512,19 +6512,28 @@ function isBuilderFastRouterSwapParams(params) {
6512
6512
  return Array.isArray(params.routers);
6513
6513
  }
6514
6514
  var _AggregatorClient = class _AggregatorClient {
6515
- constructor(endpoint, signer, client, env) {
6516
- this.endpoint = endpoint ? processEndpoint(endpoint) : DEFAULT_ENDPOINT;
6517
- this.client = client || new SuiClient({ url: getFullnodeUrl("mainnet") });
6518
- this.signer = signer || "";
6519
- this.env = env || 0 /* Mainnet */;
6515
+ constructor(params) {
6516
+ var _a;
6517
+ this.endpoint = params.endpoint ? processEndpoint(params.endpoint) : DEFAULT_ENDPOINT;
6518
+ this.client = params.client || new SuiClient({ url: getFullnodeUrl("mainnet") });
6519
+ this.signer = params.signer || "";
6520
+ this.env = params.env || 0 /* Mainnet */;
6520
6521
  this.allCoins = /* @__PURE__ */ new Map();
6521
6522
  const config2 = _AggregatorClient.CONFIG[this.env];
6522
- this.pythConnections = config2.connections;
6523
+ this.pythConnections = this.newPythClients((_a = params.pythUrls) != null ? _a : []);
6523
6524
  this.pythClient = new SuiPythClient(
6524
6525
  this.client,
6525
6526
  config2.pythStateId,
6526
6527
  config2.wormholeStateId
6527
6528
  );
6529
+ this.apiKey = params.apiKey || "";
6530
+ }
6531
+ newPythClients(pythUrls) {
6532
+ if (!pythUrls.includes("https://hermes.pyth.network")) {
6533
+ pythUrls.push("https://hermes.pyth.network");
6534
+ }
6535
+ const connections = pythUrls.map((url) => new SuiPriceServiceConnection(url, { timeout: 3e3 }));
6536
+ return connections;
6528
6537
  }
6529
6538
  getCoins(coinType, refresh = true) {
6530
6539
  return __async(this, null, function* () {
@@ -6570,7 +6579,7 @@ var _AggregatorClient = class _AggregatorClient {
6570
6579
  }
6571
6580
  findRouters(params) {
6572
6581
  return __async(this, null, function* () {
6573
- return getRouterResult(this.endpoint, params);
6582
+ return getRouterResult(this.endpoint, this.apiKey, params);
6574
6583
  });
6575
6584
  }
6576
6585
  executeFlexibleInputSwap(txb, inputCoin, routers, amountOutLimit, pythPriceIDs, partner, deepbookv3DeepFee, packages) {
@@ -7102,7 +7111,7 @@ var _AggregatorClient = class _AggregatorClient {
7102
7111
  }
7103
7112
  if (priceUpdateData == null) {
7104
7113
  throw new Error(
7105
- `No pyth price seeds update data found: ${lastError == null ? void 0 : lastError.message}`
7114
+ `All Pyth price nodes are unavailable. Cannot fetch price data. Please switch to or add new available Pyth nodes. Detailed error: ${lastError == null ? void 0 : lastError.message}`
7106
7115
  );
7107
7116
  }
7108
7117
  let priceInfoObjectIds = [];
@@ -7113,7 +7122,7 @@ var _AggregatorClient = class _AggregatorClient {
7113
7122
  priceIDs
7114
7123
  );
7115
7124
  } catch (e) {
7116
- throw new Error(`Failed to update price feeds: ${e}`);
7125
+ throw new Error(`All Pyth price nodes are unavailable. Cannot fetch price data. Please switch to or add new available Pyth nodes in the pythUrls parameter when initializing AggregatorClient, for example: new AggregatorClient({ pythUrls: ["https://your-pyth-node-url"] }). Detailed error: ${e}`);
7117
7126
  }
7118
7127
  let priceInfoObjectIdsMap = /* @__PURE__ */ new Map();
7119
7128
  for (let i = 0; i < priceIDs.length; i++) {
@@ -7125,24 +7134,10 @@ var _AggregatorClient = class _AggregatorClient {
7125
7134
  };
7126
7135
  _AggregatorClient.CONFIG = {
7127
7136
  [1 /* Testnet */]: {
7128
- connections: [
7129
- new SuiPriceServiceConnection("https://hermes-beta.pyth.network")
7130
- ],
7131
7137
  wormholeStateId: "0x31358d198147da50db32eda2562951d53973a0c0ad5ed738e9b17d88b213d790",
7132
7138
  pythStateId: "0x243759059f4c3111179da5878c12f68d612c21a8d54d85edc86164bb18be1c7c"
7133
7139
  },
7134
7140
  [0 /* Mainnet */]: {
7135
- connections: [
7136
- new SuiPriceServiceConnection(
7137
- "https://cetus-pythnet-a648.mainnet.pythnet.rpcpool.com/219cf7a8-6d75-432d-a648-d487a6dd5dc3/hermes",
7138
- {
7139
- timeout: 3e3
7140
- }
7141
- ),
7142
- new SuiPriceServiceConnection("https://hermes.pyth.network", {
7143
- timeout: 3e3
7144
- })
7145
- ],
7146
7141
  wormholeStateId: "0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c",
7147
7142
  pythStateId: "0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8"
7148
7143
  }
@@ -7322,14 +7317,14 @@ function processEndpoint(endpoint) {
7322
7317
  }
7323
7318
 
7324
7319
  // src/api.ts
7325
- var SDK_VERSION = 1000400;
7326
- function getRouterResult(endpoint, params) {
7320
+ var SDK_VERSION = 1000402;
7321
+ function getRouterResult(endpoint, apiKey, params) {
7327
7322
  return __async(this, null, function* () {
7328
7323
  let response;
7329
7324
  if (params.liquidityChanges && params.liquidityChanges.length > 0) {
7330
7325
  response = yield postRouterWithLiquidityChanges(endpoint, params);
7331
7326
  } else {
7332
- response = yield getRouter(endpoint, params);
7327
+ response = yield getRouter(endpoint, apiKey, params);
7333
7328
  }
7334
7329
  if (!response) {
7335
7330
  return null;
@@ -7387,7 +7382,7 @@ function getRouterResult(endpoint, params) {
7387
7382
  };
7388
7383
  });
7389
7384
  }
7390
- function getRouter(endpoint, params) {
7385
+ function getRouter(endpoint, apiKey, params) {
7391
7386
  return __async(this, null, function* () {
7392
7387
  try {
7393
7388
  const {
@@ -7421,6 +7416,9 @@ function getRouter(endpoint, params) {
7421
7416
  url += `&providers=${providers.join(",")}`;
7422
7417
  }
7423
7418
  }
7419
+ if (apiKey.length > 0) {
7420
+ url += `&apiKey=${apiKey}`;
7421
+ }
7424
7422
  url += `&v=${SDK_VERSION}`;
7425
7423
  const response = yield fetch(url);
7426
7424
  return response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cetusprotocol/aggregator-sdk",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "sideEffects": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/dist/src/api.d.ts DELETED
@@ -1,104 +0,0 @@
1
- import BN from "bn.js";
2
- import Decimal from "decimal.js";
3
- export interface FindRouterParams {
4
- from: string;
5
- target: string;
6
- amount: BN;
7
- byAmountIn: boolean;
8
- depth?: number;
9
- splitAlgorithm?: string;
10
- splitFactor?: number;
11
- splitCount?: number;
12
- providers?: string[];
13
- liquidityChanges?: PreSwapLpChangeParams[];
14
- }
15
- export interface PreSwapLpChangeParams {
16
- poolID: string;
17
- ticklower: number;
18
- tickUpper: number;
19
- deltaLiquidity: number;
20
- }
21
- export type ExtendedDetails = {
22
- aftermathPoolFlatness?: number;
23
- aftermathLpSupplyType?: string;
24
- turbosFeeType?: string;
25
- afterSqrtPrice?: string;
26
- deepbookv3DeepFee?: number;
27
- scallopScoinTreasury?: string;
28
- haedalPmmBasePriceSeed?: string;
29
- haedalPmmQuotePriceSeed?: string;
30
- steammBankA?: string;
31
- steammBankB?: string;
32
- steammLendingMarket?: string;
33
- steammLendingMarketType?: string;
34
- steammBCoinAType?: string;
35
- steammBCoinBType?: string;
36
- steammLPToken?: string;
37
- metastablePriceSeed?: string;
38
- metastableETHPriceSeed?: string;
39
- metastableWhitelistedAppId?: string;
40
- metastableCreateCapPkgId?: string;
41
- metastableCreateCapModule?: string;
42
- metastableCreateCapAllTypeParams?: boolean;
43
- metastableRegistryId?: string;
44
- obricCoinAPriceSeed?: string;
45
- obricCoinBPriceSeed?: string;
46
- obricCoinAPriceId?: string;
47
- obricCoinBPriceId?: string;
48
- };
49
- export type Path = {
50
- id: string;
51
- direction: boolean;
52
- provider: string;
53
- from: string;
54
- target: string;
55
- feeRate: number;
56
- amountIn: string;
57
- amountOut: string;
58
- version?: string;
59
- extendedDetails?: ExtendedDetails;
60
- };
61
- export type Router = {
62
- path: Path[];
63
- amountIn: BN;
64
- amountOut: BN;
65
- initialPrice: Decimal;
66
- };
67
- export type RouterError = {
68
- code: number;
69
- msg: string;
70
- };
71
- export type RouterData = {
72
- amountIn: BN;
73
- amountOut: BN;
74
- byAmountIn: boolean;
75
- routes: Router[];
76
- insufficientLiquidity: boolean;
77
- packages?: Map<string, string>;
78
- totalDeepFee?: number;
79
- error?: RouterError;
80
- };
81
- export type AggregatorResponse = {
82
- code: number;
83
- msg: string;
84
- data: RouterData;
85
- };
86
- export declare function getRouterResult(endpoint: string, params: FindRouterParams): Promise<RouterData | null>;
87
- export type DeepbookV3Config = {
88
- id: string;
89
- is_alternative_payment: boolean;
90
- alternative_payment_amount: number;
91
- trade_cap: string;
92
- balance_manager: string;
93
- deep_fee_vault: number;
94
- whitelist: number;
95
- package_version: 0;
96
- last_updated_time: number;
97
- whitelist_pools: string[];
98
- };
99
- export type DeepbookV3ConfigResponse = {
100
- code: number;
101
- msg: string;
102
- data: DeepbookV3Config;
103
- };
104
- export declare function getDeepbookV3Config(endpoint: string): Promise<DeepbookV3ConfigResponse | null>;
@@ -1,107 +0,0 @@
1
- import { SuiClient } from "@mysten/sui/client";
2
- import { Transaction, TransactionObjectArgument } from "@mysten/sui/transactions";
3
- import { Signer } from "@mysten/sui/cryptography";
4
- import BN from "bn.js";
5
- import { Dex, Env, FindRouterParams, Router, RouterData, DeepbookV3Config } from ".";
6
- import { CoinAsset } from "./types/sui";
7
- export declare const CETUS = "CETUS";
8
- export declare const DEEPBOOKV2 = "DEEPBOOK";
9
- export declare const KRIYA = "KRIYA";
10
- export declare const FLOWXV2 = "FLOWX";
11
- export declare const FLOWXV3 = "FLOWXV3";
12
- export declare const KRIYAV3 = "KRIYAV3";
13
- export declare const TURBOS = "TURBOS";
14
- export declare const AFTERMATH = "AFTERMATH";
15
- export declare const HAEDAL = "HAEDAL";
16
- export declare const VOLO = "VOLO";
17
- export declare const AFSUI = "AFSUI";
18
- export declare const BLUEMOVE = "BLUEMOVE";
19
- export declare const DEEPBOOKV3 = "DEEPBOOKV3";
20
- export declare const SCALLOP = "SCALLOP";
21
- export declare const SUILEND = "SUILEND";
22
- export declare const BLUEFIN = "BLUEFIN";
23
- export declare const HAEDALPMM = "HAEDALPMM";
24
- export declare const ALPHAFI = "ALPHAFI";
25
- export declare const SPRINGSUI = "SPRINGSUI";
26
- export declare const STEAMM = "STEAMM";
27
- export declare const METASTABLE = "METASTABLE";
28
- export declare const OBRIC = "OBRIC";
29
- export declare const DEFAULT_ENDPOINT = "https://api-sui.cetus.zone/router_v2";
30
- export type BuildRouterSwapParams = {
31
- routers: Router[];
32
- byAmountIn: boolean;
33
- inputCoin: TransactionObjectArgument;
34
- slippage: number;
35
- txb: Transaction;
36
- partner?: string;
37
- deepbookv3DeepFee?: TransactionObjectArgument;
38
- };
39
- export type BuildFastRouterSwapParams = {
40
- routers: Router[];
41
- byAmountIn: boolean;
42
- slippage: number;
43
- txb: Transaction;
44
- partner?: string;
45
- refreshAllCoins?: boolean;
46
- payDeepFeeAmount?: number;
47
- };
48
- export type BuildRouterSwapParamsV2 = {
49
- routers: RouterData;
50
- inputCoin: TransactionObjectArgument;
51
- slippage: number;
52
- txb: Transaction;
53
- partner?: string;
54
- deepbookv3DeepFee?: TransactionObjectArgument;
55
- };
56
- export type BuildFastRouterSwapParamsV2 = {
57
- routers: RouterData;
58
- slippage: number;
59
- txb: Transaction;
60
- partner?: string;
61
- refreshAllCoins?: boolean;
62
- payDeepFeeAmount?: number;
63
- };
64
- export interface SwapInPoolsParams {
65
- from: string;
66
- target: string;
67
- amount: BN;
68
- byAmountIn: boolean;
69
- pools: string[];
70
- }
71
- export interface SwapInPoolsResult {
72
- isExceed: boolean;
73
- routeData?: RouterData;
74
- }
75
- export declare class AggregatorClient {
76
- endpoint: string;
77
- signer: string;
78
- client: SuiClient;
79
- env: Env;
80
- private allCoins;
81
- private pythConnections;
82
- private pythClient;
83
- private static readonly CONFIG;
84
- constructor(endpoint?: string, signer?: string, client?: SuiClient, env?: Env);
85
- getCoins(coinType: string, refresh?: boolean): Promise<CoinAsset[]>;
86
- findRouters(params: FindRouterParams): Promise<RouterData | null>;
87
- executeFlexibleInputSwap(txb: Transaction, inputCoin: TransactionObjectArgument, routers: Router[], amountOutLimit: BN, pythPriceIDs: Map<string, string>, partner?: string, deepbookv3DeepFee?: TransactionObjectArgument, packages?: Map<string, string>): Promise<TransactionObjectArgument>;
88
- expectInputSwap(txb: Transaction, inputCoin: TransactionObjectArgument, routers: Router[], amountOutLimit: BN, pythPriceIDs: Map<string, string>, partner?: string, deepbookv3DeepFee?: TransactionObjectArgument, packages?: Map<string, string>): Promise<TransactionObjectArgument>;
89
- expectOutputSwap(txb: Transaction, inputCoin: TransactionObjectArgument, routers: Router[], partner?: string, packages?: Map<string, string>): Promise<TransactionObjectArgument>;
90
- swapInPools(params: SwapInPoolsParams): Promise<SwapInPoolsResult | null>;
91
- routerSwap(params: BuildRouterSwapParams | BuildRouterSwapParamsV2): Promise<TransactionObjectArgument>;
92
- fixableRouterSwap(params: BuildRouterSwapParamsV2): Promise<TransactionObjectArgument>;
93
- fastRouterSwap(params: BuildFastRouterSwapParams | BuildFastRouterSwapParamsV2): Promise<void>;
94
- publishedAtV2(): string;
95
- publishedAtV2Extend(): string;
96
- deepbookv3DeepFeeType(): string;
97
- transferOrDestoryCoin(txb: Transaction, coin: TransactionObjectArgument, coinType: string, aggregatorV2PublishedAt: string): void;
98
- checkCoinThresholdAndMergeCoin(txb: Transaction, coins: TransactionObjectArgument[], coinType: string, amountLimit: BN, aggregatorV2PublishedAt: string): TransactionObjectArgument;
99
- newDex(provider: string, pythPriceIDs: Map<string, string>, partner?: string): Dex;
100
- signAndExecuteTransaction(txb: Transaction, signer: Signer): Promise<import("@mysten/sui/client").SuiTransactionBlockResponse>;
101
- devInspectTransactionBlock(txb: Transaction): Promise<import("@mysten/sui/client").DevInspectResults>;
102
- sendTransaction(txb: Transaction, signer: Signer): Promise<import("@mysten/sui/client").SuiTransactionBlockResponse>;
103
- getDeepbookV3Config(): Promise<DeepbookV3Config | null>;
104
- updatePythPriceIDs(priceIDs: string[], txb: Transaction): Promise<Map<string, string>>;
105
- }
106
- export declare function findPythPriceIDs(routes: Router[]): string[];
107
- export declare function parseRouterResponse(data: any, byAmountIn: boolean): RouterData;
@@ -1,8 +0,0 @@
1
- import BN from "bn.js";
2
- export declare const ZERO: BN;
3
- export declare const ONE: BN;
4
- export declare const TWO: BN;
5
- export declare const U128: BN;
6
- export declare const U64_MAX_BN: BN;
7
- export declare const U64_MAX = "18446744073709551615";
8
- export declare const TEN_POW_NINE = 1000000000;