Skip to content

Instantly share code, notes, and snippets.

@vikiival
Last active March 17, 2021 09:31
Show Gist options
  • Save vikiival/6ca2b22a382423df3f70fdd0f98cf985 to your computer and use it in GitHub Desktop.
Save vikiival/6ca2b22a382423df3f70fdd0f98cf985 to your computer and use it in GitHub Desktop.
NFT crashcourse for FIIT STU

NFT crashcourse for FIIT STU

Author: Viktor Valastin (@vikiival)

Description

The goal of this course is to understand the Solidity basics.

We will cover:

  • inheritance
  • modifier
  • function visibility
  • function modifier (view/payable)
  • special keywords (storage/memory)
  • variables (primitives/struct/days/wei/msg)
  • events
  • fallback
  • libraries
  • interface

Goal

You should make your own ERC721 token using OpenZeppelin with the properties described above (We will cover section). Be creative.

Write your code inside Remix IDE

When you are satisfied with your result deploy it on the Ropsten Network.

// 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment