|
|
|
// SPDX-License-Identifier: MIT |
|
// Author Viktor Valastin (github: @vikiival) |
|
pragma solidity ^0.6.0; |
|
pragma experimental ABIEncoderV2; |
|
|
|
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.1.0/contracts/token/ERC721/ERC721.sol'; |
|
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.1.0/contracts/access/Ownable.sol'; |
|
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.1.0/contracts/utils/Counters.sol'; |
|
|
|
// ERC721 |
|
contract EpochNFT is ERC721, Ownable { |
|
using Counters for Counters.Counter; |
|
Counters.Counter private tokenId; |
|
|
|
struct Epoch { |
|
uint256 date; |
|
address payable author; |
|
string title; |
|
} |
|
|
|
event EpochBought(uint256 indexed _id, address indexed _author, address _buyer); |
|
|
|
mapping (uint256 => Epoch) private _epochs; |
|
|
|
constructor(string memory name, string memory symbol) ERC721(name, symbol) public { |
|
} |
|
|
|
function getOwner() public view returns (address) { |
|
return owner(); |
|
} |
|
|
|
function actualTokenId() public view returns (uint256) { |
|
return tokenId.current(); |
|
} |
|
|
|
function createEpoch(string memory _title, uint256 _daysFromNow) public returns (uint256) { |
|
tokenId.increment(); |
|
uint256 newTokenId = tokenId.current(); |
|
_mint(msg.sender, newTokenId); |
|
setEpoch(newTokenId, _title, _daysFromNow); |
|
return newTokenId; |
|
} |
|
|
|
function setEpoch(uint256 _id, string memory _title, uint256 _daysFromNow) private { |
|
// 259200 |
|
require( |
|
_daysFromNow * 1 days >= 2 days, |
|
"Setting epoch sooner than 48hrs before." |
|
); |
|
Epoch storage e = _epochs[_id]; |
|
e.title = _title; |
|
e.date = now + _daysFromNow; |
|
e.author = msg.sender; |
|
} |
|
|
|
function getEpoch(uint256 _id) public view returns (Epoch memory) { |
|
require(_id <= actualTokenId(), "Current epoch with id does not exist"); |
|
Epoch memory e = _epochs[_id]; |
|
return e; |
|
} |
|
|
|
modifier shouldExist(uint256 _id) { |
|
require(_id <= actualTokenId() && _id != 0, "Current epoch with id does not exist"); |
|
_; |
|
} |
|
|
|
|
|
function buyEpoch(uint256 _id) public payable shouldExist(_id) { |
|
require(msg.value >= 1 finney, "Not enough money to buy epoch"); |
|
Epoch storage e = _epochs[_id]; |
|
address payable previousOwner = e.author; |
|
e.author = msg.sender; |
|
previousOwner.transfer(msg.value); |
|
emit EpochBought(_id, previousOwner, msg.sender); |
|
} |
|
|
|
receive() external payable { |
|
payable(getOwner()).transfer(msg.value); |
|
} |
|
} |