kiban-agent-kit
Version:
Open-source framework connecting AI agents to Katana ecosystem protocols
184 lines (183 loc) • 7.08 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTokenTools = createTokenTools;
const viem_1 = require("viem");
const abi_1 = require("./abi");
function createTokenTools(publicClient, walletClient, chain) {
function ensureAddress(address) {
if (!(0, viem_1.isAddress)(address)) {
throw new Error(`Invalid address: ${address}`);
}
return address;
}
return {
async checkToken(tokenAddressOrSymbol) {
if (!walletClient.account) {
throw new Error("No wallet connected");
}
// Validate/convert address
if (!(0, viem_1.isAddress)(tokenAddressOrSymbol)) {
throw new Error("Token address lookup not implemented yet");
}
const address = tokenAddressOrSymbol;
// Get token data
const [name, symbol, decimals, balanceRaw] = await Promise.all([
publicClient.readContract({
address,
abi: abi_1.ERC20_ABI,
functionName: "name",
}),
publicClient.readContract({
address,
abi: abi_1.ERC20_ABI,
functionName: "symbol",
}),
publicClient.readContract({
address,
abi: abi_1.ERC20_ABI,
functionName: "decimals",
}),
publicClient.readContract({
address,
abi: abi_1.ERC20_ABI,
functionName: "balanceOf",
args: [walletClient.account.address],
}),
]);
return {
address,
name,
symbol,
decimals,
balanceRaw,
balance: (0, viem_1.formatUnits)(balanceRaw, decimals),
};
},
async sendTokens(params) {
if (!walletClient.account) {
throw new Error("No wallet connected");
}
// Handle native token (ETH) transfer
if (params.token.toLowerCase() === "eth") {
return walletClient.sendTransaction({
account: walletClient.account,
chain,
to: ensureAddress(params.to),
value: (0, viem_1.parseUnits)(params.amount, 18),
});
}
// Handle ERC20 transfer
if (!(0, viem_1.isAddress)(params.token)) {
throw new Error("Token address lookup not implemented yet");
}
const tokenAddress = params.token;
const token = await this.checkToken(params.token);
const { request } = await publicClient.simulateContract({
account: walletClient.account.address,
address: tokenAddress,
abi: abi_1.ERC20_ABI,
functionName: "transfer",
args: [
ensureAddress(params.to),
(0, viem_1.parseUnits)(params.amount, token.decimals),
],
chain,
});
return walletClient.writeContract(request);
},
async approveSpending(params) {
if (!walletClient.account) {
throw new Error("No wallet connected");
}
if (!(0, viem_1.isAddress)(params.token)) {
throw new Error("Token address lookup not implemented yet");
}
const tokenAddress = params.token;
const token = await this.checkToken(params.token);
const { request } = await publicClient.simulateContract({
account: walletClient.account.address,
address: tokenAddress,
abi: abi_1.ERC20_ABI,
functionName: "approve",
args: [
ensureAddress(params.spender),
(0, viem_1.parseUnits)(params.amount, token.decimals),
],
chain,
});
return walletClient.writeContract(request);
},
async getTokenMetadata(tokenAddress) {
const address = ensureAddress(tokenAddress);
const [name, symbol, decimals, totalSupply] = await Promise.all([
publicClient.readContract({
address,
abi: abi_1.ERC20_ABI,
functionName: "name",
}),
publicClient.readContract({
address,
abi: abi_1.ERC20_ABI,
functionName: "symbol",
}),
publicClient.readContract({
address,
abi: abi_1.ERC20_ABI,
functionName: "decimals",
}),
publicClient.readContract({
address,
abi: abi_1.ERC20_ABI,
functionName: "totalSupply",
}),
]);
return {
name,
symbol,
decimals,
totalSupply,
};
},
async getAllowance(params) {
const allowance = await publicClient.readContract({
address: ensureAddress(params.token),
abi: abi_1.ERC20_ABI,
functionName: "allowance",
args: [ensureAddress(params.owner), ensureAddress(params.spender)],
});
return allowance;
},
async waitForTransaction(hash) {
const receipt = await publicClient.waitForTransactionReceipt({
hash,
});
return {
hash: receipt.transactionHash,
wait: async () => {
return {
status: receipt.status === "success" ? "success" : "failure",
hash: receipt.transactionHash,
blockNumber: receipt.blockNumber,
gasUsed: receipt.gasUsed,
};
},
};
},
};
}
// Export the token service
__exportStar(require("./service"), exports);