Skip to content

Commit

Permalink
v6.1.0: introduces "only", and "exclude"
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Apr 11, 2022
1 parent 3d69383 commit 8b9075c
Show file tree
Hide file tree
Showing 8 changed files with 682 additions and 19 deletions.
143 changes: 140 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ npm install --save @depay/web3-assets
```javascript
import { getAssets } from '@depay/web3-assets'

let assets = await getAssets({ accounts: { ethereum: '0x08B277154218CCF3380CAE48d630DA13462E3950', bsc: '0x08B277154218CCF3380CAE48d630DA13462E3950' } })
let assets = await getAssets({
accounts: {
ethereum: '0x08B277154218CCF3380CAE48d630DA13462E3950',
bsc: '0x08B277154218CCF3380CAE48d630DA13462E3950'
}
})
//[
// {
// "name": "Dai Stablecoin",
Expand Down Expand Up @@ -49,7 +54,7 @@ let allAssets = await dripAssets({
{ blockchain: 'ethereum', address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb' },
{ blockchain: 'bsc', address: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb' },
{ blockchain: 'ethereum', address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' },
{ blockchain: 'bsc', address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c' },
{ blockchain: 'bsc', address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c' }
],
drip: (asset)=>{
// {
Expand Down Expand Up @@ -81,7 +86,12 @@ Retrieves all assets of the given account on the given blockchains:

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

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

#### only (getAssets)

Only gets assets and balances for given token addresses.

```javascript

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

let assets = await getAssets({
accounts: {
ethereum: '0x08B277154218CCF3380CAE48d630DA13462E3950',
bsc: '0x08B277154218CCF3380CAE48d630DA13462E3950'
},
only: {
ethereum: ['0x6B175474E89094C44Da98b954EedeAC495271d0F'],
bsc: ['0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82']
}
})
//[
// {
// "name": "Dai Stablecoin",
// "symbol": "DAI",
// "address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
// "blockchain": "ethereum",
// "type": "20",
// "balance": "8007804249707967889272"
// }, {
// "name": "PancakeSwap Token",
// "symbol": "CAKE",
// "address": "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82",
// "blockchain": "bsc",
// "type": "20",
// "balance": "2221112213212321"
// }
//]
```

#### exclude (getAssets)

Excludes given assets

```javascript

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

let assets = await getAssets({
accounts: {
ethereum: '0x08B277154218CCF3380CAE48d630DA13462E3950',
bsc: '0x08B277154218CCF3380CAE48d630DA13462E3950'
},
exclude: {
ethereum: ['0x6B175474E89094C44Da98b954EedeAC495271d0F'],
bsc: ['0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82']
}
})
//[
// {
// "name": "DePay",
// "symbol": "DEPAY",
// "address": "0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb",
// "blockchain": "ethereum",
// "type": "20",
// "balance": "212816860003097638129"
// }
//]
```

### dripAssets

Drips every single asset immediately after resolved and all assets after all assets have been resolved (just as getAssets):
Expand Down Expand Up @@ -136,6 +213,66 @@ let allAssets = await dripAssets({
})
```

#### only (dripAssets)

Only drips assets and balances for given token addresses.

```javascript

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

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

#### exclude (dripAssets)

Drips all assets except the ones you "exclude":

```javascript

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

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

## Development

### Get started
Expand Down
38 changes: 33 additions & 5 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { request } from '@depay/web3-client';
import { Blockchain } from '@depay/web3-blockchains';
import { Token } from '@depay/web3-tokens';

