ERC721Wrapper.sol 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/extensions/ERC721Wrapper.sol)
  3. pragma solidity ^0.8.19;
  4. import "../ERC721.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, IERC721Receiver {
  15. IERC721 private immutable _underlying;
  16. /**
  17. * @dev The received ERC721 token couldn't be wrapped.
  18. */
  19. error ERC721UnsupportedToken(address token);
  20. constructor(IERC721 underlyingToken) {
  21. _underlying = underlyingToken;
  22. }
  23. /**
  24. * @dev Allow a user to deposit underlying tokens and mint the corresponding tokenIds.
  25. */
  26. function depositFor(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  27. uint256 length = tokenIds.length;
  28. for (uint256 i = 0; i < length; ++i) {
  29. uint256 tokenId = tokenIds[i];
  30. // This is an "unsafe" transfer that doesn't call any hook on the receiver. With underlying() being trusted
  31. // (by design of this contract) and no other contracts expected to be called from there, we are safe.
  32. // slither-disable-next-line reentrancy-no-eth
  33. underlying().transferFrom(_msgSender(), address(this), tokenId);
  34. _safeMint(account, tokenId);
  35. }
  36. return true;
  37. }
  38. /**
  39. * @dev Allow a user to burn wrapped tokens and withdraw the corresponding tokenIds of the underlying tokens.
  40. */
  41. function withdrawTo(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  42. uint256 length = tokenIds.length;
  43. for (uint256 i = 0; i < length; ++i) {
  44. uint256 tokenId = tokenIds[i];
  45. if (!_isApprovedOrOwner(_msgSender(), tokenId)) {
  46. revert ERC721InsufficientApproval(_msgSender(), tokenId);
  47. }
  48. _burn(tokenId);
  49. // Checks were already performed at this point, and there's no way to retake ownership or approval from
  50. // the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
  51. // slither-disable-next-line reentrancy-no-eth
  52. underlying().safeTransferFrom(address(this), account, tokenId);
  53. }
  54. return true;
  55. }
  56. /**
  57. * @dev Overrides {IERC721Receiver-onERC721Received} to allow minting on direct ERC721 transfers to
  58. * this contract.
  59. *
  60. * In case there's data attached, it validates that the operator is this contract, so only trusted data
  61. * is accepted from {depositFor}.
  62. *
  63. * WARNING: Doesn't work with unsafe transfers (eg. {IERC721-transferFrom}). Use {ERC721Wrapper-_recover}
  64. * for recovering in that scenario.
  65. */
  66. function onERC721Received(address, address from, uint256 tokenId, bytes memory) public virtual returns (bytes4) {
  67. if (address(underlying()) != _msgSender()) {
  68. revert ERC721UnsupportedToken(_msgSender());
  69. }
  70. _safeMint(from, tokenId);
  71. return IERC721Receiver.onERC721Received.selector;
  72. }
  73. /**
  74. * @dev Mint a wrapped token to cover any underlyingToken that would have been transferred by mistake. Internal
  75. * function that can be exposed with access control if desired.
  76. */
  77. function _recover(address account, uint256 tokenId) internal virtual returns (uint256) {
  78. address owner = underlying().ownerOf(tokenId);
  79. if (owner != address(this)) {
  80. revert ERC721IncorrectOwner(address(this), tokenId, owner);
  81. }
  82. _safeMint(account, tokenId);
  83. return tokenId;
  84. }
  85. /**
  86. * @dev Returns the underlying token.
  87. */
  88. function underlying() public view virtual returns (IERC721) {
  89. return _underlying;
  90. }
  91. }