From 2ce8b71eafa86c0b5725865846044b2c924e7dfd Mon Sep 17 00:00:00 2001 From: Dominik Zborowski Date: Tue, 7 Nov 2023 09:55:13 +0100 Subject: [PATCH] Update for getting token id --- .../schemas/LSP8IdentifiableDigitalAsset.json | 51 ------------------- utils/fetchLsp8Metadata.ts | 49 ++++++++++-------- 2 files changed, 27 insertions(+), 73 deletions(-) delete mode 100644 shared/schemas/LSP8IdentifiableDigitalAsset.json diff --git a/shared/schemas/LSP8IdentifiableDigitalAsset.json b/shared/schemas/LSP8IdentifiableDigitalAsset.json deleted file mode 100644 index 7d0af41f..00000000 --- a/shared/schemas/LSP8IdentifiableDigitalAsset.json +++ /dev/null @@ -1,51 +0,0 @@ -[ - { - "name": "LSP8MetadataAddress:
", - "key": "0x73dcc7c3c4096cdc7f8a0000
", - "keyType": "Mapping", - "valueType": "Mixed", - "valueContent": "Mixed" - }, - { - "name": "LSP8MetadataAddress:", - "key": "0x73dcc7c3c4096cdc7f8a0000", - "keyType": "Mapping", - "valueType": "Mixed", - "valueContent": "Mixed" - }, - { - "name": "LSP8MetadataAddress:", - "key": "0x73dcc7c3c4096cdc7f8a0000", - "keyType": "Mapping", - "valueType": "Mixed", - "valueContent": "Mixed" - }, - { - "name": "LSP8MetadataJSON:
", - "key": "0x9a26b4060ae7f7d5e3cd0000
", - "keyType": "Mapping", - "valueType": "bytes", - "valueContent": "JSONURL" - }, - { - "name": "LSP8MetadataJSON:", - "key": "0x9a26b4060ae7f7d5e3cd0000", - "keyType": "Mapping", - "valueType": "bytes", - "valueContent": "JSONURL" - }, - { - "name": "LSP8MetadataJSON:", - "key": "0x9a26b4060ae7f7d5e3cd0000", - "keyType": "Mapping", - "valueType": "bytes", - "valueContent": "JSONURL" - }, - { - "name": "LSP8TokenIdType", - "key": "0x715f248956de7ce65e94d9d836bfead479f7e70d69b718d47bfe7b00e05b4fe4", - "keyType": "Singleton", - "valueType": "uint256", - "valueContent": "Number" - } -] diff --git a/utils/fetchLsp8Metadata.ts b/utils/fetchLsp8Metadata.ts index 363f4fa5..8a6e120f 100644 --- a/utils/fetchLsp8Metadata.ts +++ b/utils/fetchLsp8Metadata.ts @@ -1,26 +1,13 @@ import { LSP4DigitalAssetMetadataJSON } from '@lukso/lsp-smart-contracts' -import { ERC725JSONSchema } from '@erc725/erc725.js' +import ERC725, { ERC725JSONSchema } from '@erc725/erc725.js' +import LSP8IdentifiableDigitalAsset from '@erc725/erc725.js/schemas/LSP8IdentifiableDigitalAsset.json' import { Lsp8TokenIdType } from '@/types/assets' -import LSP8IdentifiableDigitalAsset from '@/shared/schemas/LSP8IdentifiableDigitalAsset.json' export const fetchLsp8Metadata = async ( tokenId: string, assetAddress: Address ): Promise => { - const lsp8MetadataGetter = async ( - tokenIdType: string, - tokenId: string - ): Promise => { - const lsp8Metadata = await erc725.fetchData([ - { - keyName: `LSP8MetadataJSON:<${tokenIdType}>`, - dynamicKeyParts: tokenId, - }, - ]) - return validateLsp4MetaData(lsp8Metadata[0].value) - } - const { getInstance } = useErc725() const erc725 = getInstance( assetAddress, @@ -28,20 +15,24 @@ export const fetchLsp8Metadata = async ( ) try { - const lsp8DigitalAsset = await erc725.fetchData(['LSP8TokenIdType']) - const tokenIdType = Number(lsp8DigitalAsset[0].value) + const lsp8DigitalAsset = await erc725.fetchData('LSP8TokenIdType') + const tokenIdType = Number(lsp8DigitalAsset.value) - // fetch LSP8MetadataJSON depending on tokenIdType + // fetch metadata depending on tokenIdType switch (tokenIdType) { case Lsp8TokenIdType.NUMBER: - return await lsp8MetadataGetter('uint256', parseInt(tokenId).toString()) + return await getMetadata( + 'uint256', + parseInt(tokenId).toString(), + erc725 + ) case Lsp8TokenIdType.STRING: - return await lsp8MetadataGetter('string', tokenId.toString()) + return await getMetadata('string', tokenId.toString(), erc725) case Lsp8TokenIdType.UNIQUE_ID: case Lsp8TokenIdType.HASH: - return await lsp8MetadataGetter('bytes32', tokenId.toString()) + return await getMetadata('bytes32', tokenId.toString(), erc725) case Lsp8TokenIdType.ADDRESS: - return await lsp8MetadataGetter('address', tokenId.slice(0, 42)) + return await getMetadata('address', tokenId.slice(0, 42), erc725) default: throw new Error( `Unsupported LSP8 tokenIdType '${tokenIdType}' for '${assetAddress}' asset` @@ -60,3 +51,17 @@ export const fetchLsp8Metadata = async ( } } } + +const getMetadata = async ( + tokenIdType: string, + tokenIdValue: string, + erc725: ERC725 +) => { + const lsp8Metadata = await erc725.fetchData([ + { + keyName: `LSP8MetadataTokenURI:<${tokenIdType}>`, + dynamicKeyParts: tokenIdValue, + }, + ]) + return validateLsp4MetaData(lsp8Metadata[0].value) +}