// SPDX-License-Identifier: MIT // OpenZeppelin Tron Contracts (last updated v5.5.0) (token/TRC721/extensions/TRC721Wrapper.sol) pragma solidity ^0.8.24; import {ITRC721} from "@openzeppelin/tron-contracts/token/TRC721/ITRC721.sol"; import {TRC721Upgradeable} from "../TRC721Upgradeable.sol"; import {ITRC721Receiver} from "@openzeppelin/tron-contracts/token/TRC721/ITRC721Receiver.sol"; import {Initializable} from "@openzeppelin/tron-contracts/proxy/utils/Initializable.sol"; /** * @dev Extension of the TRC-721 token contract to support token wrapping. * * Users can deposit and withdraw an "underlying token" and receive a "wrapped token" with a matching tokenId. This is * useful in conjunction with other modules. For example, combining this wrapping mechanism with {TRC721Votes} will allow * the wrapping of an existing "basic" TRC-721 into a governance token. */ abstract contract TRC721WrapperUpgradeable is Initializable, TRC721Upgradeable, ITRC721Receiver { /// @custom:storage-location erc7201:openzeppelin.storage.TRC721Wrapper struct TRC721WrapperStorage { ITRC721 _underlying; } // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.TRC721Wrapper")) - 1)) & ~bytes32(uint256(0xff)) bytes32 private constant TRC721WrapperStorageLocation = 0x5c3c24fa12a168107e8bd722e130ea71f532fdd7ee1845698e8a7f83558a4700; function _getTRC721WrapperStorage() private pure returns (TRC721WrapperStorage storage $) { assembly { $.slot := TRC721WrapperStorageLocation } } /** * @dev The received TRC-721 token couldn't be wrapped. */ error TRC721UnsupportedToken(address token); function __TRC721Wrapper_init(ITRC721 underlyingToken) internal onlyInitializing { __TRC721Wrapper_init_unchained(underlyingToken); } function __TRC721Wrapper_init_unchained(ITRC721 underlyingToken) internal onlyInitializing { TRC721WrapperStorage storage $ = _getTRC721WrapperStorage(); $._underlying = underlyingToken; } /** * @dev Allow a user to deposit underlying tokens and mint the corresponding tokenIds. */ function depositFor(address account, uint256[] memory tokenIds) public virtual returns (bool) { uint256 length = tokenIds.length; for (uint256 i = 0; i < length; ++i) { uint256 tokenId = tokenIds[i]; // This is an "unsafe" transfer that doesn't call any hook on the receiver. With underlying() being trusted // (by design of this contract) and no other contracts expected to be called from there, we are safe. // slither-disable-next-line reentrancy-no-eth underlying().transferFrom(_msgSender(), address(this), tokenId); // forge-lint: disable-line(erc20-unchecked-transfer) _safeMint(account, tokenId); } return true; } /** * @dev Allow a user to burn wrapped tokens and withdraw the corresponding tokenIds of the underlying tokens. */ function withdrawTo(address account, uint256[] memory tokenIds) public virtual returns (bool) { uint256 length = tokenIds.length; for (uint256 i = 0; i < length; ++i) { uint256 tokenId = tokenIds[i]; // Setting an "auth" arguments enables the `_isAuthorized` check which verifies that the token exists // (from != 0). Therefore, it is not needed to verify that the return value is not 0 here. _update(address(0), tokenId, _msgSender()); // Checks were already performed at this point, and there's no way to retake ownership or approval from // the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line. // slither-disable-next-line reentrancy-no-eth underlying().safeTransferFrom(address(this), account, tokenId); } return true; } /** * @dev Overrides {ITRC721Receiver-onTRC721Received} to allow minting on direct TRC-721 transfers to * this contract. * * In case there's data attached, it validates that the operator is this contract, so only trusted data * is accepted from {depositFor}. * * WARNING: Doesn't work with unsafe transfers (eg. {ITRC721-transferFrom}). Use {TRC721Wrapper-_recover} * for recovering in that scenario. */ function onTRC721Received(address, address from, uint256 tokenId, bytes memory) public virtual returns (bytes4) { if (address(underlying()) != _msgSender()) { revert TRC721UnsupportedToken(_msgSender()); } _safeMint(from, tokenId); return ITRC721Receiver.onTRC721Received.selector; } /** * @dev Mint a wrapped token to cover any underlyingToken that would have been transferred by mistake. Internal * function that can be exposed with access control if desired. */ function _recover(address account, uint256 tokenId) internal virtual returns (uint256) { address owner = underlying().ownerOf(tokenId); if (owner != address(this)) { revert TRC721IncorrectOwner(address(this), tokenId, owner); } _safeMint(account, tokenId); return tokenId; } /** * @dev Returns the underlying token. */ function underlying() public view virtual returns (ITRC721) { TRC721WrapperStorage storage $ = _getTRC721WrapperStorage(); return $._underlying; } }