SDK

A Javascript SDK to simplify integration into Dapps

Having defined a standard for NFTs allows creating pre-built tools to interact with them. The SDK developed by our team serves this purpose, it allows creating, reading, and managing factory Cells and NFTs in a simple way, so Dapps developers can adopt our standard without the need to get into the transaction implementation details. The source code along with examples of usage are available in Github.

Installing and setup

The SDK is available on npm package manager, it can be installed by running:

npm install @rather-labs/nrc-721-sdk

To run the examples available in the repository a testnet address and private key will be needed, they can be generated using this tool. We will also need some ckb to create the cells, on testnet we can get this ckb by inserting our address in this faucet.

The examples are set up to connect to a node and indexer running locally, a local testnet or dev node can be run following the instructions here, and here to run a local indexer. Nervos also maintains a public testnet node and indexer that can be used just by setting the URLs:

nodeUrl = "http://3.235.223.161:18114";
indexerUrl = "http://3.235.223.161:18116";

Minting a Factory Cell

To be able to create NFTs we first need to mint a factory cell, that will be used to mint NFTs from it and store common info, so it acts as a collection. Unless some custom functionality is needed, the factory can be created using the Type ID logic that is built into Nervos blockchain, so no contract deployment is needed.

The factory can be minted just by indicating the basic info it will contain:

const { rawTransaction, typeScript, usedCapacity } = 
  await factoryCell.mint({
    name: "Test token factory",
    symbol: "TTF",
    baseTokenUri: "http://testtoken.com",
    sourceAddress: OWNER_ADDRESS,
    targetAddress: OWNER_ADDRESS,
    fee: 0.0001,
  });
  

the full example code can be found here. After minting our factory cell we will have its typeScript that will be used to identify our cell in the blockchain and will be needed to mint our NFTs.

Now we have our factory and from the previous section we should also have a deployed contract to govern our NFTs, so we are ready to mint our tokens, we just need the typeScript of our contract cell. We have also deployed our base NRC-721 contract in the testnet and is available to anyone to test the SDK integration, you can find its typeScript in our Github.

We are now ready to mint our NFTs using this example, indicating our contract and factory typeScripts. A data object can also be included, by default it will be serialized using JSON and stored in the data part of the NFT cell:

const { rawTransaction, nftTypeScript, usedCapacity } = 
  await nftCell.mint({
    nftContractTypeScript,
    factoryTypeScript,
    sourceAddress: OWNER_ADDRESS,
    targetAddress: OWNER_ADDRESS,
    fee: 0.0001,
    data: {
      someKey: "SomeValue",
      anotherKey: "AnotherValue",
    },
});

The minted token can be retrieved from the blockchain using the SDK as shown in this example. We will be able to get all the info for the token and factory, so we can now use it on our Dapp:

Nft Token Id:  d9ae00e88a7c2c701df1a7ee410b05a67c0a13fc50bfa0bbe05553d4ddb7e75e
Nft Token URI:  
http://testtoken.com/d9ae00e88a7c2c701df1a7ee410b05a67c0a13fc50bfa0bbe05553d4ddb7e75e
Nft Cell Data:  { someKey: 'SomeValue', anotherKey: 'AnotherValue' }
Factory Data:  {
  name: 'Test token factory',
  symbol: 'TTF',
  baseTokenUri: 'http://test-token.com'
}

Last updated