ERC721Wrapper.sol 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../ERC721.sol";
  4. import "../utils/ERC721Holder.sol";
  5. /**
  6. * @dev Extension of the ERC721 token contract to support token wrapping.
  7. *
  8. * Users can deposit and withdraw an "underlying token" and receive a "wrapped token" with a matching tokenId. This is useful
  9. * in conjunction with other modules. For example, combining this wrapping mechanism with {ERC721Votes} will allow the
  10. * wrapping of an existing "basic" ERC721 into a governance token.
  11. *
  12. * _Available since v4.9.0_
  13. */
  14. abstract contract ERC721Wrapper is ERC721, ERC721Holder {
  15. IERC721 private immutable _underlying;
  16. constructor(IERC721 underlyingToken) {
  17. _underlying = underlyingToken;
  18. }
  19. /**
  20. * @dev Allow a user to deposit underlying tokens and mint the corresponding tokenIds.
  21. */
  22. function depositFor(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  23. bytes memory data = abi.encodePacked(account);
  24. uint256 length = tokenIds.length;
  25. for (uint256 i = 0; i < length; ++i) {
  26. underlying().safeTransferFrom(_msgSender(), address(this), tokenIds[i], data);
  27. }
  28. return true;
  29. }
  30. /**
  31. * @dev Allow a user to burn wrapped tokens and withdraw the corresponding tokenIds of the underlying tokens.
  32. */
  33. function withdrawTo(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  34. uint256 length = tokenIds.length;
  35. for (uint256 i = 0; i < length; ++i) {
  36. uint256 tokenId = tokenIds[i];
  37. require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721Wrapper: caller is not token owner or approved");
  38. _burn(tokenId);
  39. // Checks were already performed at this point, and there's no way to retake ownership or approval from
  40. // the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
  41. // slither-disable-next-line reentrancy-no-eth
  42. underlying().safeTransferFrom(address(this), account, tokenId);
  43. }
  44. return true;
  45. }
  46. /**
  47. * @dev Overrides {IERC721Receiver-onERC721Received} to allow minting on direct ERC721 transfers to
  48. * this contract.
  49. *
  50. * In case there's data attached, it validates that the operator is this contract, so only trusted data
  51. * is accepted from {depositFor}.
  52. *
  53. * WARNING: Doesn't work with unsafe transfers (eg. {IERC721-transferFrom}). Use {ERC721Wrapper-_recover}
  54. * for recovering in that scenario.
  55. */
  56. function onERC721Received(
  57. address operator,
  58. address from,
  59. uint256 tokenId,
  60. bytes memory data
  61. ) public override returns (bytes4) {
  62. require(address(underlying()) == _msgSender(), "ERC721Wrapper: caller is not underlying");
  63. if (data.length > 0) {
  64. require(data.length == 20 && operator == address(this), "ERC721Wrapper: Invalid data format");
  65. from = address(bytes20(data));
  66. }
  67. _safeMint(from, tokenId);
  68. return IERC721Receiver.onERC721Received.selector;
  69. }
  70. /**
  71. * @dev Mint a wrapped token to cover any underlyingToken that would have been transferred by mistake. Internal
  72. * function that can be exposed with access control if desired.
  73. */
  74. function _recover(address account, uint256 tokenId) internal virtual returns (uint256) {
  75. require(underlying().ownerOf(tokenId) == address(this), "ERC721Wrapper: wrapper is not token owner");
  76. _safeMint(account, tokenId);
  77. return tokenId;
  78. }
  79. /**
  80. * @dev Returns the underlying token.
  81. */
  82. function underlying() public view virtual returns (IERC721) {
  83. return _underlying;
  84. }
  85. }