Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
feat(assets): name and description auto-completion and sort
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 20, 2022
1 parent 5a1bb0c commit 94c36e9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/assets/ethereum-nft-moralis.ts
Expand Up @@ -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',
Expand Down
32 changes: 30 additions & 2 deletions src/assets/index.ts
Expand Up @@ -19,13 +19,15 @@ export type AssetsOptions = {
};

class Assets {
main: Main;
map: {
[key: string]: {
[key: string]: Base;
};
};

constructor(main: Main) {
this.main = main;
this.map = {
'Ethereum NFT': {
Alchemy: new EthereumNFTAlchemy(main),
Expand Down Expand Up @@ -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) => {
Expand All @@ -62,6 +66,7 @@ class Assets {
cursor: options.cursor?.[index],
}),
);
this.main.utils.removeEmpty(result.list);
return result;
}),
);
Expand All @@ -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;
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/utils.ts
Expand Up @@ -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;

0 comments on commit 94c36e9

Please sign in to comment.