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