@agentek/tools
Version:
Blockchain tools for AI agents
70 lines • 3.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLatestTokens = void 0;
const chains_1 = require("viem/chains");
const client_js_1 = require("../client.js");
const zod_1 = __importDefault(require("zod"));
const getLatestTokensParameters = zod_1.default.object({
chainId: zod_1.default.number().describe("Chain Id"),
});
// This helper maps numeric chain IDs to the corresponding Dexscreener chain identifier.
// Modify the mappings below as needed.
const resolveChainId = (chainId) => {
switch (chainId) {
case 1:
return "ethereum";
case 8453: // example: Base chain id (update as needed)
return "base";
// Add more mappings for other chains as required.
default:
return "ethereum";
}
};
exports.getLatestTokens = (0, client_js_1.createTool)({
name: "getLatestTokens",
description: "Get trending tokens and market data",
parameters: getLatestTokensParameters,
supportedChains: [chains_1.mainnet, chains_1.base],
execute: async (_client, args) => {
// Resolve the Dexscreener chain identifier from the provided chainId.
const dexChain = resolveChainId(args.chainId);
// Fetch token profiles from Dexscreener.
const profileResponse = await fetch("https://api.dexscreener.com/token-profiles/latest/v1");
if (!profileResponse.ok) {
throw new Error(`Failed to fetch token profiles: ${profileResponse.statusText}`);
}
let profileData = await profileResponse.json();
// Filter the token profiles by the resolved chain identifier.
profileData = profileData.filter((token) => token.chainId.toLowerCase() === dexChain.toLowerCase());
// Build a comma-separated list of token addresses.
const tokenAddresses = profileData
.map((token) => token.tokenAddress)
.join(",");
// Build the pair data endpoint URL using the resolved chain.
const pairUrl = `https://api.dexscreener.com/tokens/v1/${dexChain}/${tokenAddresses}`;
const pairResponse = await fetch(pairUrl);
if (!pairResponse.ok) {
console.error(pairResponse);
throw new Error(`Failed to fetch pair data: ${pairResponse.statusText}`);
}
const pairData = await pairResponse.json();
// Map the token profiles and enrich them with their corresponding pair information.
return {
trending: profileData.map((token) => {
const pairInfo = pairData.pairs?.find((pair) => pair.baseToken.address.toLowerCase() ===
token.tokenAddress.toLowerCase());
return {
tokenAddress: token.tokenAddress,
description: token.description,
priceUSD: pairInfo?.priceUsd || "0",
volume24h: pairInfo?.volume?.h24 || "0",
priceChange24h: pairInfo?.priceChange?.h24 || "0",
};
}),
};
},
});
//# sourceMappingURL=tools.js.map