Created
November 20, 2023 09:38
-
-
Save casweeney/fa085309916bb3bba75d126e9104fe4e to your computer and use it in GitHub Desktop.
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: MIT | |
pragma solidity ^0.8.4; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol"; | |
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol"; | |
contract FractionalNft is ERC20, Ownable, ERC20Permit, ERC721Holder { | |
IERC721 public collection; | |
uint16 public tokenId; | |
bool public initialized; | |
bool public forSale; | |
bool public redeemable; | |
uint256 public salePrice; | |
constructor() ERC20("Token Fraction", "TKF") ERC20Permit("Token Fraction") Ownable(msg.sender) {} | |
function initialize(address _collection, uint16 _tokenId, uint256 _amount) external onlyOwner { | |
require(!initialized, "Already initialized"); | |
require(_amount > 0, "can't initialize zero amount"); | |
collection = IERC721(_collection); | |
collection.safeTransferFrom(msg.sender, address(this), _tokenId); | |
tokenId = _tokenId; | |
initialized = true; | |
_mint(msg.sender, _amount); | |
} | |
function putForSale(uint256 price) external onlyOwner { | |
salePrice = price; | |
forSale = true; | |
} | |
function purchase() external payable { | |
require(forSale, "Not for sale"); | |
require(msg.value >= salePrice, "Not enough ether to purchase"); | |
collection.transferFrom(address(this), msg.sender, tokenId); | |
forSale = false; | |
redeemable = true; | |
} | |
function redeem(uint256 _amount) external { | |
require(redeemable, "Redemtion not available"); | |
uint256 totalEther = address(this).balance; | |
uint256 amountToRedeem = _amount * totalEther / totalSupply(); | |
_burn(msg.sender, _amount); | |
(bool sent, ) = payable(msg.sender).call{value: amountToRedeem}(""); | |
require(sent, "Failed to send Ether"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment