ERC721Wrapper.sol 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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(address, address from, uint256 tokenId, bytes memory) public virtual returns (bytes4) {
  61. require(address(underlying()) == _msgSender(), "ERC721Wrapper: caller is not underlying");
  62. _safeMint(from, tokenId);
  63. return IERC721Receiver.onERC721Received.selector;
  64. }
  65. /**
  66. * @dev Mint a wrapped token to cover any underlyingToken that would have been transferred by mistake. Internal
  67. * function that can be exposed with access control if desired.
  68. */
  69. function _recover(address account, uint256 tokenId) internal virtual returns (uint256) {
  70. require(underlying().ownerOf(tokenId) == address(this), "ERC721Wrapper: wrapper is not token owner");
  71. _safeMint(account, tokenId);
  72. return tokenId;
  73. }
  74. /**
  75. * @dev Returns the underlying token.
  76. */
  77. function underlying() public view virtual returns (IERC721) {
  78. return _underlying;
  79. }
  80. }