diff --git a/README.md b/README.md index d0b70581..22e1e882 100644 --- a/README.md +++ b/README.md @@ -1378,6 +1378,86 @@ const nft = await akord.nft.get(nftId); const nftUri = nft.asset.getUri(StorageType.ARWEAVE); // After few minutes, you should be able to view your NFT on: https://viewblock.io/arweave/tx/{nftUri} ``` + + +#### `get(nftId, options)` + +- `nftId` (`string`, required) +- `options` ([`GetOptions`][get-options], optional) +- returns `Promise` - Promise with the nft object + +
+ example + +```js +const nft = await akord.nft.get(nftId); +``` +
+ +#### `listAll(vaultId, options)` + +- `vaultId` (`string`, required) +- `options` ([`ListOptions`][list-options], optional) +- returns `Promise>` - Promise with all nfts within given vault + +
+ example + +```js +const nfts = await akord.nft.listAll(vaultId); +``` +
+ +#### `list(vaultId, options)` + +- `vaultId` (`string`, required) +- `options` ([`ListOptions`][list-options], optional) +- returns `Promise<{ items, nextToken }>` - Promise with paginated nfts within given vault + +
+ example + +```js +// retrieve first 100 nfts for the vault +const { items } = await akord.nft.list(vaultId); + +// retrieve first 20 nfts for the vault +const { items } = await akord.nft.list(vaultId, { limit: 20 }); +``` +
+ +#### `getAsset(nftId)` + +Get nft asset + +- `nftId` (`string`, required) +- returns `Promise<{ data: ArrayBuffer } & FileVersion>` - Promise with nft asset object & data buffer + +
+ example + +```js +// get nft data buffer +const { data: fileBuffer } = await akord.nft.getAsset(nftId); +``` +
+ +#### `getUri(nftId, type)` + +Get nft asset uri + +- `nftId` (`string`, required) +- `type` ([`StorageType`][storage-type], optional) - storage type, default to arweave +- returns `Promise` - Promise with nft asset uri + +
+ example + +```js +// get the arweave uri for the nft asset +const arweaveUri = await akord.nft.getUri(nftId); +``` +
### contract diff --git a/src/__tests__/nft.test.ts b/src/__tests__/nft.test.ts index 8b4bad4b..16bd950c 100644 --- a/src/__tests__/nft.test.ts +++ b/src/__tests__/nft.test.ts @@ -55,5 +55,15 @@ describe("Testing NFT functions", () => { expect(nft.asset.udl?.licenseFee?.type).toEqual(udl.licenseFee?.type); expect(nft.asset.udl?.licenseFee?.value).toEqual(udl.licenseFee?.value); expect(nft.asset.getUri(StorageType.ARWEAVE)).toBeTruthy(); + + const { data } = await akord.nft.getAsset(nftId); + expect(data).toEqual(await file.arrayBuffer()); + + const fileBuffer = await akord.file.get(nft.asset.getUri(StorageType.S3), nft.vaultId); + expect(fileBuffer).toEqual(await file.arrayBuffer()); + + const assetUri = await akord.nft.getUri(nftId); + expect(assetUri).toBeTruthy(); + expect(assetUri).toEqual(nft.asset.getUri(StorageType.ARWEAVE)); }); }); diff --git a/src/core/nft.ts b/src/core/nft.ts index 25f974ac..d9699d6c 100644 --- a/src/core/nft.ts +++ b/src/core/nft.ts @@ -1,5 +1,5 @@ import { NodeService } from "./node"; -import { nodeType } from "../types/node"; +import { FileVersion, StorageType, nodeType } from "../types/node"; import { StackCreateOptions } from "./stack"; import { FileLike } from "../types/file"; import { FileService } from "./file"; @@ -67,6 +67,28 @@ class NFTService extends NodeService { const { nodeId, transactionId, object } = await service.nodeCreate(state, { parentId: options.parentId }); return { nftId: nodeId, transactionId, object }; } + + /** + * Get NFT asset + * @param {string} nftId + * @returns Promise with NFT asset + */ + public async getAsset(nftId: string): Promise { + const nft = new NFT(await this.api.getNode(nftId, this.objectType)); + const { fileData } = await this.api.downloadFile(nft.getUri(StorageType.S3), { public: true }); + return { data: fileData, ...nft.asset } as FileVersion & { data: ArrayBuffer }; + } + + /** + * Get NFT asset uri + * @param {string} nftId + * @param {StorageType} [type] storage type, default to arweave + * @returns Promise with NFT asset uri + */ + public async getUri(nftId: string, type: StorageType = StorageType.ARWEAVE): Promise { + const nft = new NFT(await this.api.getNode(nftId, this.objectType)); + return nft.getUri(type); + } }; export const nftMetadataToTags = (metadata: NFTMetadata): Tags => { diff --git a/src/types/nft.ts b/src/types/nft.ts index d15f730d..04aa4433 100644 --- a/src/types/nft.ts +++ b/src/types/nft.ts @@ -1,5 +1,5 @@ import { AssetMetadata } from "./asset"; -import { FileVersion, Node } from "./node"; +import { FileVersion, Node, StorageType } from "./node"; export class NFT extends Node { ticker: string; @@ -25,6 +25,10 @@ export class NFT extends Node { this.claimable = nodeLike.claimable; this.asset = new FileVersion(nodeLike.asset); } + + getUri(type: StorageType = StorageType.ARWEAVE): string { + return this.asset.getUri(type); + } } export type Claim = { diff --git a/src/types/node.ts b/src/types/node.ts index 3e84c905..fe4dcae3 100644 --- a/src/types/node.ts +++ b/src/types/node.ts @@ -133,6 +133,7 @@ export class FileVersion extends Encryptable implements Version { chunkSize?: number; owner: string; createdAt: string; + status: string; constructor(fileVersionProto: any, keys?: Array, publicKey?: string) { super(keys, publicKey);