Skip to content

Instantly share code, notes, and snippets.

@THRUSTDeltaV
Forked from shobhitic/FractionalizedNFT.sol
Created June 7, 2022 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save THRUSTDeltaV/343551df1249d39dfaa3733fd256111c to your computer and use it in GitHub Desktop.
Save THRUSTDeltaV/343551df1249d39dfaa3733fd256111c to your computer and use it in GitHub Desktop.
Fractionalize NFT Smart Contract - https://youtu.be/fDRQDP2xW7o
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts@4.6.0/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@4.6.0/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts@4.6.0/access/Ownable.sol";
import "@openzeppelin/contracts@4.6.0/token/ERC20/extensions/draft-ERC20Permit.sol";
import "@openzeppelin/contracts@4.6.0/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);
}
}
// 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