I am trying to call ERC721 from another smart contract, while calling function approveAndTransfer ,I am getting “ERC721: transfer caller is not owner nor approved”, not sure reason behind it,
As require(nft.ownerOf(_tokenId) == msg.sender, “NOT OWNER”); is working as expected and not throwing any error, but source of error is _tranfer function which gets called from nft.safeTransferFrom(msg.sender, _to,_tokenId);
not sure what mistake I am making. I have NFT in my wallet, but I am not owner of external smart contract.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/IERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/IERC721Receiver.sol";
contract Tat is IERC721Receiver {
constructor() public {}
function approveAndTransfer(IERC721 nft, uint256 _tokenId, address _from, address _to) public {
require(nft.ownerOf(_tokenId) == msg.sender, "NOT OWNER ");
nft.isApprovedForAll(_from, address(this));
nft.safeTransferFrom(msg.sender, _to,_tokenId);
}
function ownerOf(IERC721 nft, uint256 tokenId) external view returns (address owner) {
return nft.ownerOf(tokenId);
}
function test() public view returns(address sc,address own){
return (address(this),msg.sender);
}
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes memory _data) public override returns(bytes4) {
return 0x150b7a02;
}
}
Notice that it is inheriting functions, and then overriding them from the interfaces.
I think you probably have done that already, but what about the opposing ERC721 contract? Does it have the functions to support what you are trying to do?
When you call isApprovedForAll, what is the result? Does it return true or false?
Then move to safeTransferFrom, make sure the _transfer is standard and doesn’t have anything different going on in it.
After taking a look, and you still can’t figure it out, please post the ERC721 contract code and I can take a look at what’s going on in their functions.
Hi, I'm having the same problem, I'm calling from another contract running the safeTransferFrom and I'm getting the same error message....
Did you solve it with the approve function?
In my specific case, I'm doing the safeTransferFrom with the admin of the contract, I don't understand why I get the error message.
When you are using safeTransferFrom you need to make sure that current owner details should be fetch from network using ownerOf(_tokenId)
Try changing params of SafeTransferFrom like this:
nftAddress.safeTransferFrom(ownerOf(_tokenId), _to,_tokenId);