From 94c36e900f676290caf38551656e80e0f5de7533 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 20 May 2022 21:01:30 +0100 Subject: [PATCH] feat(assets): name and description auto-completion and sort --- src/assets/ethereum-nft-moralis.ts | 2 +- src/assets/index.ts | 32 ++++++++++++++++++++++++++++-- src/utils.ts | 26 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/assets/ethereum-nft-moralis.ts b/src/assets/ethereum-nft-moralis.ts index a22e134..33589f7 100644 --- a/src/assets/ethereum-nft-moralis.ts +++ b/src/assets/ethereum-nft-moralis.ts @@ -79,7 +79,7 @@ class EthereumNFTMoralis extends Base { const asset: Asset = { tags: ['NFT'], owners: [utils.getAddress(item.owner_of || options.identity)], - name: metadata?.name || `${item.name} #${item.token_id}`, + name: metadata?.name, description: metadata?.description, source: 'Ethereum NFT', diff --git a/src/assets/index.ts b/src/assets/index.ts index bd69021..69d96f1 100644 --- a/src/assets/index.ts +++ b/src/assets/index.ts @@ -19,6 +19,7 @@ export type AssetsOptions = { }; class Assets { + main: Main; map: { [key: string]: { [key: string]: Base; @@ -26,6 +27,7 @@ class Assets { }; constructor(main: Main) { + this.main = main; this.map = { 'Ethereum NFT': { Alchemy: new EthereumNFTAlchemy(main), @@ -54,6 +56,8 @@ class Assets { options, ); + let result; + if (options.providers!.length > 1) { const list = await Promise.all( options.providers!.map(async (provider: string, index) => { @@ -62,6 +66,7 @@ class Assets { cursor: options.cursor?.[index], }), ); + this.main.utils.removeEmpty(result.list); return result; }), ); @@ -81,14 +86,37 @@ class Assets { const assets = values(merged); const cursor = list.map((item) => item.cursor); - return { + result = { total: assets.length, ...(cursor.find((id) => id) && { cursor: cursor }), list: assets, }; } else { - return await this.map[options.source][options.providers![0]].get(options); + result = await this.map[options.source][options.providers![0]].get(options); } + + const networks = ['Gnosis', 'Binance Smart Chain', 'Polygon', 'Ethereum']; + result.list = result.list + .map((asset: Asset) => { + if (!asset.name) { + asset.name = `${asset.metadata?.collection_name || asset.metadata?.token_symbol} #${ + asset.metadata?.token_id + }`; + } + if (!asset.description) { + asset.description = asset.name; + } + + return asset; + }) + .sort((a: Asset, b: Asset) => { + return ( + networks.indexOf(b.metadata?.network || '') - networks.indexOf(a.metadata?.network || '') || + parseInt(b.metadata?.block_number) - parseInt(a.metadata?.block_number) + ); + }); + + return result; } } diff --git a/src/utils.ts b/src/utils.ts index 079a596..4e3b570 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -53,6 +53,32 @@ class Utils { return profileId; } + + removeEmpty( + obj: any, + father?: { + obj: any; + key: string; + }, + ) { + for (let key in obj) { + if (typeof obj[key] === 'object') { + if (!obj[key] || Object.keys(obj[key]).length === 0) { + delete obj[key]; + } else { + this.removeEmpty(obj[key], { + obj, + key, + }); + } + } else if (!obj[key]) { + delete obj[key]; + } + } + if (Object.keys(obj).length === 0 && father) { + delete father.obj[father.key]; + } + } } export default Utils;