Currently, I am in the process of transferring my NFT to a marketplace
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "hardhat/console.sol";
contract NFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address contractAddress;
constructor(address marketplaceAddress) ERC721("Heliopolis NFT", "HNFT") {
contractAddress = marketplaceAddress;
}
function createToken(string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
setApprovalForAll(contractAddress, true);
return newItemId;
}
function transferToken(address from, address to, uint256 tokenId) external {
require(ownerOf(tokenId) == from, "From address must be token owner");
_transfer(from, to, tokenId);
}
function getContractAddress() public view returns (address) {
return contractAddress;
}
}
As part of my implementation using web3modal:
import { ethers } from 'ethers';
import Web3Modal from 'web3modal';
import { nftMarketplaceAddress, nftAddress } from 'utils/contracts';
import { nftMarketplaceAbi } from 'utils/nftMarketplaceAbi';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const resellNft = async (nft: any) => {
try{
const web3Modal = new Web3Modal();
const connection = await web3Modal.connect();
const provider = new ethers.providers.Web3Provider(connection);
const signer = provider.getSigner();
const contract = new ethers.Contract(nftMarketplaceAddress, nftMarketplaceAbi, signer);
// putItemToResell invokes the transferToken(*) method mentioned above
const transaction = await contract.putItemToResell(nftAddress, nft.tokenId, nft.price,
{
gasLimit: 1000000,
gasPrice: ethers.utils.parseUnits("10", "gwei"),
value: ethers.utils.parseUnits("0.001", "ether"),
}
);
const response = await transaction.wait();
console.log(response);
}catch (e:any){
throw e;
}
}
If an error such as
ERC721: transfer caller is not owner nor approved
shows up, refers to this thread (ERC721: transfer caller is not owner nor approved) suggesting granting approval to the marketplace for invoking transferFrom()
on your behalf. It's worth noting that the NFT contract includes the approve
method inherited from its super class. Is there a way around this issue?