Skip to main content

Building the NFT dApp

If it's is your first time building an Archway dApp frontend, head over to the Creating Your First dApp guide to learn how to setup your basic project scaffolding. Once you're serving a web page that connects to Keplr, you'll be ready to follow the next steps.

Loading NFT the collection#

As we saw in the previous guide, to query a contract we need to know its address on chain. We also need an instance of SigningCosmWasmClient from @cosmjs/cosmwasm-stargate that's been connected to an Archway network such as Constantine testnet.

// For more on setting up this handler see the "Creating Your First dApp" guide (linked to above):const queryHandler = CosmWasmClient.queryClient.wasm.queryContractSmart;// Replace this empty string with your deployed contract addressconst contract = "";

Using the above two variables we can write JavaScript to query the NFT collection for its token ids like this:

async function getNfts() {  let entrypoint = {    all_tokens: {}  };  let query = await queryHandler(contract, entrypoint);  console.log('All NFTs of contract ' + contract + ':', query);  return query;}

The getNfts function returns a tokens object containing an array of all tokens minted by the contract.

Here's an example of calling it:

let nfts;async function doGetNfts() {  nfts = await getNfts();  console.log('All NFT ids', nfts);}doGetNfts();

Minting from the dApp#

To mint from our dApp, we need to assemble the metadata fields for the NFT. We can achieve this by making a web UI with form fields where the NFT creator can add custom traits to their NFT.

We also need the calculateFee and GasPrice functions from the @cosmjs/stargate package.

const gasPrice = GasPrice.fromString('0.002uconst');const txFee = calculateFee(300000, this.gas.price);

We can simplify our remaining task by writing a function which takes the required NFT metadata as parameters. With that in mind, here's what a minting function looks like in JavaScript:

async function mintNft(owner, tokenId, imageUrl, name, description, attributes = []) {  // Ensure we collected the required arguments  if (!owner || !tokenId || !imageUrl || !name || !description) {    console.warn('Error parsing mint arguments', { owner, tokenId, imageUrl, name, description });    return;  }
  // Prepare Tx  let entrypoint = {    mint: {      token_id: String(nfts.tokens.length),      owner: owner,      extension: {        name: name,        description: description,        image: imageUrl      }    }  };
  // If the creator used any arbitrary custom attributes, add them to the entrypoint  if (attributes.length) {    entrypoint.mint.extension.attributes = attributes;  }
  try {    // Send Tx    let tx = await CosmWasmClient.execute(owner, contract, entrypoint, txFee);    console.log('Mint Tx', tx);    // Refresh the NFT collection to resolve the new token    await getNfts();  } catch (e) {    console.warn('Error executing mint tx', e);  }}

Here's an example of what it might be like to call our mintNft function:

// Example call:async function doMintNft() {  let mintTx = await mintNft(    owner = 'archway1f395p0gg67mmfd5zcqvpnp9cxnu0hg6r9hfczq',     tokenId = '2',     imageUrl = "ipfs://QmY8Ei2NQkMg8rCFiUkcXH2NPWPNKMoWLBYDwBeMNsP5sr",     name = "Archway NFT #2",     description = "Building With NFTs (and dApps)",     attributes = [{"trait_type": "tutorial","value": "https://docs.archway.io/docs/create/guides/nft-project/deploy"}]  );  console.log('Mint tx', mintTx);}doMintNft();

Transferring NFTs#

Now that we can mint from our dApp, the form and entrypoint parameters for transferring NFTs are much simpler. Just like transfers initiated from the Developer CLI, we need the recipient's Archway address and the token_id we are sending to them. We also need to know the address of the current owner, and the references to the contract address and txFee that we set up earlier.

Here's what a JavaScript function to transfer an Archway NFT looks like:

async function transferNft(owner, recipient, tokenId) {  if (!owner || !recipient || !tokenId) {    console.warn('Error parsing transfer_nft arguments', { owner, recipient, tokenId});    return;  }  // Prepare Tx  let entrypoint = {    transfer_nft: {      recipient: recipient,      token_id: tokenId    }  };  // Send Tx  try {    let tx = await CosmWasmClient.execute(owner, contract, entrypoint, txFee);    console.log('Transfer Tx', tx);    // Refresh the NFT collection    await getNfts();  } catch (e) {    console.warn("Error executing NFT transfer", e);  }}

Calling the above function might look like this:

// Example call:async function doTransferNft() {  let transferTx = await transferNft(    owner = 'archway1f395p0gg67mmfd5zcqvpnp9cxnu0hg6r9hfczq',     recipient = 'archway1y00hm50lffnxt5m0kuy9afk83gyuye684zwcr5',     tokenId = '2'  );  console.log('Transfer tx', transferTx);}doTransferNft();

Displaying NFTs#

The getNfts function we created returns all token ids minted by the contract, but one more step is needed before we can display NFTs from our collection in our dApp. To read an NFT's metadata we call the nft_info entrypoint we executed from CLI in the previous lesson.

Here's what loading token metadata looks like using JavaScript:

async function getTokenMeta(tokenId) {  if (typeof tokenId !== 'string') {    console.warn('Invalid token ID. Token ID must be a string, but got ' + typeof tokenId);    return;  }  let entrypoint = {    nft_info: {      token_id: tokenId    }  };  let query = await this.handlers.query(contract, entrypoint);  console.log('NFT contract succesfully  queried for token ID ' + tokenId, query);  return query;}

An example of calling the getTokenMeta function might look like this:

// Example call:async function doGetTokenMeta() {  let token2 = await getTokenMeta('2');  console.log('Metadata for token_id: "2"', token2);}doGetTokenMeta();
info

Note: If your NFT image was uploaded to IPFS, using an IPFS url like ipfs://... your web application will need to resolve the image source from an IPFS gateway (read more about IPFS gateways and providers).

Clone it and try yourself#

This guide focused on core features of Archway NFTs, some basic aspects of web development haven't been looked at in detail, but you'll find working example frontends for this project in the dApp examples repository.