hak-saucerswap-plugin 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
+ import { BaseTool, handleTransaction } from '@hashgraph/hedera-agent-kit';
1
2
  import { z } from 'zod';
2
3
  import axios from 'axios';
3
- import { ContractFunctionParameters, ContractExecuteTransaction, AccountId, TokenId, ContractId } from '@hashgraph/sdk';
4
- import { AgentMode } from 'hedera-agent-kit';
4
+ import { ContractFunctionParameters, ContractExecuteTransaction, AccountId, TokenId, ContractId } from '@hiero-ledger/sdk';
5
5
 
6
6
  // src/tools/farms.ts
7
7
 
@@ -34,7 +34,8 @@ var SaucerSwapClient = class {
34
34
  this.retries = options.retries ?? 2;
35
35
  this.http = options.http ?? axios.create({
36
36
  baseURL: options.baseUrl ?? "https://api.saucerswap.finance",
37
- timeout: options.timeoutMs ?? 1e4
37
+ timeout: options.timeoutMs ?? 1e4,
38
+ headers: options.apiKey ? { "x-api-key": options.apiKey } : void 0
38
39
  });
39
40
  }
40
41
  async request(path, params) {
@@ -176,6 +177,7 @@ var resolveSaucerSwapConfig = (context) => {
176
177
  baseUrl: ctxConfig.baseUrl ?? process.env.SAUCERSWAP_BASE_URL ?? DEFAULT_CONFIG.baseUrl,
177
178
  timeoutMs: ctxConfig.timeoutMs ?? toNumber(process.env.SAUCERSWAP_TIMEOUT_MS, DEFAULT_CONFIG.timeoutMs),
178
179
  retries: ctxConfig.retries ?? toNumber(process.env.SAUCERSWAP_RETRIES, DEFAULT_CONFIG.retries),
180
+ apiKey: ctxConfig.apiKey ?? process.env.SAUCERSWAP_API_KEY,
179
181
  routerContractId: ctxConfig.routerContractId ?? process.env.SAUCERSWAP_ROUTER_CONTRACT_ID,
180
182
  routerV2ContractId: ctxConfig.routerV2ContractId ?? process.env.SAUCERSWAP_ROUTER_V2_CONTRACT_ID,
181
183
  wrappedHbarTokenId: ctxConfig.wrappedHbarTokenId ?? process.env.SAUCERSWAP_WRAPPED_HBAR_TOKEN_ID,
@@ -193,13 +195,18 @@ var resolveSaucerSwapConfig = (context) => {
193
195
  var farmsInputSchema = z.object({
194
196
  poolId: z.number().int().positive().optional().describe("Optional pool ID to filter farms")
195
197
  });
196
- var farmsTool = {
197
- method: "saucerswap_get_farms",
198
- name: "SaucerSwap Get Farms",
199
- description: "Get active farming opportunities on SaucerSwap.",
200
- parameters: farmsInputSchema,
201
- execute: async (_client, context, params) => {
202
- const args = farmsInputSchema.parse(params);
198
+ var FarmsTool = class extends BaseTool {
199
+ constructor() {
200
+ super(...arguments);
201
+ this.method = "saucerswap_get_farms";
202
+ this.name = "SaucerSwap Get Farms";
203
+ this.description = "Get active farming opportunities on SaucerSwap.";
204
+ this.parameters = farmsInputSchema;
205
+ }
206
+ async normalizeParams(params, _context, _client) {
207
+ return farmsInputSchema.parse(params);
208
+ }
209
+ async coreAction(args, context, _client) {
203
210
  const config = resolveSaucerSwapConfig(context);
204
211
  const api = context.saucerswapClient ?? createSaucerSwapClient(config);
205
212
  try {
@@ -216,7 +223,14 @@ var farmsTool = {
216
223
  };
217
224
  }
218
225
  }
226
+ async shouldSecondaryAction(_coreActionResult, _context) {
227
+ return false;
228
+ }
229
+ async secondaryAction(_request, _client, _context) {
230
+ return null;
231
+ }
219
232
  };
233
+ var farmsTool = new FarmsTool();
220
234
 
221
235
  // src/utils/amm.ts
222
236
  var calculatePriceImpact = (inputAmount, outputAmount, poolReserveIn, poolReserveOut) => {
@@ -266,25 +280,6 @@ var accountIdToSolidityAddress = (accountId) => {
266
280
  var contractIdFromString = (contractId) => {
267
281
  return ContractId.fromString(contractId);
268
282
  };
269
- var finalizeTransaction = async (transaction, client, context, extra) => {
270
- const mode = context.mode;
271
- if (mode === AgentMode.RETURN_BYTES || mode === "returnBytes" || mode === "RETURN_BYTES") {
272
- const txBytes = await transaction.toBytes();
273
- return {
274
- success: true,
275
- transactionBytes: Buffer.from(txBytes).toString("base64"),
276
- ...extra
277
- };
278
- }
279
- const response = await transaction.execute(client);
280
- const receipt = await response.getReceipt(client);
281
- return {
282
- success: true,
283
- transactionId: response.transactionId?.toString(),
284
- status: receipt.status.toString(),
285
- ...extra
286
- };
287
- };
288
283
 
289
284
  // src/utils/units.ts
290
285
  var parseUnits = (amount, decimals) => {
@@ -323,6 +318,9 @@ var removeLiquidityInputSchema = z.object({
323
318
  minAmountA: z.string().describe("Minimum amount of tokenA to receive"),
324
319
  minAmountB: z.string().describe("Minimum amount of tokenB to receive")
325
320
  });
321
+ var isLiquidityCorePayload = (value) => typeof value === "object" && value !== null && "transaction" in value;
322
+ var addLiquidityPostProcess = (response) => `SaucerSwap add liquidity submitted. Status: ${response.status}. Transaction ID: ${response.transactionId}`;
323
+ var removeLiquidityPostProcess = (response) => `SaucerSwap remove liquidity submitted. Status: ${response.status}. Transaction ID: ${response.transactionId}`;
326
324
  var resolveDeadline = (defaultMinutes) => {
327
325
  return Math.floor(Date.now() / 1e3) + defaultMinutes * 60;
328
326
  };
@@ -333,13 +331,18 @@ var resolveRouterContract = (config) => {
333
331
  }
334
332
  return routerContractId;
335
333
  };
336
- var addLiquidityTool = {
337
- method: "saucerswap_add_liquidity",
338
- name: "SaucerSwap Add Liquidity",
339
- description: "Add liquidity to a SaucerSwap pool.",
340
- parameters: addLiquidityInputSchema,
341
- execute: async (client, context, params) => {
342
- const args = addLiquidityInputSchema.parse(params);
334
+ var AddLiquidityTool = class extends BaseTool {
335
+ constructor() {
336
+ super(...arguments);
337
+ this.method = "saucerswap_add_liquidity";
338
+ this.name = "SaucerSwap Add Liquidity";
339
+ this.description = "Add liquidity to a SaucerSwap pool.";
340
+ this.parameters = addLiquidityInputSchema;
341
+ }
342
+ async normalizeParams(params, _context, _client) {
343
+ return addLiquidityInputSchema.parse(params);
344
+ }
345
+ async coreAction(args, context, client) {
343
346
  const config = resolveSaucerSwapConfig(context);
344
347
  const operatorAccountId = client?.operatorAccountId?.toString();
345
348
  const slippageTolerance = args.slippageTolerance ?? 0.5;
@@ -370,14 +373,13 @@ var addLiquidityTool = {
370
373
  const routerContractId = resolveRouterContract(config);
371
374
  const deadline = resolveDeadline(config.deadlineMinutes);
372
375
  const toAddress = accountIdToSolidityAddress(operatorAccountId);
373
- const params2 = new ContractFunctionParameters().addAddress(tokenIdToSolidityAddress(requireTokenId(tokenAId))).addAddress(tokenIdToSolidityAddress(requireTokenId(tokenBId))).addUint256(amountADesired).addUint256(amountBDesired).addUint256(amountAMin).addUint256(amountBMin).addAddress(toAddress).addUint256(deadline);
374
- const transaction = new ContractExecuteTransaction().setContractId(contractIdFromString(routerContractId)).setGas(config.gasLimit).setFunction("addLiquidity", params2);
375
- return await finalizeTransaction(transaction, client, context, {
376
- amountADesired,
377
- amountBDesired,
378
- amountAMin,
379
- amountBMin
380
- });
376
+ const params = new ContractFunctionParameters().addAddress(tokenIdToSolidityAddress(requireTokenId(tokenAId))).addAddress(tokenIdToSolidityAddress(requireTokenId(tokenBId))).addUint256(amountADesired).addUint256(amountBDesired).addUint256(amountAMin).addUint256(amountBMin).addAddress(toAddress).addUint256(deadline);
377
+ const transaction = new ContractExecuteTransaction().setContractId(contractIdFromString(routerContractId)).setGas(config.gasLimit).setFunction("addLiquidity", params);
378
+ const payload = {
379
+ transaction,
380
+ extras: { amountADesired, amountBDesired, amountAMin, amountBMin }
381
+ };
382
+ return payload;
381
383
  } catch (error) {
382
384
  return {
383
385
  success: false,
@@ -385,14 +387,31 @@ var addLiquidityTool = {
385
387
  };
386
388
  }
387
389
  }
390
+ async shouldSecondaryAction(coreActionResult, _context) {
391
+ return isLiquidityCorePayload(coreActionResult);
392
+ }
393
+ async secondaryAction(payload, client, context) {
394
+ const result = await handleTransaction(
395
+ payload.transaction,
396
+ client,
397
+ context,
398
+ addLiquidityPostProcess
399
+ );
400
+ return { ...result, ...payload.extras };
401
+ }
388
402
  };
389
- var removeLiquidityTool = {
390
- method: "saucerswap_remove_liquidity",
391
- name: "SaucerSwap Remove Liquidity",
392
- description: "Remove liquidity from a SaucerSwap pool.",
393
- parameters: removeLiquidityInputSchema,
394
- execute: async (client, context, params) => {
395
- const args = removeLiquidityInputSchema.parse(params);
403
+ var RemoveLiquidityTool = class extends BaseTool {
404
+ constructor() {
405
+ super(...arguments);
406
+ this.method = "saucerswap_remove_liquidity";
407
+ this.name = "SaucerSwap Remove Liquidity";
408
+ this.description = "Remove liquidity from a SaucerSwap pool.";
409
+ this.parameters = removeLiquidityInputSchema;
410
+ }
411
+ async normalizeParams(params, _context, _client) {
412
+ return removeLiquidityInputSchema.parse(params);
413
+ }
414
+ async coreAction(args, context, client) {
396
415
  const config = resolveSaucerSwapConfig(context);
397
416
  const operatorAccountId = client?.operatorAccountId?.toString();
398
417
  if (!operatorAccountId) {
@@ -428,13 +447,13 @@ var removeLiquidityTool = {
428
447
  const routerContractId = resolveRouterContract(config);
429
448
  const deadline = resolveDeadline(config.deadlineMinutes);
430
449
  const toAddress = accountIdToSolidityAddress(operatorAccountId);
431
- const params2 = new ContractFunctionParameters().addAddress(tokenIdToSolidityAddress(requireTokenId(tokenAId))).addAddress(tokenIdToSolidityAddress(requireTokenId(tokenBId))).addUint256(lpAmount).addUint256(minAmountA).addUint256(minAmountB).addAddress(toAddress).addUint256(deadline);
432
- const transaction = new ContractExecuteTransaction().setContractId(contractIdFromString(routerContractId)).setGas(config.gasLimit).setFunction("removeLiquidity", params2);
433
- return await finalizeTransaction(transaction, client, context, {
434
- lpAmount,
435
- minAmountA,
436
- minAmountB
437
- });
450
+ const params = new ContractFunctionParameters().addAddress(tokenIdToSolidityAddress(requireTokenId(tokenAId))).addAddress(tokenIdToSolidityAddress(requireTokenId(tokenBId))).addUint256(lpAmount).addUint256(minAmountA).addUint256(minAmountB).addAddress(toAddress).addUint256(deadline);
451
+ const transaction = new ContractExecuteTransaction().setContractId(contractIdFromString(routerContractId)).setGas(config.gasLimit).setFunction("removeLiquidity", params);
452
+ const payload = {
453
+ transaction,
454
+ extras: { lpAmount, minAmountA, minAmountB }
455
+ };
456
+ return payload;
438
457
  } catch (error) {
439
458
  return {
440
459
  success: false,
@@ -442,20 +461,39 @@ var removeLiquidityTool = {
442
461
  };
443
462
  }
444
463
  }
464
+ async shouldSecondaryAction(coreActionResult, _context) {
465
+ return isLiquidityCorePayload(coreActionResult);
466
+ }
467
+ async secondaryAction(payload, client, context) {
468
+ const result = await handleTransaction(
469
+ payload.transaction,
470
+ client,
471
+ context,
472
+ removeLiquidityPostProcess
473
+ );
474
+ return { ...result, ...payload.extras };
475
+ }
445
476
  };
477
+ var addLiquidityTool = new AddLiquidityTool();
478
+ var removeLiquidityTool = new RemoveLiquidityTool();
446
479
  var poolsInputSchema = z.object({
447
480
  tokenA: z.string().optional().describe("First token ID or symbol"),
448
481
  tokenB: z.string().optional().describe("Second token ID or symbol"),
449
482
  version: z.enum(["v1", "v2"]).optional().describe("Pool version"),
450
483
  limit: z.number().int().positive().optional().describe("Maximum number of pools to return")
451
484
  });
452
- var poolsTool = {
453
- method: "saucerswap_get_pools",
454
- name: "SaucerSwap Get Pools",
455
- description: "Query SaucerSwap liquidity pools and reserves.",
456
- parameters: poolsInputSchema,
457
- execute: async (_client, context, params) => {
458
- const args = poolsInputSchema.parse(params);
485
+ var PoolsTool = class extends BaseTool {
486
+ constructor() {
487
+ super(...arguments);
488
+ this.method = "saucerswap_get_pools";
489
+ this.name = "SaucerSwap Get Pools";
490
+ this.description = "Query SaucerSwap liquidity pools and reserves.";
491
+ this.parameters = poolsInputSchema;
492
+ }
493
+ async normalizeParams(params, _context, _client) {
494
+ return poolsInputSchema.parse(params);
495
+ }
496
+ async coreAction(args, context, _client) {
459
497
  const config = resolveSaucerSwapConfig(context);
460
498
  const api = context.saucerswapClient ?? createSaucerSwapClient(config);
461
499
  try {
@@ -488,7 +526,14 @@ var poolsTool = {
488
526
  };
489
527
  }
490
528
  }
529
+ async shouldSecondaryAction(_coreActionResult, _context) {
530
+ return false;
531
+ }
532
+ async secondaryAction(_request, _client, _context) {
533
+ return null;
534
+ }
491
535
  };
536
+ var poolsTool = new PoolsTool();
492
537
 
493
538
  // src/utils/quote.ts
494
539
  var readAmount = (value) => {
@@ -522,16 +567,21 @@ var quoteInputSchema = z.object({
522
567
  amount: z.string().describe("Amount to swap (decimal format)"),
523
568
  slippageTolerance: z.number().optional().default(0.5).describe("Maximum slippage tolerance percentage")
524
569
  });
525
- var quoteTool = {
526
- method: "saucerswap_get_swap_quote",
527
- name: "SaucerSwap Get Swap Quote",
528
- description: "Get a price quote for swapping tokens on SaucerSwap.",
529
- parameters: quoteInputSchema,
530
- execute: async (_client, context, params) => {
531
- const args = quoteInputSchema.parse(params);
570
+ var QuoteTool = class extends BaseTool {
571
+ constructor() {
572
+ super(...arguments);
573
+ this.method = "saucerswap_get_swap_quote";
574
+ this.name = "SaucerSwap Get Swap Quote";
575
+ this.description = "Get a price quote for swapping tokens on SaucerSwap.";
576
+ this.parameters = quoteInputSchema;
577
+ }
578
+ async normalizeParams(params, _context, _client) {
579
+ return quoteInputSchema.parse(params);
580
+ }
581
+ async coreAction(args, context, _client) {
532
582
  const config = resolveSaucerSwapConfig(context);
533
- const client = context.saucerswapClient;
534
- const api = client ?? createSaucerSwapClient(config);
583
+ const cachedApi = context.saucerswapClient;
584
+ const api = cachedApi ?? createSaucerSwapClient(config);
535
585
  const slippageTolerance = args.slippageTolerance ?? 0.5;
536
586
  try {
537
587
  const fromToken = normalizeTokenAlias(args.fromToken, config);
@@ -577,7 +627,14 @@ var quoteTool = {
577
627
  };
578
628
  }
579
629
  }
630
+ async shouldSecondaryAction(_coreActionResult, _context) {
631
+ return false;
632
+ }
633
+ async secondaryAction(_request, _client, _context) {
634
+ return null;
635
+ }
580
636
  };
637
+ var quoteTool = new QuoteTool();
581
638
  var swapInputSchema = z.object({
582
639
  fromToken: z.string().describe("Token ID to swap from (e.g., 'HBAR' or '0.0.123456')"),
583
640
  toToken: z.string().describe("Token ID to swap to"),
@@ -585,6 +642,8 @@ var swapInputSchema = z.object({
585
642
  slippageTolerance: z.number().optional().default(0.5).describe("Maximum slippage tolerance percentage"),
586
643
  deadline: z.number().optional().describe("Transaction deadline in minutes from now or a unix timestamp")
587
644
  });
645
+ var isSwapCorePayload = (value) => typeof value === "object" && value !== null && "transaction" in value;
646
+ var swapPostProcess = (response) => `SaucerSwap swap submitted. Status: ${response.status}. Transaction ID: ${response.transactionId}`;
588
647
  var resolveDeadline2 = (deadlineInput, defaultMinutes) => {
589
648
  const now = Math.floor(Date.now() / 1e3);
590
649
  if (!deadlineInput) {
@@ -601,13 +660,18 @@ var amountToSmallest = (amount, decimals) => {
601
660
  var expectedToSmallest = (amount, decimals) => {
602
661
  return amount.includes(".") ? parseUnits(amount, decimals) : amount;
603
662
  };
604
- var swapTool = {
605
- method: "saucerswap_swap_tokens",
606
- name: "SaucerSwap Swap Tokens",
607
- description: "Execute a token swap on SaucerSwap DEX.",
608
- parameters: swapInputSchema,
609
- execute: async (client, context, params) => {
610
- const args = swapInputSchema.parse(params);
663
+ var SwapTool = class extends BaseTool {
664
+ constructor() {
665
+ super(...arguments);
666
+ this.method = "saucerswap_swap_tokens";
667
+ this.name = "SaucerSwap Swap Tokens";
668
+ this.description = "Execute a token swap on SaucerSwap DEX.";
669
+ this.parameters = swapInputSchema;
670
+ }
671
+ async normalizeParams(params, _context, _client) {
672
+ return swapInputSchema.parse(params);
673
+ }
674
+ async coreAction(args, context, client) {
611
675
  const config = resolveSaucerSwapConfig(context);
612
676
  const operatorAccountId = client?.operatorAccountId?.toString();
613
677
  const slippageTolerance = args.slippageTolerance ?? 0.5;
@@ -655,14 +719,18 @@ var swapTool = {
655
719
  tokenIdToSolidityAddress(requireTokenId(fromTokenId)),
656
720
  tokenIdToSolidityAddress(requireTokenId(toTokenId))
657
721
  ];
658
- const params2 = new ContractFunctionParameters().addUint256(amountInSmallest).addUint256(minOutSmallest).addAddressArray(path).addAddress(toAddress).addUint256(deadline);
659
- const transaction = new ContractExecuteTransaction().setContractId(contractIdFromString(routerContractId)).setGas(config.gasLimit).setFunction("swapExactTokensForTokens", params2);
660
- return await finalizeTransaction(transaction, client, context, {
661
- estimatedOutput: quote.expectedOutput,
662
- minOutput: minOutSmallest,
663
- priceImpact: quote.priceImpact,
664
- route: quote.route
665
- });
722
+ const params = new ContractFunctionParameters().addUint256(amountInSmallest).addUint256(minOutSmallest).addAddressArray(path).addAddress(toAddress).addUint256(deadline);
723
+ const transaction = new ContractExecuteTransaction().setContractId(contractIdFromString(routerContractId)).setGas(config.gasLimit).setFunction("swapExactTokensForTokens", params);
724
+ const payload = {
725
+ transaction,
726
+ extras: {
727
+ estimatedOutput: quote.expectedOutput,
728
+ minOutput: minOutSmallest,
729
+ priceImpact: quote.priceImpact,
730
+ route: quote.route
731
+ }
732
+ };
733
+ return payload;
666
734
  } catch (error) {
667
735
  return {
668
736
  success: false,
@@ -670,15 +738,36 @@ var swapTool = {
670
738
  };
671
739
  }
672
740
  }
741
+ async shouldSecondaryAction(coreActionResult, _context) {
742
+ return isSwapCorePayload(coreActionResult);
743
+ }
744
+ async secondaryAction(payload, client, context) {
745
+ const result = await handleTransaction(
746
+ payload.transaction,
747
+ client,
748
+ context,
749
+ swapPostProcess
750
+ );
751
+ return { ...result, ...payload.extras };
752
+ }
673
753
  };
754
+ var swapTool = new SwapTool();
674
755
 
675
756
  // src/index.ts
757
+ var saucerswapPluginToolNames = {
758
+ SAUCERSWAP_GET_SWAP_QUOTE_TOOL: "saucerswap_get_swap_quote",
759
+ SAUCERSWAP_SWAP_TOKENS_TOOL: "saucerswap_swap_tokens",
760
+ SAUCERSWAP_GET_POOLS_TOOL: "saucerswap_get_pools",
761
+ SAUCERSWAP_ADD_LIQUIDITY_TOOL: "saucerswap_add_liquidity",
762
+ SAUCERSWAP_REMOVE_LIQUIDITY_TOOL: "saucerswap_remove_liquidity",
763
+ SAUCERSWAP_GET_FARMS_TOOL: "saucerswap_get_farms"
764
+ };
676
765
  var saucerswapPlugin = {
677
766
  name: "saucerswap",
678
767
  description: "Integration with SaucerSwap DEX for token swaps, liquidity provision, and yield farming",
679
768
  tools: () => [swapTool, quoteTool, poolsTool, addLiquidityTool, removeLiquidityTool, farmsTool]
680
769
  };
681
770
 
682
- export { saucerswapPlugin as default, saucerswapPlugin };
771
+ export { saucerswapPlugin as default, saucerswapPlugin, saucerswapPluginToolNames };
683
772
  //# sourceMappingURL=index.js.map
684
773
  //# sourceMappingURL=index.js.map