-
Notifications
You must be signed in to change notification settings - Fork 4
/
MartenitsaToken.sol
93 lines (77 loc) · 3.21 KB
/
MartenitsaToken.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MartenitsaToken is ERC721, Ownable {
uint256 private _nextTokenId;
address[] public producers;
mapping(address => uint256) public countMartenitsaTokensOwner;
mapping(address => bool) public isProducer;
mapping(uint256 => string) public tokenDesigns;
event Created(address indexed owner, uint256 indexed tokenId, string indexed design);
constructor() ERC721("MartenitsaToken", "MT") Ownable(msg.sender) {}
/**
* @notice Function to set producers.
* @param _producersList The addresses of the producers.
*/
function setProducers(address[] memory _producersList) public onlyOwner{
for (uint256 i = 0; i < _producersList.length; i++) {
isProducer[_producersList[i]] = true;
producers.push(_producersList[i]);
}
}
/**
* @notice Function to create a new martenitsa. Only producers can call the function.
* @param design The type (bracelet, necklace, Pizho and Penda and other) of martenitsa.
*/
function createMartenitsa(string memory design) external {
require(isProducer[msg.sender], "You are not a producer!");
require(bytes(design).length > 0, "Design cannot be empty");
uint256 tokenId = _nextTokenId++;
tokenDesigns[tokenId] = design;
countMartenitsaTokensOwner[msg.sender] += 1;
emit Created(msg.sender, tokenId, design);
_safeMint(msg.sender, tokenId);
}
/**
* @notice Function to get the design of a martenitsa.
* @param tokenId The Id of the MartenitsaToken.
*/
function getDesign(uint256 tokenId) external view returns (string memory) {
require(tokenId < _nextTokenId, "The tokenId doesn't exist!");
return tokenDesigns[tokenId];
}
/**
* @notice Function to update the count of martenitsaTokens for a specific address.
* @param owner The address of the owner.
* @param operation Operation for update: "add" for +1 and "sub" for -1.
*/
function updateCountMartenitsaTokensOwner(address owner, string memory operation) external {
if (keccak256(abi.encodePacked(operation)) == keccak256(abi.encodePacked("add"))) {
countMartenitsaTokensOwner[owner] += 1;
} else if (keccak256(abi.encodePacked(operation)) == keccak256(abi.encodePacked("sub"))) {
countMartenitsaTokensOwner[owner] -= 1;
} else {
revert("Wrong operation");
}
}
/**
* @notice Function to get the count of martenitsaTokens for a specific address.
* @param owner The address of the owner.
*/
function getCountMartenitsaTokensOwner(address owner) external view returns (uint256) {
return countMartenitsaTokensOwner[owner];
}
/**
* @notice Function to get the list of addresses of all producers.
*/
function getAllProducers() external view returns (address[] memory) {
return producers;
}
/**
* @notice Function to get the next tokenId.
*/
function getNextTokenId() external view returns (uint256) {
return _nextTokenId;
}
}