agentswap-sdk
v0.1.0
Published
AgentSwap Protocol SDK - TypeScript SDK for interacting with AgentSwap smart contracts
Maintainers
Readme
@agentswap/sdk
AgentSwap Protocol 官方 TypeScript SDK,用于与 AgentSwap 智能合约交互。
安装
npm install @agentswap/sdk
# 或
pnpm add @agentswap/sdk
# 或
yarn add @agentswap/sdk快速开始
import { AgentSwap } from '@agentswap/sdk';
// 创建实例(只读模式)
const sdk = new AgentSwap({
chain: 'base-sepolia',
});
// 创建实例(带钱包)
const sdk = new AgentSwap({
chain: 'base-sepolia',
privateKey: '0x...your-private-key',
oneInchApiKey: 'your-1inch-api-key', // 可选,用于 1inch 报价
});API 文档
Agent 注册
registerAgent(metadataURI)
注册新的 AI Agent,铸造 ERC-721 身份 NFT。
const { tokenId, txHash } = await sdk.registerAgent(
'https://example.com/agent-metadata.json'
);
console.log('Agent Token ID:', tokenId);getAgentInfo(tokenId)
获取 Agent 信息。
const info = await sdk.getAgentInfo(1n);
console.log('Owner:', info.owner);
console.log('Metadata URI:', info.metadataURI);
console.log('Is Active:', info.isActive);isRegisteredAgent(tokenId)
检查是否是已注册的 Agent。
const isRegistered = await sdk.isRegisteredAgent(1n);账户管理
createAccount(params)
为 Agent 创建 AgentAccount(AA 钱包)。
const { accountAddress, txHash } = await sdk.createAccount({
agentAddress: '0x...agent-address',
agentTokenId: 1n,
});
console.log('Account:', accountAddress);getAccountByAgent(tokenId)
获取 Agent 对应的账户地址。
const accountAddress = await sdk.getAccountByAgent(1n);Swap 交易
swap(params)
执行代币交换。
const { amountOut, txHash } = await sdk.swap({
agentAccount: '0x...account-address',
agentTokenId: 1n,
tokenIn: '0x...USDC',
tokenOut: '0x...WETH',
amountIn: 1000000n, // 1 USDC (6 decimals)
minAmountOut: 0n, // 或指定最小输出
slippageBps: 50, // 0.5% 滑点
routerType: 0, // 0: Auto, 1: 1inch, 2: Uniswap V3
});get1inchQuote(params)
获取 1inch 报价(需要配置 API Key)。
const quote = await sdk.get1inchQuote({
tokenIn: '0x...USDC',
tokenOut: '0x...WETH',
amountIn: 1000000n,
});
console.log('Expected output:', quote.amountOut);策略管理
createStrategy(params)
创建自动化交易策略。
// 限价单
const { strategyId, txHash } = await sdk.createStrategy({
type: 'LIMIT_ORDER',
agentAccount: '0x...account',
agentTokenId: 1n,
tokenIn: '0x...USDC',
tokenOut: '0x...WETH',
amountIn: 1000000000n, // 1000 USDC
targetPrice: 2500_00000000n, // $2500 (8 decimals)
slippageBps: 50,
});
// DCA(定投)
const { strategyId, txHash } = await sdk.createStrategy({
type: 'DCA',
agentAccount: '0x...account',
agentTokenId: 1n,
tokenIn: '0x...USDC',
tokenOut: '0x...WETH',
amountIn: 100000000n, // 每次 100 USDC
interval: 86400, // 每 24 小时
maxExecutions: 30, // 最多执行 30 次
slippageBps: 100,
});
// 止盈
const { strategyId, txHash } = await sdk.createStrategy({
type: 'TAKE_PROFIT',
agentAccount: '0x...account',
agentTokenId: 1n,
tokenIn: '0x...WETH',
tokenOut: '0x...USDC',
amountIn: 1000000000000000000n, // 1 ETH
targetPrice: 3000_00000000n, // 当价格达到 $3000 时卖出
slippageBps: 50,
});
// 止损
const { strategyId, txHash } = await sdk.createStrategy({
type: 'STOP_LOSS',
agentAccount: '0x...account',
agentTokenId: 1n,
tokenIn: '0x...WETH',
tokenOut: '0x...USDC',
amountIn: 1000000000000000000n, // 1 ETH
targetPrice: 2000_00000000n, // 当价格跌到 $2000 时卖出
slippageBps: 100,
});cancelStrategy(strategyId)
取消策略。
const { txHash } = await sdk.cancelStrategy(1n);getStrategy(strategyId)
获取策略详情。
const strategy = await sdk.getStrategy(1n);
console.log('Strategy Type:', strategy.strategyType);
console.log('Is Active:', strategy.isActive);
console.log('Executions:', strategy.executionCount, '/', strategy.maxExecutions);getUserStrategies(address)
获取用户的所有策略 ID。
const strategyIds = await sdk.getUserStrategies('0x...user-address');信誉系统
getReputation(tokenId)
获取 Agent 信誉分(0-100)。
const score = await sdk.getReputation(1n);
console.log('Reputation:', score);getReputationData(tokenId)
获取详细信誉数据。
const data = await sdk.getReputationData(1n);
console.log('Total Trades:', data.totalTrades);
console.log('Successful Trades:', data.successfulTrades);
console.log('Reviews:', data.reviewCount);submitReview(tokenId, score, comment)
提交 Agent 评价(1-5 分)。
const { txHash } = await sdk.submitReview(1n, 5, 'Great agent!');工具函数
approveToken(token, spender, amount)
授权代币。
const { txHash } = await sdk.approveToken(
'0x...USDC',
sdk.getAddresses().SwapRouter,
1000000000n
);getTokenBalance(token, address)
获取代币余额。
const balance = await sdk.getTokenBalance('0x...USDC', '0x...account');getAddresses()
获取当前链的合约地址。
const addresses = sdk.getAddresses();
console.log('SwapRouter:', addresses.SwapRouter);
console.log('StrategyEngine:', addresses.StrategyEngine);支持的链
| 链 | Chain ID | 配置名称 |
|----|----------|---------|
| Base Mainnet | 8453 | base |
| Base Sepolia | 84532 | base-sepolia |
| BSC Mainnet | 56 | bsc |
| BSC Testnet | 97 | bsc-testnet |
类型定义
interface AgentSwapConfig {
chain: 'base' | 'base-sepolia' | 'bsc' | 'bsc-testnet';
privateKey?: `0x${string}`;
rpcUrl?: string;
oneInchApiKey?: string;
}
interface AgentInfo {
owner: `0x${string}`;
metadataURI: string;
registeredAt: bigint;
isActive: boolean;
}
interface Strategy {
strategyType: number;
agentAccountAddress: `0x${string}`;
agentTokenId: bigint;
tokenIn: `0x${string}`;
tokenOut: `0x${string}`;
amountIn: bigint;
targetPrice: bigint;
interval: bigint;
lastExecuted: bigint;
maxExecutions: bigint;
executionCount: bigint;
slippageBps: bigint;
isActive: boolean;
creator: `0x${string}`;
}
interface SwapParams {
agentAccount: `0x${string}`;
agentTokenId: bigint;
tokenIn: `0x${string}`;
tokenOut: `0x${string}`;
amountIn: bigint;
minAmountOut?: bigint;
slippageBps?: number;
routerType?: number;
feeTier?: number;
routerData?: `0x${string}`;
}
interface CreateStrategyParams {
type: 'LIMIT_ORDER' | 'DCA' | 'TAKE_PROFIT' | 'STOP_LOSS';
agentAccount: `0x${string}`;
agentTokenId: bigint;
tokenIn: `0x${string}`;
tokenOut: `0x${string}`;
amountIn: bigint;
targetPrice?: bigint;
interval?: number;
maxExecutions?: number;
slippageBps?: number;
}许可证
MIT
