Skip to content

Commit

Permalink
v6.7.0: upgrade web3-blockchains and remove web3-constants internally
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Mar 25, 2023
1 parent 38990e0 commit 8c37fc3
Show file tree
Hide file tree
Showing 24 changed files with 806 additions and 420 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,24 @@ This library supports the following blockchains:
- [Ethereum](https://ethereum.org)
- [BNB Smart Chain](https://www.binance.org/smartChain)
- [Polygon](https://polygon.technology)
- [Solana](https://solana.com)
- [Fantom](https://fantom.foundation)
- [Velas](https://velas.com)

## Platform specific packaging

In case you want to use and package only specific platforms, use the platform-specific package:

### EVM platform specific packaging

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

### Solana platform specific packaging

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

## Functionalities
Expand Down
21 changes: 10 additions & 11 deletions dist/esm/index.evm.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { CONSTANTS } from '@depay/web3-constants';
import { request } from '@depay/web3-client-evm';
import { Blockchain } from '@depay/web3-blockchains';
import Blockchains from '@depay/web3-blockchains';
import { Token } from '@depay/web3-tokens-evm';

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 }
if(options.only && options.only[blockchain] && !options.only[blockchain].find((only)=>(only.toLowerCase() == Blockchains[blockchain].currency.address.toLowerCase()))){ return assets }
if(options.exclude && options.exclude[blockchain] && !!options.exclude[blockchain].find((exclude)=>(exclude.toLowerCase() == Blockchains[blockchain].currency.address.toLowerCase()))){ return assets }

const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == CONSTANTS[blockchain].NATIVE.toLowerCase()));
const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == Blockchains[blockchain].currency.address.toLowerCase()));
if(nativeTokenMissing) {
let balance = await request(
{
Expand All @@ -18,9 +17,9 @@ const ensureNativeTokenAsset = async ({ address, options, assets, blockchain })
{ cache: 30000 }
);
assets = [{
name: CONSTANTS[blockchain].CURRENCY,
symbol: CONSTANTS[blockchain].SYMBOL,
address: CONSTANTS[blockchain].NATIVE,
name: Blockchains[blockchain].currency.name,
symbol: Blockchains[blockchain].currency.symbol,
address: Blockchains[blockchain].currency.address,
type: 'NATIVE',
blockchain,
balance: balance.toString()
Expand Down Expand Up @@ -108,7 +107,7 @@ const isFiltered = ({ options, address, blockchain })=> {
return false
};

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

let assets = [];
Expand Down Expand Up @@ -147,7 +146,7 @@ var dripAssets_evm = async (options) => {

let majorTokens = [];
for (var blockchain in options.accounts){
Blockchain.findByName(blockchain).tokens.forEach((token)=>{
Blockchains.findByName(blockchain).tokens.forEach((token)=>{
if(isFiltered({ options, address: token.address, blockchain })){ return }
majorTokens.push(Object.assign({}, token, { blockchain }));
});
Expand Down Expand Up @@ -194,4 +193,4 @@ var dripAssets_evm = async (options) => {
return assets
};

export { dripAssets_evm as dripAssets, getAssets };
export { dripAssets, getAssets };
17 changes: 8 additions & 9 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { CONSTANTS } from '@depay/web3-constants';
import { request } from '@depay/web3-client';
import { Blockchain } from '@depay/web3-blockchains';
import Blockchains from '@depay/web3-blockchains';
import { Token } from '@depay/web3-tokens';

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 }
if(options.only && options.only[blockchain] && !options.only[blockchain].find((only)=>(only.toLowerCase() == Blockchains[blockchain].currency.address.toLowerCase()))){ return assets }
if(options.exclude && options.exclude[blockchain] && !!options.exclude[blockchain].find((exclude)=>(exclude.toLowerCase() == Blockchains[blockchain].currency.address.toLowerCase()))){ return assets }

const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == CONSTANTS[blockchain].NATIVE.toLowerCase()));
const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == Blockchains[blockchain].currency.address.toLowerCase()));
if(nativeTokenMissing) {
let balance = await request(
{
Expand All @@ -18,9 +17,9 @@ const ensureNativeTokenAsset = async ({ address, options, assets, blockchain })
{ cache: 30000 }
);
assets = [{
name: CONSTANTS[blockchain].CURRENCY,
symbol: CONSTANTS[blockchain].SYMBOL,
address: CONSTANTS[blockchain].NATIVE,
name: Blockchains[blockchain].currency.name,
symbol: Blockchains[blockchain].currency.symbol,
address: Blockchains[blockchain].currency.address,
type: 'NATIVE',
blockchain,
balance: balance.toString()
Expand Down Expand Up @@ -147,7 +146,7 @@ var dripAssets = async (options) => {

let majorTokens = [];
for (var blockchain in options.accounts){
Blockchain.findByName(blockchain).tokens.forEach((token)=>{
Blockchains.findByName(blockchain).tokens.forEach((token)=>{
if(isFiltered({ options, address: token.address, blockchain })){ return }
majorTokens.push(Object.assign({}, token, { blockchain }));
});
Expand Down
196 changes: 196 additions & 0 deletions dist/esm/index.solana.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import { request } from '@depay/web3-client-solana';
import Blockchains from '@depay/web3-blockchains';
import { Token } from '@depay/web3-tokens-solana';

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

const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == Blockchains[blockchain].currency.address.toLowerCase()));
if(nativeTokenMissing) {
let balance = await request(
{
blockchain: blockchain,
address,
method: 'balance',
},
{ cache: 30000 }
);
assets = [{
name: Blockchains[blockchain].currency.name,
symbol: Blockchains[blockchain].currency.symbol,
address: Blockchains[blockchain].currency.address,
type: 'NATIVE',
blockchain,
balance: balance.toString()
}, ...assets];
}
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: {} }; }

let assets = Promise.all(
(Object.keys(options.accounts)).map((blockchain) =>{

return new Promise((resolve, reject)=>{
const address = options.accounts[blockchain];
const controller = new AbortController();
setTimeout(()=>controller.abort(), 10000);
fetch(`https://public.depay.com/accounts/${blockchain}/${address}/assets`, { signal: controller.signal })
.catch((error) => { console.log(error); resolve([]); })
.then((response) => {
if(response && response.ok) {
return response.json()
} else {
resolve([]);
}
})
.then(async (assets) => {
if(assets && assets.length) {
return await ensureNativeTokenAsset({
address,
options,
assets: filterAssets({ assets, blockchain, options }).map((asset) => Object.assign(asset, { blockchain })),
blockchain
})
} else {
resolve([]);
}
})
.then(resolve)
.catch((error) => { console.log(error); resolve([]); });
})
}),
).then((responses) => responses.flat());

return assets
};

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() })
};

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: [] }; }

let assets = [];
let promises = [];

// Prioritized Assets

promises = promises.concat((options.priority || []).map((asset)=>{
return new Promise(async (resolve, reject)=>{
try {
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()
}
);
if(completedAsset.balance != '0') {
if(exists({ assets, asset })) { return resolve() }
assets.push(completedAsset);
if(typeof options.drip == 'function') { options.drip(completedAsset); }
resolve(completedAsset);
} else {
resolve();
}
} catch (e) {
resolve();
}
})
}));

