@coinbase/agentkit 0.10.2 → 0.10.3

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 (62) hide show
  1. package/README.md +42 -0
  2. package/dist/action-providers/cdp/cdpApiActionProvider.js +7 -30
  3. package/dist/action-providers/cdp/cdpApiActionProvider.test.js +2 -8
  4. package/dist/action-providers/cdp/faucetUtils.d.ts +38 -0
  5. package/dist/action-providers/cdp/faucetUtils.js +81 -0
  6. package/dist/action-providers/clanker/schemas.d.ts +4 -4
  7. package/dist/action-providers/enso/constants.d.ts +4 -0
  8. package/dist/action-providers/enso/constants.js +10 -0
  9. package/dist/action-providers/enso/ensoActionProvider.d.ts +34 -0
  10. package/dist/action-providers/enso/ensoActionProvider.js +125 -0
  11. package/dist/action-providers/enso/ensoActionProvider.test.d.ts +1 -0
  12. package/dist/action-providers/enso/ensoActionProvider.test.js +141 -0
  13. package/dist/action-providers/enso/index.d.ts +1 -0
  14. package/dist/action-providers/enso/index.js +17 -0
  15. package/dist/action-providers/enso/schemas.d.ts +23 -0
  16. package/dist/action-providers/enso/schemas.js +22 -0
  17. package/dist/action-providers/erc20/constants.d.ts +2 -0
  18. package/dist/action-providers/erc20/constants.js +2 -0
  19. package/dist/action-providers/erc20/erc20ActionProvider.d.ts +17 -1
  20. package/dist/action-providers/erc20/erc20ActionProvider.js +103 -1
  21. package/dist/action-providers/erc20/erc20ActionProvider.test.js +201 -0
  22. package/dist/action-providers/erc20/schemas.d.ts +29 -0
  23. package/dist/action-providers/erc20/schemas.js +34 -1
  24. package/dist/action-providers/index.d.ts +3 -1
  25. package/dist/action-providers/index.js +3 -1
  26. package/dist/action-providers/wallet/walletActionProvider.js +1 -1
  27. package/dist/action-providers/weth/constants.d.ts +0 -1
  28. package/dist/action-providers/weth/constants.js +1 -2
  29. package/dist/action-providers/weth/schemas.js +6 -2
  30. package/dist/action-providers/weth/wethActionProvider.d.ts +7 -0
  31. package/dist/action-providers/weth/wethActionProvider.js +57 -32
  32. package/dist/action-providers/weth/wethActionProvider.test.js +60 -11
  33. package/dist/action-providers/x402/utils.d.ts +4 -4
  34. package/dist/action-providers/x402/utils.js +42 -5
  35. package/dist/action-providers/x402/x402ActionProvider.d.ts +6 -6
  36. package/dist/action-providers/x402/x402ActionProvider.js +23 -8
  37. package/dist/action-providers/x402/x402ActionProvider.test.js +50 -6
  38. package/dist/action-providers/yelay/constants.d.ts +64 -0
  39. package/dist/action-providers/yelay/constants.js +137 -0
  40. package/dist/action-providers/yelay/index.d.ts +2 -0
  41. package/dist/action-providers/yelay/index.js +18 -0
  42. package/dist/action-providers/yelay/schemas.d.ts +47 -0
  43. package/dist/action-providers/yelay/schemas.js +59 -0
  44. package/dist/action-providers/yelay/types.d.ts +24 -0
  45. package/dist/action-providers/yelay/types.js +2 -0
  46. package/dist/action-providers/yelay/yelayActionProvider.d.ts +70 -0
  47. package/dist/action-providers/yelay/yelayActionProvider.js +329 -0
  48. package/dist/action-providers/yelay/yelayActionProvider.test.d.ts +1 -0
  49. package/dist/action-providers/yelay/yelayActionProvider.test.js +302 -0
  50. package/dist/wallet-providers/cdpSmartWalletProvider.js +1 -3
  51. package/dist/wallet-providers/cdpSolanaWalletProvider.d.ts +14 -0
  52. package/dist/wallet-providers/cdpSolanaWalletProvider.js +39 -3
  53. package/dist/wallet-providers/cdpSolanaWalletProvider.test.js +16 -0
  54. package/dist/wallet-providers/privySvmWalletProvider.d.ts +14 -0
  55. package/dist/wallet-providers/privySvmWalletProvider.js +17 -0
  56. package/dist/wallet-providers/privySvmWalletProvider.test.js +10 -0
  57. package/dist/wallet-providers/solanaKeypairWalletProvider.d.ts +14 -0
  58. package/dist/wallet-providers/solanaKeypairWalletProvider.js +17 -0
  59. package/dist/wallet-providers/svmWalletProvider.d.ts +34 -0
  60. package/dist/wallet-providers/svmWalletProvider.js +43 -0
  61. package/dist/wallet-providers/svmWalletProvider.test.js +10 -0
  62. package/package.json +3 -1
