@7kprotocol/sdk-ts 3.5.2 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/lib/cjs/config/index.js +9 -0
  2. package/lib/cjs/constants/apiEndpoints.js +1 -1
  3. package/lib/cjs/features/metaAg/common.js +103 -0
  4. package/lib/cjs/features/metaAg/error.js +35 -0
  5. package/lib/cjs/features/metaAg/index.js +99 -115
  6. package/lib/cjs/features/metaAg/providers/bluefin7k.js +57 -0
  7. package/lib/cjs/features/metaAg/providers/{bluefin.js → bluefin7kLegacy.js} +13 -13
  8. package/lib/cjs/features/metaAg/providers/cetus.js +7 -7
  9. package/lib/cjs/features/metaAg/providers/flowx.js +6 -6
  10. package/lib/cjs/features/metaAg/providers/okx.js +182 -0
  11. package/lib/cjs/features/swap/buildTx.js +3 -0
  12. package/lib/cjs/features/swap/buildTxV2.js +10 -6
  13. package/lib/cjs/features/swap/config.js +2 -1
  14. package/lib/cjs/features/swap/getQuote.js +3 -2
  15. package/lib/cjs/libs/protocols/bluefinx/client.js +1 -1
  16. package/lib/cjs/types/config/index.d.ts +4 -0
  17. package/lib/cjs/types/config/index.d.ts.map +1 -1
  18. package/lib/cjs/types/features/metaAg/common.d.ts +24 -0
  19. package/lib/cjs/types/features/metaAg/common.d.ts.map +1 -0
  20. package/lib/cjs/types/features/metaAg/error.d.ts +60 -0
  21. package/lib/cjs/types/features/metaAg/error.d.ts.map +1 -0
  22. package/lib/cjs/types/features/metaAg/index.d.ts +11 -2
  23. package/lib/cjs/types/features/metaAg/index.d.ts.map +1 -1
  24. package/lib/cjs/types/features/metaAg/providers/bluefin7k.d.ts +11 -0
  25. package/lib/cjs/types/features/metaAg/providers/bluefin7k.d.ts.map +1 -0
  26. package/lib/cjs/types/features/metaAg/providers/bluefin7kLegacy.d.ts +11 -0
  27. package/lib/cjs/types/features/metaAg/providers/bluefin7kLegacy.d.ts.map +1 -0
  28. package/lib/cjs/types/features/metaAg/providers/cetus.d.ts +4 -5
  29. package/lib/cjs/types/features/metaAg/providers/cetus.d.ts.map +1 -1
  30. package/lib/cjs/types/features/metaAg/providers/flowx.d.ts +3 -3
  31. package/lib/cjs/types/features/metaAg/providers/flowx.d.ts.map +1 -1
  32. package/lib/cjs/types/features/metaAg/providers/okx.d.ts +21 -0
  33. package/lib/cjs/types/features/metaAg/providers/okx.d.ts.map +1 -0
  34. package/lib/cjs/types/features/swap/buildTx.d.ts.map +1 -1
  35. package/lib/cjs/types/features/swap/buildTxV2.d.ts +3 -3
  36. package/lib/cjs/types/features/swap/buildTxV2.d.ts.map +1 -1
  37. package/lib/cjs/types/features/swap/config.d.ts.map +1 -1
  38. package/lib/cjs/types/features/swap/getQuote.d.ts +1 -3
  39. package/lib/cjs/types/features/swap/getQuote.d.ts.map +1 -1
  40. package/lib/cjs/types/index.d.ts +3 -1
  41. package/lib/cjs/types/index.d.ts.map +1 -1
  42. package/lib/cjs/types/metaAg.js +10 -4
  43. package/lib/cjs/types/okx.js +6 -0
  44. package/lib/cjs/types/types/metaAg.d.ts +62 -12
  45. package/lib/cjs/types/types/metaAg.d.ts.map +1 -1
  46. package/lib/cjs/types/types/okx.d.ts +206 -0
  47. package/lib/cjs/types/types/okx.d.ts.map +1 -0
  48. package/lib/cjs/types/utils/sui.d.ts +1 -0
  49. package/lib/cjs/types/utils/sui.d.ts.map +1 -1
  50. package/lib/cjs/utils/sui.js +17 -1
  51. package/lib/esm/config/index.js +9 -0
  52. package/lib/esm/constants/apiEndpoints.js +1 -1
  53. package/lib/esm/features/metaAg/common.js +96 -0
  54. package/lib/esm/features/metaAg/error.js +33 -0
  55. package/lib/esm/features/metaAg/index.js +100 -116
  56. package/lib/esm/features/metaAg/providers/bluefin7k.js +55 -0
  57. package/lib/esm/features/metaAg/providers/{bluefin.js → bluefin7kLegacy.js} +11 -11
  58. package/lib/esm/features/metaAg/providers/cetus.js +7 -7
  59. package/lib/esm/features/metaAg/providers/flowx.js +6 -6
  60. package/lib/esm/features/metaAg/providers/okx.js +147 -0
  61. package/lib/esm/features/swap/buildTx.js +3 -0
  62. package/lib/esm/features/swap/buildTxV2.js +6 -2
  63. package/lib/esm/features/swap/config.js +2 -1
  64. package/lib/esm/features/swap/getQuote.js +3 -2
  65. package/lib/esm/libs/protocols/bluefinx/client.js +1 -1
  66. package/lib/esm/types/config/index.d.ts +4 -0
  67. package/lib/esm/types/config/index.d.ts.map +1 -1
  68. package/lib/esm/types/features/metaAg/common.d.ts +24 -0
  69. package/lib/esm/types/features/metaAg/common.d.ts.map +1 -0
  70. package/lib/esm/types/features/metaAg/error.d.ts +60 -0
  71. package/lib/esm/types/features/metaAg/error.d.ts.map +1 -0
  72. package/lib/esm/types/features/metaAg/index.d.ts +11 -2
  73. package/lib/esm/types/features/metaAg/index.d.ts.map +1 -1
  74. package/lib/esm/types/features/metaAg/providers/bluefin7k.d.ts +11 -0
  75. package/lib/esm/types/features/metaAg/providers/bluefin7k.d.ts.map +1 -0
  76. package/lib/esm/types/features/metaAg/providers/bluefin7kLegacy.d.ts +11 -0
  77. package/lib/esm/types/features/metaAg/providers/bluefin7kLegacy.d.ts.map +1 -0
  78. package/lib/esm/types/features/metaAg/providers/cetus.d.ts +4 -5
  79. package/lib/esm/types/features/metaAg/providers/cetus.d.ts.map +1 -1
  80. package/lib/esm/types/features/metaAg/providers/flowx.d.ts +3 -3
  81. package/lib/esm/types/features/metaAg/providers/flowx.d.ts.map +1 -1
  82. package/lib/esm/types/features/metaAg/providers/okx.d.ts +21 -0
  83. package/lib/esm/types/features/metaAg/providers/okx.d.ts.map +1 -0
  84. package/lib/esm/types/features/swap/buildTx.d.ts.map +1 -1
  85. package/lib/esm/types/features/swap/buildTxV2.d.ts +3 -3
  86. package/lib/esm/types/features/swap/buildTxV2.d.ts.map +1 -1
  87. package/lib/esm/types/features/swap/config.d.ts.map +1 -1
  88. package/lib/esm/types/features/swap/getQuote.d.ts +1 -3
  89. package/lib/esm/types/features/swap/getQuote.d.ts.map +1 -1
  90. package/lib/esm/types/index.d.ts +3 -1
  91. package/lib/esm/types/index.d.ts.map +1 -1
  92. package/lib/esm/types/metaAg.js +7 -2
  93. package/lib/esm/types/okx.js +5 -0
  94. package/lib/esm/types/types/metaAg.d.ts +62 -12
  95. package/lib/esm/types/types/metaAg.d.ts.map +1 -1
  96. package/lib/esm/types/types/okx.d.ts +206 -0
  97. package/lib/esm/types/types/okx.d.ts.map +1 -0
  98. package/lib/esm/types/utils/sui.d.ts +1 -0
  99. package/lib/esm/types/utils/sui.d.ts.map +1 -1
  100. package/lib/esm/utils/sui.js +16 -1
  101. package/package.json +3 -1
  102. package/lib/cjs/types/features/metaAg/providers/bluefin.d.ts +0 -11
  103. package/lib/cjs/types/features/metaAg/providers/bluefin.d.ts.map +0 -1
  104. package/lib/cjs/types/utils/condition.d.ts +0 -2
  105. package/lib/cjs/types/utils/condition.d.ts.map +0 -1
  106. package/lib/cjs/utils/condition.js +0 -8
  107. package/lib/esm/types/features/metaAg/providers/bluefin.d.ts +0 -11
  108. package/lib/esm/types/features/metaAg/providers/bluefin.d.ts.map +0 -1
  109. package/lib/esm/types/utils/condition.d.ts +0 -2
  110. package/lib/esm/types/utils/condition.d.ts.map +0 -1
  111. package/lib/esm/utils/condition.js +0 -5
