0

I have the following code:

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract Collectioning is ERC721 {
...
address ownerAddress = ownerOf(_tokenId);
address payable pSender = payable(msg.sender);
transferFrom(ownerAddress, pSender, _tokenId);
address payable ownerAddressPayable = _make_payable(ownerAddress);
ownerAddressPayable.transfer(itemCost);
if(msg.value > starCost) {
  pSender.transfer(msg.value - itemCost);
}

transferFrom includes also the approval by the owner, but I keep getting this:

ERC721: transfer caller is not owner nor approved.

I cannot debug to the console (don't know how), so the only clue I have is that the error comes from somwewhere in this block of code, triggered buy one of the transfers.

1

The error is coming from here: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/0d40f705a7d4a42ff622ae3a0e1a90305fc5b93e/contracts/token/ERC721/ERC721.sol#L153

The error means that you can't issue transferFrom from an account which hasn't approved the transfer (or you're not the owner). Whenever you use transferFrom, the token owner has to first call the approve function to approve the transfer.

You're saying that "transferFrom includes also the approval by the owner", but unless you have overwritten the function, it doesn't do approvals - it only uses them. And regardless, you can't approve and trasferFrom in the same contract for one token transfer, since the msg.sender would be incorrect.

2
  • Thank you. I don't require you to write down code, but how would I go to solve this? – LowFieldTheory May 3 at 10:25
  • You should research how the approve + transferFrom pattern works. If you just want to transfer your tokens to another address, use the simple transfer function. But if you want a two-step transfer (you approve someone else to withdraw your tokens + that someone else withdraws them), you need transactions from two different entities – Lauri Peltonen May 3 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.