-
-
Save jeftarmascarenhas/bbcf6ac3c2d7ce2f57e9f632771e3382 to your computer and use it in GitHub Desktop.
Fractionalize NFT Smart Contract - https://youtu.be/fDRQDP2xW7o
This file contains hidden or 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/[email protected]/token/ERC20/ERC20.sol"; | |
import "@openzeppelin/[email protected]/token/ERC721/IERC721.sol"; | |
import "@openzeppelin/[email protected]/access/Ownable.sol"; | |
import "@openzeppelin/[email protected]/token/ERC20/extensions/draft-ERC20Permit.sol"; | |
import "@openzeppelin/[email protected]/token/ERC721/utils/ERC721Holder.sol"; | |
contract FractionalizedNFT is ERC20, Ownable, ERC20Permit, ERC721Holder { | |
IERC721 public collection; | |
uint256 public tokenId; | |
bool public initialized = false; | |
bool public forSale = false; | |
uint256 public salePrice; | |
bool public canRedeem = false; | |
constructor() ERC20("MyToken", "MTK") ERC20Permit("MyToken") {} | |
function initialize(address _collection, uint256 _tokenId, uint256 _amount) external onlyOwner { | |
require(!initialized, "Already initialized"); | |
require(_amount > 0, "Amount needs to be more than 0"); | |
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 sent"); | |
collection.transferFrom(address(this), msg.sender, tokenId); | |
forSale = false; | |
canRedeem = true; | |
} | |
function redeem(uint256 _amount) external { | |
require(canRedeem, "Redemption not available"); | |
uint256 totalEther = address(this).balance; | |
uint256 toRedeem = _amount * totalEther / totalSupply(); | |
_burn(msg.sender, _amount); | |
payable(msg.sender).transfer(toRedeem); | |
} | |
} |
This file contains hidden or 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/ERC721/ERC721.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
contract MyNFT is ERC721, Ownable { | |
constructor() ERC721("MyNFT", "MTK") {} | |
function safeMint(address to, uint256 tokenId) public onlyOwner { | |
_safeMint(to, tokenId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment