-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecondAttempt
184 lines (146 loc) · 7.74 KB
/
SecondAttempt
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
abstract contract MyERC1155 is ERC1155 {
event Transfer (address indexed from, address indexed to, uint256 indexed tokenId, uint256 amount);
event Mint(address to, uint256 tokenId, uint256 amount);
event Burn(address from, uint256 tokenId, uint256 amount);
event OwnershipTransferError(address msgSender, address owner, uint256 tokenId);
constructor() {
}
function _transferFrom(address from, address to, uint256 tokenId, uint256 amount) internal override {
super._transferFrom(from, to, tokenId, amount);
emit Transfer(from, to, tokenId, amount);
}
function _mint(address to, uint256 tokenId, uint256 amount) internal override {
super._mint(to, tokenId, amount);
emit Mint(to, tokenId, amount);
}
function _burn(address from, uint256 tokenId, uint256 amount) internal override {
super._burn(from, tokenId, amount);
emit Burn(from, tokenId, amount);
}
}
contract SLBResendeRJ is ERC1155 {
address owner;
string public name;
string public location;
string public imageHash;
uint256 public landArea;
uint256 public builtArea;
uint256 public value;
uint256 public capRate;
mapping(address => bool) public tokens; // mapping para armazenar quais tokens pertencem a qual endereço
uint256 public totalSupply;
address[] public whitelist;
mapping (address => uint256) public fragments; // mapping para armazenar a quantidade de fragmentos de cada endereço
uint256 public maxSupply = 1000000; // limite máximo de fragmentos
mapping(uint256 => address) public tokenIdToOwner;
event OwnershipTransferError(address msgSender, address owner, uint256 tokenId);
constructor() public {
owner = msg.sender;
name = "SLB Resende-RJ";
location = "Resende-RJ";
landArea = 70000;
builtArea = 33533;
value = 100000000;
capRate = 10 + IPCA;
totalSupply = 1000000;
whitelist.push(msg.sender);
}
function fragment() public {
require(msg.sender == owner, "Apenas o proprietario pode fragmentar a NFT"); // verifica se o remetente é o proprietário
require(totalSupply + 2000000 <= maxSupply, "Nao e possivel fragmentar mais, limite maximo alcancado"); // verifica se o totalSupply não ultrapassa o limite
require(fragments[msg.sender] < totalSupply, "Voce ja possui todos os fragmentos"); // verifica se o remetente já possui todos os fragmentos existentes
require(fragments[msg.sender] != totalSupply, "Voce ja possui todos os fragmentos existentes, nao e possivel adicionar mais");
for (uint i = 0; i < 2000000; i++) {
require(tokenIdToOwner[totalSupply + i] == address(0), "Esse fragmento ja foi atribuido");
tokenIdToOwner[totalSupply + i] = msg.sender;
}
if (msg.sender != owner) {
emit OwnershipTransferError(msg.sender, owner, totalSupply);
return;
}
fragments[msg.sender] += 2000000; // adiciona os fragmentos ao endereço do remetente
totalSupply += 2000000; // adiciona os fragmentos ao totalSupply
}
function getValue() public view returns (uint256) {
return value;
}
function getCapRate() public view returns (uint256) {
return capRate;
}
function balanceOf(address tokenOwner) public view returns (uint256) {
require(whitelist[msg.sender] == true, "Voce nao esta na whitelist"); // verifica se o remetente está na whitelist
if (tokens[tokenOwner]){
return 1;
} else {
return 0;
}
}
function addWhitelist(address _address) public {
require(msg.sender == owner, "Apenas o proprietario pode adicionar enderecos a whitelist");
bool addressExist = false;
for(uint i = 0; i< whitelist.length; i++){
if(whitelist[i] == _address) addressExist = true;
}
require(!addressExist, "Endereco ja esta na whitelist");
whitelist.push(_address);
}
function removeWhitelist(address _address) public {
require(msg.sender == owner, "Apenas o proprietario pode remover enderecos da whitelist");
bool addressExist = false;
for (uint i = 0; i < whitelist.length; i++) {
if (whitelist[i] == _address) {
addressExist = true;
break;
}
}
uint256 i = whitelist.indexOf(msg.sender); // Encontra o índice do elemento a ser removido
require(i != -1, "Endereco nao encontrado na whitelist"); // Certifique-se de que o elemento existe no array
whitelist.splice(i, 1); // Remove o elemento do array
}
function split(address to, uint256 fragmentCount) public {
require(msg.sender == tokenIdToOwner[tokenId], "Voce nao e o proprietario deste token");
require(fragmentCount > 0 && fragmentCount < totalSupply, "Numero invalido de fragmentos");
for (uint256 i = 0; i < fragmentCount; i++) {
require(tokenIdToOwner[i] == msg.sender, "Voce nao e o proprietario desse fragmento");
require(msg.sender == owner, "Apenas o proprietario pode comprar fragmentos de NFT"); // verifica se o remetente é o proprietário
require(fragments[msg.sender] >= fragmentCount, "Voce nao possui fragmentos suficientes para vender"); // verifica se o remetente possui fragmentos suficientes
require(whitelist[to] == true, "O destinatario nao esta na whitelist"); // verifica se o destinatário está na whitelist
require(fragments[to] + fragmentCount <= maxSupply, "A compra ultrapassa o limite maximo de fragmentos"); // verifica se o total de fragmentos do destinatário não ultrapassa o limite
fragments[msg.sender] -= fragmentCount; // remove fragmentos do remetente
fragments[to] += fragmentCount; // adiciona fragmentos ao destinatário
tokenIdToOwner[i] = to;
}
totalSupply -= fragmentCount;
}
function merge(address from, uint256 fragmentCount) public {
require(tokenIdToOwner[from] == msg.sender, "Voce nao e o proprietario desse fragmento");
require(fragmentCount > 0 && fragmentCount < totalSupply, "Numero invalido de fragmentos");
for (uint256 i = 0; i < fragmentCount; i++) {
require (tokenIdToOwner[i] == from, "O remetente nao e o proprietario desse fragmento");
require(msg.sender == owner, "Apenas o proprietario pode vender fragmentos de NFT"); // verifica se o remetente é o proprietário
require(fragments[from] >= fragmentCount, "O remetente nao possui fragmentos suficientes para vender"); // verifica se o remetente possui fragmentos suficientes
require(whitelist[from] == true, "O remetente nao esta na whitelist"); // verifica se o remetente está na whitelist
fragments[from] -= fragmentCount; // remove fragmentos do remetente
fragments[msg.sender] += fragmentCount; // adiciona fragmentos ao destinatário
tokenIdToOwner[i] = msg.sender;
}
totalSupply += fragmentCount;
}
function checkPermission(address user) public view returns (bool) { // verifica se o usuário atual é o proprietário ou está na whitelist
if(msg.sender == owner || whitelist[user]){
return true;
} else {
return false;
}
}
function setImageHash(string memory newImageHash) public {
require(msg.sender == owner, "Somente o proprietario pode atribuir o hash da imagem");
imageHash = newImageHash;
}
function getImageHash() public view returns (string memory) {
return imageHash;
}
}