From 1ca056b6649e5f716b27f90071d71eb565ef3e0d Mon Sep 17 00:00:00 2001 From: Sebastian Pape Date: Sun, 10 Apr 2022 12:17:45 +0200 Subject: [PATCH] v6.0.2: parallelize getting assets --- dist/esm/index.js | 47 ++++++++++++++++--------------- dist/umd/index.js | 47 ++++++++++++++++--------------- package.json | 2 +- src/dripAssets.js | 47 ++++++++++++++++--------------- tests/units/dripAssets.spec.js | 51 ++++++++++++++++++---------------- 5 files changed, 103 insertions(+), 91 deletions(-) diff --git a/dist/esm/index.js b/dist/esm/index.js index a4f7bc3..51aa6f5 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -62,14 +62,19 @@ const reduceAssetWithBalance = (asset, balance)=>{ }, { balance: balance.toString() }) }; +const exists = ({ assets, asset })=> { + return !!assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase()) +}; + var dripAssets = async (options) => { if(options === undefined) { options = { accounts: {}, priority: [] }; } let assets = []; + let promises = []; // Prioritized Assets - await Promise.all((options.priority || []).map((asset)=>{ + promises = promises.concat((options.priority || []).map((asset)=>{ return new Promise(async (resolve, reject)=>{ let token = new Token(asset); let completedAsset = Object.assign({}, @@ -82,6 +87,7 @@ var dripAssets = async (options) => { } ); if(completedAsset.balance != '0') { + if(exists({ assets, asset })) { return resolve() } assets.push(completedAsset); if(typeof options.drip == 'function') { options.drip(completedAsset); } resolve(completedAsset); @@ -99,13 +105,11 @@ var dripAssets = async (options) => { majorTokens.push(Object.assign({}, token, { blockchain })); }); } - await Promise.all(majorTokens.map((asset)=>{ + promises = promises.concat((majorTokens.map((asset)=>{ return new Promise((resolve, reject)=>{ - if(assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())) { - return resolve() // already part of assets - } new Token(asset).balance(options.accounts[asset.blockchain]) .then((balance)=>{ + if(exists({ assets, asset })) { return resolve() } const assetWithBalance = reduceAssetWithBalance(asset, balance); if(assetWithBalance.balance != '0') { assets.push(assetWithBalance); @@ -116,29 +120,28 @@ var dripAssets = async (options) => { } }).catch((error)=>{ console.log(error); }); }) - })); + }))); // All other assets let allAssets = await getAssets(options); - await Promise.all(allAssets.map((asset)=>{ + promises = promises.concat((allAssets.map((asset)=>{ return new Promise((resolve, reject)=>{ - if(assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())) { - resolve(); // already part of assets - } else { - return new Token(asset).balance(options.accounts[asset.blockchain]).then((balance)=>{ - const assetWithBalance = reduceAssetWithBalance(asset, balance); - if(assetWithBalance.balance != '0') { - assets.push(assetWithBalance); - if(typeof options.drip == 'function') { options.drip(assetWithBalance); } - resolve(assetWithBalance); - } else { - resolve(); - } - }) - } + return new Token(asset).balance(options.accounts[asset.blockchain]).then((balance)=>{ + if(exists({ assets, asset })) { return resolve() } + const assetWithBalance = reduceAssetWithBalance(asset, balance); + if(assetWithBalance.balance != '0') { + assets.push(assetWithBalance); + if(typeof options.drip == 'function') { options.drip(assetWithBalance); } + resolve(assetWithBalance); + } else { + resolve(); + } + }) }) - })); + }))); + + await Promise.all(promises); return assets }; diff --git a/dist/umd/index.js b/dist/umd/index.js index 50116f6..378e14e 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -63,14 +63,19 @@ }, { balance: balance.toString() }) }; + const exists = ({ assets, asset })=> { + return !!assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase()) + }; + var dripAssets = async (options) => { if(options === undefined) { options = { accounts: {}, priority: [] }; } let assets = []; + let promises = []; // Prioritized Assets - await Promise.all((options.priority || []).map((asset)=>{ + promises = promises.concat((options.priority || []).map((asset)=>{ return new Promise(async (resolve, reject)=>{ let token = new web3Tokens.Token(asset); let completedAsset = Object.assign({}, @@ -83,6 +88,7 @@ } ); if(completedAsset.balance != '0') { + if(exists({ assets, asset })) { return resolve() } assets.push(completedAsset); if(typeof options.drip == 'function') { options.drip(completedAsset); } resolve(completedAsset); @@ -100,13 +106,11 @@ majorTokens.push(Object.assign({}, token, { blockchain })); }); } - await Promise.all(majorTokens.map((asset)=>{ + promises = promises.concat((majorTokens.map((asset)=>{ return new Promise((resolve, reject)=>{ - if(assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())) { - return resolve() // already part of assets - } new web3Tokens.Token(asset).balance(options.accounts[asset.blockchain]) .then((balance)=>{ + if(exists({ assets, asset })) { return resolve() } const assetWithBalance = reduceAssetWithBalance(asset, balance); if(assetWithBalance.balance != '0') { assets.push(assetWithBalance); @@ -117,29 +121,28 @@ } }).catch((error)=>{ console.log(error); }); }) - })); + }))); // All other assets let allAssets = await getAssets(options); - await Promise.all(allAssets.map((asset)=>{ + promises = promises.concat((allAssets.map((asset)=>{ return new Promise((resolve, reject)=>{ - if(assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())) { - resolve(); // already part of assets - } else { - return new web3Tokens.Token(asset).balance(options.accounts[asset.blockchain]).then((balance)=>{ - const assetWithBalance = reduceAssetWithBalance(asset, balance); - if(assetWithBalance.balance != '0') { - assets.push(assetWithBalance); - if(typeof options.drip == 'function') { options.drip(assetWithBalance); } - resolve(assetWithBalance); - } else { - resolve(); - } - }) - } + return new web3Tokens.Token(asset).balance(options.accounts[asset.blockchain]).then((balance)=>{ + if(exists({ assets, asset })) { return resolve() } + const assetWithBalance = reduceAssetWithBalance(asset, balance); + if(assetWithBalance.balance != '0') { + assets.push(assetWithBalance); + if(typeof options.drip == 'function') { options.drip(assetWithBalance); } + resolve(assetWithBalance); + } else { + resolve(); + } + }) }) - })); + }))); + + await Promise.all(promises); return assets }; diff --git a/package.json b/package.json index 13133aa..f3731a6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-assets", "moduleName": "Web3Assets", - "version": "6.0.1", + "version": "6.0.2", "description": "", "main": "dist/umd/index.js", "module": "dist/esm/index.js", diff --git a/src/dripAssets.js b/src/dripAssets.js index 114486c..b8efe13 100644 --- a/src/dripAssets.js +++ b/src/dripAssets.js @@ -13,14 +13,19 @@ const reduceAssetWithBalance = (asset, balance)=>{ }, { balance: balance.toString() }) } +const exists = ({ assets, asset })=> { + return !!assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase()) +} + export default async (options) => { if(options === undefined) { options = { accounts: {}, priority: [] } } let assets = [] + let promises = [] // Prioritized Assets - await Promise.all((options.priority || []).map((asset)=>{ + promises = promises.concat((options.priority || []).map((asset)=>{ return new Promise(async (resolve, reject)=>{ let token = new Token(asset) let completedAsset = Object.assign({}, @@ -33,6 +38,7 @@ export default async (options) => { } ) if(completedAsset.balance != '0') { + if(exists({ assets, asset })) { return resolve() } assets.push(completedAsset) if(typeof options.drip == 'function') { options.drip(completedAsset) } resolve(completedAsset) @@ -50,14 +56,12 @@ export default async (options) => { majorTokens.push(Object.assign({}, token, { blockchain })) }) } - await Promise.all(majorTokens.map((asset)=>{ + promises = promises.concat((majorTokens.map((asset)=>{ return new Promise((resolve, reject)=>{ let requestOptions - if(assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())) { - return resolve() // already part of assets - } new Token(asset).balance(options.accounts[asset.blockchain]) .then((balance)=>{ + if(exists({ assets, asset })) { return resolve() } const assetWithBalance = reduceAssetWithBalance(asset, balance) if(assetWithBalance.balance != '0') { assets.push(assetWithBalance) @@ -68,29 +72,28 @@ export default async (options) => { } }).catch((error)=>{ console.log(error) }) }) - })) + }))) // All other assets let allAssets = await getAssets(options) - await Promise.all(allAssets.map((asset)=>{ + promises = promises.concat((allAssets.map((asset)=>{ return new Promise((resolve, reject)=>{ - if(assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())) { - resolve() // already part of assets - } else { - return new Token(asset).balance(options.accounts[asset.blockchain]).then((balance)=>{ - const assetWithBalance = reduceAssetWithBalance(asset, balance) - if(assetWithBalance.balance != '0') { - assets.push(assetWithBalance) - if(typeof options.drip == 'function') { options.drip(assetWithBalance) } - resolve(assetWithBalance) - } else { - resolve() - } - }) - } + return new Token(asset).balance(options.accounts[asset.blockchain]).then((balance)=>{ + if(exists({ assets, asset })) { return resolve() } + const assetWithBalance = reduceAssetWithBalance(asset, balance) + if(assetWithBalance.balance != '0') { + assets.push(assetWithBalance) + if(typeof options.drip == 'function') { options.drip(assetWithBalance) } + resolve(assetWithBalance) + } else { + resolve() + } + }) }) - })) + }))) + + await Promise.all(promises) return assets } diff --git a/tests/units/dripAssets.spec.js b/tests/units/dripAssets.spec.js index d05d087..2980d83 100644 --- a/tests/units/dripAssets.spec.js +++ b/tests/units/dripAssets.spec.js @@ -305,30 +305,6 @@ describe('dripAssets', ()=>{ expect(dripsCount).toEqual(19) let expectedAssets = [ - { - blockchain: 'ethereum', - address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb', - name: 'DePay', - symbol: 'DEPAY', - decimals: 18, - balance: '56789' - }, - { - blockchain: 'bsc', - address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb', - name: 'DePay', - symbol: 'DEPAY', - decimals: 18, - balance: '56789' - }, - { - blockchain: 'bsc', - address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', - name: 'Wrapped BNB', - symbol: 'BNB', - decimals: 18, - balance: '123456789' - }, { address: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE', symbol: 'ETH', @@ -419,6 +395,15 @@ describe('dripAssets', ()=>{ blockchain: 'ethereum', balance: '123456789' }, + { + address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', + symbol: 'WBNB', + name: 'Wrapped BNB', + decimals: 18, + type: '20', + blockchain: 'bsc', + balance: '123456789' + }, { address: '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56', symbol: 'BUSD', @@ -472,6 +457,24 @@ describe('dripAssets', ()=>{ type: '20', blockchain: 'bsc', balance: '123456789' + }, + { + address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb', + symbol: 'DEPAY', + name: 'DePay', + decimals: 18, + type: '20', + blockchain: 'ethereum', + balance: '56789' + }, + { + address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb', + symbol: 'DEPAY', + name: 'DePay', + decimals: 18, + type: '20', + blockchain: 'bsc', + balance: '56789' } ]