ERC721:当将代币从账户发送到市场时,转账调用者既不是所有者,也不是批准的



我正试图将我的NFT转移到市场

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;
}
}

当我运行我的web3mode代码如下:

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);
// putItemToResll is a Marketplace function, that uses transferToken(*) 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;
}
}

我将收到一个错误ERC721: transfer caller is not owner nor approved。这个线程(ERC721:transfer caller既不是所有者也不是批准的(建议我应该approve市场代表我调用transferFrom(),但我认识到我的NFT合同中有approve方法,超级-超一流。有什么办法解决这个问题吗?

是的,您的假设是正确的。如果你希望另一份合同代表你转让NFT,你需要批准该合同。

你可以看到功能:

function setApprovalForAll(address _operator, bool _approved) external;

这是ERC721标准的一部分。

因此,为了让你在市场上代表他转让用户NFT,例如,你想先打电话给

yourNftContract.setApprovalForAll(marketplaceContractAddress, true)

同样,如果你试图在公海上出售NFT,你首先必须进行审批交易才能授权公海合同。

相关内容

  • 没有找到相关文章

最新更新