@agentek/tools
Version:
Blockchain tools for AI agents
105 lines • 4.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNFTMetadataTool = void 0;
const zod_1 = require("zod");
const client_js_1 = require("../client.js");
const utils_js_1 = require("../utils.js");
const constants_js_1 = require("./constants.js");
const viem_1 = require("viem");
exports.getNFTMetadataTool = (0, client_js_1.createTool)({
name: "getNFTMetadata",
description: "Gets metadata for an NFT token by contract address and token ID",
supportedChains: constants_js_1.supportedChains,
parameters: zod_1.z.object({
contractAddress: utils_js_1.addressSchema.describe("The NFT contract address"),
tokenId: zod_1.z.string().describe("The token ID of the NFT"),
chainId: zod_1.z.number().describe("The chain ID where the NFT exists"),
}),
execute: async (client, args) => {
const { contractAddress, tokenId, chainId } = args;
const publicClient = client.getPublicClient(chainId);
try {
// Get basic NFT information
const [name, symbol, tokenURI] = await Promise.all([
publicClient.readContract({
address: contractAddress,
abi: viem_1.erc721Abi,
functionName: "name",
}),
publicClient.readContract({
address: contractAddress,
abi: viem_1.erc721Abi,
functionName: "symbol",
}),
publicClient.readContract({
address: contractAddress,
abi: viem_1.erc721Abi,
functionName: "tokenURI",
args: [BigInt(tokenId)],
}),
]);
// Get owner of NFT
let owner = "";
try {
owner = await publicClient.readContract({
address: contractAddress,
abi: viem_1.erc721Abi,
functionName: "ownerOf",
args: [BigInt(tokenId)],
});
}
catch (error) {
return {
chain: chainId,
error: `Failed to fetch NFT owner: ${typeof error === "object" && error
? "shortMessage" in error
? error.shortMessage
: "message" in error
? error.message
: "Unknown error"
: String(error)}`,
};
}
// Fetch metadata from tokenURI if it exists
let metadata = null;
if (tokenURI) {
try {
// Handle both HTTP and IPFS URIs
const url = tokenURI.startsWith('ipfs://')
? `https://ipfs.io/ipfs/${tokenURI.slice(7)}`
: tokenURI;
const response = await fetch(url);
if (response.ok) {
metadata = await response.json();
}
}
catch (error) {
// Silently handle error but continue with other data
}
}
return (0, utils_js_1.clean)({
chain: chainId,
contractAddress,
tokenId,
name,
symbol,
owner,
tokenURI,
metadata,
});
}
catch (error) {
return {
chain: chainId,
error: `Failed to fetch NFT metadata: ${typeof error === "object" && error
? "shortMessage" in error
? error.shortMessage
: "message" in error
? error.message
: "Unknown error"
: String(error)}`,
};
}
},
});
//# sourceMappingURL=tools.js.map