-
Notifications
You must be signed in to change notification settings - Fork 0
/
WatermarkNFT.sol
107 lines (91 loc) · 2.61 KB
/
WatermarkNFT.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
///////////////// Contract /////////////////
contract WatermarkNFT is ERC721 {
constructor() public ERC721("WatermarkNFT", "WMNFT") {}
///////////////// Variable /////////////////
uint256 private _tokenIdCounter;
function _baseURI() internal view virtual override returns (string memory){
return "ipfs://";
}
struct Watermark{
string ipfsHash;
uint256 id;
bool valid;
}
Watermark[] private WatermarkStorage;
function isValid(
uint256 _id
) public view returns(bool) {
return (
Watermark[_id].valid
);
}
function getWatermarkStorage() public view returns(Watermark[] memory) {
return WatermarkStorage;
}
function getWatermark(
uint256 _id
) public view returns(Watermark memory) {
require(WatermarkStorage[_id].valid, "Invalid watermark.");
return WatermarkStorage[_id];
}
function tokenURI(
uint256 _id
) public view override(ERC721) returns(string memory) {
require(WatermarkStorage[_id].valid, "Invalid watermark.");
string memory baseURI = _baseURI();
string memory ipfsHash = WatermarkStorage[_id].ipfsHash;
return bytes(baseURI).length > 0 ? string.concat(baseURI, ipfsHash) : "";
}
///////////////// Functions /////////////////
// Check if can use calldata or storage instead of memory
function _setWatermark(
string memory _ipfsHash,
uint256 _id
) internal {
Watermark memory WatermarkInst;
WatermarkInst.ipfsHash = _ipfsHash;
WatermarkInst.id = _id;
WatermarkInst.valid = true;
WatermarkStorage.push(WatermarkInst);
}
function registerWatermark(
address _to,
string memory _ipfsHash
) public returns(uint256) {
require(msg.sender == _to, "Only the owner can mint!");
// Implement this to avoid overflow error
require(_tokenIdCounter < type(uint256).max, "Token ID overflow");
_tokenIdCounter = WatermarkStorage.length;
_safeMint(_to, _tokenIdCounter);
_setWatermark(_ipfsHash, _tokenIdCounter);
return _tokenIdCounter;
}
function transferFrom(
address _from,
address _to,
uint256 _tokenId
) public override(ERC721) {
revert("Watermarks cannot be transferred");
}
function safeTransferFrom(
address _from,
address _to,
uint256 _tokenId
) public override(ERC721) {
revert("Watermarks cannot be transferred");
}
function safeTransferFrom(
address _from,
address _to,
uint256 _tokenId,
bytes memory _data
) public override(ERC721) {
revert("Watermarks cannot be transferred");
}
fallback() external payable {}
receive() external payable {}
}