@cetusprotocol/aggregator-sdk 0.0.6 → 0.0.7

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/dist/index.d.mts CHANGED
@@ -110,11 +110,16 @@ type Router = {
110
110
  amountOut: BN;
111
111
  initialPrice: Decimal;
112
112
  };
113
+ type RouterError = {
114
+ code: number;
115
+ msg: string;
116
+ };
113
117
  type RouterData = {
114
118
  amountIn: BN;
115
119
  amountOut: BN;
116
120
  routes: Router[];
117
121
  insufficientLiquidity: boolean;
122
+ error?: RouterError;
118
123
  };
119
124
  type AggregatorResponse = {
120
125
  code: number;
@@ -266,4 +271,4 @@ declare const TESTNET_AFTERMATH_INSURANCE_FUND_ID = "";
266
271
  declare const MAINNET_AFTERMATH_REFERRAL_VAULT_ID = "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278";
267
272
  declare const TESTNET_AFTERMATH_REFERRAL_VAULT_ID = "";
268
273
 
269
- export { AFTERMATH_AMM, AFTERMATH_MODULE, AGGREGATOR, AggregatorClient, AggregatorConfig, type AggregatorResponse, type BuildRouterSwapParams, CETUS_DEX, CETUS_MODULE, CETUS_PUBLISHED_AT, CHECK_COINS_THRESHOLD_FUNC, CLOCK_ADDRESS, CoinInfoAddress, CoinStoreAddress, DEEPBOOK_CLOB_V2_MODULE, DEEPBOOK_CUSTODIAN_V2_MODULE, DEEPBOOK_DEX, DEEPBOOK_MODULE, DEEPBOOK_PACKAGE_ID, DEEPBOOK_PUBLISHED_AT, ENV, type ExtendedDetails, FLOWX_AMM, FLOWX_AMM_MODULE, type FindRouterParams, FlashSwapA2BFunc, FlashSwapB2AFunc, FlashSwapFunc, FlashSwapWithPartnerA2BFunc, FlashSwapWithPartnerB2AFunc, FlashSwapWithPartnerFunc, INTEGRATE, JOIN_FUNC, KRIYA_DEX, KRIYA_MODULE, MAINNET_AFTERMATH_INSURANCE_FUND_ID, MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID, MAINNET_AFTERMATH_REFERRAL_VAULT_ID, MAINNET_AFTERMATH_REGISTRY_ID, MAINNET_AFTERMATH_TREASURY_ID, MAINNET_CETUS_GLOBAL_CONFIG_ID, MAINNET_FLOWX_AMM_CONTAINER_ID, ONE, PAY_MODULE, POOL_MODULT, type Package, type Path, REPAY_FLASH_SWAP_A2B_FUNC, REPAY_FLASH_SWAP_B2A_FUNC, REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC, REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC, RepayFalshSwapFunc, RepayFlashSwapWithPartnerFunc, type Router, type RouterData, SWAP_A2B_FUNC, SWAP_B2A_FUNC, SuiZeroCoinFn, type SwapInPoolsParams, type SwapInPoolsResult, TEN_POW_NINE, TESTNET_AFTERMATH_INSURANCE_FUND_ID, TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID, TESTNET_AFTERMATH_REFERRAL_VAULT_ID, TESTNET_AFTERMATH_REGISTRY_ID, TESTNET_AFTERMATH_TREASURY_ID, TESTNET_CETUS_GLOBAL_CONFIG_ID, TESTNET_FLOWX_AMM_CONTAINER_ID, TRANSFER_ACCOUNT_CAP, TRANSFER_OR_DESTORY_COIN_FUNC, TURBOS_DEX, TURBOS_MODULE, TURBOS_VERSIONED, TWO, U128, U64_MAX, U64_MAX_BN, UTILS_MODULE, ZERO, composeType, createTarget, expectInputRouterSwap, expectOutputRouterSwap, extractAddressFromType, extractStructTagFromType, fixSuiObjectId, isSortedSymbols, normalizeCoinType, parseRouterResponse, patchFixSuiObjectId };
274
+ export { AFTERMATH_AMM, AFTERMATH_MODULE, AGGREGATOR, AggregatorClient, AggregatorConfig, type AggregatorResponse, type BuildRouterSwapParams, CETUS_DEX, CETUS_MODULE, CETUS_PUBLISHED_AT, CHECK_COINS_THRESHOLD_FUNC, CLOCK_ADDRESS, CoinInfoAddress, CoinStoreAddress, DEEPBOOK_CLOB_V2_MODULE, DEEPBOOK_CUSTODIAN_V2_MODULE, DEEPBOOK_DEX, DEEPBOOK_MODULE, DEEPBOOK_PACKAGE_ID, DEEPBOOK_PUBLISHED_AT, ENV, type ExtendedDetails, FLOWX_AMM, FLOWX_AMM_MODULE, type FindRouterParams, FlashSwapA2BFunc, FlashSwapB2AFunc, FlashSwapFunc, FlashSwapWithPartnerA2BFunc, FlashSwapWithPartnerB2AFunc, FlashSwapWithPartnerFunc, INTEGRATE, JOIN_FUNC, KRIYA_DEX, KRIYA_MODULE, MAINNET_AFTERMATH_INSURANCE_FUND_ID, MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID, MAINNET_AFTERMATH_REFERRAL_VAULT_ID, MAINNET_AFTERMATH_REGISTRY_ID, MAINNET_AFTERMATH_TREASURY_ID, MAINNET_CETUS_GLOBAL_CONFIG_ID, MAINNET_FLOWX_AMM_CONTAINER_ID, ONE, PAY_MODULE, POOL_MODULT, type Package, type Path, REPAY_FLASH_SWAP_A2B_FUNC, REPAY_FLASH_SWAP_B2A_FUNC, REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC, REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC, RepayFalshSwapFunc, RepayFlashSwapWithPartnerFunc, type Router, type RouterData, type RouterError, SWAP_A2B_FUNC, SWAP_B2A_FUNC, SuiZeroCoinFn, type SwapInPoolsParams, type SwapInPoolsResult, TEN_POW_NINE, TESTNET_AFTERMATH_INSURANCE_FUND_ID, TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID, TESTNET_AFTERMATH_REFERRAL_VAULT_ID, TESTNET_AFTERMATH_REGISTRY_ID, TESTNET_AFTERMATH_TREASURY_ID, TESTNET_CETUS_GLOBAL_CONFIG_ID, TESTNET_FLOWX_AMM_CONTAINER_ID, TRANSFER_ACCOUNT_CAP, TRANSFER_OR_DESTORY_COIN_FUNC, TURBOS_DEX, TURBOS_MODULE, TURBOS_VERSIONED, TWO, U128, U64_MAX, U64_MAX_BN, UTILS_MODULE, ZERO, composeType, createTarget, expectInputRouterSwap, expectOutputRouterSwap, extractAddressFromType, extractStructTagFromType, fixSuiObjectId, isSortedSymbols, normalizeCoinType, parseRouterResponse, patchFixSuiObjectId };
package/dist/index.d.ts CHANGED
@@ -110,11 +110,16 @@ type Router = {
110
110
  amountOut: BN;
111
111
  initialPrice: Decimal;
112
112
  };
113
+ type RouterError = {
114
+ code: number;
115
+ msg: string;
116
+ };
113
117
  type RouterData = {
114
118
  amountIn: BN;
115
119
  amountOut: BN;
116
120
  routes: Router[];
117
121
  insufficientLiquidity: boolean;
122
+ error?: RouterError;
118
123
  };
119
124
  type AggregatorResponse = {
120
125
  code: number;
@@ -266,4 +271,4 @@ declare const TESTNET_AFTERMATH_INSURANCE_FUND_ID = "";
266
271
  declare const MAINNET_AFTERMATH_REFERRAL_VAULT_ID = "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278";
267
272
  declare const TESTNET_AFTERMATH_REFERRAL_VAULT_ID = "";
268
273
 
269
- export { AFTERMATH_AMM, AFTERMATH_MODULE, AGGREGATOR, AggregatorClient, AggregatorConfig, type AggregatorResponse, type BuildRouterSwapParams, CETUS_DEX, CETUS_MODULE, CETUS_PUBLISHED_AT, CHECK_COINS_THRESHOLD_FUNC, CLOCK_ADDRESS, CoinInfoAddress, CoinStoreAddress, DEEPBOOK_CLOB_V2_MODULE, DEEPBOOK_CUSTODIAN_V2_MODULE, DEEPBOOK_DEX, DEEPBOOK_MODULE, DEEPBOOK_PACKAGE_ID, DEEPBOOK_PUBLISHED_AT, ENV, type ExtendedDetails, FLOWX_AMM, FLOWX_AMM_MODULE, type FindRouterParams, FlashSwapA2BFunc, FlashSwapB2AFunc, FlashSwapFunc, FlashSwapWithPartnerA2BFunc, FlashSwapWithPartnerB2AFunc, FlashSwapWithPartnerFunc, INTEGRATE, JOIN_FUNC, KRIYA_DEX, KRIYA_MODULE, MAINNET_AFTERMATH_INSURANCE_FUND_ID, MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID, MAINNET_AFTERMATH_REFERRAL_VAULT_ID, MAINNET_AFTERMATH_REGISTRY_ID, MAINNET_AFTERMATH_TREASURY_ID, MAINNET_CETUS_GLOBAL_CONFIG_ID, MAINNET_FLOWX_AMM_CONTAINER_ID, ONE, PAY_MODULE, POOL_MODULT, type Package, type Path, REPAY_FLASH_SWAP_A2B_FUNC, REPAY_FLASH_SWAP_B2A_FUNC, REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC, REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC, RepayFalshSwapFunc, RepayFlashSwapWithPartnerFunc, type Router, type RouterData, SWAP_A2B_FUNC, SWAP_B2A_FUNC, SuiZeroCoinFn, type SwapInPoolsParams, type SwapInPoolsResult, TEN_POW_NINE, TESTNET_AFTERMATH_INSURANCE_FUND_ID, TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID, TESTNET_AFTERMATH_REFERRAL_VAULT_ID, TESTNET_AFTERMATH_REGISTRY_ID, TESTNET_AFTERMATH_TREASURY_ID, TESTNET_CETUS_GLOBAL_CONFIG_ID, TESTNET_FLOWX_AMM_CONTAINER_ID, TRANSFER_ACCOUNT_CAP, TRANSFER_OR_DESTORY_COIN_FUNC, TURBOS_DEX, TURBOS_MODULE, TURBOS_VERSIONED, TWO, U128, U64_MAX, U64_MAX_BN, UTILS_MODULE, ZERO, composeType, createTarget, expectInputRouterSwap, expectOutputRouterSwap, extractAddressFromType, extractStructTagFromType, fixSuiObjectId, isSortedSymbols, normalizeCoinType, parseRouterResponse, patchFixSuiObjectId };
274
+ export { AFTERMATH_AMM, AFTERMATH_MODULE, AGGREGATOR, AggregatorClient, AggregatorConfig, type AggregatorResponse, type BuildRouterSwapParams, CETUS_DEX, CETUS_MODULE, CETUS_PUBLISHED_AT, CHECK_COINS_THRESHOLD_FUNC, CLOCK_ADDRESS, CoinInfoAddress, CoinStoreAddress, DEEPBOOK_CLOB_V2_MODULE, DEEPBOOK_CUSTODIAN_V2_MODULE, DEEPBOOK_DEX, DEEPBOOK_MODULE, DEEPBOOK_PACKAGE_ID, DEEPBOOK_PUBLISHED_AT, ENV, type ExtendedDetails, FLOWX_AMM, FLOWX_AMM_MODULE, type FindRouterParams, FlashSwapA2BFunc, FlashSwapB2AFunc, FlashSwapFunc, FlashSwapWithPartnerA2BFunc, FlashSwapWithPartnerB2AFunc, FlashSwapWithPartnerFunc, INTEGRATE, JOIN_FUNC, KRIYA_DEX, KRIYA_MODULE, MAINNET_AFTERMATH_INSURANCE_FUND_ID, MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID, MAINNET_AFTERMATH_REFERRAL_VAULT_ID, MAINNET_AFTERMATH_REGISTRY_ID, MAINNET_AFTERMATH_TREASURY_ID, MAINNET_CETUS_GLOBAL_CONFIG_ID, MAINNET_FLOWX_AMM_CONTAINER_ID, ONE, PAY_MODULE, POOL_MODULT, type Package, type Path, REPAY_FLASH_SWAP_A2B_FUNC, REPAY_FLASH_SWAP_B2A_FUNC, REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC, REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC, RepayFalshSwapFunc, RepayFlashSwapWithPartnerFunc, type Router, type RouterData, type RouterError, SWAP_A2B_FUNC, SWAP_B2A_FUNC, SuiZeroCoinFn, type SwapInPoolsParams, type SwapInPoolsResult, TEN_POW_NINE, TESTNET_AFTERMATH_INSURANCE_FUND_ID, TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID, TESTNET_AFTERMATH_REFERRAL_VAULT_ID, TESTNET_AFTERMATH_REGISTRY_ID, TESTNET_AFTERMATH_TREASURY_ID, TESTNET_CETUS_GLOBAL_CONFIG_ID, TESTNET_FLOWX_AMM_CONTAINER_ID, TRANSFER_ACCOUNT_CAP, TRANSFER_OR_DESTORY_COIN_FUNC, TURBOS_DEX, TURBOS_MODULE, TURBOS_VERSIONED, TWO, U128, U64_MAX, U64_MAX_BN, UTILS_MODULE, ZERO, composeType, createTarget, expectInputRouterSwap, expectOutputRouterSwap, extractAddressFromType, extractStructTagFromType, fixSuiObjectId, isSortedSymbols, normalizeCoinType, parseRouterResponse, patchFixSuiObjectId };
package/dist/index.js CHANGED
@@ -5332,6 +5332,22 @@ var MAINNET_AFTERMATH_INSURANCE_FUND_ID = "0xf0c40d67b078000e18032334c3325c47b9e
5332
5332
  var TESTNET_AFTERMATH_INSURANCE_FUND_ID = "";
5333
5333
  var MAINNET_AFTERMATH_REFERRAL_VAULT_ID = "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278";
5334
5334
  var TESTNET_AFTERMATH_REFERRAL_VAULT_ID = "";
5335
+
5336
+ // src/errors.ts
5337
+ function getAggregatorServerErrorMessage(code) {
5338
+ switch (code) {
5339
+ case 1e4 /* CalculateError */:
5340
+ return "Calculate error";
5341
+ case 10001 /* NumberTooLarge */:
5342
+ return "Input number too large can not fit in target type";
5343
+ case 10002 /* NoRouter */:
5344
+ return "No router";
5345
+ case 10003 /* InsufficientLiquidity */:
5346
+ return "Insufficient Liquidity";
5347
+ default:
5348
+ return "Unknown error";
5349
+ }
5350
+ }
5335
5351
  function mintZeroCoin(txb, coinType) {
5336
5352
  return txb.moveCall({
5337
5353
  target: SuiZeroCoinFn,
@@ -5348,7 +5364,10 @@ function buildInputCoin(txb, allCoins, amount, coinType) {
5348
5364
  targetCoinAmount: 0
5349
5365
  };
5350
5366
  }
5367
+ console.log("used coin asests", usedCoinAsests);
5351
5368
  let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
5369
+ console.log("totalCoinBalance", totalCoinBalance);
5370
+ console.log("amount", amount);
5352
5371
  if (totalCoinBalance < amount) {
5353
5372
  throw new AggregateError(
5354
5373
  "Insufficient balance when build merge coin",
@@ -6537,20 +6556,38 @@ var AggregatorClient = class {
6537
6556
  }
6538
6557
  }
6539
6558
  const response = yield fetch(url);
6540
- const data = yield response.json();
6541
- if (data.data != null) {
6542
- const res = parseRouterResponse(data.data);
6543
- return res;
6544
- }
6545
- if (data.msg === "liquidity is not enough") {
6559
+ if (!response.ok) {
6546
6560
  return {
6547
6561
  amountIn: ZERO,
6548
6562
  amountOut: ZERO,
6549
6563
  routes: [],
6550
- insufficientLiquidity: true
6564
+ insufficientLiquidity: false,
6565
+ error: {
6566
+ code: 10001 /* NumberTooLarge */,
6567
+ msg: getAggregatorServerErrorMessage(
6568
+ 10001 /* NumberTooLarge */
6569
+ )
6570
+ }
6551
6571
  };
6552
6572
  }
6553
- return null;
6573
+ const data = yield response.json();
6574
+ if (data.data != null) {
6575
+ const res = parseRouterResponse(data.data);
6576
+ return res;
6577
+ }
6578
+ const insufficientLiquidity = data.msg === "liquidity is not enough";
6579
+ return {
6580
+ amountIn: ZERO,
6581
+ amountOut: ZERO,
6582
+ routes: [],
6583
+ insufficientLiquidity,
6584
+ error: {
6585
+ code: 10003 /* InsufficientLiquidity */,
6586
+ msg: getAggregatorServerErrorMessage(
6587
+ 10003 /* InsufficientLiquidity */
6588
+ )
6589
+ }
6590
+ };
6554
6591
  });
6555
6592
  }
6556
6593
  swapInPools(params) {
@@ -6677,7 +6714,6 @@ var AggregatorClient = class {
6677
6714
  }
6678
6715
  devInspectTransactionBlock(txb) {
6679
6716
  return __async(this, null, function* () {
6680
- console.log(this.wallet);
6681
6717
  const res = yield this.client.devInspectTransactionBlock({
6682
6718
  transactionBlock: txb,
6683
6719
  sender: this.wallet
package/dist/index.mjs CHANGED
@@ -5330,6 +5330,22 @@ var MAINNET_AFTERMATH_INSURANCE_FUND_ID = "0xf0c40d67b078000e18032334c3325c47b9e
5330
5330
  var TESTNET_AFTERMATH_INSURANCE_FUND_ID = "";
5331
5331
  var MAINNET_AFTERMATH_REFERRAL_VAULT_ID = "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278";
5332
5332
  var TESTNET_AFTERMATH_REFERRAL_VAULT_ID = "";
5333
+
5334
+ // src/errors.ts
5335
+ function getAggregatorServerErrorMessage(code) {
5336
+ switch (code) {
5337
+ case 1e4 /* CalculateError */:
5338
+ return "Calculate error";
5339
+ case 10001 /* NumberTooLarge */:
5340
+ return "Input number too large can not fit in target type";
5341
+ case 10002 /* NoRouter */:
5342
+ return "No router";
5343
+ case 10003 /* InsufficientLiquidity */:
5344
+ return "Insufficient Liquidity";
5345
+ default:
5346
+ return "Unknown error";
5347
+ }
5348
+ }
5333
5349
  function mintZeroCoin(txb, coinType) {
5334
5350
  return txb.moveCall({
5335
5351
  target: SuiZeroCoinFn,
@@ -5346,7 +5362,10 @@ function buildInputCoin(txb, allCoins, amount, coinType) {
5346
5362
  targetCoinAmount: 0
5347
5363
  };
5348
5364
  }
5365
+ console.log("used coin asests", usedCoinAsests);
5349
5366
  let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
5367
+ console.log("totalCoinBalance", totalCoinBalance);
5368
+ console.log("amount", amount);
5350
5369
  if (totalCoinBalance < amount) {
5351
5370
  throw new AggregateError(
5352
5371
  "Insufficient balance when build merge coin",
@@ -6535,20 +6554,38 @@ var AggregatorClient = class {
6535
6554
  }
6536
6555
  }
6537
6556
  const response = yield fetch(url);
6538
- const data = yield response.json();
6539
- if (data.data != null) {
6540
- const res = parseRouterResponse(data.data);
6541
- return res;
6542
- }
6543
- if (data.msg === "liquidity is not enough") {
6557
+ if (!response.ok) {
6544
6558
  return {
6545
6559
  amountIn: ZERO,
6546
6560
  amountOut: ZERO,
6547
6561
  routes: [],
6548
- insufficientLiquidity: true
6562
+ insufficientLiquidity: false,
6563
+ error: {
6564
+ code: 10001 /* NumberTooLarge */,
6565
+ msg: getAggregatorServerErrorMessage(
6566
+ 10001 /* NumberTooLarge */
6567
+ )
6568
+ }
6549
6569
  };
6550
6570
  }
6551
- return null;
6571
+ const data = yield response.json();
6572
+ if (data.data != null) {
6573
+ const res = parseRouterResponse(data.data);
6574
+ return res;
6575
+ }
6576
+ const insufficientLiquidity = data.msg === "liquidity is not enough";
6577
+ return {
6578
+ amountIn: ZERO,
6579
+ amountOut: ZERO,
6580
+ routes: [],
6581
+ insufficientLiquidity,
6582
+ error: {
6583
+ code: 10003 /* InsufficientLiquidity */,
6584
+ msg: getAggregatorServerErrorMessage(
6585
+ 10003 /* InsufficientLiquidity */
6586
+ )
6587
+ }
6588
+ };
6552
6589
  });
6553
6590
  }
6554
6591
  swapInPools(params) {
@@ -6675,7 +6712,6 @@ var AggregatorClient = class {
6675
6712
  }
6676
6713
  devInspectTransactionBlock(txb) {
6677
6714
  return __async(this, null, function* () {
6678
- console.log(this.wallet);
6679
6715
  const res = yield this.client.devInspectTransactionBlock({
6680
6716
  transactionBlock: txb,
6681
6717
  sender: this.wallet
@@ -27,11 +27,16 @@ export type Router = {
27
27
  amountOut: BN;
28
28
  initialPrice: Decimal;
29
29
  };
30
+ export type RouterError = {
31
+ code: number;
32
+ msg: string;
33
+ };
30
34
  export type RouterData = {
31
35
  amountIn: BN;
32
36
  amountOut: BN;
33
37
  routes: Router[];
34
38
  insufficientLiquidity: boolean;
39
+ error?: RouterError;
35
40
  };
36
41
  export type AggregatorResponse = {
37
42
  code: number;
@@ -16,7 +16,7 @@ export declare enum TransactionErrorCode {
16
16
  MissTurbosFeeType = "MissTurbosFeeType",
17
17
  MissAftermathLpSupplyType = "MissAftermathLpSupplyType"
18
18
  }
19
- export type AggregatorErrorCode = TypesErrorCode;
19
+ export type AggregatorErrorCode = TypesErrorCode | ConfigErrorCode | TransactionErrorCode;
20
20
  /**
21
21
  * AggregatorError is a custom error class that extends the built-in Error class. It is used to represent errors that occur during aggregation operations.
22
22
  * The key functionality of this code includes:
@@ -29,3 +29,10 @@ export declare class AggregatorError extends Error {
29
29
  constructor(message: string, errorCode?: AggregatorErrorCode);
30
30
  static isAggregatorErrorCode(e: any, code: AggregatorErrorCode): boolean;
31
31
  }
32
+ export declare enum AggregatorServerErrorCode {
33
+ CalculateError = 10000,
34
+ NumberTooLarge = 10001,
35
+ NoRouter = 10002,
36
+ InsufficientLiquidity = 10003
37
+ }
38
+ export declare function getAggregatorServerErrorMessage(code: AggregatorServerErrorCode): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cetusprotocol/aggregator-sdk",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "sideEffects": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/client.ts CHANGED
@@ -17,6 +17,10 @@ import { swapInPools } from "./transaction/swap"
17
17
  import { completionCoin } from "./utils/coin"
18
18
  import { SUI_FRAMEWORK_ADDRESS } from "@mysten/sui/utils"
19
19
  import { AFTERMATH_AMM, JOIN_FUNC, PAY_MODULE, TURBOS_DEX, ZERO } from "./const"
20
+ import {
21
+ AggregatorServerErrorCode,
22
+ getAggregatorServerErrorMessage,
23
+ } from "./errors"
20
24
 
21
25
  export type ExtendedDetails = {
22
26
  aftermathPoolFlatness?: number
@@ -44,11 +48,17 @@ export type Router = {
44
48
  initialPrice: Decimal
45
49
  }
46
50
 
51
+ export type RouterError = {
52
+ code: number
53
+ msg: string
54
+ }
55
+
47
56
  export type RouterData = {
48
57
  amountIn: BN
49
58
  amountOut: BN
50
59
  routes: Router[]
51
60
  insufficientLiquidity: boolean
61
+ error?: RouterError
52
62
  }
53
63
 
54
64
  export type AggregatorResponse = {
@@ -184,6 +194,21 @@ export class AggregatorClient {
184
194
  }
185
195
 
186
196
  const response = await fetch(url)
197
+ if (!response.ok) {
198
+ return {
199
+ amountIn: ZERO,
200
+ amountOut: ZERO,
201
+ routes: [],
202
+ insufficientLiquidity: false,
203
+ error: {
204
+ code: AggregatorServerErrorCode.NumberTooLarge,
205
+ msg: getAggregatorServerErrorMessage(
206
+ AggregatorServerErrorCode.NumberTooLarge
207
+ ),
208
+ },
209
+ }
210
+ }
211
+
187
212
  const data = await response.json()
188
213
 
189
214
  if (data.data != null) {
@@ -191,15 +216,20 @@ export class AggregatorClient {
191
216
  return res
192
217
  }
193
218
 
194
- if (data.msg === "liquidity is not enough") {
195
- return {
196
- amountIn: ZERO,
197
- amountOut: ZERO,
198
- routes: [],
199
- insufficientLiquidity: true,
200
- }
219
+ const insufficientLiquidity = data.msg === "liquidity is not enough"
220
+
221
+ return {
222
+ amountIn: ZERO,
223
+ amountOut: ZERO,
224
+ routes: [],
225
+ insufficientLiquidity,
226
+ error: {
227
+ code: AggregatorServerErrorCode.InsufficientLiquidity,
228
+ msg: getAggregatorServerErrorMessage(
229
+ AggregatorServerErrorCode.InsufficientLiquidity
230
+ ),
231
+ },
201
232
  }
202
- return null
203
233
  }
204
234
 
205
235
  async swapInPools(
@@ -236,7 +266,6 @@ export class AggregatorClient {
236
266
  )
237
267
 
238
268
  const txb = new Transaction()
239
- // txb.setGasBudget(42392686100000000)
240
269
 
241
270
  if (refreshAllCoins || this.allCoins.length === 0) {
242
271
  this.allCoins = await this.getAllCoins()
@@ -332,7 +361,6 @@ export class AggregatorClient {
332
361
  }
333
362
 
334
363
  async devInspectTransactionBlock(txb: Transaction) {
335
- console.log(this.wallet)
336
364
  const res = await this.client.devInspectTransactionBlock({
337
365
  transactionBlock: txb,
338
366
  sender: this.wallet,
package/src/errors.ts CHANGED
@@ -19,7 +19,10 @@ export enum TransactionErrorCode {
19
19
  MissAftermathLpSupplyType = `MissAftermathLpSupplyType`,
20
20
  }
21
21
 
22
- export type AggregatorErrorCode = TypesErrorCode
22
+ export type AggregatorErrorCode =
23
+ | TypesErrorCode
24
+ | ConfigErrorCode
25
+ | TransactionErrorCode
23
26
 
24
27
  /**
25
28
  * AggregatorError is a custom error class that extends the built-in Error class. It is used to represent errors that occur during aggregation operations.
@@ -42,3 +45,27 @@ export class AggregatorError extends Error {
42
45
  return e instanceof AggregatorError && e.errorCode === code
43
46
  }
44
47
  }
48
+
49
+ export enum AggregatorServerErrorCode {
50
+ CalculateError = 10000,
51
+ NumberTooLarge = 10001,
52
+ NoRouter = 10002,
53
+ InsufficientLiquidity = 10003,
54
+ }
55
+
56
+ export function getAggregatorServerErrorMessage(
57
+ code: AggregatorServerErrorCode
58
+ ): string {
59
+ switch (code) {
60
+ case AggregatorServerErrorCode.CalculateError:
61
+ return "Calculate error"
62
+ case AggregatorServerErrorCode.NumberTooLarge:
63
+ return "Input number too large can not fit in target type"
64
+ case AggregatorServerErrorCode.NoRouter:
65
+ return "No router"
66
+ case AggregatorServerErrorCode.InsufficientLiquidity:
67
+ return "Insufficient Liquidity"
68
+ default:
69
+ return "Unknown error"
70
+ }
71
+ }
@@ -51,7 +51,12 @@ export function buildInputCoin(
51
51
  }
52
52
  }
53
53
 
54
+ console.log("used coin asests", usedCoinAsests)
55
+
54
56
  let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests)
57
+
58
+ console.log("totalCoinBalance", totalCoinBalance)
59
+ console.log("amount", amount)
55
60
  if (totalCoinBalance < amount) {
56
61
  throw new AggregateError(
57
62
  "Insufficient balance when build merge coin",
@@ -43,13 +43,13 @@ describe("router module", () => {
43
43
  const byte = Buffer.from(secret, "base64")
44
44
  const u8Array = new Uint8Array(byte)
45
45
 
46
- keypair = Ed25519Keypair.fromSecretKey(fromB64(secret).slice(1, 33))
46
+ // keypair = Ed25519Keypair.fromSecretKey(fromB64(secret).slice(1, 33))
47
47
 
48
- const wallet = keypair.getPublicKey().toSuiAddress()
49
- console.log("wallet", wallet)
48
+ // const wallet = keypair.getPublicKey().toSuiAddress()
49
+ // console.log("wallet", wallet)
50
50
 
51
- // const wallet =
52
- // "0x1d30e55c730f92a02a33dbdf6b052cd178e5d924aa58c5e2350a24852250ae58"
51
+ const wallet =
52
+ "0xfba94aa36e93ccc7d84a6a57040fc51983223f1b522a8d0be3c3bf2c98977ebb"
53
53
  // const wallet = "0xaabf2fedcb36146db164bec930b74a47969c4df98216e049342a3c49b6d11580"
54
54
  // const wallet = "0x410456cfc689666936b6bf80fbec958b69499b9f7183ecba07de577c17248a44"
55
55
  // const wallet = "0xca171941521153181ff729d53489eaae7e99c3f4692884afd7cca61154e4cec4"
@@ -111,7 +111,7 @@ describe("router module", () => {
111
111
  })
112
112
 
113
113
  test("Find router", async () => {
114
- const amount = "42392676100000000"
114
+ const amount = "423926761000000000000000000000"
115
115
 
116
116
  const res = await client.findRouter({
117
117
  from: M_SUI,
@@ -135,10 +135,10 @@ describe("router module", () => {
135
135
 
136
136
  test("Build router tx", async () => {
137
137
  const byAmountIn = true
138
- const amount = "42392676100000000"
138
+ const amount = "1000000"
139
139
 
140
- const from = M_SUI
141
- const target = M_USDC
140
+ const from = M_USDC
141
+ const target = M_SUI
142
142
 
143
143
  const res = await client.findRouter({
144
144
  from,
@@ -149,7 +149,7 @@ describe("router module", () => {
149
149
  splitAlgorithm: null,
150
150
  splitFactor: null,
151
151
  splitCount: null,
152
- providers: ["CETUS"],
152
+ providers: ["CETUS", "DEEPBOOK", "AFTERMATH", "FLOWX", "KRIYA", "TURBOS"],
153
153
  })
154
154
 
155
155
  if (res != null) {
@@ -174,6 +174,8 @@ describe("router module", () => {
174
174
  refreshAllCoins: true,
175
175
  })
176
176
 
177
+ printTransaction(routerTx)
178
+
177
179
  let result = await client.devInspectTransactionBlock(routerTx)
178
180
 
179
181
  if (result.effects.status.status === "success") {