-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractionalizedNFT.sol
49 lines (43 loc) · 1.84 KB
/
FractionalizedNFT.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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);
}
}