import type { NFTCollectionMetadataResponse, NFTMetadataResponse, NFTStandard, ProtoNFT, } from "@ledgerhq/types-live"; import { getEnv } from "@ledgerhq/live-env"; import type { CryptoCurrency } from "../types"; import { CollectionMetadataInput, NftMetadataInput, NftRequestsBatcher } from "./types"; import { makeBatcher } from "@ledgerhq/live-network/batcher"; export function isNFTActive(currencyId: string | undefined | null): boolean { return !!currencyId && getEnv("NFT_CURRENCIES").includes(currencyId); } const nftCapabilities: Record = { hasQuantity: ["ERC1155"], }; type NftCapabilty = keyof typeof nftCapabilities; export const getNftCapabilities = ( nft: ProtoNFT | undefined | null, ): Record => (Object.entries(nftCapabilities) as [NftCapabilty, NFTStandard[]][]).reduce( (acc, [capability, standards]) => ({ ...acc, [capability]: nft?.standard ? standards.includes(nft.standard) : false, }), {} as Record, ); const batchersMap = new Map(); /** * In order to `instanciate`/make only 1 batcher by currency, * they're `cached` in a Map and retrieved by this method * This method is still EVM based for now but can be improved * to implement an even more generic solution */ export const metadataCallBatcher = ( currency: CryptoCurrency, api: { getNftMetadata: (input: NftMetadataInput[], params: Params) => Promise; getNftCollectionMetadata: ( input: CollectionMetadataInput[], params: Params, ) => Promise; getParams: (currency: CryptoCurrency) => Params; }, ): NftRequestsBatcher => { if (!batchersMap.has(currency.id)) { const params = api.getParams(currency); const batcher = { loadNft: makeBatcher(api.getNftMetadata, params), loadCollection: makeBatcher(api.getNftCollectionMetadata, params), }; batchersMap.set(currency.id, batcher); } return batchersMap.get(currency.id)!; };