// SPDX-License-Identifier: MIT pragma solidity ^0.8.23; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol"; import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; /** * @title MFT (Meme-Fungible Token) * @dev ERC20 token that can be swapped 1:1 with an ERC1155 NFT. * * This is the same model used by Non-Playable Coin (NPC). * Holders can: * - Transform: send NFTs to contract, receive ERC20 tokens * - Respawn: send ERC20 tokens to contract, receive NFTs back * * Deployment flow: * 1. Deploy this contract (mints all ERC20 supply to deployer) * 2. Deploy or use an existing ERC1155 NFT contract * 3. Call setNFTContract() to link them * 4. Transfer NFTs to this contract so they can be distributed via Transform * * Constructor: (name_, symbol_, supply_, uri_) */ contract MFT is ERC20, ERC1155Holder, Ownable { /// @notice The linked ERC1155 NFT contract IERC1155 public nftContract; /// @notice The ERC1155 token ID used for swaps uint256 public nftTokenId; /// @notice Metadata URI for off-chain display string private _uri; /// @notice Whether the NFT contract has been linked bool public nftLinked; event NFTContractLinked(address indexed nftContract, uint256 tokenId); event Transformed(address indexed user, uint256 amount); event Respawned(address indexed user, uint256 amount); constructor( string memory name_, string memory symbol_, uint256 supply_, string memory uri_ ) ERC20(name_, symbol_) Ownable(msg.sender) { _mint(msg.sender, supply_ * (10 ** decimals())); _uri = uri_; } /** * @dev Links the ERC1155 NFT contract. Can only be called once by owner. * @param nftContract_ Address of the ERC1155 NFT contract * @param tokenId_ The ERC1155 token ID to use for swaps */ function setNFTContract(address nftContract_, uint256 tokenId_) external onlyOwner { require(!nftLinked, "NFT contract already linked"); require(nftContract_ != address(0), "Invalid NFT contract"); nftContract = IERC1155(nftContract_); nftTokenId = tokenId_; nftLinked = true; emit NFTContractLinked(nftContract_, tokenId_); } /** * @dev Transform NFTs into ERC20 tokens. * Send `amount` NFTs to this contract and receive `amount` ERC20 tokens. * @param amount Number of NFTs to transform (1 NFT = 1 whole token) */ function transform(uint256 amount) external { require(nftLinked, "NFT contract not linked"); require(amount >= 1, "Amount must be at least 1"); require( balanceOf(address(this)) >= amount * (10 ** decimals()), "Not enough tokens in contract" ); nftContract.safeTransferFrom( msg.sender, address(this), nftTokenId, amount, "" ); _transfer(address(this), msg.sender, amount * (10 ** decimals())); emit Transformed(msg.sender, amount); } /** * @dev Respawn ERC20 tokens back into NFTs. * Send `amount` ERC20 tokens to this contract and receive `amount` NFTs. * @param amount Number of whole tokens to respawn (1 token = 1 NFT) */ function respawn(uint256 amount) external { require(nftLinked, "NFT contract not linked"); require(amount >= 1, "Amount must be at least 1"); require( nftContract.balanceOf(address(this), nftTokenId) >= amount, "Not enough NFTs in contract" ); _transfer(msg.sender, address(this), amount * (10 ** decimals())); nftContract.safeTransferFrom( address(this), msg.sender, nftTokenId, amount, "" ); emit Respawned(msg.sender, amount); } /** * @dev Returns the metadata URI. */ function uri() public view returns (string memory) { return _uri; } /** * @dev Updates the metadata URI (owner only). */ function setURI(string memory newURI) external onlyOwner { _uri = newURI; } /** * @dev Required override for ERC1155Holder + ERC165 */ function supportsInterface(bytes4 interfaceId) public view override(ERC1155Holder) returns (bool) { return super.supportsInterface(interfaceId); } }