diff --git a/web3/wallets.js b/web3/wallets.js index f3bf1412..e4cd9988 100644 --- a/web3/wallets.js +++ b/web3/wallets.js @@ -514,30 +514,56 @@ exports.getAllWallets = async (req, res) => { return res.status(401).end('Account not found') } + const getNetworkByToken = async (idCrypto) => { try { if ( - cache.get('networks') && - Date.now() - new Date(cache.get('networks').date).getTime() < 604800000 - ) { + cache.get('networks') ) { return cache.get('networks').data; } else { - const options = { - method: 'GET', - url: process.env.CMC_CRYPTO_DETAILS, - params: { - id: idCrypto, - }, - headers: { - 'X-CMC_PRO_API_KEY': process.env.CMCAPIKEY, - }, - }; + const idCryptoArray = idCrypto.split(','); + const batchSize = 1300; // Number of cryptos per batch + + // Split the idCryptoArray into batches + const batches = []; + for (let i = 0; i < idCryptoArray.length; i += batchSize) { + const batch = idCryptoArray.slice(i, i + batchSize).join(','); + batches.push(batch); + } + + const results = []; + + // Send requests for each batch concurrently using Promise.all + await Promise.all( + batches.map(async (batch) => { + const options = { + method: 'GET', + url: process.env.CMC_CRYPTO_DETAILS, + params: { + id: batch, + }, + headers: { + 'X-CMC_PRO_API_KEY': process.env.CMCAPIKEY, + }, + }; + + try { + const result = await rp.request(options); + results.push(result); + } catch (err) { + console.error('Error fetching data for batch:', err); + // Handle the error as needed, e.g., retry or skip + } + }) + ); - const result = await rp.request(options); - const networksContract = Object.values(result.data.data).map((innerObj) => ({ - symbol: innerObj.symbol, - contract_address: innerObj.contract_address, - })); + // Process the results + const networksContract = results.flatMap((result) => + Object.values(result.data.data).map((innerObj) => ({ + symbol: innerObj.symbol, + contract_address: innerObj.contract_address, + })) + ); const networks = { data: networksContract, date: Date.now() }; cache.put('networks', networks); @@ -548,6 +574,7 @@ const getNetworkByToken = async (idCrypto) => { throw new Error('Error fetching networks'); } }; + exports.getPrices = async () => { try { if ( @@ -562,7 +589,7 @@ exports.getPrices = async () => { url: process.env.CMC_URl, params: { start: '1', - limit: '1200', + limit: '5000', convert: 'USD', }, headers: { @@ -678,7 +705,6 @@ exports.getPrices = async () => { var finalMap = {} const idcrypto = priceMap.map((token) => token.id.toString()); - priceMap.forEach((token) => { finalMap[token.symbol] = { ...token, networkSupported: '' }; delete finalMap[token.symbol].symbol; @@ -1017,7 +1043,7 @@ exports.getNativeBalance = async (web3Instance, walletAddress, network) => { exports.getTronBalance = async (webTron, token, address, isTrx = false) => { try { if (isTrx) { - + let amount = await webTron.trx.getBalance(address) return amount.toString() }