Skip to content

Commit

Permalink
Merge pull request #204 from Akord-com/nft-get-asset
Browse files Browse the repository at this point in the history
Nft get asset
  • Loading branch information
wkolod authored Nov 13, 2023
2 parents 498f054 + c26065b commit cfde23c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
```
</details>

#### `get(nftId, options)`

- `nftId` (`string`, required)
- `options` ([`GetOptions`][get-options], optional)
- returns `Promise<NFT>` - Promise with the nft object

<details>
<summary>example</summary>

```js
const nft = await akord.nft.get(nftId);
```
</details>

#### `listAll(vaultId, options)`

- `vaultId` (`string`, required)
- `options` ([`ListOptions`][list-options], optional)
- returns `Promise<Array<NFT>>` - Promise with all nfts within given vault

<details>
<summary>example</summary>

```js
const nfts = await akord.nft.listAll(vaultId);
```
</details>

#### `list(vaultId, options)`

- `vaultId` (`string`, required)
- `options` ([`ListOptions`][list-options], optional)
- returns `Promise<{ items, nextToken }>` - Promise with paginated nfts within given vault

<details>
<summary>example</summary>

```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 });
```
</details>

#### `getAsset(nftId)`

Get nft asset

- `nftId` (`string`, required)
- returns `Promise<{ data: ArrayBuffer } & FileVersion>` - Promise with nft asset object & data buffer

<details>
<summary>example</summary>

```js
// get nft data buffer
const { data: fileBuffer } = await akord.nft.getAsset(nftId);
```
</details>

#### `getUri(nftId, type)`

Get nft asset uri

- `nftId` (`string`, required)
- `type` ([`StorageType`][storage-type], optional) - storage type, default to arweave
- returns `Promise<string>` - Promise with nft asset uri

<details>
<summary>example</summary>

```js
// get the arweave uri for the nft asset
const arweaveUri = await akord.nft.getUri(nftId);
```
</details>

### contract

Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/nft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
24 changes: 23 additions & 1 deletion src/core/nft.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -67,6 +67,28 @@ class NFTService extends NodeService<NFT> {
const { nodeId, transactionId, object } = await service.nodeCreate<NFT>(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<FileVersion & { data: ArrayBuffer }> {
const nft = new NFT(await this.api.getNode<NFT>(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<string> {
const nft = new NFT(await this.api.getNode<NFT>(nftId, this.objectType));
return nft.getUri(type);
}
};

export const nftMetadataToTags = (metadata: NFTMetadata): Tags => {
Expand Down
6 changes: 5 additions & 1 deletion src/types/nft.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions src/types/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class FileVersion extends Encryptable implements Version {
chunkSize?: number;
owner: string;
createdAt: string;
status: string;

constructor(fileVersionProto: any, keys?: Array<EncryptedKeys>, publicKey?: string) {
super(keys, publicKey);
Expand Down

0 comments on commit cfde23c

Please sign in to comment.