Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Update for getting token id
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbo committed Nov 7, 2023
1 parent a34d6b2 commit 2ce8b71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 73 deletions.
51 changes: 0 additions & 51 deletions shared/schemas/LSP8IdentifiableDigitalAsset.json

This file was deleted.

49 changes: 27 additions & 22 deletions utils/fetchLsp8Metadata.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
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<LSP4DigitalAssetMetadataJSON> => {
const lsp8MetadataGetter = async (
tokenIdType: string,
tokenId: string
): Promise<LSP4DigitalAssetMetadataJSON> => {
const lsp8Metadata = await erc725.fetchData([
{
keyName: `LSP8MetadataJSON:<${tokenIdType}>`,
dynamicKeyParts: tokenId,
},
])
return validateLsp4MetaData(lsp8Metadata[0].value)
}

const { getInstance } = useErc725()
const erc725 = getInstance(
assetAddress,
LSP8IdentifiableDigitalAsset as ERC725JSONSchema[]
)

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`
Expand All @@ -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)
}

0 comments on commit 2ce8b71

Please sign in to comment.