Skip to content

Commit

Permalink
v6.0.0: introduces dripAssets and simplifies getAssets (DePayFi#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
spape authored Apr 10, 2022
1 parent 80a91ee commit 45eb9d5
Show file tree
Hide file tree
Showing 12 changed files with 968 additions and 1,371 deletions.
82 changes: 51 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm install --save @depay/web3-assets
```javascript
import { getAssets } from '@depay/web3-assets'

let assets = await getAssets()
let assets = await getAssets({ accounts: { ethereum: '0x08B277154218CCF3380CAE48d630DA13462E3950', bsc: '0x08B277154218CCF3380CAE48d630DA13462E3950' } })
//[
// {
// "name": "Dai Stablecoin",
Expand All @@ -40,24 +40,48 @@ let assets = await getAssets()
//]
```

```javascript
import { dripAssets } from '@depay/web3-assets'

let allAssets = await dripAssets({
accounts: { ethereum: '0x08B277154218CCF3380CAE48d630DA13462E3950', bsc: '0x08B277154218CCF3380CAE48d630DA13462E3950' },
priority: [
{ blockchain: 'ethereum', address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb' },
{ blockchain: 'bsc', address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb' },
{ blockchain: 'ethereum', address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' },
{ blockchain: 'bsc', address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c' },
],
drip: (asset)=>{
// {
// "name": "DePay",
// "symbol": "DEPAY",
// "address": "0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb",
// "blockchain": "ethereum",
// "type": "20",
// "balance": "212816860003097638129"
// }
}
})
```

## Support

This library supports the following blockchains:

- [Ethereum](https://ethereum.org)
- [Binance Smart Chain](https://www.binance.org/en/smartChain)
- [BNB Smart Chain](https://www.binance.org/en/smartChain)

## Functionalities

### getAssets

Retrieves all assets of the connected crypto wallet account for all supported blockchains at once.
Retrieves all assets of the given account on the given blockchains:

```javascript

import { getAssets } from '@depay/web3-assets'

let assets = await getAssets()
let assets = await getAssets({ accounts: { ethereum: '0x08B277154218CCF3380CAE48d630DA13462E3950', bsc: '0x08B277154218CCF3380CAE48d630DA13462E3950' } })
//[
// {
// "name": "Dai Stablecoin",
Expand All @@ -84,36 +108,32 @@ let assets = await getAssets()
//]
```

Also allows you to retrieve assets only for a given blockchain:

```javascript
let assets = await getAssets({ blockchain: 'bsc' })
//[
// {
// "name": "PancakeSwap Token",
// "symbol": "CAKE",
// "address": "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82",
// "blockchain": "bsc",
// "type": "20",
// "balance": "2221112213212321"
// }
//]
```
### dripAssets

Also allows you to retrieve assets for a given account for a given blockchain:
Drips every single asset immediately after resolved and all assets after all assets have been resolved (just as getAssets):

```javascript
let assets = await getAssets({ account: '0xEcA533Ef096f191A35DE76aa4580FA3A722724bE', blockchain: 'bsc' })
//[
// {
// "name": "PancakeSwap Token",
// "symbol": "CAKE",
// "address": "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82",
// "blockchain": "bsc",
// "type": "20",
// "balance": "2221112213212321"
// }
//]
import { dripAssets } from '@depay/web3-assets'

let allAssets = await dripAssets({
accounts: { ethereum: '0x08B277154218CCF3380CAE48d630DA13462E3950', bsc: '0x08B277154218CCF3380CAE48d630DA13462E3950' },
priority: [
{ blockchain: 'ethereum', address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb' },
{ blockchain: 'bsc', address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb' },
{ blockchain: 'ethereum', address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' },
{ blockchain: 'bsc', address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c' },
],
drip: (asset)=>{
// {
// "name": "DePay",
// "symbol": "DEPAY",
// "address": "0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb",
// "blockchain": "ethereum",
// "type": "20",
// "balance": "212816860003097638129"
// }
}
})
```

## Development
Expand Down
114 changes: 94 additions & 20 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { CONSTANTS } from '@depay/web3-constants';
import { getWallet } from '@depay/web3-wallets';
import { request } from '@depay/web3-client';
import { Blockchain } from '@depay/web3-blockchains';
import { Token } from '@depay/web3-tokens';

const ensureNativeTokenAsset = async ({ account, assets, blockchain }) => {
const ensureNativeTokenAsset = async ({ address, assets, blockchain }) => {
if(assets.find((asset)=> {
return asset.address.toLowerCase() == CONSTANTS[blockchain].NATIVE.toLowerCase()
}) == undefined) {
let balance = await request(
{
blockchain: blockchain,
address: account,
address,
method: 'balance',
},
{ cache: 30000 }
Expand All @@ -26,35 +27,108 @@ const ensureNativeTokenAsset = async ({ account, assets, blockchain }) => {
return assets
};

const getAssets = async (options) => {
if(options === undefined) { options = {}; }

let wallet = await getWallet();

let account;
if(options.account) {
account = options.account;
} else if(wallet) {
account = await wallet.account();
}
if (!account) { return }
var getAssets = async (options) => {
if(options === undefined) { options = { accounts: {} }; }

let assets = Promise.all(
(options.blockchain ? [options.blockchain] : wallet.blockchains).map((blockchain) =>{
(Object.keys(options.accounts)).map((blockchain) =>{

const address = options.accounts[blockchain];

return fetch(`https://public.depay.fi/accounts/${blockchain}/${account}/assets`)
return fetch(`https://public.depay.fi/accounts/${blockchain}/${address}/assets`)
.catch((error) => { console.log(error); })
.then((response) => response.json())
.then(async (assets) => {
return await ensureNativeTokenAsset({
account,
address,
assets: assets.map((asset) => Object.assign(asset, { blockchain })),
blockchain
})
})
}).catch((error) => { console.log(error); })
}),
).then((responses) => responses.flat());

return assets
};

export { getAssets };
const reduceAssetWithBalance = (asset, balance)=>{
return Object.assign({}, {
address: asset.address,
symbol: asset.symbol,
name: asset.name,
decimals: asset.decimals,
type: asset.type,
blockchain: asset.blockchain
}, { balance: balance.toString() })
};

var dripAssets = async (options) => {
if(options === undefined) { options = { accounts: {}, priority: [] }; }

let assets = [];

// Prioritized Assets

await Promise.all((options.priority || []).map((asset)=>{
return new Promise(async (resolve, reject)=>{
let token = new Token(asset);
let completedAsset = Object.assign({},
asset,
{
name: await token.name(),
symbol: await token.symbol(),
decimals: await token.decimals(),
balance: (await token.balance(options.accounts[asset.blockchain])).toString()
}
);
assets.push(completedAsset);
if(typeof options.drip == 'function') { options.drip(completedAsset); }
resolve(completedAsset);
})
}));

// Major Tokens

let majorTokens = [];
for (var blockchain in options.accounts){
Blockchain.findByName(blockchain).tokens.forEach((token)=>{
majorTokens.push(Object.assign({}, token, { blockchain }));
});
}
await Promise.all(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)=>{
const assetWithBalance = reduceAssetWithBalance(asset, balance);
assets.push(assetWithBalance);
if(typeof options.drip == 'function') { options.drip(assetWithBalance); }
resolve(assetWithBalance);
}).catch((error)=>{ console.log(error); });
})
}));

// All other assets

let allAssets = await getAssets(options);
await Promise.all(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);
assets.push(assetWithBalance);
if(typeof options.drip == 'function') { options.drip(assetWithBalance); }
resolve(assetWithBalance);
})
}
})
}));

return assets
};

export { dripAssets, getAssets };
Loading

0 comments on commit 45eb9d5

Please sign in to comment.