A guide on how to transfer the ownership of an NFT from one wallet address to another wallet
In the context of this article, we are trying to safely transfer the NFTs from one address to another on server side using the Ethers.js library and safeTransferFrom(from, to, tokenId) method of ERC-721 smart contract. All the transactions as part of this are carried out on Polygon Mumbai Testnet.
Pre — Requisites
1.Deploy your ERC-721 smart contract on any blockchain network
2. Mint an NFT based on the deployed smart contract
Now let’s see how we can transfer the NFT from one wallet address to another!
Step 1: Get some fake MATIC
As blockchain transactions are bound to gas fees, we need to collect some fake MATIC to transfer the NFT.
Get some fake MATIC from here: https://faucet.polygon.technology/
Other MATIC faucets available:
- https://faucet.pearzap.com/
- https://matic.supply/
- https://www.coinclarified.com/tools/faucets/polygon
- https://faucet.firebird.finance/
Step 2: Create .env file
API_URL = “RPC Node URL"
PRIVATE_KEY = "sendor's metamask private key"
PUBLIC_KEY = "sendor's metamask wallet address"
CONTRACT_ADDRESS = "deployed contract address"
USER_ADDRESS = "recipient's wallet address"
Step 3: Create nft-trasnfer.js file
Copy the following content to the file.
tokenId specifies the NFT token to transfer which you will get once you mint an NFT.
safeTransferFrom is an overloaded function. In ethers.js, the syntax to call an overloaded function is different from a non-overloaded function. You can refer to it at here.
const { ethers } = require("ethers");
const contract = require("../artifacts/contracts/ArGram.sol/ArGram.json");
const {
API_URL,
PRIVATE_KEY,
PUBLIC_KEY,
CONTRACT_ADDRESS,
USER_ADDRESS
} = process.env;
const provider = new ethers.providers.JsonRpcProvider(API_URL);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
//Get gas price
const gasPrice = await provider.getGasPrice();
//Grab contract ABI and create an instance
const nftContract = new ethers.Contract(
CONTRACT_ADDRESS,
contract.abi,
wallet
);
//Estimate gas limit
const gasLimit = await nftContract.estimateGas["safeTransferFrom(address,address,uint256)"](PUBLIC_KEY, USER_ADDRESS, tokenId, { gasPrice });
//Call the safetransfer method
const transaction = await nftContract["safeTransferFrom(address,address,uint256)"](PUBLIC_KEY, USER_ADDRESS, tokenId, { gasLimit });
//Wait for the transaction to complete
await transaction.wait();
console.log("Transaction Hash: ", transaction.hash);
You will get a response like below:
Transaction Hash: 0xfcfaf0afb6ed3b38ab7583d90a8fd9b10cb0c17c1218c0668c94c33482283c5f
You can verify the transaction on Mumbai polygonscan.
Great! You have successfully transferred your NFT!