How to Transfer an NFT in your dapp

How to Transfer an NFT in your dapp

December 4 2022

In today’s post, we’ll look at the process of transferring a non-fungible token (NFT) with just a few lines of code using Sequence.

NFTs provide game builders with an array of opportunities to create unique user experiences. Gaming characters, weapons, and wearables are examples of NFTs that players can acquire, earn and trade.

As player ownership of interoperable in-game assets increases with the rise of shared worlds and cross-game economies, new possibilities will open up for buying, selling and transferring NFTs. Users will benefit from being able to transfer their NFTs between games, marketplaces, and wallets.

Adding functionality to enable dapps to transfer NFTs has previously been challenging. This is because the process typically requires a complex data structure, which can involve considerable time and effort to develop.

Sequence allows developers to set up an NFT transfer in mere lines of code, abstracting away the complexities of the movement of in-game assets. Sequence users benefit from its underlying infrastructure, which handles all the heavy lifting and allows developers to focus on creating their best blockchain projects.

Let’s dive in and see just how easy it is!

Feel free to copy and paste the code below to implement it.

If you would like to see the code in action, check out the Sequence demo on GitHub.

Install prerequisites

For this tutorial, you will be using ethers.js.

First, you cd into your dapp’s directory and install Sequence and ethers.js using your favorite package manager.

yarn add 0sequence ethers

or

npm install —-save 0sequence ethers

Instantiate the Sequence Wallet into your dapp

This step is for dapps running on the Ethereum mainnet. Sequence also works with other EVM-compatible chains and testnets; you can find them here.

import { sequence } from “0xsequence”;
const wallet = sequence.initWallet(‘mainnet’)

Connect to the wallet

Allow users to connect to your dapp using the Sequence Wallet.

const wallet = sequence.getWallet()
const connectDetails = await wallet.connect({
 app: 'Your Dapp name',
 authorize: true,
})
console.log('user accepted connect?', connectDetails.connected)
console.log('users signed connect proof to valid their account address:', connectDetails.proof)

This will connect to the default wallet. The Sequence Wallet is highly customizable, with settings allowing you to turn email login on and off, remove payment providers, toggle dark mode and more. You can check out the settings here.

Instantiate an ERC-721 token instance

An ERC-721 token is the Ethereum NFT token standard. You can instantiate one in your dapp using the following:

const erc721Interface = new ethers.utils.Interface([
'function safeTransferFrom(address _from, address _to, uint256 _tokenId)'
])

Create the NFT transaction

First, use encodeFunctionData() to encode the transfer of the NFT tokenId to the recipient.

const address = await wallet.getAddress()
const data = erc721Interface.encodeFunctionData(
 ‘safeTransferFrom’, [address, recipientAddress, tokenId]
)```
Using this encoded data, create a transaction object.
```const transaction = {
to: erc721TokenAddress,
data
}

This will provide you with the transaction data required to sign the NFT transfer.

Send the NFT

const signer = wallet.getSigner()
const txnResponse = await signer.sendTransaction(transaction)

//Wait for the transaction to be validated.

await txnResponse.wait()

Check Etherscan

Running this code will send your NFT to your recipient.

Check your wallets and Etherscan to ensure your NFT has been successfully transferred.

Now start building

In this quick tutorial, we’ve looked at how to transfer an NFT in your dapp using Sequence.

The Sequence SDK is built to make transactions cheap, fast and easy. You can also bundle this NFT transaction with another for cheaper gas.

Sequence allows you to easily integrate multichain ERC-20 tokens, NFTs, SFTs, gasless transactions, onramps, blockchain indexing, web wallets and more.

Get started with Sequence here. If you get stuck at any point, you can always dive into Github or get help on Discord.

We’re here to answer any questions you have along the way.