const ensureNativeTokenAsset = async ({ address, assets, blockchain }) => {
if(assets.find((asset)=> {
return asset.address.toLowerCase() == CONSTANTS[blockchain].NATIVE.toLowerCase()
}) == undefined) {
const ensureNativeTokenAsset = async ({ address, options, assets, blockchain }) => {
if(options.only && options.only[blockchain] && !options.only[blockchain].find((only)=>(only.toLowerCase() == CONSTANTS[blockchain].NATIVE.toLowerCase()))){ return assets }
if(options.exclude && options.exclude[blockchain] && !!options.exclude[blockchain].find((exclude)=>(exclude.toLowerCase() == CONSTANTS[blockchain].NATIVE.toLowerCase()))){ return assets }

const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == CONSTANTS[blockchain].NATIVE.toLowerCase()));
if(nativeTokenMissing) {
let balance = await request(
{
blockchain: blockchain,
Expand All @@ -27,6 +29,20 @@ const ensureNativeTokenAsset = async ({ address, assets, blockchain }) => {
return assets
};

const filterAssets = ({ assets, blockchain, options })=>{
if(options.only) {
return assets.filter((asset)=>{
return (options.only[blockchain] || []).find((onlyAsset)=>(onlyAsset.toLowerCase() == asset.address.toLowerCase()))
})
} else if(options.exclude) {
return assets.filter((asset)=>{
return (options.exclude[blockchain] || []).find((excludeAsset)=>(excludeAsset.toLowerCase() != asset.address.toLowerCase()))
})
} else {
return assets
}
};

var getAssets = async (options) => {
if(options === undefined) { options = { accounts: {} }; }

Expand All @@ -41,7 +57,8 @@ var getAssets = async (options) => {
.then(async (assets) => {
return await ensureNativeTokenAsset({
address,
assets: assets.map((asset) => Object.assign(asset, { blockchain })),
options,
assets: filterAssets({ assets, blockchain, options }).map((asset) => Object.assign(asset, { blockchain })),
blockchain
})
}).catch((error) => { console.log(error); })
Expand All @@ -66,6 +83,16 @@ const exists = ({ assets, asset })=> {
return !!assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())
};

const isFiltered = ({ options, address, blockchain })=> {
if(options && options.only && options.only[blockchain] && !options.only[blockchain].find((only)=>only.toLowerCase()==address.toLowerCase())){
return true
}
if(options && options.exclude && options.exclude[blockchain] && options.exclude[blockchain].find((only)=>only.toLowerCase()==address.toLowerCase())){
return true
}
return false
};

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

Expand Down Expand Up @@ -102,6 +129,7 @@ var dripAssets = async (options) => {
let majorTokens = [];
for (var blockchain in options.accounts){
Blockchain.findByName(blockchain).tokens.forEach((token)=>{
if(isFiltered({ options, address: token.address, blockchain })){ return }
majorTokens.push(Object.assign({}, token, { blockchain }));
});
}
Expand Down
38 changes: 33 additions & 5 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Web3Assets = {}, global.Web3Constants, global.Web3Client, global.Web3Blockchains, global.Web3Tokens));
}(this, (function (exports, web3Constants, web3Client, web3Blockchains, web3Tokens) { 'use strict';

const ensureNativeTokenAsset = async ({ address, assets, blockchain }) => {
if(assets.find((asset)=> {
return asset.address.toLowerCase() == web3Constants.CONSTANTS[blockchain].NATIVE.toLowerCase()
}) == undefined) {
const ensureNativeTokenAsset = async ({ address, options, assets, blockchain }) => {
if(options.only && options.only[blockchain] && !options.only[blockchain].find((only)=>(only.toLowerCase() == web3Constants.CONSTANTS[blockchain].NATIVE.toLowerCase()))){ return assets }
if(options.exclude && options.exclude[blockchain] && !!options.exclude[blockchain].find((exclude)=>(exclude.toLowerCase() == web3Constants.CONSTANTS[blockchain].NATIVE.toLowerCase()))){ return assets }

const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == web3Constants.CONSTANTS[blockchain].NATIVE.toLowerCase()));
if(nativeTokenMissing) {
let balance = await web3Client.request(
{
blockchain: blockchain,
Expand All @@ -28,6 +30,20 @@
return assets
};

const filterAssets = ({ assets, blockchain, options })=>{
if(options.only) {
return assets.filter((asset)=>{
return (options.only[blockchain] || []).find((onlyAsset)=>(onlyAsset.toLowerCase() == asset.address.toLowerCase()))
})
} else if(options.exclude) {
return assets.filter((asset)=>{
return (options.exclude[blockchain] || []).find((excludeAsset)=>(excludeAsset.toLowerCase() != asset.address.toLowerCase()))
})
} else {
return assets
}
};

var getAssets = async (options) => {
if(options === undefined) { options = { accounts: {} }; }

Expand All @@ -42,7 +58,8 @@
.then(async (assets) => {
return await ensureNativeTokenAsset({
address,
assets: assets.map((asset) => Object.assign(asset, { blockchain })),
options,
assets: filterAssets({ assets, blockchain, options }).map((asset) => Object.assign(asset, { blockchain })),
blockchain
})
}).catch((error) => { console.log(error); })
Expand All @@ -67,6 +84,16 @@
return !!assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())
};

const isFiltered = ({ options, address, blockchain })=> {
if(options && options.only && options.only[blockchain] && !options.only[blockchain].find((only)=>only.toLowerCase()==address.toLowerCase())){
return true
}
if(options && options.exclude && options.exclude[blockchain] && options.exclude[blockchain].find((only)=>only.toLowerCase()==address.toLowerCase())){
return true
}
return false
};

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

Expand Down Expand Up @@ -103,6 +130,7 @@
let majorTokens = [];
for (var blockchain in options.accounts){
web3Blockchains.Blockchain.findByName(blockchain).tokens.forEach((token)=>{
if(isFiltered({ options, address: token.address, blockchain })){ return }
majorTokens.push(Object.assign({}, token, { blockchain }));
});
}
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.2",
"version": "6.1.0",
"description": "",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down
11 changes: 11 additions & 0 deletions src/dripAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ const exists = ({ assets, asset })=> {
return !!assets.find(element => element.blockchain == asset.blockchain && element.address.toLowerCase() == asset.address.toLowerCase())
}

const isFiltered = ({ options, address, blockchain })=> {
if(options && options.only && options.only[blockchain] && !options.only[blockchain].find((only)=>only.toLowerCase()==address.toLowerCase())){
return true
}
if(options && options.exclude && options.exclude[blockchain] && options.exclude[blockchain].find((only)=>only.toLowerCase()==address.toLowerCase())){
return true
}
return false
}

export default async (options) => {
if(options === undefined) { options = { accounts: {}, priority: [] } }

Expand Down Expand Up @@ -53,6 +63,7 @@ export default async (options) => {
let majorTokens = []
for (var blockchain in options.accounts){
Blockchain.findByName(blockchain).tokens.forEach((token)=>{
if(isFiltered({ options, address: token.address, blockchain })){ return }
majorTokens.push(Object.assign({}, token, { blockchain }))
})
}
Expand Down
Loading

0 comments on commit 8b9075c

Please sign in to comment.