UNPKG

@coinbase/agentkit

Version:

Coinbase AgentKit core primitives

176 lines (175 loc) 9.21 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); 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; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _CdpApiActionProvider_instances, _CdpApiActionProvider_getCdpSdkNetwork; Object.defineProperty(exports, "__esModule", { value: true }); exports.cdpApiActionProvider = exports.CdpApiActionProvider = void 0; const zod_1 = require("zod"); const wallet_providers_1 = require("../../wallet-providers"); const cdpShared_1 = require("../../wallet-providers/cdpShared"); const actionDecorator_1 = require("../actionDecorator"); const actionProvider_1 = require("../actionProvider"); const schemas_1 = require("./schemas"); /** * CdpApiActionProvider is an action provider for CDP API. * * This provider is used for any action that uses the CDP API, but does not require a CDP Wallet. */ class CdpApiActionProvider extends actionProvider_1.ActionProvider { /** * Constructor for the CdpApiActionProvider class. */ constructor() { super("cdp_api", []); _CdpApiActionProvider_instances.add(this); /** * Checks if the Cdp action provider supports the given network. * * NOTE: Network scoping is done at the action implementation level * * @param _ - The network to check. * @returns True if the Cdp action provider supports the network, false otherwise. */ this.supportsNetwork = (_) => true; } /** * Requests test tokens from the faucet for the default address in the wallet. * * @param walletProvider - The wallet provider to request funds from. * @param args - The input arguments for the action. * @returns A confirmation message with transaction details. */ async faucet(walletProvider, args) { const network = walletProvider.getNetwork(); const networkId = network.networkId; if ((0, cdpShared_1.isWalletProviderWithClient)(walletProvider)) { if (network.protocolFamily === "evm") { if (networkId !== "base-sepolia" && networkId !== "ethereum-sepolia") { throw new Error("Faucet is only supported on 'base-sepolia' or 'ethereum-sepolia' evm networks."); } const faucetTx = await walletProvider.getClient().evm.requestFaucet({ address: walletProvider.getAddress(), token: (args.assetId || "eth"), network: networkId, }); return `Received ${args.assetId || "ETH"} from the faucet. Transaction hash: ${faucetTx.transactionHash}`; } else if (network.protocolFamily === "svm") { if (networkId !== "solana-devnet") { throw new Error("Faucet is only supported on 'solana-devnet' solana networks."); } const faucetTx = await walletProvider.getClient().solana.requestFaucet({ address: walletProvider.getAddress(), token: (args.assetId || "sol"), }); return `Received ${args.assetId || "SOL"} from the faucet. Transaction signature hash: ${faucetTx.signature}`; } else { throw new Error("Faucet is only supported on Ethereum and Solana protocol families."); } } else { throw new Error("Wallet provider is not a CDP Wallet Provider."); } } /** * Swaps tokens using the CDP client. * * @param walletProvider - The wallet provider to perform the swap with. * @param args - The input arguments for the swap action. * @returns A confirmation message with transaction details. */ async swap(walletProvider, args) { const network = walletProvider.getNetwork(); const networkId = network.networkId; if ((0, cdpShared_1.isWalletProviderWithClient)(walletProvider)) { if (network.protocolFamily === "evm") { try { const cdpNetwork = __classPrivateFieldGet(this, _CdpApiActionProvider_instances, "m", _CdpApiActionProvider_getCdpSdkNetwork).call(this, networkId); // Get the account for the wallet address const account = await walletProvider.getClient().evm.getAccount({ address: walletProvider.getAddress(), }); // Execute swap using the all-in-one pattern const swapResult = await account.swap({ // eslint-disable-next-line @typescript-eslint/no-explicit-any network: cdpNetwork, from: args.fromAssetId, to: args.toAssetId, amount: args.amount, slippageBps: 100, // 1% slippage tolerance // eslint-disable-next-line @typescript-eslint/no-explicit-any }); return `Successfully swapped ${args.amount} ${args.fromAssetId.toUpperCase()} to ${args.toAssetId.toUpperCase()}. Transaction hash: ${swapResult.transactionHash}`; } catch (error) { throw new Error(`Swap failed: ${error}`); } } else { throw new Error("Swap is currently only supported on EVM networks."); } } else { throw new Error("Wallet provider is not a CDP Wallet Provider."); } } } exports.CdpApiActionProvider = CdpApiActionProvider; _CdpApiActionProvider_instances = new WeakSet(), _CdpApiActionProvider_getCdpSdkNetwork = function _CdpApiActionProvider_getCdpSdkNetwork(networkId) { switch (networkId) { case "base-sepolia": return "base-sepolia"; case "base-mainnet": return "base"; case "ethereum-sepolia": return "ethereum-sepolia"; case "ethereum-mainnet": return "ethereum"; default: return networkId; // For other networks, use as-is } }; __decorate([ (0, actionDecorator_1.CreateAction)({ name: "request_faucet_funds", description: `This tool will request test tokens from the faucet for the default address in the wallet. It takes the wallet and asset ID as input. Faucet is only allowed on 'base-sepolia' or 'solana-devnet'. If fauceting on 'base-sepolia', user can only provide asset ID 'eth', 'usdc', 'eurc' or 'cbbtc', if no asset ID is provided, the faucet will default to 'eth'. If fauceting on 'solana-devnet', user can only provide asset ID 'sol' or 'usdc', if no asset ID is provided, the faucet will default to 'sol'. You are not allowed to faucet with any other network or asset ID. If you are on another network, suggest that the user sends you some ETH from another wallet and provide the user with your wallet details.`, schema: schemas_1.RequestFaucetFundsV2Schema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [wallet_providers_1.WalletProvider, void 0]), __metadata("design:returntype", Promise) ], CdpApiActionProvider.prototype, "faucet", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "swap", description: `This tool swaps tokens using the CDP API. It takes the wallet, from asset ID, to asset ID, and amount as input. Swaps are currently supported on EVM networks like Base and Ethereum. Example usage: - Swap 0.1 ETH to USDC: { fromAssetId: "eth", toAssetId: "usdc", amount: "0.1" } - Swap 100 USDC to ETH: { fromAssetId: "usdc", toAssetId: "eth", amount: "100" }`, schema: schemas_1.SwapSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [wallet_providers_1.WalletProvider, void 0]), __metadata("design:returntype", Promise) ], CdpApiActionProvider.prototype, "swap", null); const cdpApiActionProvider = () => new CdpApiActionProvider(); exports.cdpApiActionProvider = cdpApiActionProvider;