package/README.md CHANGED
@@ -313,6 +313,15 @@ const agent = createReactAgent({
313
313
  </table>
314
314
  </details>
315
315
  <details>
316
+ <summary><strong>Enso</strong></summary>
317
+ <table width="100%">
318
+ <tr>
319
+ <td width="200"><code>route</code></td>
320
+ <td width="768">Find and execute a route for entering or exiting any DeFi position or swapping any ERC20 tokens.</td>
321
+ </tr>
322
+ </table>
323
+ </details>
324
+ <details>
316
325
  <summary><strong>ERC20</strong></summary>
317
326
  <table width="100%">
318
327
  <tr>
@@ -323,6 +332,14 @@ const agent = createReactAgent({
323
332
  <td width="200"><code>transfer</code></td>
324
333
  <td width="768">Transfers a specified amount of ERC-20 tokens to a destination address.</td>
325
334
  </tr>
335
+ <tr>
336
+ <td width="200"><code>approve</code></td>
337
+ <td width="768">Approves a spender to transfer ERC-20 tokens on behalf of the wallet.</td>
338
+ </tr>
339
+ <tr>
340
+ <td width="200"><code>get_allowance</code></td>
341
+ <td width="768">Checks the allowance amount for a spender of an ERC-20 token.</td>
342
+ </tr>
326
343
  <tr>
327
344
  <td width="200"><code>get_erc20_token_address</code></td>
328
345
  <td width="768">Gets the contract address for frequently used ERC20 tokens on different networks by token symbol.</td>
@@ -650,6 +667,31 @@ it will return payment details that can be used on retry.</td>
650
667
  </table>
651
668
  </details>
652
669
  <details>
670
+ <summary><strong>Yelay</strong></summary>
671
+ <table width="100%">
672
+ <tr>
673
+ <td width="200"><code>get_vaults</code></td>
674
+ <td width="768">Fetches a list of available Yelay Vaults with their current APYs and contract addresses.</td>
675
+ </tr>
676
+ <tr>
677
+ <td width="200"><code>deposit</code></td>
678
+ <td width="768">Deposits assets into a specified Yelay Vault.</td>
679
+ </tr>
680
+ <tr>
681
+ <td width="200"><code>redeem</code></td>
682
+ <td width="768">Withdraws assets from a Yelay Vault.</td>
683
+ </tr>
684
+ <tr>
685
+ <td width="200"><code>claim</code></td>
686
+ <td width="768">Claims accumulated yield from a Yelay Vault.</td>
687
+ </tr>
688
+ <tr>
689
+ <td width="200"><code>get_balance</code></td>
690
+ <td width="768">Gets the user's balance and yield information for a specific vault.</td>
691
+ </tr>
692
+ </table>
693
+ </details>
694
+ <details>
653
695
  <summary><strong>ZeroX</strong></summary>
654
696
  <table width="100%">
655
697
  <tr>
@@ -12,10 +12,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.cdpApiActionProvider = exports.CdpApiActionProvider = void 0;
13
13
  const zod_1 = require("zod");
14
14
  const wallet_providers_1 = require("../../wallet-providers");
15
- const cdpShared_1 = require("../../wallet-providers/cdpShared");
16
15
  const actionDecorator_1 = require("../actionDecorator");
17
16
  const actionProvider_1 = require("../actionProvider");
18
17
  const schemas_1 = require("./schemas");
18
+ const faucetUtils_1 = require("./faucetUtils");
19
19
  /**
20
20
  * CdpApiActionProvider is an action provider for CDP API.
21
21
  *
@@ -47,35 +47,12 @@ class CdpApiActionProvider extends actionProvider_1.ActionProvider {
47
47
  async faucet(walletProvider, args) {
48
48
  const network = walletProvider.getNetwork();
49
49
  const networkId = network.networkId;
50
- if ((0, cdpShared_1.isWalletProviderWithClient)(walletProvider)) {
51
- if (network.protocolFamily === "evm") {
52
- if (networkId !== "base-sepolia" && networkId !== "ethereum-sepolia") {
53
- throw new Error("Faucet is only supported on 'base-sepolia' or 'ethereum-sepolia' evm networks.");
54
- }
55
- const faucetTx = await walletProvider.getClient().evm.requestFaucet({
56
- address: walletProvider.getAddress(),
57
- token: (args.assetId || "eth"),
58
- network: networkId,
59
- });
60
- return `Received ${args.assetId || "ETH"} from the faucet. Transaction hash: ${faucetTx.transactionHash}`;
61
- }
62
- else if (network.protocolFamily === "svm") {
63
- if (networkId !== "solana-devnet") {
64
- throw new Error("Faucet is only supported on 'solana-devnet' solana networks.");
65
- }
66
- const faucetTx = await walletProvider.getClient().solana.requestFaucet({
67
- address: walletProvider.getAddress(),
68
- token: (args.assetId || "sol"),
69
- });
70
- return `Received ${args.assetId || "SOL"} from the faucet. Transaction signature hash: ${faucetTx.signature}`;
71
- }
72
- else {
73
- throw new Error("Faucet is only supported on Ethereum and Solana protocol families.");
74
- }
75
- }
76
- else {
77
- throw new Error("Wallet provider is not a CDP Wallet Provider.");
78
- }
50
+ const address = walletProvider.getAddress();
51
+ const cdpClient = (0, faucetUtils_1.getCdpClient)(walletProvider);
52
+ (0, faucetUtils_1.validateNetworkSupport)(network, networkId);
53
+ return network.protocolFamily === "evm"
54
+ ? (0, faucetUtils_1.handleEvmFaucet)(cdpClient, address, networkId, args)
55
+ : (0, faucetUtils_1.handleSvmFaucet)(cdpClient, address, args);
79
56
  }
80
57
  }
81
58
  exports.CdpApiActionProvider = CdpApiActionProvider;
@@ -72,19 +72,13 @@ describe("CDP API Action Provider", () => {
72
72
  const mockNetwork = { protocolFamily: "evm", networkId: "ethereum-mainnet" };
73
73
  mockWalletProvider.getNetwork.mockReturnValue(mockNetwork);
74
74
  mockWalletProvider.getClient.mockReturnValue(mockCdpClient);
75
- await expect(actionProvider.faucet(mockWalletProvider, { assetId: "eth" })).rejects.toThrow("Faucet is only supported on 'base-sepolia' or 'ethereum-sepolia' evm networks");
75
+ await expect(actionProvider.faucet(mockWalletProvider, { assetId: "eth" })).rejects.toThrow("Faucet is only supported on base-sepolia or ethereum-sepolia evm networks");
76
76
  });
77
77
  it("should throw error for unsupported Solana network", async () => {
78
78
  const mockNetwork = { protocolFamily: "svm", networkId: "solana-mainnet" };
79
79
  mockWalletProvider.getNetwork.mockReturnValue(mockNetwork);
80
80
  mockWalletProvider.getClient.mockReturnValue(mockCdpClient);
81
- await expect(actionProvider.faucet(mockWalletProvider, { assetId: "sol" })).rejects.toThrow("Faucet is only supported on 'solana-devnet' solana networks");
82
- });
83
- it("should throw error for wallet provider without client", async () => {
84
- const mockWalletWithoutClient = {
85
- getNetwork: jest.fn().mockReturnValue({ protocolFamily: "evm", networkId: "base-sepolia" }),
86
- };
87
- await expect(actionProvider.faucet(mockWalletWithoutClient, { assetId: "eth" })).rejects.toThrow("Wallet provider is not a CDP Wallet Provider");
81
+ await expect(actionProvider.faucet(mockWalletProvider, { assetId: "sol" })).rejects.toThrow("Faucet is only supported on solana-devnet svm networks");
88
82
  });
89
83
  });
90
84
  describe("RequestFaucetFundsV2Schema", () => {
@@ -0,0 +1,38 @@
1
+ import { z } from "zod";
2
+ import { CdpClient } from "@coinbase/cdp-sdk";
3
+ import { Network } from "../../network";
4
+ import { WalletProvider } from "../../wallet-providers";
5
+ import { RequestFaucetFundsV2Schema } from "./schemas";
6
+ /**
7
+ * Gets or creates a CDP client from the wallet provider or environment variables.
8
+ *
9
+ * @param walletProvider - The wallet provider to get the client from.
10
+ * @returns The CDP client.
11
+ */
12
+ export declare function getCdpClient(walletProvider: WalletProvider): CdpClient;
13
+ /**
14
+ * Validates that the network and protocol family are supported for faucet operations.
15
+ *
16
+ * @param network - The network to validate.
17
+ * @param networkId - The network ID to validate.
18
+ */
19
+ export declare function validateNetworkSupport(network: Network, networkId: string): void;
20
+ /**
21
+ * Handles faucet requests for EVM networks.
22
+ *
23
+ * @param cdpClient - The CDP client to use.
24
+ * @param address - The address to request funds from.
25
+ * @param networkId - The network ID to request funds from.
26
+ * @param args - The arguments to request funds from.
27
+ * @returns The transaction hash.
28
+ */
29
+ export declare function handleEvmFaucet(cdpClient: CdpClient, address: string, networkId: string, args: z.infer<typeof RequestFaucetFundsV2Schema>): Promise<string>;
30
+ /**
31
+ * Handles faucet requests for Solana networks.
32
+ *
33
+ * @param cdpClient - The CDP client to use.
34
+ * @param address - The address to request funds from.
35
+ * @param args - The arguments to request funds from.
36
+ * @returns The transaction hash.
37
+ */
38
+ export declare function handleSvmFaucet(cdpClient: CdpClient, address: string, args: z.infer<typeof RequestFaucetFundsV2Schema>): Promise<string>;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCdpClient = getCdpClient;
4
+ exports.validateNetworkSupport = validateNetworkSupport;
5
+ exports.handleEvmFaucet = handleEvmFaucet;
6
+ exports.handleSvmFaucet = handleSvmFaucet;
7
+ const cdp_sdk_1 = require("@coinbase/cdp-sdk");
8
+ const cdpShared_1 = require("../../wallet-providers/cdpShared");
9
+ /**
10
+ * Gets or creates a CDP client from the wallet provider or environment variables.
11
+ *
12
+ * @param walletProvider - The wallet provider to get the client from.
13
+ * @returns The CDP client.
14
+ */
15
+ function getCdpClient(walletProvider) {
16
+ if ((0, cdpShared_1.isWalletProviderWithClient)(walletProvider)) {
17
+ return walletProvider.getClient();
18
+ }
19
+ const apiKeyId = process.env.CDP_API_KEY_ID;
20
+ const apiKeySecret = process.env.CDP_API_KEY_SECRET;
21
+ if (!apiKeyId || !apiKeySecret) {
22
+ throw new Error("Faucet requires CDP_API_KEY_ID and CDP_API_KEY_SECRET environment variables to be set.");
23
+ }
24
+ return new cdp_sdk_1.CdpClient({ apiKeyId, apiKeySecret });
25
+ }
26
+ /**
27
+ * Validates that the network and protocol family are supported for faucet operations.
28
+ *
29
+ * @param network - The network to validate.
30
+ * @param networkId - The network ID to validate.
31
+ */
32
+ function validateNetworkSupport(network, networkId) {
33
+ const supportedProtocols = ["evm", "svm"];
34
+ if (!supportedProtocols.includes(network.protocolFamily)) {
35
+ throw new Error("Faucet is only supported on Ethereum and Solana protocol families.");
36
+ }
37
+ const supportedEvmNetworks = ["base-sepolia", "ethereum-sepolia"];
38
+ const supportedSvmNetworks = ["solana-devnet"];
39
+ const isEvmSupported = network.protocolFamily === "evm" && supportedEvmNetworks.includes(networkId);
40
+ const isSvmSupported = network.protocolFamily === "svm" && supportedSvmNetworks.includes(networkId);
41
+ if (!isEvmSupported && !isSvmSupported) {
42
+ const supportedNetworks = network.protocolFamily === "evm"
43
+ ? supportedEvmNetworks.join(" or ")
44
+ : supportedSvmNetworks.join(" or ");
45
+ throw new Error(`Faucet is only supported on ${supportedNetworks} ${network.protocolFamily} networks.`);
46
+ }
47
+ }
48
+ /**
49
+ * Handles faucet requests for EVM networks.
50
+ *
51
+ * @param cdpClient - The CDP client to use.
52
+ * @param address - The address to request funds from.
53
+ * @param networkId - The network ID to request funds from.
54
+ * @param args - The arguments to request funds from.
55
+ * @returns The transaction hash.
56
+ */
57
+ async function handleEvmFaucet(cdpClient, address, networkId, args) {
58
+ const token = (args.assetId || "eth");
59
+ const faucetTx = await cdpClient.evm.requestFaucet({
60
+ address,
61
+ token,
62
+ network: networkId,
63
+ });
64
+ return `Received ${args.assetId || "ETH"} from the faucet. Transaction hash: ${faucetTx.transactionHash}`;
65
+ }
66
+ /**
67
+ * Handles faucet requests for Solana networks.
68
+ *
69
+ * @param cdpClient - The CDP client to use.
70
+ * @param address - The address to request funds from.
71
+ * @param args - The arguments to request funds from.
72
+ * @returns The transaction hash.
73
+ */
74
+ async function handleSvmFaucet(cdpClient, address, args) {
75
+ const token = (args.assetId || "sol");
76
+ const faucetTx = await cdpClient.solana.requestFaucet({
77
+ address,
78
+ token,
79
+ });
80
+ return `Received ${args.assetId || "SOL"} from the faucet. Transaction signature hash: ${faucetTx.signature}`;
81
+ }
@@ -17,11 +17,11 @@ export declare const ClankTokenSchema: z.ZodObject<{
17
17
  platform: z.ZodString;
18
18
  url: z.ZodString;
19
19
  }, "strip", z.ZodTypeAny, {
20
- url: string;
21
20
  platform: string;
22
- }, {
23
21
  url: string;
22
+ }, {
24
23
  platform: string;
24
+ url: string;
25
25
  }>, "many">>;
26
26
  interface: z.ZodDefault<z.ZodString>;
27
27
  id: z.ZodDefault<z.ZodString>;
@@ -36,8 +36,8 @@ export declare const ClankTokenSchema: z.ZodObject<{
36
36
  interface: string;
37
37
  description?: string | undefined;
38
38
  socialMediaUrls?: {
39
- url: string;
40
39
  platform: string;
40
+ url: string;
41
41
  }[] | undefined;
42
42
  }, {
43
43
  tokenName: string;
@@ -49,8 +49,8 @@ export declare const ClankTokenSchema: z.ZodObject<{
49
49
  id?: string | undefined;
50
50
  description?: string | undefined;
51
51
  socialMediaUrls?: {
52
- url: string;
53
52
  platform: string;
53
+ url: string;
54
54
  }[] | undefined;
55
55
  interface?: string | undefined;
56
56
  }>;
@@ -0,0 +1,4 @@
1
+ export declare const ENSO_API_KEY: "1e02632d-6feb-4a75-a157-documentation";
2
+ export declare const ENSO_ETH: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
3
+ export declare const ENSO_ROUTE_SINGLE_SIG: "0xb94c3609";
4
+ export declare const ENSO_SUPPORTED_NETWORKS: Set<number>;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ENSO_SUPPORTED_NETWORKS = exports.ENSO_ROUTE_SINGLE_SIG = exports.ENSO_ETH = exports.ENSO_API_KEY = void 0;
4
+ exports.ENSO_API_KEY = "1e02632d-6feb-4a75-a157-documentation";
5
+ exports.ENSO_ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
6
+ exports.ENSO_ROUTE_SINGLE_SIG = "0xb94c3609";
7
+ exports.ENSO_SUPPORTED_NETWORKS = new Set([
8
+ 1, 10, 56, 100, 130, 137, 146, 480, 999, 1868, 8453, 42161, 43114, 57073, 59144, 80094, 98866,
9
+ 747474,
10
+ ]);
@@ -0,0 +1,34 @@
1
+ import { z } from "zod";
2
+ import { ActionProvider } from "../actionProvider";
3
+ import { Network } from "../../network";
4
+ import { EnsoActionProviderParams, EnsoRouteSchema } from "./schemas";
5
+ import { EvmWalletProvider } from "../../wallet-providers";
6
+ import { EnsoClient } from "@ensofinance/sdk";
7
+ /**
8
+ * EnsoActionProvider is an action provider for Enso.
9
+ */
10
+ export declare class EnsoActionProvider extends ActionProvider<EvmWalletProvider> {
11
+ readonly ensoClient: EnsoClient;
12
+ /**
13
+ * Constructor for the EnsoActionProvider
14
+ *
15
+ * @param params - The initialization parameters for the Enso action provider
16
+ */
17
+ constructor(params?: EnsoActionProviderParams);
18
+ /**
19
+ * Finds the optimal route from a token to a token and executes it.
20
+ *
21
+ * @param walletProvider - The wallet to execute the transaction from.
22
+ * @param args - The input arguments for the action.
23
+ * @returns A message containing the token route details.
24
+ */
25
+ route(walletProvider: EvmWalletProvider, args: z.infer<typeof EnsoRouteSchema>): Promise<string>;
26
+ /**
27
+ * Checks if the Enso action provider supports the given network.
28
+ *
29
+ * @param network - The network to check.
30
+ * @returns True if the Enso action provider supports the network, false otherwise.
31
+ */
32
+ supportsNetwork: (network: Network) => boolean;
33
+ }
34
+ export declare const ensoActionProvider: () => EnsoActionProvider;
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ensoActionProvider = exports.EnsoActionProvider = void 0;
13
+ const zod_1 = require("zod");
14
+ const actionProvider_1 = require("../actionProvider");
15
+ const actionDecorator_1 = require("../actionDecorator");
16
+ const schemas_1 = require("./schemas");
17
+ const viem_1 = require("viem");
18
+ const wallet_providers_1 = require("../../wallet-providers");
19
+ const constants_1 = require("./constants");
20
+ const sdk_1 = require("@ensofinance/sdk");
21
+ /**
22
+ * EnsoActionProvider is an action provider for Enso.
23
+ */
24
+ class EnsoActionProvider extends actionProvider_1.ActionProvider {
25
+ /**
26
+ * Constructor for the EnsoActionProvider
27
+ *
28
+ * @param params - The initialization parameters for the Enso action provider
29
+ */
30
+ constructor(params = {}) {
31
+ super("enso", []);
32
+ /**
33
+ * Checks if the Enso action provider supports the given network.
34
+ *
35
+ * @param network - The network to check.
36
+ * @returns True if the Enso action provider supports the network, false otherwise.
37
+ */
38
+ this.supportsNetwork = (network) => {
39
+ const chainIdCheck = network.chainId && constants_1.ENSO_SUPPORTED_NETWORKS.has(Number(network.chainId));
40
+ return Boolean(chainIdCheck);
41
+ };
42
+ this.ensoClient = new sdk_1.EnsoClient({ apiKey: params.apiKey || constants_1.ENSO_API_KEY });
43
+ }
44
+ /**
45
+ * Finds the optimal route from a token to a token and executes it.
46
+ *
47
+ * @param walletProvider - The wallet to execute the transaction from.
48
+ * @param args - The input arguments for the action.
49
+ * @returns A message containing the token route details.
50
+ */
51
+ async route(walletProvider, args) {
52
+ try {
53
+ const chainId = Number(walletProvider.getNetwork().chainId);
54
+ if (!chainId || !constants_1.ENSO_SUPPORTED_NETWORKS.has(chainId)) {
55
+ return `Network ${chainId} is not supported by Enso`;
56
+ }
57
+ const fromAddress = (0, viem_1.getAddress)(walletProvider.getAddress());
58
+ const tokenIn = (0, viem_1.getAddress)(args.tokenIn);
59
+ const tokenOut = (0, viem_1.getAddress)(args.tokenOut);
60
+ const tokenInResponse = await this.ensoClient.getTokenData({
61
+ address: tokenIn,
62
+ chainId,
63
+ });
64
+ if (tokenInResponse.data.length !== 1) {
65
+ throw `Could not find data for provided tokenIn`;
66
+ }
67
+ const tokenInData = tokenInResponse.data[0];
68
+ const amountIn = (0, viem_1.parseUnits)(args.amountIn, tokenInData.decimals).toString();
69
+ const params = {
70
+ chainId,
71
+ tokenIn: [tokenIn],
72
+ tokenOut: [tokenOut],
73
+ amountIn: [amountIn],
74
+ routingStrategy: "router",
75
+ fromAddress,
76
+ receiver: fromAddress,
77
+ spender: fromAddress,
78
+ };
79
+ if (args.slippage) {
80
+ params.slippage = args.slippage;
81
+ }
82
+ const routeData = await this.ensoClient.getRouteData(params);
83
+ if (!routeData.tx.data.startsWith(constants_1.ENSO_ROUTE_SINGLE_SIG)) {
84
+ return `Unsupported calldata returned from Enso API`;
85
+ }
86
+ // If the tokenIn is ERC20, do approve
87
+ if (args.tokenIn.toLowerCase() !== constants_1.ENSO_ETH.toLowerCase()) {
88
+ const approval = await this.ensoClient.getApprovalData({
89
+ chainId,
90
+ amount: amountIn,
91
+ fromAddress,
92
+ tokenAddress: (0, viem_1.getAddress)(args.tokenIn),
93
+ });
94
+ const hash = await walletProvider.sendTransaction({
95
+ to: approval.tx.to,
96
+ data: approval.tx.data,
97
+ });
98
+ await walletProvider.waitForTransactionReceipt(hash);
99
+ }
100
+ const hash = await walletProvider.sendTransaction({
101
+ to: routeData.tx.to,
102
+ value: BigInt(routeData.tx.value),
103
+ data: routeData.tx.data,
104
+ });
105
+ await walletProvider.waitForTransactionReceipt(hash);
106
+ return `Route executed successfully, transaction hash: ${hash}`;
107
+ }
108
+ catch (error) {
109
+ return `Error routing token through Enso: ${error}`;
110
+ }
111
+ }
112
+ }
113
+ exports.EnsoActionProvider = EnsoActionProvider;
114
+ __decorate([
115
+ (0, actionDecorator_1.CreateAction)({
116
+ name: "route",
117
+ description: `This tool will find the optimal route for entering or exiting any DeFi position or swapping any ERC20 tokens.`,
118
+ schema: schemas_1.EnsoRouteSchema,
119
+ }),
120
+ __metadata("design:type", Function),
121
+ __metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]),
122
+ __metadata("design:returntype", Promise)
123
+ ], EnsoActionProvider.prototype, "route", null);
124
+ const ensoActionProvider = () => new EnsoActionProvider();
125
+ exports.ensoActionProvider = ensoActionProvider;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const viem_1 = require("viem");
4
+ const ensoActionProvider_1 = require("./ensoActionProvider");
5
+ const schemas_1 = require("./schemas");
6
+ const MOCK_ADDRESS = "0x1234567890123456789012345678901234543210";
7
+ const ENSO_ROUTER_BASE = "0xF75584eF6673aD213a685a1B58Cc0330B8eA22Cf";
8
+ const WETH = "0x4200000000000000000000000000000000000006";
9
+ const USDC = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
10
+ const mockGetTokenData = jest.fn();
11
+ const mockGetRouteData = jest.fn();
12
+ const mockGetApprovalData = jest.fn();
13
+ jest.mock("@ensofinance/sdk", () => {
14
+ return {
15
+ EnsoClient: jest.fn().mockImplementation(() => {
16
+ return {
17
+ getTokenData: mockGetTokenData,
18
+ getRouteData: mockGetRouteData,
19
+ getApprovalData: mockGetApprovalData,
20
+ };
21
+ }),
22
+ };
23
+ });
24
+ describe("Enso Route Schema", () => {
25
+ it("should successfully parse valid input", () => {
26
+ const validInput = {
27
+ tokenIn: USDC,
28
+ tokenOut: WETH,
29
+ amountIn: "100",
30
+ slippage: 50,
31
+ };
32
+ const result = schemas_1.EnsoRouteSchema.safeParse(validInput);
33
+ expect(result.success).toBe(true);
34
+ expect(result.data).toEqual(validInput);
35
+ });
36
+ it("should fail parsing empty input", () => {
37
+ const emptyInput = {};
38
+ const result = schemas_1.EnsoRouteSchema.safeParse(emptyInput);
39
+ expect(result.success).toBe(false);
40
+ });
41
+ });
42
+ describe("Enso Route Action", () => {
43
+ let mockWallet;
44
+ const actionProvider = (0, ensoActionProvider_1.ensoActionProvider)();
45
+ const args = {
46
+ tokenIn: USDC,
47
+ tokenOut: WETH,
48
+ amountIn: "100",
49
+ };
50
+ beforeEach(async () => {
51
+ jest.clearAllMocks();
52
+ // Set up default mock responses for EnsoClient methods for successful scenarios
53
+ mockGetTokenData.mockResolvedValue({
54
+ data: [
55
+ {
56
+ address: USDC,
57
+ decimals: 6,
58
+ symbol: "USDC",
59
+ },
60
+ ],
61
+ });
62
+ mockGetRouteData.mockResolvedValue({
63
+ tx: {
64
+ to: ENSO_ROUTER_BASE,
65
+ value: "0",
66
+ data: "0xb94c3609000000000000000000000000f75584ef6673ad213a685a1b58cc0330b8ea22cf0000000000000000000000000000000000000000000000000000000005f5e100",
67
+ },
68
+ });
69
+ mockGetApprovalData.mockResolvedValue({
70
+ tx: {
71
+ to: USDC,
72
+ data: "0x095ea7b3000000000000000000000000f75584ef6673ad213a685a1b58cc0330b8ea22cf0000000000000000000000000000000000000000000000000000000005f5e100",
73
+ },
74
+ });
75
+ mockWallet = {
76
+ getAddress: jest.fn().mockReturnValue(MOCK_ADDRESS),
77
+ sendTransaction: jest.fn(),
78
+ waitForTransactionReceipt: jest.fn(),
79
+ getNetwork: jest.fn().mockReturnValue({ chainId: "8453" }),
80
+ };
81
+ });
82
+ it("should successfully respond", async () => {
83
+ const hash = "0x1234567890123456789012345678901234567890";
84
+ mockWallet.sendTransaction.mockResolvedValue(hash);
85
+ const response = await actionProvider.route(mockWallet, args);
86
+ // First transaction should be the approval
87
+ expect(mockWallet.sendTransaction).toHaveBeenNthCalledWith(1, // First call
88
+ expect.objectContaining({
89
+ to: (0, viem_1.getAddress)(args.tokenIn),
90
+ }));
91
+ // Second transaction should be the route execution
92
+ expect(mockWallet.sendTransaction).toHaveBeenNthCalledWith(2, // Second call
93
+ expect.objectContaining({
94
+ to: ENSO_ROUTER_BASE,
95
+ value: BigInt(0),
96
+ }));
97
+ expect(response).toContain(`Route executed successfully, transaction hash: ${hash}`);
98
+ });
99
+ it("should fail with an error", async () => {
100
+ const error = new Error("Failed to route through Enso");
101
+ mockWallet.sendTransaction.mockRejectedValue(error);
102
+ const response = await actionProvider.route(mockWallet, args);
103
+ // First transaction should be the approval
104
+ expect(mockWallet.sendTransaction).toHaveBeenNthCalledWith(1, // First call
105
+ expect.objectContaining({
106
+ to: (0, viem_1.getAddress)(args.tokenIn),
107
+ }));
108
+ expect(response).toContain(`Error routing token through Enso: ${error}`);
109
+ });
110
+ });
111
+ describe("supportsNetwork", () => {
112
+ const actionProvider = (0, ensoActionProvider_1.ensoActionProvider)();
113
+ it("should return false for base-mainnet (supported only with chainId)", () => {
114
+ const result = actionProvider.supportsNetwork({
115
+ protocolFamily: "evm",
116
+ networkId: "base-mainnet",
117
+ });
118
+ expect(result).toBe(false);
119
+ });
120
+ it("should return true for 8453 (base)", () => {
121
+ const result = actionProvider.supportsNetwork({
122
+ protocolFamily: "evm",
123
+ chainId: "8453",
124
+ });
125
+ expect(result).toBe(true);
126
+ });
127
+ it("should return false for base-sepolia", () => {
128
+ const result = actionProvider.supportsNetwork({
129
+ protocolFamily: "evm",
130
+ networkId: "base-sepolia",
131
+ });
132
+ expect(result).toBe(false);
133
+ });
134
+ it("should return true for 1 (mainnet)", () => {
135
+ const result = actionProvider.supportsNetwork({
136
+ protocolFamily: "evm",
137
+ chainId: "1",
138
+ });
139
+ expect(result).toBe(true);
140
+ });
141
+ });
@@ -0,0 +1 @@
1
+ export * from "./ensoActionProvider";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./ensoActionProvider"), exports);
@@ -0,0 +1,23 @@
1
+ import { z } from "zod";
2
+ export interface EnsoActionProviderParams {
3
+ apiKey?: string;
4
+ }
5
+ /**
6
+ * Input schema for route action.
7
+ */
8
+ export declare const EnsoRouteSchema: z.ZodObject<{
9
+ tokenIn: z.ZodString;
10
+ tokenOut: z.ZodString;
11
+ amountIn: z.ZodString;
12
+ slippage: z.ZodOptional<z.ZodNumber>;
13
+ }, "strip", z.ZodTypeAny, {
14
+ tokenIn: string;
15
+ tokenOut: string;
16
+ amountIn: string;
17
+ slippage?: number | undefined;
18
+ }, {
19
+ tokenIn: string;
20
+ tokenOut: string;
21
+ amountIn: string;
22
+ slippage?: number | undefined;
23
+ }>;