Skip to content

Commit

Permalink
v6.0.2: parallelize getting assets
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Apr 10, 2022
1 parent d53382c commit 1ca056b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 91 deletions.
47 changes: 25 additions & 22 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({},
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
};
Expand Down
47 changes: 25 additions & 22 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({},
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
47 changes: 25 additions & 22 deletions src/dripAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({},
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
51 changes: 27 additions & 24 deletions tests/units/dripAssets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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'
}
]

Expand Down

0 comments on commit 1ca056b

Please sign in to comment.