ERC721Wrapper.sol 3.8 KB

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