-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynthetic.sol
331 lines (267 loc) · 11.7 KB
/
Synthetic.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/**
* @title DPLSynthetic
* @dev A modular infrastructure controlled by a DAO to manage the synthetics workflow of Accelar protocol
*/
contract DPLSynthetic is ERC721Enumerable, Ownable, ReentrancyGuard {
uint256 private _tokenIds;
uint256 private _itemsSolds;
uint256 private _NFTsMintedByOwner;
uint256 public marketplaceFee = 3; // Base 100 - 3 equals 3%
// ICP oracle address allowed to manage the tokens - can be changed through a DAO, for instance
address public icpOracle;
// Flag to enable or disable contract functionalities - controlled by DAO
bool public isEnabled = true;
// ERC20 token addresses (USDC and USDT) used for transactions, both have 6 decimals
IERC20 public USDCAddress;
IERC20 public USDTAddress;
// Payment type enum to define payment methods for marketplace transactions
enum PaymentType {
Matic,
USD
}
enum Pool {
HorizonProtocol,
Landx,
OceanProtocol
}
// Mapping to track NFTs minted per wallet
mapping(uint256 => mapping(address => uint256)) public nftsMintedPerWallet;
// Mapping to store URIs for NFT characters
mapping(uint256 => string) public NFTCharacterToURI;
// Struct to represent an item in the marketplace - each NFT has its own item
struct Item {
uint256 id;
uint256 bidAmount;
string asset;
string poolHash;
bool live;
uint256 price;
address seller;
PaymentType paymentType;
Pool pool;
}
// Mapping to store items in the marketplace
mapping(uint256 => Item) public Items;
/**
* @dev Constructor to initialize the contract with ICP oracle address and marketplace fee
* @param _icpOracle Address of the ICP oracle
* @param _marketplaceFee Fee for the marketplace transactions
*/
constructor(address _icpOracle, uint256 _marketplaceFee, address initialOwner) Ownable(initialOwner) ERC721("AccelarDchainDepin", "ACD") {
icpOracle = _icpOracle;
marketplaceFee = _marketplaceFee;
}
/**
* @dev Sets the ERC20 token addresses for USDC and USDT
* @param _USDCAddress Address of the USDC token contract
* @param _USDTAddress Address of the USDT token contract
*/
function setTokensAddress(address _USDCAddress, address _USDTAddress) public onlyOwner {
USDCAddress = IERC20(_USDCAddress);
USDTAddress = IERC20(_USDTAddress);
}
event DeploymentCreated(uint256 id, string uri, address minter);
event DeploymentCanceled(uint256 id);
/**
* @dev Updates the deployment status for a given token asset ID by the ICP oracle
* @param _tokenId Token ID of the deployment
* @param _poolHash Pool transaction hash
*/
function updateDeployment(uint256 _tokenId, string memory _poolHash) public {
require(icpOracle == msg.sender, "Only Oracle can change deployment");
Items[_tokenId].poolHash = _poolHash;
}
/**
* @dev Allows the owner to mint NFTs for users
* @param _asset Asset to interact with
* @param _to Address of the recipient
*/
function createDeployment(string memory _asset, address _to, Pool _pool) public payable {
require(isEnabled, "The contract is not enabled");
_tokenIds += 1;
uint256 newItemId = _tokenIds;
_safeMint(_to, newItemId);
Items[newItemId] = Item({
id: newItemId,
bidAmount: msg.value,
asset: _asset,
poolHash: "0x",
live: true,
price: 0,
seller: _to,
paymentType: PaymentType(0),
pool: _pool
});
emit DeploymentCreated(newItemId, _asset, _to);
}
function closeDeployment(uint256 _tokenId) public {
require(Items[_tokenId].seller == msg.sender, "Only deployment owner can do this change");
Items[_tokenId].live = false;
}
/**
* @dev Sets the contract enabled or disabled
* @param _bool Boolean value to enable or disable the contract
*/
function setContractEnabled(bool _bool) public onlyOwner {
isEnabled = _bool;
}
/**
* @dev Sets a new ICP oracle address
* @param _address New ICP oracle address
*/
function setNewICPOracle(address _address) public onlyOwner {
icpOracle = _address;
}
/**
* @dev Allows the owner to burn NFTs
* @param _tokenId Token ID of the NFT to be burned
*/
function burnNFTOwner(uint256 _tokenId) public onlyOwner {
_burn(_tokenId);
}
/**
* @dev Cancels the deployment of a given token ID
* @param _tokenId Token ID of the deployment to be canceled
*/
function cancelDeployment(uint256 _tokenId) public {
require(ownerOf(_tokenId) == msg.sender, "You are not the token owner");
require(Items[_tokenId].live == true, "Deployment already canceled");
Items[_tokenId].live = false;
emit DeploymentCanceled(_tokenId);
}
// Marketplace events
event itemAddedForSale(uint256 id, uint256 price, address seller);
event itemSold(uint256 id, uint256 price, address seller, address buyer, PaymentType _paymentType);
/**
* @dev Lists an NFT for sale on the marketplace
* @param _tokenId Token ID of the NFT
* @param _price Sale price of the NFT
* @param _paymentType Payment type for the sale (Matic or USD)
*/
function putItemForSale(uint256 _tokenId, uint256 _price, PaymentType _paymentType) public {
require(ownerOf(_tokenId) == msg.sender, "You are not the token owner");
require(_price > 0, "Price needs to be greater than 0");
Items[_tokenId].price = _price;
Items[_tokenId].seller = msg.sender;
Items[_tokenId].paymentType = _paymentType;
_transfer(msg.sender, address(this), _tokenId);
emit itemAddedForSale(_tokenId, _price, msg.sender);
}
/**
* @dev Allows the owner to list an item for sale on the marketplace
* @param _tokenId Token ID of the NFT
* @param _price Sale price of the NFT
* @param seller Address of the seller
* @param _paymentType Payment type for the sale (Matic or USD)
*/
function putItemForSaleOwner(uint256 _tokenId, uint256 _price, address seller, PaymentType _paymentType) public onlyOwner {
require(_price > 0, "Price needs to be greater than 0");
Items[_tokenId].price = _price;
Items[_tokenId].seller = seller;
Items[_tokenId].paymentType = _paymentType;
_transfer(seller, address(this), _tokenId);
emit itemAddedForSale(_tokenId, _price, seller);
}
/**
* @dev Purchases an NFT listed for sale with Matic
* @param _tokenId Token ID of the NFT
*/
function buyItemWithNative(uint256 _tokenId) payable external nonReentrant {
require(_tokenIds >= _tokenId, "NFT does not exist");
require(ownerOf(_tokenId) == address(this), "Token not for sale");
require(msg.value >= Items[_tokenId].price, "Not enough funds sent");
require(msg.sender != Items[_tokenId].seller, "The seller cannot buy it");
require(Items[_tokenId].paymentType == PaymentType(0), "This item is not for Matic sale");
uint256 priceEmit = Items[_tokenId].price;
Items[_tokenId].price = 0;
Items[_tokenId].seller = msg.sender;
// 3% of royalties
payable(Items[_tokenId].seller).transfer((msg.value * 97) / 100);
_itemsSolds += 1;
_transfer(address(this), msg.sender, _tokenId);
emit itemSold(_tokenId, priceEmit, Items[_tokenId].seller, msg.sender, PaymentType(0));
}
enum USDPaymentType {
USDC,
USDT
}
/**
* @dev Purchases an NFT listed for sale with USD (USDC or USDT)
* @param _tokenId Token ID of the NFT
* @param _USDPaymentType Payment type (USDC or USDT)
*/
function buyItemWithUSD(uint256 _tokenId, USDPaymentType _USDPaymentType) external nonReentrant {
require(_tokenIds >= _tokenId, "NFT does not exist");
require(ownerOf(_tokenId) == address(this), "Token not for sale");
require(msg.sender != Items[_tokenId].seller, "The seller cannot buy it");
require(Items[_tokenId].paymentType == PaymentType(1), "This item is not for USD sale");
if (_USDPaymentType == USDPaymentType(0)) {
bool sent1 = USDCAddress.transferFrom(msg.sender, address(this), (Items[_tokenId].price * marketplaceFee) / 100);
bool sent2 = USDCAddress.transferFrom(msg.sender, Items[_tokenId].seller, (Items[_tokenId].price * (100 - marketplaceFee)) / 100);
require(sent1, "Failed to transfer to contract");
require(sent2, "Failed to transfer to user");
} else {
bool sent1 = USDTAddress.transferFrom(msg.sender, address(this), (Items[_tokenId].price * marketplaceFee) / 100);
bool sent2 = USDTAddress.transferFrom(msg.sender, Items[_tokenId].seller, (Items[_tokenId].price * (100 - marketplaceFee)) / 100);
require(sent1, "Failed to transfer to contract");
require(sent2, "Failed to transfer to user");
}
uint256 priceEmit = Items[_tokenId].price;
Items[_tokenId].price = 0;
Items[_tokenId].seller = msg.sender;
_transfer(address(this), msg.sender, _tokenId);
_itemsSolds += 1;
emit itemSold(_tokenId, priceEmit, Items[_tokenId].seller, msg.sender, PaymentType(1));
}
event unsaledItem(uint256 tokenId, address seller);
/**
* @dev Removes an NFT from sale
* @param _tokenId Token ID of the NFT
*/
function unsaleItem(uint256 _tokenId) payable external {
require(_tokenIds >= _tokenId, "NFT does not exist");
require(ownerOf(_tokenId) == address(this), "NFT not for sale");
require(msg.sender == Items[_tokenId].seller, "Only the seller can unsale it");
Items[_tokenId].price = 0;
_transfer(address(this), msg.sender, _tokenId);
emit unsaledItem(_tokenId, msg.sender);
}
/**
* @dev Withdraws Ether from the contract
* @param _to Address to send the Ether to
* @param _amount Amount of Ether to withdraw
*/
function withdraw(address payable _to, uint256 _amount) public onlyOwner {
(bool sent,) = _to.call{value: _amount}("");
require(sent, "Failed to send Ether");
}
/**
* @dev Withdraws ERC20 tokens from the contract
* @param _to Address to send the tokens to
* @param amount Amount of tokens to withdraw
* @param _contractAddress Address of the ERC20 token contract
*/
function withdrawERC20(address _to, uint256 amount, address _contractAddress) public onlyOwner {
IERC20 contractAddress = IERC20(_contractAddress);
bool sent = contractAddress.transfer(_to, amount);
require(sent, "Failed to send ERC20");
}
function getAllNFTIds(address owner) public view returns (uint256[] memory) {
uint256 ownerTokenCount = balanceOf(owner);
uint256[] memory tokenIds = new uint256[](ownerTokenCount);
for (uint256 i = 0; i < ownerTokenCount; i++) {
tokenIds[i] = tokenOfOwnerByIndex(owner, i);
}
return tokenIds;
}
}