[go: up one dir, main page]

@unique-nft/sdk
TypeScript icon, indicating that this package has built-in type declarations

2.4.1 • Public • Published

Unique Network SDK (thin-client)

This package is a thin client for Unique Network. It is a wrapper around the Substrate RPC API that provides a more convenient API for interacting with Unique Network.

You can read more about working with SDK and Unique Network in the official documentation

Install

npm install @unique-nft/sdk

Usage

Create a Unique Chain Instance

To establish a connection, you may use one of the publicly available endpoints:

Network Endpoint
Unique https://rest.unique.network/v2/unique
Quartz https://rest.unique.network/v2/quartz
Opal https://rest.unique.network/v2/opal
import { UniqueChain } from '@unique-nft/sdk'
import { Sr25519Account } from '@unique-nft/utils/sr25519'

// Create an account from a mnemonic
const account = Sr25519Account.fromUri('your mnemonic phrase here')

// Initialize UniqueChain with the account
const uniqueChain = UniqueChain({baseUrl: 'https://rest.unique.network/v2/opal'})

Managing Balances

Get Balance

Retrieve the balance details of a specific account.

const balance = await uniqueChain.balance.get({ address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" })

Transfer Balance

Transfer a specified amount from one account to another. The SDK supports transferring amounts in both Wei (smallest unit) and Coin (standard unit).

Transfer balance in wei:

const transferResult = await uniqueChain.balance.transfer(
  {
    to: toAddress,
    amount, // Amount in Wei
  },
  { signerAddress: fromAddress },
  aliceAccount, // Signer account
)

Transfer Balance in coins:

const transferResult = await uniqueChain.balance.transfer(
  {
    to: toAddress,
    amount, // Amount in Coins
    isAmountInCoins: true,
  },
  { signerAddress: fromAddress },
  aliceAccount, // Signer account
)

Managing Collections and NFTs

Creating a Collection

Create a new collection on the Unique Network. Collections can be used to group related NFTs.

const collectionResult = await uniqueChain.collection.create({
  mode: 'Nft',
  name: 'My Collection',
  description: 'A description for my collection',
  symbol: 'MC',
  info: {
    cover_image: { url: 'https://ipfs.unique.network/ipfs/Qmau5RNqJMf6bR5mcatnAZGaF1hhiCY1UoaFctfw7rMhTU' },
  },
})

Minting NFTs

Mint new NFTs within a specific collection.

const mintResult = await uniqueChain.token.mintNFTs({
  collectionId,
  tokens: [
    {
      data: {
        image: 'https://ipfs.unique.network/ipfs/QmRJYgnXfo9WfTckYk8J1h4NTC35VBBGLsjhpkkFDuzQ9L',
        attributes: [{ trait_type: 'Power', value: '10' }],
      },
      owner: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
    },
    {
      data: {
        image: 'https://ipfs.unique.network/ipfs/QmduruSNgY9h13p2q6hnsaot9vgVxYVzg9DL7ZWxZ6U5w6',
        attributes: [{ trait_type: 'Speed', value: '8' }],
      },
      owner: '5H5ymQAo198Ap2rVGG7mNfAVkomFG98FdwL4mz8XqAJeGHHt',
    },
  ],
})

Transferring NFTs

Transfer ownership of an NFT from one account to another.

const transferResult = await uniqueChain.token.transfer(
  {
    to: '5H5ymQAo198Ap2rVGG7mNfAVkomFG98FdwL4mz8XqAJeGHHt',
    collectionId: 1,
    tokenId: 1,
  },
  { signer: alice },
)

Updating NFT Attributes and Image

Modify the attributes or image of an existing NFT.

const updateResult = await uniqueChain.token.updateNft({
  collectionId: 1,
  tokenId: 1,
  data: {
    attributes: [{ trait_type: 'Power', value: '15' }],
    image: 'https://ipfs.unique.network/ipfs/QmNewImageURL',
  },
})

Burning NFTs

Remove an NFT permanently from the collection.

const burnResult = await uniqueChain.token.burn({
  collectionId: 1,
  tokenId: 2,
})

Nesting and Unnesting NFTs

Nest one NFT within another, allowing for hierarchical ownership structures.

const parent = { collectionId: 1, tokenId: 1 }
const nested = { collectionId: 1, tokenId: 2 }

const nestResult = await uniqueChain.token.nest({
  parent,
  nested,
})

Approving Spenders for NFTs

Authorize another account to manage or transfer your NFTs.

const approveResult = await uniqueChain.token.approve({
  spender: '5H5ymQAo198Ap2rVGG7mNfAVkomFG98FdwL4mz8XqAJeGHHt',
  collectionId: 1,
  tokenId: 1,
})

const isApproved = await uniqueChain.token.getApproved({
  collectionId: 1,
  tokenId: 1,
  spender: '5H5ymQAo198Ap2rVGG7mNfAVkomFG98FdwL4mz8XqAJeGHHt',
})

Managing EVM Contracts

The Unique Network SDK provides robust methods to manage EVM-compatible contracts, including deploying contracts, interacting with them, and handling NFT collections within the EVM environment. Below are the key functionalities and how to use them.

Read more about how Substrate accounts work with EVM in the documentation.

Creating an NFT Collection via EVM

Create a new NFT collection using the EVM-compatible methods.

const collectionCreationFee = await uniqueChain.evm.collectionHelpers.call({
  functionName: 'collectionCreationFee',
  functionArgs: [],
})

console.log('Collection Creation Fee:', collectionCreationFee)

const collectionResult = await uniqueChain.evm.collectionHelpers.send({
  functionName: 'createNFTCollection',
  functionArgs: ['Test Collection', 'Description of Test Collection', 'TC'],
  value: collectionCreationFee, // Ensure to send the required fee
})

Deploying and Interacting with Smart Contracts

Deploy EVM-compatible smart contracts and interact with their functions seamlessly using the SDK.

const abi = [
  {
    "inputs": [
      {
        "internalType": "uint256",
        "name": "num",
        "type": "uint256"
      }
    ],
    "name": "store",
  },
  //...
]

const deployResult = await uniqueChain.evm.deploy({
  bytecode: storageArtifacts.bytecode,
})

const contractAddress = deployResult.result.contractAddress
console.log('Contract Deployed at:', contractAddress)

// Verify the contract exists
const { exists } = await uniqueChain.evm.contractExists({ address: contractAddress })
console.log('Contract Exists:', exists)

// Interact with the contract's `store` function
const storeTx = await uniqueChain.evm.send({
  functionName: 'store',
  functionArgs: [123n],
  contract: {
    address: contractAddress,
    abi,
  },
})

Blockchain Indexer

The Unique Network SDK includes a typed wrapper around Axios to interact seamlessly with the Unique Network blockchain indexer API. This allows you to fetch and query blockchain data efficiently.

You can read more about working with Indexer and Unique Network in the official documentation

API Endpoints

Network Endpoint URL
Unique https://api-unique.uniquescan.io/v2
Quartz https://api-quartz.uniquescan.io/v2

SDK Initialization

Initialize the UniqueIndexer client with the desired base URL to start interacting with the blockchain indexer API.

import { UniqueIndexer } from '@unique-nft/sdk';

const indexerClient = UniqueIndexer({ baseUrl: 'https://api-unique.uniquescan.io/v2' });

Usage Examples

Fetch the Last 5 Blocks

Retrieve the latest 5 blocks in descending order by block number.

const blocks = await indexerClient.blocks({ limit: 5, orderByNumber: 'desc' });

Search Collections

Retrieve collections based on various search criteria like name, description, admin, owner, and sponsor.

const collections = await indexerClient.collections({
  nameLike: '%substra%',
  descriptionLike: '%First NFT collection%',
  adminIn: ['5F6TPxrxZBhhpvRA8Lu1PWjcpoeoEkAQ4TVALpaxgenTU3sM'],
  ownerIn: ['5H684Wa69GpbgwQ7w9nZyzVpDmEDCTexhRNmZ7mkqM1Rt7dH'],
  sponsorIn: ['5H684Wa69GpbgwQ7w9nZyzVpDmEDCTexhRNmZ7mkqM1Rt7dH'],
  isBurned: false,
  orderByCollectionId: 'asc',
  orderByName: 'asc',
});

Search NFTs

Retrieve NFTs based on criteria such as collection ID, token ID, ownership, royalty recipients, and attributes.

const nfts = await indexerClient.nfts({
  collectionIdIn: ['1', '0x17C4e6453cC49AAaaEaCA894E6D9683e00000001'],
  tokenIdIn: [1],
  isBurned: false,
  royaltyRecipientIn: ['5Gus5r7HSZv9ScdaTNVbFMBEsxMtc4cZBPTLfJJbLXQK8m9d'],
  attributeTraitTypeIn: ['traits'],
  attributeValueIn: ['Up Hair', 'Teeth Smile'],
  isBundle: false,
  ownerIn: ['5FZeTmbZQZsJcyEevjGVK1HHkcKfWBYxWpbgEffQ2M1SqAnP'],
  orderByTokenId: 'asc',
});

Package Sidebar

Install

npm i @unique-nft/sdk

Weekly Downloads

269

Version

2.4.1

License

MIT

Unpacked Size

9.91 MB

Total Files

40

Last publish

Collaborators

  • ut-akuznetsov
  • nonfungible
  • lach
  • ashkuc
  • max.andreev