-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreasureMarket.sol
More file actions
324 lines (272 loc) · 11.2 KB
/
TreasureMarket.sol
File metadata and controls
324 lines (272 loc) · 11.2 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
//for letting users use USDC and other erc20s in the marketplace
import {IERC20Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
//Includes BaseRelayRecipient, for meta transactions.
import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import {Treasure} from "./Treasure.sol";
contract TreasureMarket is
Initializable,
UUPSUpgradeable,
PausableUpgradeable, // pausable by owner.
OwnableUpgradeable, //owned by deployer.
ReentrancyGuardUpgradeable
{
//Time of last meta transaction by this user.
//mapping(address => uint) private lastSignedMetaTransaction;
uint256 public gasLessRateLimit;
address private _trustedForwarder;
uint256 public feePercentagePoint; //out of 1000. 25/1000 = 2.5%
uint256 public royaltyPercentagePoint; //out of 1000.
//sale storage
mapping(uint256 => bool) public isForSaleById;
mapping(uint256 => uint256) public priceById;
mapping(uint256 => address) public seller;
//list of token addresses that are allowed to be used in our marketplace.
mapping(address => bool) tokenAddressIsAllowed;
mapping(uint256 => uint256) priceByIdTokens;
mapping(uint256 => address) forSaleWithToken;
Treasure treasure;
//events
event ListItem(address _seller, uint256 _id, uint256 _price);
event UnlistItem(address _seller, uint256 _id);
event SaleComplete(address _seller, uint256 _id, address _buyer);
event ListItemWithToken(
address _seller,
uint256 _id,
uint256 _price,
address _tokenAddress
);
event UnlistItemWithToken(address _seller, uint256 _id);
event SaleCompleteWithToken(
address _seller,
uint256 _id,
address _buyer,
address _tokenAddress
);
event RoyaltySet(uint256 _royalty);
event FeeSet(uint256 _fee);
event FeesWithdrawn(address _caller, address _to, uint256 _amount);
event FeesWithdrawnToken(
address _caller,
address _to,
uint256 _amount,
address tokenAdd
);
event AllowedTokenAdded(address _caller, address _token);
event AllowedTokenRemoved(address _caller, address _token);
function initialize(
uint256 _gasLessRateLimit,
address payable _treasureDeployedAddress,
address _forwarder,
uint256 _feePercentagePoint,
uint256 _royaltyPercentagePoint,
address _defaultTokenAddress
) public initializer {
gasLessRateLimit = _gasLessRateLimit;
treasure = Treasure(_treasureDeployedAddress);
feePercentagePoint = _feePercentagePoint;
royaltyPercentagePoint = _royaltyPercentagePoint;
_trustedForwarder = _forwarder;
__Ownable_init();
__Pausable_init();
__ReentrancyGuard_init();
tokenAddressIsAllowed[_defaultTokenAddress] = true;
}
function _authorizeUpgrade(address) internal override onlyOwner {}
//https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.3.0/contracts/metatx/ERC2771Context.sol
function isTrustedForwarder(address forwarder)
public
view
virtual
returns (bool)
{
return forwarder == _trustedForwarder;
}
function _msgSender()
internal
view
virtual
override
returns (address sender)
{
if (isTrustedForwarder(msg.sender)) {
// The assembly code is more direct than the Solidity version using `abi.decode`.
assembly {
sender := shr(96, calldataload(sub(calldatasize(), 20)))
}
} else {
return super._msgSender();
}
}
function _msgData()
internal
view
virtual
override
returns (bytes calldata)
{
if (isTrustedForwarder(msg.sender)) {
return msg.data[:msg.data.length - 20];
} else {
return super._msgData();
}
}
//======== Marketplace Sales In Native Coin ========//
function listItem(uint256 _id, uint256 _price) public {
require(
treasure.ownerOf(_id) == _msgSender(),
"You can not list assets you dont own."
);
require(_price != 0, "Price can not be set to 0.");
require(
isForSaleById[_id] == false && forSaleWithToken[_id] == address(0),
"This item is already for sale."
);
isForSaleById[_id] = true;
priceById[_id] = _price;
seller[_id] = _msgSender();
treasure.transferFrom(_msgSender(), address(this), _id);
emit ListItem(seller[_id], _id, _price);
}
function unlistItem(uint256 _id) public {
require(seller[_id] == _msgSender());
require(isForSaleById[_id] == true);
isForSaleById[_id] = false;
priceById[_id] = 0;
seller[_id] = address(0);
treasure.safeTransferFrom(address(this), _msgSender(), _id);
emit UnlistItem(_msgSender(), _id);
}
//signature validation can be checked in SignatureChecker.sol
//assets are listed offchain using eth-sig-util to sign messages
function instantBuy(uint256 _id) public payable nonReentrant {
//check SignatureChecker of sale. if its invalid, remove it.
require(isForSaleById[_id], "Item not for sale.");
require(msg.value == priceById[_id], "Incorrect amount of value sent.");
require(seller[_id] != address(0));
//Split profits
uint256 royalty = ((msg.value * royaltyPercentagePoint) / 1000);
address originalPlayer = treasure.getOriginalPlayer(_id);
(bool sentRoyalty, ) = originalPlayer.call{value: royalty}("");
require(sentRoyalty, "Failed to send royalty. Transaction fails.");
uint256 funds = (msg.value *
(1000 - royaltyPercentagePoint - feePercentagePoint)) / 1000;
(bool sentFunds, ) = seller[_id].call{value: funds}("");
require(
sentFunds,
"Failed to send funds to seller. Transaction fails."
);
//remaining funds (msg.value*feePercentagePoint/1000) are held in this contract until owner wants to withdraw.
isForSaleById[_id] = false;
treasure.safeTransferFrom(treasure.ownerOf(_id), _msgSender(), _id);
emit SaleComplete(seller[_id], _id, _msgSender());
}
//======== Marketplace Sales With Approved Tokens - default should be USDC ========//
function tokenListItem(
uint256 _id,
uint256 _price,
address _tokenAddress
) public {
require(
tokenAddressIsAllowed[_tokenAddress],
"This is not one of the approved tokens for the marketplace. Try USDC or ask an admin what they are."
);
require(
treasure.ownerOf(_id) == _msgSender(),
"You can not list assets you dont own."
);
require(_price != 0, "Price can not be set to 0.");
require(
isForSaleById[_id] == false && forSaleWithToken[_id] == address(0),
"This item is already for sale."
);
priceByIdTokens[_id] = _price;
forSaleWithToken[_id] = _tokenAddress; //normally is true/false, but with token lets just store the address here
seller[_id] = _msgSender();
treasure.transferFrom(_msgSender(), address(this), _id);
emit ListItemWithToken(seller[_id], _id, _price, _tokenAddress);
}
function tokenUnlistItem(uint256 _id) public {
require(seller[_id] == _msgSender());
require(forSaleWithToken[_id] != address(0));
priceByIdTokens[_id] = 0;
forSaleWithToken[_id] = address(0);
seller[_id] = address(0);
treasure.safeTransferFrom(address(this), _msgSender(), _id);
emit UnlistItemWithToken(_msgSender(), _id);
}
//@dev - moving tokens to the contract is the same gas as moving them to the platform owner, so just do that.
function tokenInstantBuy(uint256 _id, address _tokenAddress) public nonReentrant {
//check SignatureChecker of sale. if its invalid, remove it.
require(forSaleWithToken[_id] != address(0), "Item not for sale.");
require(
seller[_id] != address(0),
"Something is not quite right here. The seller cant be 0"
);
IERC20Upgradeable paymentToken = IERC20Upgradeable(_tokenAddress);
require(
paymentToken.balanceOf(_msgSender()) >= priceByIdTokens[_id],
"The buyer token balance is too small."
);
//Split profits
uint256 royalty = (priceByIdTokens[_id] * royaltyPercentagePoint) /
1000; //send to original player
uint256 funds = (priceByIdTokens[_id] *
(1000 - royaltyPercentagePoint - feePercentagePoint)) / 1000; //send to seller
uint256 platformFee = (priceByIdTokens[_id] * feePercentagePoint) /
1000;
paymentToken.transferFrom(
_msgSender(),
treasure.getOriginalPlayer(_id),
royalty
);
paymentToken.transferFrom(_msgSender(), seller[_id], funds);
paymentToken.transferFrom(_msgSender(), owner(), platformFee);
//payment made. Move the NFT and emit
address sellerAddress = seller[_id];
forSaleWithToken[_id] = address(0);
seller[_id] = address(0);
treasure.safeTransferFrom(address(this), _msgSender(), _id);
emit SaleCompleteWithToken(sellerAddress, _id, _msgSender(), _tokenAddress);
}
//======== Admin functions ========//
function addAllowedToken(address _tokenAddress) public onlyOwner {
tokenAddressIsAllowed[_tokenAddress] = true;
emit AllowedTokenAdded(_msgSender(), _tokenAddress);
}
function removeAllowedToken(address _tokenAddress) public onlyOwner {
tokenAddressIsAllowed[_tokenAddress] = false;
emit AllowedTokenRemoved(_msgSender(), _tokenAddress);
}
function setRoyalty(uint256 _points) public onlyOwner {
royaltyPercentagePoint = _points;
emit RoyaltySet(_points);
}
function setFee(uint256 _points) public onlyOwner {
feePercentagePoint = _points;
emit FeeSet(_points);
}
//Marketplace Owner can withdraw fees.
function ownerWithdrawFees(address payable _to, uint256 _amount)
public
onlyOwner
{
//use `call` here to pass on gas, so you can do more stuff when this is called.
(bool sent, ) = _to.call{value: _amount}("");
require(sent, "Failed to send ETH. Transaction fails.");
emit FeesWithdrawn(_msgSender(), _to, _amount);
}
function ownerWithdrawFeesToken(
address _to,
uint256 _amount,
address _tokenAdd
) public onlyOwner {
IERC20Upgradeable(_tokenAdd).transfer(_to, _amount);
emit FeesWithdrawnToken(_msgSender(), _to, _amount, _tokenAdd);
}
}