diff --git a/package.json b/package.json index 28eb233..c97b5b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@itheum/sdk-mx-data-nft", - "version": "3.0.0-alpha.3", + "version": "3.0.0-alpha.5", "description": "SDK for Itheum's Data NFT Technology on MultiversX Blockchain", "main": "out/index.js", "types": "out/index.d.js", diff --git a/src/common/utils.ts b/src/common/utils.ts index f9bc199..df093ed 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -4,7 +4,8 @@ import { ErrFetch, ErrInvalidTokenIdentifier, ErrMissingTrait, - ErrMissingValueForTrait + ErrMissingValueForTrait, + ErrParseNft } from '../errors'; import { Bond, @@ -149,7 +150,26 @@ export function parseRefund(value: any): Refund { } export function parseDataNft(value: NftType): DataNft { - return new DataNft({ + let attributes; + try { + attributes = DataNft.decodeAttributes(value.attributes); // normal attributes + } catch (error: any) { + try { + attributes = { + dataPreview: value.metadata?.itheum_data_preview_url ?? '', + dataStream: value.metadata?.itheum_data_stream_url ?? '', + dataMarshal: value.metadata?.itheum_data_marshal_url ?? '', + creator: value.metadata?.itheum_creator ?? '', + creationTime: new Date(value.timestamp * 1000), + description: value.metadata?.description ?? '', + isDataNFTPH: true, + title: value.name + }; + } catch (error: any) { + throw new ErrParseNft(error.message); + } + } + const returnValue = { tokenIdentifier: value.identifier, nftImgUrl: value.url ?? '', tokenName: value.name, @@ -163,8 +183,9 @@ export function parseDataNft(value: NftType): DataNft { collection: value.collection, balance: value.balance ? Number(value.balance) : 0, owner: value.owner ? value.owner : '', - ...DataNft.decodeAttributes(value.attributes) - }); + ...attributes + }; + return new DataNft(returnValue); } export async function checkTraitsUrl(traitsUrl: string) { diff --git a/src/datanft.ts b/src/datanft.ts index bb0f356..d960943 100644 --- a/src/datanft.ts +++ b/src/datanft.ts @@ -49,6 +49,7 @@ export class DataNft implements DataNftType { readonly owner: string = ''; // works if tokenIdentifier is an NFT readonly overrideDataMarshal: string = ''; readonly overrideDataMarshalChainId: string = ''; + readonly isDataNFTPH: boolean = false; static networkConfiguration: Config; static apiConfiguration: string; diff --git a/src/errors.ts b/src/errors.ts index 8733103..9e3642b 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -52,6 +52,12 @@ export class ErrDecodeAttributes extends Error { } } +export class ErrParseNft extends Error { + public constructor(message?: string) { + super(`Failed to parse NFT: ${message}`); + } +} + export class ErrAttributeNotSet extends Error { public constructor(attribute: string) { super(`Attribute "${attribute}" is not set`); diff --git a/src/interfaces.ts b/src/interfaces.ts index f960a12..eecd1b6 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -36,6 +36,10 @@ export interface NftType { fileType?: string; fileUri?: string; fileName?: string; + itheum_data_preview_url?: string; + itheum_data_stream_url?: string; + itheum_data_marshal_url?: string; + itheum_creator?: string; }; media?: { url: string; diff --git a/tests/bond.test.ts b/tests/bond.test.ts index 7587f83..4f347cb 100644 --- a/tests/bond.test.ts +++ b/tests/bond.test.ts @@ -43,11 +43,11 @@ describe('Bond test', () => { expect(bond).toMatchObject; const sameBond: Bond[] = await bondContract.viewBonds( [tokenIdentifier], - [76] + [172] ); expect(sameBond).toMatchObject; const sameBond2: Bond[] = await bondContract.viewBonds([ - createTokenIdentifier(tokenIdentifier, 76) + createTokenIdentifier(tokenIdentifier, 172) ]); expect(sameBond2).toMatchObject; expect(sameBond).toStrictEqual(sameBond2); @@ -65,7 +65,7 @@ describe('Bond test', () => { expect(compensation).toMatchObject; const compensations: Compensation[] = await bondContract.viewCompensations([ - { tokenIdentifier: tokenIdentifier, nonce: 76 } + { tokenIdentifier: tokenIdentifier, nonce: 172 } ]); expect(compensations).toMatchObject; expect(compensations[0]).toStrictEqual(compensation);