6

The code below produces an error:

code:

 function safeTransferFrom(
    address _from,
    address _to,
    uint256 _tokenId,
    bytes calldata _data
  )
   external
  {
    _safeTransferFrom(_from, _to, _tokenId, _data);
  }

The error is:

Invalid type for arguement in function call. Invalid implicit 
conversion from bytes calldata to bytes storage pointer requested.

It would be very helpful if someone could explain both the problem and the solution!

1
  • can you provide what is inside _safeTransferFrom ? Commented Jul 31, 2019 at 9:13

2 Answers 2

1

That error will appear if you have declared the _data parameter of _safeTransferFrom as bytes storage. A way to fix it is to declare it as memory

  function _safeTransferFrom(
    address _from,
    address _to,
    uint256 _tokenId,
    bytes memory _data       // _data is declared as memory
  )
1

judging by that error, it seems _safeTransferFrom has a signature that includes something along the lines of (bytes storage _data).

When you pass in bytes that you've allocated to calldata, and then pass it in as an argument to a function that expects a pointer to storage, you're doing something that the solidity compiler refuses to handle for you (and would be inefficient even if it did, honestly): it won't take the argument, write it to calldata, and then copy that argument over to storage for you silently in order to pass in the proper storage pointer to the function you then call with the data.

But hey: if it has to be in storage anyways, no reason to allocate it to both locations.

Solidity is funny about calldata in this way, but it will implicitly copy from memory to storage, as per the other answer. But you'd probably get the best gas savings by just declaring the argument in your function as storage, actually, as opposed to memory (as suggested in other answer, which compiler will copy for you) or calldata (as you're attempting here, and compiler refuses to handle).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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