@accret/api-client
Version:
A comprehensive SDK for blockchain data access via Moralis, Alchemy, and Shyft APIs
258 lines • 14.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenPrice = getTokenPrice;
exports.getTokenMetadata = getTokenMetadata;
exports.getHistoricalTokenPrice = getHistoricalTokenPrice;
const types_1 = require("../types");
const token_price_1 = require("./token-price");
const supportedChain_1 = require("../utils/supportedChain");
const token_metadata_1 = require("../token/token-metadata");
const historical_price_for_tokens_1 = require("./historical-price-for-tokens");
/**
* @description Combines the logic for fetching the current price of tokens across EVM and Solana chains in bulk.
* @param addresses Array of token addresses with their respective chain IDs.
* @returns An object containing the token prices for each chain.
*/
async function getTokenPrice(addresses) {
const evmChainResults = await abstractEVMTokenPrice(addresses);
const solanaAddresses = addresses
.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.SOLANA_CHAIN)
.map(({ address }) => address)
.join(",");
const solanaChainResults = solanaAddresses
? [await (0, token_price_1.getSolanaTokenPrice)(solanaAddresses)]
: [];
return {
eth: evmChainResults.eth,
bnb: evmChainResults.bnb,
polygon: evmChainResults.polygon,
base: evmChainResults.base,
arbitrum: evmChainResults.arbitrum,
avalanche: evmChainResults.avalanche,
solana: solanaChainResults,
};
}
/**
* @description Combines the logic for fetching the metadata for tokens across EVM and Solana chains in bulk.
* @param addresses Array of token addresses with their respective chain IDs.
* @returns An object containing the token metadata for each chain.
*/
async function getTokenMetadata(addresses) {
const evmChainResults = await abstractEVMTokenMetadata(addresses);
const solanaAddresses = addresses
.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.SOLANA_CHAIN)
.map(({ address }) => address);
const solanaChainResults = solanaAddresses
? [
...(await Promise.all(solanaAddresses.map((address) => (0, token_metadata_1.getSolanaTokenMetadata)(address)))),
]
: [];
return {
eth: evmChainResults.eth,
bnb: evmChainResults.bnb,
polygon: evmChainResults.polygon,
base: evmChainResults.base,
arbitrum: evmChainResults.arbitrum,
avalanche: evmChainResults.avalanche,
solana: solanaChainResults,
};
}
/**
* @description Combines the logic for fetching the historical price of tokens across EVM and Solana chains in bulk.
* @param addresses Array of historical token price request parameters.
* @returns An object containing the historical token prices for each chain.
*/
async function getHistoricalTokenPrice(addresses) {
const evmChainResults = await abstractEVMTokenHistoricalPrice(addresses);
const solanaAddresses = addresses
.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.SOLANA_CHAIN)
.map((address) => address);
const solanaChainResults = solanaAddresses
? await Promise.all(solanaAddresses.map((address) => (0, historical_price_for_tokens_1.getHistoricalPriceTokensAddress)(address.address, (0, supportedChain_1.getChainIdentifiers)({ chain: types_1.AccretSupportedChain.SOLANA_CHAIN })
.alchemy, address.startTime, address.endTime, address.interval)))
: [];
return {
eth: evmChainResults.eth,
bnb: evmChainResults.bnb,
polygon: evmChainResults.polygon,
base: evmChainResults.base,
arbitrum: evmChainResults.arbitrum,
avalanche: evmChainResults.avalanche,
solana: solanaChainResults,
};
}
/**
* @description Abstracts the logic for fetching token metadata from EVM chains (Ethereum, BNB, Polygon, Base, Arbitrum, Avalanche).
* @param addresses Array of token addresses with their respective chain IDs.
* @returns An object containing the token metadata for each chain.
*/
async function abstractEVMTokenMetadata(addresses) {
const evmChainResults = {
eth: [],
bnb: [],
polygon: [],
base: [],
arbitrum: [],
avalanche: [],
};
const ethAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.ETHEREUM_CHAIN);
const ethChainResults = ethAddresses
? await Promise.all(ethAddresses.map(({ address, chainId }) => {
return (0, token_metadata_1.getEVMTokenMetadata)([address], (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.eth.push(...ethChainResults);
const bnbAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BNB_CHAIN);
const bnbChainResults = bnbAddresses
? await Promise.all(bnbAddresses.map(({ address, chainId }) => {
return (0, token_metadata_1.getEVMTokenMetadata)([address], (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.bnb.push(...bnbChainResults);
const polygonAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.POLYGON_CHAIN);
const polygonChainResults = polygonAddresses
? await Promise.all(polygonAddresses.map(({ address, chainId }) => {
return (0, token_metadata_1.getEVMTokenMetadata)([address], (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.polygon.push(...polygonChainResults);
const baseAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BASE_CHAIN);
const baseChainResults = baseAddresses
? await Promise.all(baseAddresses.map(({ address, chainId }) => {
return (0, token_metadata_1.getEVMTokenMetadata)([address], (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.base.push(...baseChainResults);
const arbitrumAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.ARBITRUM_CHAIN);
const arbitrumChainResults = arbitrumAddresses
? await Promise.all(arbitrumAddresses.map(({ address, chainId }) => {
return (0, token_metadata_1.getEVMTokenMetadata)([address], (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.arbitrum.push(...arbitrumChainResults);
const avalancheAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.AVALANCHE_CHAIN);
const avalancheChainResults = avalancheAddresses
? await Promise.all(avalancheAddresses.map(({ address, chainId }) => {
return (0, token_metadata_1.getEVMTokenMetadata)([address], (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.avalanche.push(...avalancheChainResults);
return evmChainResults;
}
/**
* @description Abstracts the logic for fetching historical token prices from EVM chains (Ethereum, BNB, Polygon, Base, Arbitrum, Avalanche)
* @param addresses Array of historical token price request parameters.
* @returns An object containing the historical token prices for each chain.
*/
async function abstractEVMTokenHistoricalPrice(addresses) {
const evmChainResults = {
eth: [],
bnb: [],
polygon: [],
base: [],
arbitrum: [],
avalanche: [],
};
const ethAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.ETHEREUM_CHAIN);
const ethChainResults = ethAddresses
? await Promise.all(ethAddresses.map(({ address, chainId, startTime, endTime, interval }) => {
return (0, historical_price_for_tokens_1.getHistoricalPriceTokensAddress)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).alchemy, startTime, endTime, interval);
}))
: [];
evmChainResults.eth.push(...ethChainResults);
const bnbAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BNB_CHAIN);
const bnbChainResults = bnbAddresses
? await Promise.all(bnbAddresses.map(({ address, chainId, startTime, endTime, interval }) => {
return (0, historical_price_for_tokens_1.getHistoricalPriceTokensAddress)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).alchemy, startTime, endTime, interval);
}))
: [];
evmChainResults.bnb.push(...bnbChainResults);
const polygonAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.POLYGON_CHAIN);
const polygonChainResults = polygonAddresses
? await Promise.all(polygonAddresses.map(({ address, chainId, startTime, endTime, interval }) => {
return (0, historical_price_for_tokens_1.getHistoricalPriceTokensAddress)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).alchemy, startTime, endTime, interval);
}))
: [];
evmChainResults.polygon.push(...polygonChainResults);
const baseAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BASE_CHAIN);
const baseChainResults = baseAddresses
? await Promise.all(baseAddresses.map(({ address, chainId, startTime, endTime, interval }) => {
return (0, historical_price_for_tokens_1.getHistoricalPriceTokensAddress)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).alchemy, startTime, endTime, interval);
}))
: [];
evmChainResults.base.push(...baseChainResults);
const arbitrumAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.ARBITRUM_CHAIN);
const arbitrumChainResults = arbitrumAddresses
? await Promise.all(arbitrumAddresses.map(({ address, chainId, startTime, endTime, interval }) => {
return (0, historical_price_for_tokens_1.getHistoricalPriceTokensAddress)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).alchemy, startTime, endTime, interval);
}))
: [];
evmChainResults.arbitrum.push(...arbitrumChainResults);
const avalancheAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.AVALANCHE_CHAIN);
const avalancheChainResults = avalancheAddresses
? await Promise.all(avalancheAddresses.map(({ address, chainId, startTime, endTime, interval }) => {
return (0, historical_price_for_tokens_1.getHistoricalPriceTokensAddress)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).alchemy, startTime, endTime, interval);
}))
: [];
evmChainResults.avalanche.push(...avalancheChainResults);
return evmChainResults;
}
/**
* @description This function abstracts the logic for fetching token prices from EVM chains (Ethereum, BNB, Polygon, Base, Arbitrum, Avalanche).
* @param addresses Array of token addresses with their respective chain IDs.
* @returns An object containing the token prices for each EVM chain.
*/
async function abstractEVMTokenPrice(addresses) {
const evmChainResults = {
eth: [],
bnb: [],
polygon: [],
base: [],
arbitrum: [],
avalanche: [],
};
const ethAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.ETHEREUM_CHAIN);
const ethChainResults = ethAddresses
? await Promise.all(ethAddresses.map(({ address, chainId }) => {
return (0, token_price_1.getEVMTokenPrice)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.eth.push(...ethChainResults);
const bnbAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BNB_CHAIN);
const bnbChainResults = bnbAddresses
? await Promise.all(bnbAddresses.map(({ address, chainId }) => {
return (0, token_price_1.getEVMTokenPrice)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.bnb.push(...bnbChainResults);
const polygonAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.POLYGON_CHAIN);
const polygonChainResults = polygonAddresses
? await Promise.all(polygonAddresses.map(({ address, chainId }) => {
return (0, token_price_1.getEVMTokenPrice)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.polygon.push(...polygonChainResults);
const baseAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BASE_CHAIN);
const baseChainResults = baseAddresses
? await Promise.all(baseAddresses.map(({ address, chainId }) => {
return (0, token_price_1.getEVMTokenPrice)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.base.push(...baseChainResults);
const arbitrumAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.ARBITRUM_CHAIN);
const arbitrumChainResults = arbitrumAddresses
? await Promise.all(arbitrumAddresses.map(({ address, chainId }) => {
return (0, token_price_1.getEVMTokenPrice)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.arbitrum.push(...arbitrumChainResults);
const avalancheAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.AVALANCHE_CHAIN);
const avalancheChainResults = avalancheAddresses
? await Promise.all(avalancheAddresses.map(({ address, chainId }) => {
return (0, token_price_1.getEVMTokenPrice)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
}))
: [];
evmChainResults.avalanche.push(...avalancheChainResults);
return evmChainResults;
}
//# sourceMappingURL=index.js.map