pharos-agent-kit
Version:
Connect AI Agents to Pharos protocols
71 lines • 2.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenDataByAddress = getTokenDataByAddress;
exports.getTokenDataByTicker = getTokenDataByTicker;
exports.getTokenAddressFromTicker = getTokenAddressFromTicker;
/**
* Fetches token data from DexScreener using the token address.
*
* @param tokenAddress - The address of the token to fetch data for.
* @returns A promise that resolves to the token data as a JSON string.
* @throws An error if the token address is not provided or if the fetch operation fails.
*/
async function getTokenDataByAddress(tokenAddress) {
try {
if (!tokenAddress) {
throw new Error("Token is required for fetching token data");
}
const response = await fetch(`https://api.dexscreener.com/tokens/v1/movement/${tokenAddress}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
return await response.json();
}
catch (error) {
throw new Error(`Error fetching token data by address from DexScreener: ${error.message}`);
}
}
/**
* Fetches token data from DexScreener using the token ticker symbol.
*
* @param ticker - The ticker symbol of the token to fetch data for.
* @returns A promise that resolves to the token data as a JSON string.
* @throws An error if the token address cannot be found or if the fetch operation fails.
*/
async function getTokenDataByTicker(ticker) {
const address = await getTokenAddressFromTicker(ticker);
if (!address) {
throw new Error(`Token address not found for ticker: ${ticker}`);
}
return getTokenDataByAddress(address);
}
/**
* Retrieves the token address from DexScreener using the token ticker symbol.
*
* @param ticker - The ticker symbol of the token to find the address for.
* @returns A promise that resolves to the token address as a string, or null if not found.
* @throws An error if the fetch operation fails.
*/
async function getTokenAddressFromTicker(ticker) {
try {
const response = await fetch(`https://api.dexscreener.com/latest/dex/search?q=${ticker}`);
const data = await response.json();
if (!data.pairs || data.pairs.length === 0) {
return "";
}
// Filter for Movement pairs only and sort by FDV
let movementPairs = data.pairs
.filter((pair) => pair.chainId === "movement")
.sort((a, b) => (b.fdv || 0) - (a.fdv || 0));
movementPairs = movementPairs.filter((pair) => pair.baseToken.symbol.toLowerCase() === ticker.toLowerCase());
console.log("movementPairs", movementPairs);
// Return the address of the highest FDV Movement pair
return movementPairs[0].baseToken.address;
}
catch (error) {
throw new Error(`Error fetching token address based on ticker from DexScreener: ${error.message}`);
}
}
//# sourceMappingURL=get_token_data.js.map