Last active
February 10, 2023 15:49
-
-
Save pcaversaccio/b01a77bcd5a293297fd72a01f7b567a9 to your computer and use it in GitHub Desktop.
A simple wrapped ERC-721 token. Do not trust this code unless you want to test it in prod.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: WTFPL | |
pragma solidity 0.8.18; | |
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol"; | |
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | |
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | |
/** | |
* @dev Error that occurs when an unauthorised transfer is triggered. | |
* @param emitter The contract that emits the error. | |
*/ | |
error Unauthorised(address emitter); | |
/** | |
* @title Wrapped ERC-721 | |
* @author pcaversaccio | |
* @dev Do not trust this code unless you want to test it in prod. | |
*/ | |
contract WERC721 is ERC721, IERC721Receiver, Ownable, ReentrancyGuard { | |
ERC721 public immutable token; | |
// solhint-disable-next-line var-name-mixedcase | |
string private _URI; | |
constructor( | |
ERC721 token_, | |
// solhint-disable-next-line var-name-mixedcase | |
string memory URI_ | |
) ERC721("WERC721", "WNFT") { | |
token = token_; | |
_URI = URI_; | |
} | |
function wrap(uint256[] calldata tokenIds) public nonReentrant { | |
uint256 length = tokenIds.length; | |
for (uint256 i; i < length; ) { | |
uint256 tokenId = tokenIds[i]; | |
token.safeTransferFrom(msg.sender, address(this), tokenId); | |
_safeMint(msg.sender, tokenId); | |
unchecked { | |
++i; | |
} | |
} | |
} | |
function unwrap(uint256[] calldata tokenIds) public nonReentrant { | |
uint256 length = tokenIds.length; | |
for (uint256 i; i < length; ) { | |
uint256 tokenId = tokenIds[i]; | |
if (!_isApprovedOrOwner(msg.sender, tokenId)) { | |
revert Unauthorised(address(this)); | |
} | |
token.safeTransferFrom(address(this), msg.sender, tokenId); | |
_burn(tokenId); | |
unchecked { | |
++i; | |
} | |
} | |
} | |
function _baseURI() internal view override returns (string memory) { | |
return _URI; | |
} | |
// solhint-disable-next-line var-name-mixedcase | |
function setBaseURI(string calldata URI_) public onlyOwner { | |
_URI = URI_; | |
} | |
function onERC721Received( | |
address /*operator*/, | |
address /*from*/, | |
uint256 /*tokenId*/, | |
bytes calldata /*data*/ | |
) public pure returns (bytes4) { | |
return IERC721Receiver.onERC721Received.selector; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment