ERC721Wrapper.sol 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/extensions/ERC721Wrapper.sol)
  3. pragma solidity ^0.8.0;
  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. require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721Wrapper: caller is not token owner or approved");
  42. _burn(tokenId);
  43. // Checks were already performed at this point, and there's no way to retake ownership or approval from
  44. // the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
  45. // slither-disable-next-line reentrancy-no-eth
  46. underlying().safeTransferFrom(address(this), account, tokenId);
  47. }
  48. return true;
  49. }
  50. /**
  51. * @dev Overrides {IERC721Receiver-onERC721Received} to allow minting on direct ERC721 transfers to
  52. * this contract.
  53. *
  54. * In case there's data attached, it validates that the operator is this contract, so only trusted data
  55. * is accepted from {depositFor}.
  56. *
  57. * WARNING: Doesn't work with unsafe transfers (eg. {IERC721-transferFrom}). Use {ERC721Wrapper-_recover}
  58. * for recovering in that scenario.
  59. */
  60. function onERC721Received(
  61. address,
  62. address from,
  63. uint256 tokenId,
  64. bytes memory
  65. ) public virtual override returns (bytes4) {
  66. require(address(underlying()) == _msgSender(), "ERC721Wrapper: caller is not underlying");
  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. }