@accret/api-client
Version:
A comprehensive SDK for blockchain data access via Moralis, Alchemy, and Shyft APIs
288 lines • 13.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokensForWallet = getTokensForWallet;
exports.getTransactionHistory = getTransactionHistory;
const types_1 = require("../types");
const portfolio_1 = require("../portfolio/portfolio");
const transaction_history_1 = require("../portfolio/transaction-history");
const supportedChain_1 = require("../utils/supportedChain");
const alchemy_sdk_1 = require("alchemy-sdk");
/**
* @description This function retrieves tokens for a given wallet address across multiple chains (EVM and Solana) in bulk.
* @description Uses the Moralis SDK for EVM chains and the Jupiter API for Solana.
* @param addresses An array of objects containing wallet addresses and their respective chain IDs.
* @returns An object containing the token balances for each chain.
*/
async function getTokensForWallet(addresses) {
try {
const evmChainResults = await abstractEVMTokensForWallet(addresses);
const solanaChainAddresses = addresses
.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.SOLANA_CHAIN)
.map(({ address }) => address);
const solanaChainResults = solanaChainAddresses
? await Promise.all(solanaChainAddresses.map(async (address) => {
const response = await (0, portfolio_1.getSolanaTokensForWallet)(address);
return {
address,
chainId: (0, supportedChain_1.getAccretSupportedChain)({
chain: alchemy_sdk_1.Network.SOLANA_MAINNET,
}),
tokens: [response],
};
}))
: [];
return {
eth: evmChainResults.eth,
bnb: evmChainResults.bnb,
polygon: evmChainResults.polygon,
base: evmChainResults.base,
arbitrum: evmChainResults.arbitrum,
avalanche: evmChainResults.avalanche,
solana: solanaChainResults,
};
}
catch (error) {
console.log("Error fetching tokens for wallet:", error);
return null;
}
}
/**
* @description This function retrieves the transaction history for a given wallet address across multiple chains (EVM and Solana) in bulk.
* @description Uses the Moralis SDK for EVM chains and the Jupiter API for Solana.
* @param addresses An array of objects containing wallet addresses and their respective chain IDs.
* @returns An array of transaction history objects for each wallet address.
*/
async function getTransactionHistory(addresses) {
try {
const evmChainResults = await abstractEVMTransactionHistory(addresses);
const solanaChainAddresses = addresses
.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.SOLANA_CHAIN)
.map(({ address }) => address);
const solanaChainResults = solanaChainAddresses
? await Promise.all(solanaChainAddresses.map((address) => {
const response = (0, transaction_history_1.getSolanaTransactionHistory)(address);
return {
address,
network: (0, supportedChain_1.getAccretSupportedChain)({
chain: alchemy_sdk_1.Network.SOLANA_MAINNET,
}),
...response,
};
}))
: [];
return {
eth: evmChainResults.eth,
bnb: evmChainResults.bnb,
polygon: evmChainResults.polygon,
base: evmChainResults.base,
arbitrum: evmChainResults.arbitrum,
avalanche: evmChainResults.avalanche,
solana: solanaChainResults,
};
}
catch (error) {
console.log("Error fetching transaction history:", error);
return null;
}
}
/**
* @description This function abstracts the retrieval of EVM tokens for multiple wallet addresses across different EVM chains.
* @param addresses An array of objects containing wallet addresses and their respective chain IDs.
* @returns An object containing the token balances for each chain.
*/
async function abstractEVMTokensForWallet(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(async ({ address, chainId }) => {
const response = await (0, portfolio_1.getEVMTokensForWallet)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
return {
address,
chainId: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
tokens: response,
};
}))
: [];
evmChainResults.eth.push(...ethChainResults);
const bnbAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BNB_CHAIN);
const bnbChainResults = bnbAddresses
? await Promise.all(bnbAddresses.map(async ({ address, chainId }) => {
const response = await (0, portfolio_1.getEVMTokensForWallet)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
return {
address,
chainId: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
tokens: response,
};
}))
: [];
evmChainResults.bnb.push(...bnbChainResults);
const polygonAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.POLYGON_CHAIN);
const polygonChainResults = polygonAddresses
? await Promise.all(polygonAddresses.map(async ({ address, chainId }) => {
const response = await (0, portfolio_1.getEVMTokensForWallet)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
return {
address,
chainId: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
tokens: response,
};
}))
: [];
evmChainResults.polygon.push(...polygonChainResults);
const baseAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BASE_CHAIN);
const baseChainResults = baseAddresses
? await Promise.all(baseAddresses.map(async ({ address, chainId }) => {
const response = await (0, portfolio_1.getEVMTokensForWallet)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
return {
address,
chainId: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
tokens: response,
};
}))
: [];
evmChainResults.base.push(...baseChainResults);
const arbitrumAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.ARBITRUM_CHAIN);
const arbitrumChainResults = arbitrumAddresses
? await Promise.all(arbitrumAddresses.map(async ({ address, chainId }) => {
const response = await (0, portfolio_1.getEVMTokensForWallet)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
return {
address,
chainId: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
tokens: response,
};
}))
: [];
evmChainResults.arbitrum.push(...arbitrumChainResults);
const avalancheAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.AVALANCHE_CHAIN);
const avalancheChainResults = avalancheAddresses
? await Promise.all(avalancheAddresses.map(async ({ address, chainId }) => {
const response = await (0, portfolio_1.getEVMTokensForWallet)(address, (0, supportedChain_1.getChainIdentifiers)({ chain: chainId }).moralis);
return {
address,
chainId: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
tokens: response,
};
}))
: [];
evmChainResults.avalanche.push(...avalancheChainResults);
return evmChainResults;
}
/**
* @description This function abstracts the retrieval of EVM transaction history for multiple wallet addresses across different EVM chains.
* @param addresses An array of objects containing wallet addresses and their respective chain IDs.
* @returns A promise that resolves to an object containing the transaction history for each address
*/
async function abstractEVMTransactionHistory(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(async ({ address, chainId }) => {
const response = await (0, transaction_history_1.getEVMTransactionHistory)({
address: address,
network: (0, supportedChain_1.getChainIdentifiers)({ chain: chainId })
.moralis,
});
return {
address,
network: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
history: response,
};
}))
: [];
evmChainResults.eth.push(...ethChainResults);
const bnbAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BNB_CHAIN);
const bnbChainResults = bnbAddresses
? await Promise.all(bnbAddresses.map(async ({ address, chainId }) => {
const response = await (0, transaction_history_1.getEVMTransactionHistory)({
address: address,
network: (0, supportedChain_1.getChainIdentifiers)({ chain: chainId })
.moralis,
});
return {
address,
network: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
history: response,
};
}))
: [];
evmChainResults.bnb.push(...bnbChainResults);
const polygonAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.POLYGON_CHAIN);
const polygonChainResults = polygonAddresses
? await Promise.all(polygonAddresses.map(async ({ address, chainId }) => {
const response = await (0, transaction_history_1.getEVMTransactionHistory)({
address: address,
network: (0, supportedChain_1.getChainIdentifiers)({ chain: chainId })
.moralis,
});
return {
address,
network: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
history: response,
};
}))
: [];
evmChainResults.polygon.push(...polygonChainResults);
const baseAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.BASE_CHAIN);
const baseChainResults = baseAddresses
? await Promise.all(baseAddresses.map(async ({ address, chainId }) => {
const response = await (0, transaction_history_1.getEVMTransactionHistory)({
address: address,
network: (0, supportedChain_1.getChainIdentifiers)({ chain: chainId })
.moralis,
});
return {
address,
network: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
history: response,
};
}))
: [];
evmChainResults.base.push(...baseChainResults);
const arbitrumAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.ARBITRUM_CHAIN);
const arbitrumChainResults = arbitrumAddresses
? await Promise.all(arbitrumAddresses.map(async ({ address, chainId }) => {
const response = await (0, transaction_history_1.getEVMTransactionHistory)({
address: address,
network: (0, supportedChain_1.getChainIdentifiers)({ chain: chainId })
.moralis,
});
return {
address,
network: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
history: response,
};
}))
: [];
evmChainResults.arbitrum.push(...arbitrumChainResults);
const avalancheAddresses = addresses.filter(({ chainId }) => chainId === types_1.AccretSupportedChain.AVALANCHE_CHAIN);
const avalancheChainResults = avalancheAddresses
? await Promise.all(avalancheAddresses.map(async ({ address, chainId }) => {
const response = await (0, transaction_history_1.getEVMTransactionHistory)({
address: address,
network: (0, supportedChain_1.getChainIdentifiers)({ chain: chainId })
.moralis,
});
return {
address,
network: (0, supportedChain_1.getAccretSupportedChain)({ chain: chainId }),
history: response,
};
}))
: [];
evmChainResults.avalanche.push(...avalancheChainResults);
return evmChainResults;
}
//# sourceMappingURL=index.js.map