@@ -6,6 +6,7 @@ const pyth_sui_js_1 = require("@pythnetwork/pyth-sui-js");
6
6
  const HERMES_API = "https://hermes.pyth.network";
7
7
  const WORMHOLE_STATE_ID = "0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c";
8
8
  const PYTH_STATE_ID = "0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8";
9
+ let api = "";
9
10
  let apiKey = "";
10
11
  let bluefinXApiKey = "";
11
12
  let suiClient = new client_1.SuiClient({
@@ -13,6 +14,12 @@ let suiClient = new client_1.SuiClient({
13
14
  });
14
15
  let pythClient = new pyth_sui_js_1.SuiPythClient(suiClient, PYTH_STATE_ID, WORMHOLE_STATE_ID);
15
16
  let pythConnection = new pyth_sui_js_1.SuiPriceServiceConnection(HERMES_API);
17
+ function setApi(url) {
18
+ api = url;
19
+ }
20
+ function getApi() {
21
+ return api;
22
+ }
16
23
  function setApiKey(key) {
17
24
  apiKey = key;
18
25
  }
@@ -44,6 +51,8 @@ function getPythConnection() {
44
51
  return pythConnection;
45
52
  }
46
53
  const Config = {
54
+ setApi,
55
+ getApi,
47
56
  setApiKey,
48
57
  getApiKey,
49
58
  setBluefinXApiKey,
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.API_ENDPOINTS = void 0;
4
4
  exports.API_ENDPOINTS = {
5
- MAIN: "https://aggregator.api.sui-prod.bluefin.io",
5
+ MAIN: "https://api.7k.ag",
6
6
  LO_DCA: "https://lod-dca.7k.ag",
7
7
  PRICES: "https://prices.7k.ag",
8
8
  STATISTIC: "https://statistic.7k.ag",
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timeout = exports.metaSettle = exports.simulateAggregator = exports.simulateSwapTx = void 0;
4
+ const transactions_1 = require("@mysten/sui/transactions");
5
+ const _7k_1 = require("../../constants/_7k");
6
+ const buildTx_1 = require("../swap/buildTx");
7
+ const error_1 = require("./error");
8
+ const simulateSwapTx = async (tx, inspector, simulation) => {
9
+ const res = await (0, exports.timeout)(() => inspector.devInspectTransactionBlock({
10
+ sender: simulation.sender,
11
+ transactionBlock: tx,
12
+ }), simulation.timeout ?? 2000);
13
+ if (res.effects.status.status === "failure") {
14
+ throw new error_1.MetaAgError(res.error ?? "Simulation failed", error_1.MetaAgErrorCode.SIMULATION_FAILED, { error: res.error });
15
+ }
16
+ const amountOut = extractAmountOutWrapper(res.events);
17
+ return {
18
+ simulatedAmountOut: amountOut,
19
+ gasUsed: res.effects.gasUsed,
20
+ };
21
+ };
22
+ exports.simulateSwapTx = simulateSwapTx;
23
+ const simulateAggregator = async (provider, quote, simulation, inspector, options) => {
24
+ const tx = new transactions_1.Transaction();
25
+ const coinOut = await provider.swap({
26
+ quote,
27
+ coinIn: (0, transactions_1.coinWithBalance)({
28
+ balance: BigInt(quote.amountIn),
29
+ type: quote.coinTypeIn,
30
+ useGasCoin: false,
31
+ }),
32
+ signer: simulation.sender,
33
+ tx,
34
+ });
35
+ tx.add((0, exports.metaSettle)(quote, coinOut, 10000, options.tipBps, options.partner, options.partnerCommissionBps));
36
+ tx.transferObjects([coinOut], simulation.sender);
37
+ const res = await (0, exports.simulateSwapTx)(tx, inspector, simulation);
38
+ return {
39
+ id: quote.id,
40
+ provider: provider.kind,
41
+ ...res,
42
+ };
43
+ };
44
+ exports.simulateAggregator = simulateAggregator;
45
+ /**
46
+ * this settlement does not charge commission fee for partner, since all integrated aggregators already charge commission fee for partner
47
+ * @param quote Meta Aggregator Quote
48
+ * @param coinOut Coin Out Object
49
+ * @param slippageBps Slippage Bps
50
+ * @param tipBps Tip Bps default = 0
51
+ * @param partner address of partner for analytic default is zero address
52
+ */
53
+ const metaSettle = (quote, coinOut, slippageBps = 100, tipBps = 0, partner, commissionBps = 0) => {
54
+ return (tx) => {
55
+ const { minAmount, expectedAmount } = (0, buildTx_1.getExpectedReturn)(quote.rawAmountOut, slippageBps, commissionBps, tipBps);
56
+ if (tipBps > 0) {
57
+ tx.moveCall({
58
+ target: `${_7k_1._7K_META_PUBLISHED_AT}::vault::collect_tip`,
59
+ typeArguments: [quote.coinTypeOut],
60
+ arguments: [
61
+ tx.object(_7k_1._7K_META_VAULT),
62
+ tx.object(_7k_1._7K_META_CONFIG),
63
+ coinOut,
64
+ tx.pure.u64(tipBps),
65
+ ],
66
+ });
67
+ }
68
+ tx.moveCall({
69
+ target: `${_7k_1._7K_META_PUBLISHED_AT}::settle::settle`,
70
+ typeArguments: [quote.coinTypeIn, quote.coinTypeOut],
71
+ arguments: [
72
+ tx.object(_7k_1._7K_META_CONFIG),
73
+ tx.object(_7k_1._7K_META_VAULT),
74
+ tx.pure.u64(quote.amountIn),
75
+ coinOut,
76
+ tx.pure.u64(minAmount),
77
+ tx.pure.u64(expectedAmount),
78
+ tx.pure.option("address", partner),
79
+ tx.pure.u64(commissionBps),
80
+ tx.pure.u64(0), // ps
81
+ ],
82
+ });
83
+ };
84
+ };
85
+ exports.metaSettle = metaSettle;
86
+ const extractAmountOutWrapper = (events) => {
87
+ const swapEvent = events
88
+ .filter((event) => event.type === `${_7k_1._7K_META_PACKAGE_ID}::settle::Swap`)
89
+ ?.pop();
90
+ return swapEvent?.parsedJson?.amount_out;
91
+ };
92
+ const timeout = async (fn, timeout, msg) => {
93
+ if (timeout <= 0)
94
+ return fn();
95
+ return new Promise((resolve, reject) => {
96
+ const timer = setTimeout(() => reject(new error_1.MetaAgError(`Timeout ${msg ?? "operation"}`, error_1.MetaAgErrorCode.TIMEOUT, { timeout })), timeout);
97
+ fn()
98
+ .then(resolve)
99
+ .catch(reject)
100
+ .finally(() => clearTimeout(timer));
101
+ });
102
+ };
103
+ exports.timeout = timeout;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MetaAgError = exports.MetaAgErrorCode = void 0;
4
+ var MetaAgErrorCode;
5
+ (function (MetaAgErrorCode) {
6
+ MetaAgErrorCode[MetaAgErrorCode["UNKNOWN"] = 1000] = "UNKNOWN";
7
+ MetaAgErrorCode[MetaAgErrorCode["TIMEOUT"] = 1001] = "TIMEOUT";
8
+ MetaAgErrorCode[MetaAgErrorCode["PROVIDER_NOT_FOUND"] = 1002] = "PROVIDER_NOT_FOUND";
9
+ MetaAgErrorCode[MetaAgErrorCode["PROVIDER_NOT_SUPPORTED"] = 1003] = "PROVIDER_NOT_SUPPORTED";
10
+ MetaAgErrorCode[MetaAgErrorCode["INVALID_QUOTE"] = 1004] = "INVALID_QUOTE";
11
+ MetaAgErrorCode[MetaAgErrorCode["QUOTE_NOT_FOUND"] = 1005] = "QUOTE_NOT_FOUND";
12
+ MetaAgErrorCode[MetaAgErrorCode["INVALID_SIGNER_ADDRESS"] = 1006] = "INVALID_SIGNER_ADDRESS";
13
+ MetaAgErrorCode[MetaAgErrorCode["PROVIDER_NOT_SUPPORT_SWAP"] = 1007] = "PROVIDER_NOT_SUPPORT_SWAP";
14
+ MetaAgErrorCode[MetaAgErrorCode["SIMULATION_FAILED"] = 1008] = "SIMULATION_FAILED";
15
+ // OKX error
16
+ MetaAgErrorCode[MetaAgErrorCode["OKX_FINALIZE_COMMAND_NOT_FOUND"] = 1100] = "OKX_FINALIZE_COMMAND_NOT_FOUND";
17
+ // BluefinX error
18
+ MetaAgErrorCode[MetaAgErrorCode["BLUEFINX_TRANSACTION_NOT_FOUND"] = 1200] = "BLUEFINX_TRANSACTION_NOT_FOUND";
19
+ MetaAgErrorCode[MetaAgErrorCode["BLUEFINX_TRANSACTION_NOT_APPROVED"] = 1201] = "BLUEFINX_TRANSACTION_NOT_APPROVED";
20
+ MetaAgErrorCode[MetaAgErrorCode["BLUEFINX_TRANSACTION_DIGEST_NOT_FOUND"] = 1202] = "BLUEFINX_TRANSACTION_DIGEST_NOT_FOUND";
21
+ })(MetaAgErrorCode || (exports.MetaAgErrorCode = MetaAgErrorCode = {}));
22
+ class MetaAgError extends Error {
23
+ constructor(message, code, details) {
24
+ super(message);
25
+ this.name = "MetaAgError";
26
+ this.code = code ?? MetaAgErrorCode.UNKNOWN;
27
+ this.details = details;
28
+ }
29
+ static assert(condition, message, code, details) {
30
+ if (!condition) {
31
+ throw new MetaAgError(message ?? "Assertion failed", code, details);
32
+ }
33
+ }
34
+ }
35
+ exports.MetaAgError = MetaAgError;
@@ -37,13 +37,15 @@ exports.MetaAg = void 0;
37
37
  const client_1 = require("@mysten/sui/client");
38
38
  const transactions_1 = require("@mysten/sui/transactions");
39
39
  const utils_1 = require("@mysten/sui/utils");
40
- const _7k_1 = require("../../constants/_7k");
41
40
  const sui_1 = require("../../constants/sui");
42
41
  const metaAg_1 = require("../../types/metaAg");
43
- const condition_1 = require("../../utils/condition");
42
+ const sui_2 = require("../../utils/sui");
44
43
  const SuiClientUtils_1 = require("../../utils/SuiClientUtils");
45
44
  const buildTx_1 = require("../swap/buildTx");
46
- const bluefin_1 = require("./providers/bluefin");
45
+ const common_1 = require("./common");
46
+ const error_1 = require("./error");
47
+ const bluefin7kLegacy_1 = require("./providers/bluefin7kLegacy");
48
+ const okx_1 = require("./providers/okx");
47
49
  const HERMES_API = "https://hermes.pyth.network";
48
50
  const DEFAULT_PROVIDERS = {
49
51
  [metaAg_1.EProvider.BLUEFIN7K]: {},
@@ -72,10 +74,14 @@ class MetaAg {
72
74
  if (p)
73
75
  return p;
74
76
  const providerOptions = this.options.providers[provider];
75
- (0, condition_1.assert)(!!providerOptions, `Provider not found: ${provider}`);
77
+ error_1.MetaAgError.assert(!!providerOptions, `Provider not found: ${provider}`, error_1.MetaAgErrorCode.PROVIDER_NOT_FOUND, { provider });
76
78
  switch (provider) {
79
+ case metaAg_1.EProvider.BLUEFIN7K_LEGACY:
80
+ this.providers[metaAg_1.EProvider.BLUEFIN7K_LEGACY] = new bluefin7kLegacy_1.BluefinLegacyProvider(providerOptions, this.options, this.client);
81
+ break;
77
82
  case metaAg_1.EProvider.BLUEFIN7K:
78
- this.providers[metaAg_1.EProvider.BLUEFIN7K] = new bluefin_1.BluefinProvider(providerOptions, this.options, this.client);
83
+ const { Bluefin7kProvider } = await Promise.resolve().then(() => __importStar(require("./providers/bluefin7k"))).catch(catchImportError(metaAg_1.EProvider.BLUEFIN7K));
84
+ this.providers[metaAg_1.EProvider.BLUEFIN7K] = new Bluefin7kProvider(providerOptions, this.options, this.client);
79
85
  break;
80
86
  case metaAg_1.EProvider.FLOWX:
81
87
  const { FlowxProvider } = await Promise.resolve().then(() => __importStar(require("./providers/flowx"))).catch(catchImportError(metaAg_1.EProvider.FLOWX));
@@ -85,69 +91,64 @@ class MetaAg {
85
91
  const { CetusProvider } = await Promise.resolve().then(() => __importStar(require("./providers/cetus"))).catch(catchImportError(metaAg_1.EProvider.CETUS));
86
92
  this.providers[metaAg_1.EProvider.CETUS] = new CetusProvider(providerOptions, this.options, this.client);
87
93
  break;
94
+ case metaAg_1.EProvider.OKX:
95
+ this.providers[metaAg_1.EProvider.OKX] = new okx_1.OkxProvider(providerOptions, this.options, this.client);
96
+ break;
88
97
  default:
89
- throw new Error(`Provider not supported: ${provider}`);
98
+ throw new error_1.MetaAgError(`Provider not supported: ${provider}`, error_1.MetaAgErrorCode.PROVIDER_NOT_SUPPORTED, { provider });
90
99
  }
91
100
  return this.providers[provider];
92
101
  }
93
102
  async _simulate(provider, quote, simulation) {
94
103
  try {
95
- const tx = new transactions_1.Transaction();
96
- const id = quote.id;
97
- const coinOut = await provider.swap({
98
- quote,
99
- coinIn: (0, transactions_1.coinWithBalance)({
100
- balance: BigInt(quote.amountIn),
101
- type: quote.coinTypeIn,
102
- useGasCoin: false,
103
- }),
104
- signer: simulation.sender,
105
- tx,
106
- });
107
- tx.add(metaSettle(quote, coinOut, 10000, this.options.tipBps, this.options.partner, this.options.partnerCommissionBps));
108
- tx.transferObjects([coinOut], simulation.sender);
109
- const res = await timeout(() => this.inspector.devInspectTransactionBlock({
110
- sender: simulation.sender,
111
- transactionBlock: tx,
112
- }), simulation.timeout ?? 2000, `simulation for ${provider.kind} provider with id ${id}`);
113
- if (res.effects.status.status === "failure") {
114
- throw new Error(res.error ?? "Simulation failed");
104
+ if ((0, metaAg_1.isAggregatorProvider)(provider)) {
105
+ return (0, common_1.simulateAggregator)(provider, quote, simulation, this.inspector, this.options);
106
+ }
107
+ switch (quote.provider) {
108
+ case metaAg_1.EProvider.OKX:
109
+ return (0, okx_1.simulateOKXSwap)(quote, this.inspector, simulation, this.options);
110
+ default:
111
+ throw new error_1.MetaAgError(`Provider not supported: ${provider.kind}`, error_1.MetaAgErrorCode.PROVIDER_NOT_SUPPORTED, { provider: provider.kind });
115
112
  }
116
- const amountOut = extractAmountOutWrapper(res.events);
117
- return {
118
- id,
119
- simulatedAmountOut: amountOut,
120
- gasUsed: res.effects.gasUsed,
121
- provider: provider.kind,
122
- };
123
113
  }
124
114
  catch (error) {
125
- console.warn(`Failed to simulate ${provider.kind}: `, error);
115
+ console.warn(error, { provider: provider.kind, quote: quote.id });
126
116
  }
127
117
  }
128
- async _quote(provider, options, simulation) {
129
- const quote = await timeout(async () => {
118
+ async _quote(provider, options) {
119
+ const quote = await (0, common_1.timeout)(async () => {
130
120
  const quote = await provider.quote(options);
121
+ if (!quote)
122
+ return null;
131
123
  const { expectedAmount } = (0, buildTx_1.getExpectedReturn)(quote.rawAmountOut, 0, this.options.partnerCommissionBps, this.options.tipBps);
132
124
  quote.amountOut = expectedAmount;
133
125
  return quote;
134
- }, options.timeout ?? 2000, `quote for ${provider.kind} provider from ${options.coinInType} to ${options.coinOutType}`);
135
- if (simulation) {
136
- if (simulation.onSimulated) {
137
- this._simulate(provider, quote, simulation).then((payload) => {
138
- if (payload) {
139
- simulation.onSimulated?.(payload);
140
- }
141
- });
142
- }
143
- else {
144
- const updated = await this._simulate(provider, quote, simulation);
145
- quote.simulatedAmountOut = updated?.simulatedAmountOut;
146
- quote.gasUsed = updated?.gasUsed;
147
- }
148
- }
126
+ }, options.timeout ?? 2000, `quote for ${provider.kind} provider from ${options.coinTypeIn} to ${options.coinTypeOut}`);
149
127
  return quote;
150
128
  }
129
+ async _fastSwap({ quote, signer, useGasCoin, signTransaction }, getTransactionBlockParams) {
130
+ const tx = new transactions_1.Transaction();
131
+ const coin = await this.swap({
132
+ quote,
133
+ signer,
134
+ tx,
135
+ coinIn: (0, transactions_1.coinWithBalance)({
136
+ type: quote.coinTypeIn,
137
+ balance: BigInt(quote.amountIn),
138
+ useGasCoin,
139
+ }),
140
+ });
141
+ tx.transferObjects([coin], signer);
142
+ tx.setSenderIfNotSet(signer);
143
+ const txBytes = await tx.build({ client: this.client });
144
+ const { signature, bytes } = await signTransaction((0, utils_1.toBase64)(txBytes));
145
+ return this.client.executeTransactionBlock({
146
+ transactionBlock: bytes,
147
+ signature,
148
+ options: getTransactionBlockParams?.options,
149
+ signal: getTransactionBlockParams?.signal,
150
+ });
151
+ }
151
152
  /**
152
153
  * Get quotes from all providers
153
154
  * @param options - quote options
@@ -157,35 +158,73 @@ class MetaAg {
157
158
  async quote(options, simulation) {
158
159
  const opts = {
159
160
  ...options,
160
- coinInType: (0, utils_1.normalizeStructTag)(options.coinInType),
161
- coinOutType: (0, utils_1.normalizeStructTag)(options.coinOutType),
161
+ coinTypeIn: (0, utils_1.normalizeStructTag)(options.coinTypeIn),
162
+ coinTypeOut: (0, utils_1.normalizeStructTag)(options.coinTypeOut),
162
163
  };
163
164
  const quotes = await Promise.allSettled(Object.entries(this.options.providers)
164
165
  .filter(([_k, v]) => !v.disabled)
165
166
  .map(async ([provider]) => {
166
167
  const p = await this._getProvider(provider);
167
- return this._quote(p, opts, simulation);
168
+ return this._quote(p, opts);
168
169
  }));
169
- return quotes
170
+ const result = quotes
170
171
  .map((quote) => quote.status === "fulfilled"
171
172
  ? quote.value
172
173
  : (console.log(quote.reason), null))
173
174
  .filter((quote) => quote !== null);
175
+ if (simulation) {
176
+ const requests = result.map(async (quote) => {
177
+ const provider = await this._getProvider(quote.provider);
178
+ const updated = await this._simulate(provider, quote, simulation);
179
+ quote.simulatedAmountOut = updated?.simulatedAmountOut;
180
+ quote.gasUsed = updated?.gasUsed;
181
+ simulation?.onSimulated?.({ ...quote });
182
+ });
183
+ if (!simulation.onSimulated) {
184
+ await Promise.all(requests);
185
+ }
186
+ }
187
+ return result;
174
188
  }
175
189
  /**
176
190
  * Build transaction from quote
191
+ * @info Use this function to build composable transaction (ie: add more commands after the swap, consume the coin out object)
192
+ * @warning Providers that build transaction on the fly (typically RFQ, Swap-API providers ie: BluefinX, Okx, ...) are not supported, please use `fastSwap` instead
177
193
  * @param options - build tx options
178
194
  * @param slippageBps - slippage bps if not specified, fallback to global slippage bps, if none of them specified, default to 100
179
195
  * @returns coin out object, you must consume it by transferObjects, or other sub sequence commands
180
196
  */
181
197
  async swap(options, slippageBps) {
182
198
  const provider = await this._getProvider(options.quote.provider);
183
- (0, condition_1.assert)(!!provider, `Provider not found: ${options.quote.provider}`);
199
+ error_1.MetaAgError.assert(!!provider, `Provider not found: ${options.quote.provider}`, error_1.MetaAgErrorCode.PROVIDER_NOT_FOUND, { provider: options.quote.provider });
200
+ error_1.MetaAgError.assert((0, metaAg_1.isAggregatorProvider)(provider), `Provider does not support swap: ${provider.kind}, use fastSwap instead`, error_1.MetaAgErrorCode.PROVIDER_NOT_SUPPORT_SWAP, { provider: provider.kind });
201
+ error_1.MetaAgError.assert(!(0, sui_2.isSystemAddress)(options.signer), "Invalid signer address", error_1.MetaAgErrorCode.INVALID_SIGNER_ADDRESS, { signer: options.signer });
184
202
  const coinOut = await provider.swap(options);
185
- options.tx.add(metaSettle(options.quote, coinOut, slippageBps ?? this.options.slippageBps ?? 100, this.options.tipBps, this.options.partner, this.options.partnerCommissionBps));
203
+ options.tx.add((0, common_1.metaSettle)(options.quote, coinOut, slippageBps ?? this.options.slippageBps ?? 100, this.options.tipBps, this.options.partner, this.options.partnerCommissionBps));
186
204
  options.tx.setSenderIfNotSet(options.signer);
187
205
  return coinOut;
188
206
  }
207
+ /**
208
+ * Build, Sign, and Execute transaction in one step
209
+ * @param options - fast swap options
210
+ * @returns - txDigest of the transaction
211
+ */
212
+ async fastSwap(options, getTransactionBlockParams) {
213
+ error_1.MetaAgError.assert(!(0, sui_2.isSystemAddress)(options.signer), "Invalid signer address", error_1.MetaAgErrorCode.INVALID_SIGNER_ADDRESS, { signer: options.signer });
214
+ const provider = await this._getProvider(options.quote.provider);
215
+ if ((0, metaAg_1.isAggregatorProvider)(provider)) {
216
+ return this._fastSwap(options, getTransactionBlockParams);
217
+ }
218
+ else if ((0, metaAg_1.isSwapAPIProvider)(provider)) {
219
+ return this.client.waitForTransaction({
220
+ ...getTransactionBlockParams,
221
+ digest: await provider.fastSwap(options),
222
+ });
223
+ }
224
+ else {
225
+ throw new error_1.MetaAgError(`Provider not supported: ${provider.kind}`, error_1.MetaAgErrorCode.PROVIDER_NOT_SUPPORTED, { provider: provider.kind });
226
+ }
227
+ }
189
228
  /**
190
229
  * Update meta aggregator options
191
230
  * @param options - update options payload
@@ -219,71 +258,16 @@ class MetaAg {
219
258
  }
220
259
  }
221
260
  exports.MetaAg = MetaAg;
222
- /**
223
- * this settlement does not charge commission fee for partner, since all integrated aggregators already charge commission fee for partner
224
- * @param quote Meta Aggregator Quote
225
- * @param coinOut Coin Out Object
226
- * @param slippageBps Slippage Bps
227
- * @param tipBps Tip Bps default = 0
228
- * @param partner address of partner for analytic default is zero address
229
- */
230
- const metaSettle = (quote, coinOut, slippageBps = 100, tipBps = 0, partner, commissionBps = 0) => {
231
- return (tx) => {
232
- const { minAmount, expectedAmount } = (0, buildTx_1.getExpectedReturn)(quote.rawAmountOut, slippageBps, commissionBps, tipBps);
233
- if (tipBps > 0) {
234
- tx.moveCall({
235
- target: `${_7k_1._7K_META_PUBLISHED_AT}::vault::collect_tip`,
236
- typeArguments: [quote.coinTypeOut],
237
- arguments: [
238
- tx.object(_7k_1._7K_META_VAULT),
239
- tx.object(_7k_1._7K_META_CONFIG),
240
- coinOut,
241
- tx.pure.u64(tipBps),
242
- ],
243
- });
244
- }
245
- tx.moveCall({
246
- target: `${_7k_1._7K_META_PUBLISHED_AT}::settle::settle`,
247
- typeArguments: [quote.coinTypeIn, quote.coinTypeOut],
248
- arguments: [
249
- tx.object(_7k_1._7K_META_CONFIG),
250
- tx.object(_7k_1._7K_META_VAULT),
251
- tx.pure.u64(quote.amountIn),
252
- coinOut,
253
- tx.pure.u64(minAmount),
254
- tx.pure.u64(expectedAmount),
255
- tx.pure.option("address", partner),
256
- tx.pure.u64(commissionBps),
257
- tx.pure.u64(0), // ps
258
- ],
259
- });
260
- };
261
- };
262
- const extractAmountOutWrapper = (events) => {
263
- const swapEvent = events
264
- .filter((event) => event.type === `${_7k_1._7K_META_PACKAGE_ID}::settle::Swap`)
265
- ?.pop();
266
- return swapEvent?.parsedJson?.amount_out;
267
- };
268
261
  const catchImportError = (provider) => {
269
262
  return (e) => {
270
263
  const map = {
271
264
  [metaAg_1.EProvider.CETUS]: "@cetusprotocol/aggregator-sdk",
272
265
  [metaAg_1.EProvider.FLOWX]: "@flowx-finance/sdk",
273
- [metaAg_1.EProvider.BLUEFIN7K]: "@7kprotocol/sdk-ts",
266
+ [metaAg_1.EProvider.BLUEFIN7K_LEGACY]: "@7kprotocol/sdk-ts",
267
+ [metaAg_1.EProvider.BLUEFIN7K]: "@bluefin-exchange/bluefin7k-aggregator-sdk",
268
+ [metaAg_1.EProvider.OKX]: "",
274
269
  };
275
270
  console.warn(`Please install ${map[provider]} to use ${provider} provider`);
276
271
  throw e;
277
272
  };
278
273
  };
279
- const timeout = async (fn, timeout, msg) => {
280
- if (timeout <= 0)
281
- return fn();
282
- return new Promise((resolve, reject) => {
283
- const timer = setTimeout(() => reject(new Error(`Timeout ${msg ?? "operation"}`)), timeout);
284
- fn()
285
- .then(resolve)
286
- .catch(reject)
287
- .finally(() => clearTimeout(timer));
288
- });
289
- };
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Bluefin7kProvider = void 0;
4
+ const bluefin7k_aggregator_sdk_1 = require("@bluefin-exchange/bluefin7k-aggregator-sdk");
5
+ const uuid_1 = require("uuid");
6
+ const _7k_1 = require("../../../constants/_7k");
7
+ const metaAg_1 = require("../../../types/metaAg");
8
+ const error_1 = require("../error");
9
+ class Bluefin7kProvider {
10
+ constructor(options, metaOptions, client) {
11
+ this.options = options;
12
+ this.metaOptions = metaOptions;
13
+ this.kind = metaAg_1.EProvider.BLUEFIN7K;
14
+ if (options.apiKey)
15
+ bluefin7k_aggregator_sdk_1.Config.setApiKey(options.apiKey);
16
+ bluefin7k_aggregator_sdk_1.Config.setSuiClient(client);
17
+ }
18
+ async quote(quoteOptions) {
19
+ const quote = await (0, bluefin7k_aggregator_sdk_1.getQuote)({
20
+ tokenIn: quoteOptions.coinTypeIn,
21
+ tokenOut: quoteOptions.coinTypeOut,
22
+ amountIn: quoteOptions.amountIn,
23
+ sources: this.options.sources,
24
+ excludedPools: this.options.excludedPools,
25
+ targetPools: this.options.targetPools,
26
+ });
27
+ error_1.MetaAgError.assert(!!quote, "No quote found", error_1.MetaAgErrorCode.QUOTE_NOT_FOUND, { provider: this.kind });
28
+ return {
29
+ id: (0, uuid_1.v4)(),
30
+ provider: metaAg_1.EProvider.BLUEFIN7K,
31
+ quote,
32
+ amountIn: quote.swapAmountWithDecimal,
33
+ rawAmountOut: quote.returnAmountWithDecimal,
34
+ amountOut: quote.returnAmountWithDecimal,
35
+ coinTypeIn: quoteOptions.coinTypeIn,
36
+ coinTypeOut: quoteOptions.coinTypeOut,
37
+ };
38
+ }
39
+ async swap({ quote, signer, tx, coinIn }) {
40
+ error_1.MetaAgError.assert(quote.provider === metaAg_1.EProvider.BLUEFIN7K, "Invalid quote", error_1.MetaAgErrorCode.INVALID_QUOTE, { quote, expectedProvider: metaAg_1.EProvider.BLUEFIN7K });
41
+ const { coinOut } = await (0, bluefin7k_aggregator_sdk_1.buildTx)({
42
+ quoteResponse: quote.quote,
43
+ accountAddress: signer,
44
+ commission: {
45
+ commissionBps: 0,
46
+ partner: _7k_1._7K_PARTNER_ADDRESS,
47
+ },
48
+ slippage: 1,
49
+ extendTx: {
50
+ tx,
51
+ coinIn,
52
+ },
53
+ });
54
+ return coinOut;
55
+ }
56
+ }
57
+ exports.Bluefin7kProvider = Bluefin7kProvider;
@@ -1,26 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BluefinProvider = void 0;
3
+ exports.BluefinLegacyProvider = void 0;
4
4
  const pyth_sui_js_1 = require("@pythnetwork/pyth-sui-js");
5
5
  const uuid_1 = require("uuid");
6
6
  const config_1 = require("../../../config");
7
7
  const _7k_1 = require("../../../constants/_7k");
8
- const apiEndpoints_1 = require("../../../constants/apiEndpoints");
9
8
  const metaAg_1 = require("../../../types/metaAg");
10
- const condition_1 = require("../../../utils/condition");
11
9
  const buildTxV2_1 = require("../../swap/buildTxV2");
12
10
  const getQuote_1 = require("../../swap/getQuote");
11
+ const error_1 = require("../error");
13
12
  const WORMHOLE_STATE_ID = "0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c";
14
13
  const PYTH_STATE_ID = "0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8";
15
- class BluefinProvider {
14
+ class BluefinLegacyProvider {
16
15
  constructor(options, metaOptions, client) {
17
16
  this.options = options;
18
17
  this.metaOptions = metaOptions;
19
- this.kind = metaAg_1.EProvider.BLUEFIN7K;
18
+ this.kind = metaAg_1.EProvider.BLUEFIN7K_LEGACY;
20
19
  const pythClient = new pyth_sui_js_1.SuiPythClient(client, PYTH_STATE_ID, WORMHOLE_STATE_ID);
21
20
  const pythConnection = new pyth_sui_js_1.SuiPriceServiceConnection(this.metaOptions.hermesApi);
22
21
  if (options.apiKey)
23
22
  config_1.Config.setApiKey(options.apiKey);
23
+ if (options.api)
24
+ config_1.Config.setApi(options.api);
24
25
  config_1.Config.setSuiClient(client);
25
26
  config_1.Config.setPythClient(pythClient);
26
27
  config_1.Config.setPythConnection(pythConnection);
@@ -28,9 +29,8 @@ class BluefinProvider {
28
29
  async quote(options) {
29
30
  const quote = await (0, getQuote_1.getQuote)({
30
31
  amountIn: options.amountIn,
31
- tokenIn: options.coinInType,
32
- tokenOut: options.coinOutType,
33
- api: this.options.api || apiEndpoints_1.API_ENDPOINTS.MAIN,
32
+ tokenIn: options.coinTypeIn,
33
+ tokenOut: options.coinTypeOut,
34
34
  sources: this.options.sources,
35
35
  maxPaths: this.options.maxPaths,
36
36
  excludedPools: this.options.excludedPools,
@@ -38,17 +38,17 @@ class BluefinProvider {
38
38
  });
39
39
  return {
40
40
  id: (0, uuid_1.v4)(),
41
- provider: metaAg_1.EProvider.BLUEFIN7K,
41
+ provider: metaAg_1.EProvider.BLUEFIN7K_LEGACY,
42
42
  quote,
43
43
  amountIn: quote.swapAmountWithDecimal,
44
44
  rawAmountOut: quote.returnAmountWithDecimal,
45
45
  amountOut: quote.returnAmountWithDecimal,
46
- coinTypeIn: options.coinInType,
47
- coinTypeOut: options.coinOutType,
46
+ coinTypeIn: options.coinTypeIn,
47
+ coinTypeOut: options.coinTypeOut,
48
48
  };
49
49
  }
50
50
  async swap({ quote, signer, tx, coinIn }) {
51
- (0, condition_1.assert)(quote.provider === metaAg_1.EProvider.BLUEFIN7K, "Invalid quote");
51
+ error_1.MetaAgError.assert(quote.provider === metaAg_1.EProvider.BLUEFIN7K_LEGACY, "Invalid quote", error_1.MetaAgErrorCode.INVALID_QUOTE, { quote, expectedProvider: metaAg_1.EProvider.BLUEFIN7K_LEGACY });
52
52
  const { coinOut } = await (0, buildTxV2_1.buildTxV2)({
53
53
  quoteResponse: quote.quote,
54
54
  accountAddress: signer,
@@ -65,4 +65,4 @@ class BluefinProvider {
65
65
  return coinOut;
66
66
  }
67
67
  }
68
- exports.BluefinProvider = BluefinProvider;
68
+ exports.BluefinLegacyProvider = BluefinLegacyProvider;
@@ -5,7 +5,7 @@ const aggregator_sdk_1 = require("@cetusprotocol/aggregator-sdk");
5
5
  const uuid_1 = require("uuid");
6
6
  const _7k_1 = require("../../../constants/_7k");
7
7
  const metaAg_1 = require("../../../types/metaAg");
8
- const condition_1 = require("../../../utils/condition");
8
+ const error_1 = require("../error");
9
9
  class CetusProvider {
10
10
  constructor(options, metaOptions, client) {
11
11
  this.options = options;
@@ -24,8 +24,8 @@ class CetusProvider {
24
24
  const quote = await this.cetusClient.findRouters({
25
25
  amount: quoteOptions.amountIn,
26
26
  byAmountIn: true,
27
- from: quoteOptions.coinInType,
28
- target: quoteOptions.coinOutType,
27
+ from: quoteOptions.coinTypeIn,
28
+ target: quoteOptions.coinTypeOut,
29
29
  providers: this.options.sources,
30
30
  splitCount: this.options.splitCount,
31
31
  splitAlgorithm: this.options.splitAlgorithm,
@@ -33,7 +33,7 @@ class CetusProvider {
33
33
  depth: this.options.depth,
34
34
  liquidityChanges: this.options.liquidityChanges,
35
35
  });
36
- (0, condition_1.assert)(!!quote, "No quote found");
36
+ error_1.MetaAgError.assert(!!quote, "No quote found", error_1.MetaAgErrorCode.QUOTE_NOT_FOUND, { provider: this.kind });
37
37
  return {
38
38
  id: (0, uuid_1.v4)(),
39
39
  provider: metaAg_1.EProvider.CETUS,
@@ -41,12 +41,12 @@ class CetusProvider {
41
41
  amountIn: quote.amountIn.toString() || "0",
42
42
  rawAmountOut: quote.amountOut.toString() || "0",
43
43
  amountOut: quote.amountOut.toString() || "0",
44
- coinTypeIn: quoteOptions.coinInType,
45
- coinTypeOut: quoteOptions.coinOutType,
44
+ coinTypeIn: quoteOptions.coinTypeIn,
45
+ coinTypeOut: quoteOptions.coinTypeOut,
46
46
  };
47
47
  }
48
48
  async swap(options) {
49
- (0, condition_1.assert)(options.quote.provider === metaAg_1.EProvider.CETUS, "Expect Cetus quote");
49
+ error_1.MetaAgError.assert(options.quote.provider === metaAg_1.EProvider.CETUS, "Expect Cetus quote", error_1.MetaAgErrorCode.INVALID_QUOTE, { quote: options.quote, expectedProvider: metaAg_1.EProvider.CETUS });
50
50
  const coinOut = await this.cetusClient.routerSwap({
51
51
  inputCoin: options.coinIn,
52
52
  router: options.quote.quote,