pharos-agent-kit
Version:
Connect AI Agents to Pharos protocols
64 lines • 2.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSlugMatch = getSlugMatch;
exports.getProtocolTvl = getProtocolTvl;
const constants_1 = require("./constants");
const fuse_js_1 = __importDefault(require("fuse.js"));
const axios_1 = __importDefault(require("axios"));
/**
* Find the closest matching slug for a given ticker or name
* @param ticker - The ticker or name to match against available slugs
* @param category - The category to search in ("protocol" or "network")
* @returns The best matching slug or empty string if no match found
*/
function getSlugMatch(ticker, category) {
const slugList = category === "protocol"
? constants_1.DEFILLAMA_PROTOCOL_SLUGS
: constants_1.DEFILLAMA_NETWORK_SLUGS;
const fuse = new fuse_js_1.default(slugList, {
includeScore: true,
threshold: 0.3,
});
const fuseResults = fuse.search(ticker);
return fuseResults.slice(0, 4).map((result) => result.item);
}
/**
* Get Total Value Locked (TVL) for a specific protocol
* @param slug - The protocol slug identifier
* @returns Promise with TVL information as a formatted string
*/
async function getProtocolTvl(slug) {
const slugMatches = getSlugMatch(slug, "protocol");
if (slugMatches.length === 0) {
throw new Error(`No matching protocol slugs found. Here is the list of available protocol slugs: ${constants_1.DEFILLAMA_PROTOCOL_SLUGS}`);
}
let result = null;
let usedSlug = "";
let lastError = null;
for (const match of slugMatches) {
if (constants_1.DEFILLAMA_PROTOCOL_SLUGS.includes(match)) {
console.error("match", match);
try {
const apiUrl = `${constants_1.DEFILLAMA_BASE_URL}/tvl/${match}`;
const response = await axios_1.default.get(apiUrl);
if (response.data) {
result = response.data;
usedSlug = match;
break;
}
}
catch (e) {
lastError = e;
}
}
}
if (result === null) {
console.error("Error fetching protocol TVL:", lastError);
throw new Error("Failed to fetch TVL data for any matching protocol slug");
}
return `The TVL for ${usedSlug} is ${result} usd dollars`;
}
//# sourceMappingURL=get_protocol_tvl.js.map