// Major Tokens

let majorTokens = [];
for (var blockchain in options.accounts){
Blockchains.findByName(blockchain).tokens.forEach((token)=>{
if(isFiltered({ options, address: token.address, blockchain })){ return }
majorTokens.push(Object.assign({}, token, { blockchain }));
});
}
promises = promises.concat((majorTokens.map((asset)=>{
return new Promise((resolve, reject)=>{
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();
}}).catch((error)=>{ console.log(error); resolve(); });
})
})));

// All other assets

if(options.only == undefined || Object.keys(options.only).every((list)=>list.length == 0)) {
let allAssets = await getAssets(options);
promises = promises.concat((allAssets.map((asset)=>{
return new Promise((resolve, reject)=>{
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();
}}).catch((error)=>{ console.log(error); resolve(); })
})
})));
}

await Promise.all(promises);

return assets
};

export { dripAssets, getAssets };
30 changes: 17 additions & 13 deletions dist/umd/index.evm.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@depay/web3-constants'), require('@depay/web3-client-evm'), require('@depay/web3-blockchains'), require('@depay/web3-tokens-evm')) :
typeof define === 'function' && define.amd ? define(['exports', '@depay/web3-constants', '@depay/web3-client-evm', '@depay/web3-blockchains', '@depay/web3-tokens-evm'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Web3Assets = {}, global.Web3Constants, global.Web3Client, global.Web3Blockchains, global.Web3Tokens));
}(this, (function (exports, web3Constants, web3ClientEvm, web3Blockchains, web3TokensEvm) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@depay/web3-client-evm'), require('@depay/web3-blockchains'), require('@depay/web3-tokens-evm')) :
typeof define === 'function' && define.amd ? define(['exports', '@depay/web3-client-evm', '@depay/web3-blockchains', '@depay/web3-tokens-evm'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Web3Assets = {}, global.Web3Client, global.Web3Blockchains, global.Web3Tokens));
}(this, (function (exports, web3ClientEvm, Blockchains, web3TokensEvm) { 'use strict';

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var Blockchains__default = /*#__PURE__*/_interopDefaultLegacy(Blockchains);

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 }
if(options.only && options.only[blockchain] && !options.only[blockchain].find((only)=>(only.toLowerCase() == Blockchains__default['default'][blockchain].currency.address.toLowerCase()))){ return assets }
if(options.exclude && options.exclude[blockchain] && !!options.exclude[blockchain].find((exclude)=>(exclude.toLowerCase() == Blockchains__default['default'][blockchain].currency.address.toLowerCase()))){ return assets }

const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == web3Constants.CONSTANTS[blockchain].NATIVE.toLowerCase()));
const nativeTokenMissing = !assets.find((asset)=>(asset.address.toLowerCase() == Blockchains__default['default'][blockchain].currency.address.toLowerCase()));
if(nativeTokenMissing) {
let balance = await web3ClientEvm.request(
{
Expand All @@ -19,9 +23,9 @@
{ cache: 30000 }
);
assets = [{
name: web3Constants.CONSTANTS[blockchain].CURRENCY,
symbol: web3Constants.CONSTANTS[blockchain].SYMBOL,
address: web3Constants.CONSTANTS[blockchain].NATIVE,
name: Blockchains__default['default'][blockchain].currency.name,
symbol: Blockchains__default['default'][blockchain].currency.symbol,
address: Blockchains__default['default'][blockchain].currency.address,
type: 'NATIVE',
blockchain,
balance: balance.toString()
Expand Down Expand Up @@ -109,7 +113,7 @@
return false
};

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

let assets = [];
Expand Down Expand Up @@ -148,7 +152,7 @@

let majorTokens = [];
for (var blockchain in options.accounts){
web3Blockchains.Blockchain.findByName(blockchain).tokens.forEach((token)=>{
Blockchains__default['default'].findByName(blockchain).tokens.forEach((token)=>{
if(isFiltered({ options, address: token.address, blockchain })){ return }
majorTokens.push(Object.assign({}, token, { blockchain }));
});
Expand Down Expand Up @@ -195,7 +199,7 @@
return assets
};

exports.dripAssets = dripAssets_evm;
exports.dripAssets = dripAssets;
exports.getAssets = getAssets;

Object.defineProperty(exports, '__esModule', { value: true });
Expand Down
Loading

0 comments on commit 8c37fc3

Please sign in to comment.