tensaikit
Version:
An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.
94 lines (91 loc) • 4.74 kB
JavaScript
;
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sushiSwapExecuteOnlyActionProvider = exports.SushiSwapExecuteOnlyActionProvider = void 0;
const zod_1 = require("zod");
const walletProviders_1 = require("../../walletProviders");
const actionProvider_1 = require("../actionProvider");
const actionDecorator_1 = require("../actionDecorator");
const schemas_1 = require("./schemas");
const utlis_1 = require("./utlis");
const logic_1 = require("./logic");
const utils_1 = require("../../common/utils");
const errors_1 = require("../../common/errors");
/**
* SushiSwapExecuteOnlyActionProvider is a minimal action provider for SushiSwap,
* exposing only swap execution functionality using Viem-compatible wallet providers.
*
* Registered under action provider key: `sushi_swap.execute_only`
*/
class SushiSwapExecuteOnlyActionProvider extends actionProvider_1.ActionProvider {
/**
* Constructs the minimal SushiSwap provider and registers only the execute actions.
*/
constructor() {
super("sushi_swap.execute_only", []);
/**
* Checks if the SushiSwap action provider supports the given network.
*
* @param network - The network to validate.
* @returns `true` if the network uses the EVM protocol family, else `false`.
*/
this.supportsNetwork = (network) => network.protocolFamily === "evm" &&
utlis_1.SUSHISWAP_SUPPORTED_NETWORK.includes(network.networkId);
}
/**
* Executes a token swap on SushiSwap using Viem, with automatic approval if necessary.
*
* @param walletProvider - An instance of Viem-compatible WalletProvider
* @param args - Object containing tokenIn, tokenOut, amount, and optional maxSlippage
* @returns A formatted string containing the transaction hash and simulation result
* @throws If approval, quote generation, simulation, or transaction sending fails
*/
async executeSwap(walletProvider, args) {
try {
const { tokenIn, tokenOut, amount, maxSlippage = utlis_1.DEFAULT_MAX_SLIPPAGE, } = args;
const response = await (0, logic_1.prepareAndSendSwapTransaction)(walletProvider, {
tokenIn,
tokenOut,
amount,
maxSlippage,
});
return (0, utils_1.wrapAndStringify)("sushi_swap.execute_only.execute_swap", response);
}
catch (error) {
throw (0, errors_1.handleError)("Failed to execute swap", error);
}
}
}
exports.SushiSwapExecuteOnlyActionProvider = SushiSwapExecuteOnlyActionProvider;
__decorate([
(0, actionDecorator_1.CreateAction)({
name: "execute_swap",
description: `
Executes a token swap on SushiSwap using the Viem client.
Inputs:
- tokenIn: Address of the input token. Example: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE (native MATIC)
- tokenOut: Address of the output token. Example: 0xc2132D05D31c914a87C6611C10748AEb04B58e8F (USDT)
- amount: Amount of the input token to swap. Example: 3.5 (will be converted to base units using token decimals)
- maxSlippage: Optional maximum allowed slippage for the swap (e.g., 0.005 for 0.5%). Default: 0.005 (0.5%)
Note: This action will broadcast a transaction on-chain and may consume gas.
`,
schema: schemas_1.GetExecuteSwapSchema,
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [walletProviders_1.ViemWalletProvider, void 0]),
__metadata("design:returntype", Promise)
], SushiSwapExecuteOnlyActionProvider.prototype, "executeSwap", null);
/**
* Creates a new instance of SushiSwapExecuteOnlyActionProvider.
* @returns A minimal action provider focused solely on swap execution.
*/
const sushiSwapExecuteOnlyActionProvider = () => new SushiSwapExecuteOnlyActionProvider();
exports.sushiSwapExecuteOnlyActionProvider = sushiSwapExecuteOnlyActionProvider;