-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityForging.sol
255 lines (203 loc) · 10.6 KB
/
EntityForging.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import { ReentrancyGuard } from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import { Pausable } from "@openzeppelin/contracts/security/Pausable.sol";
import { ITraitForgeNft } from "./interfaces/ITraitForgeNft.sol";
import { IEntityForging } from "./interfaces/IEntityForging.sol";
import { AddressProviderResolver } from "contracts/core/AddressProviderResolver.sol";
contract EntityForging is IEntityForging, AddressProviderResolver, ReentrancyGuard, Pausable {
// Type declarations
// Events
// Modifiers
// State variables
address payable public nukeFundAddress;
uint256 public taxCut = 1000; //10%
uint256 private constant BPS = 10_000; // denominator of basis points
uint256 public oneYearInDays = 365 days;
uint256 public listingCount = 0;
/// @dev tokenid -> listings index
mapping(uint256 => uint256) public listedTokenIds;
mapping(uint256 => mapping(uint256 => bool)) private forgedPairs; //innefficient
/// @dev index -> listing info
mapping(uint256 => Listing) public listings;
mapping(uint256 => uint8) public forgingCounts; // track forgePotential
mapping(uint256 => uint256) private lastForgeResetTimestamp;
//Errors
error EntityForging__OffsetOutOfBounds();
error EntityForging__TokenAlreadyListed();
error EntityForging__TokenNotOwnedByCaller();
error EntityForging__FeeTooLow();
error EntityForging__NotForger();
error EntityForging__MergerTokenIdIsZero();
error EntityForging__ForgerTokenIdNotListed();
error EntityForging__MergerTokenNotOwnedByCaller();
error EntityForging__TokensNotSameGeneration();
error EntityForging__TokensAlreadyForged();
error EntityForging__FeeMismatchWithEthSent();
error EntityForging__MergerEntropyCannotMerge();
error EntityForging__InsufficientForgerForgePotential();
error EntityForging__InsufficientMergerForgePotential();
error EntityForging__TokenNotListedForForging(uint256 tokenId);
// Functions
constructor(address _addressProvider) AddressProviderResolver(_addressProvider) { }
/**
* external & public functions *******************************
*/
//////////////////////////// write functions ////////////////////////////
function pause() public onlyProtocolMaintainer {
_pause();
}
function unpause() public onlyProtocolMaintainer {
_unpause();
}
function setTaxCut(uint256 _taxCut) external onlyProtocolMaintainer {
require(_taxCut <= BPS, "Tax cut cannot exceed 100%");
taxCut = _taxCut;
}
function setOneYearInDays(uint256 value) external onlyProtocolMaintainer {
oneYearInDays = value;
}
function listForForging(uint256 tokenId, uint256 fee) public whenNotPaused nonReentrant {
Listing memory _listingInfo = listings[listedTokenIds[tokenId]];
ITraitForgeNft traitForgeNft = _getTraitForgeNft();
if (_listingInfo.isListed) revert EntityForging__TokenAlreadyListed();
if (traitForgeNft.ownerOf(tokenId) != msg.sender) revert EntityForging__TokenNotOwnedByCaller();
if (fee < traitForgeNft.calculateMintPrice()) revert EntityForging__FeeTooLow();
_resetForgingCountIfNeeded(tokenId);
uint256 entropy = traitForgeNft.getTokenEntropy(tokenId); // Retrieve entropy for tokenId
bool isForger = (entropy % 3) == 0; // Determine if the token is a forger need to keep the logic somewhere else
if (!isForger) revert EntityForging__NotForger();
uint8 forgePotential = uint8((entropy / 10) % 10); // Extract the 5th digit from the entropy
if (!(forgePotential > 0 && forgingCounts[tokenId] < forgePotential)) {
revert EntityForging__InsufficientForgerForgePotential();
}
++listingCount;
listings[listingCount] = Listing(msg.sender, tokenId, true, fee);
listedTokenIds[tokenId] = listingCount;
emit ListedForForging(tokenId, fee);
}
function forgeWithListed(
uint256 forgerTokenId,
uint256 mergerTokenId
)
external
payable
whenNotPaused
nonReentrant
returns (uint256)
{
Listing memory _forgerListingInfo = listings[listedTokenIds[forgerTokenId]];
_forgePreGuards(forgerTokenId, mergerTokenId, _forgerListingInfo.isListed);
if (msg.value != _forgerListingInfo.fee) revert EntityForging__FeeMismatchWithEthSent();
uint256 lowerId = _lowerId(forgerTokenId, mergerTokenId);
uint256 higherId = _higherId(forgerTokenId, mergerTokenId); //innefficient
if (forgedPairs[lowerId][higherId]) revert EntityForging__TokensAlreadyForged();
uint256 forgerId = forgerTokenId;
uint256 mergerId = mergerTokenId;
_resetForgingCountIfNeeded(forgerId); // Reset for forger if needed
_resetForgingCountIfNeeded(mergerId); // Reset for merger if needed
// Check forger's breed count increment but do not check forge potential here
// as it is already checked in listForForging for the forger
forgingCounts[forgerId]++;
ITraitForgeNft traitForgeNft = _getTraitForgeNft();
// Check and update for merger token's forge potential
uint256 mergerEntropy = traitForgeNft.getTokenEntropy(mergerId);
if (!(mergerEntropy % 3 != 0)) revert EntityForging__MergerEntropyCannotMerge();
uint8 mergerForgePotential = uint8((mergerEntropy / 10) % 10); // Extract the 5th digit from the entropy
forgingCounts[mergerId]++;
if (!(mergerForgePotential > 0 && forgingCounts[mergerId] <= mergerForgePotential)) {
revert EntityForging__InsufficientMergerForgePotential();
}
/// TODO Stack too deep
// uint256 devShare = (msg.value * taxCut) / BPS;
// uint256 forgingFee = _forgerListingInfo.fee;
// uint256 forgerShare = forgingFee - devShare;
// address payable forgerOwner = payable(traitForgeNft.ownerOf(forgerId));
// (bool success,) = nukeFundAddress.call{ value: devShare }("");
// require(success, "Failed to send to NukeFund");
// (bool success_forge,) = forgerOwner.call{ value: forgerShare }("");
// require(success_forge, "Failed to send to Forge Owner");
_processTransferShares(forgerId, _forgerListingInfo.fee, traitForgeNft);
uint256 newTokenId = traitForgeNft.forge(msg.sender, forgerId, mergerId, "");
forgedPairs[lowerId][higherId] = true;
// Cancel listed forger nft
_cancelListingForForging(forgerId);
uint256 newEntropy = traitForgeNft.getTokenEntropy(newTokenId);
emit EntityForged(newTokenId, forgerId, mergerId, newEntropy, _forgerListingInfo.fee);
return newTokenId;
}
function cancelListingForForging(uint256 tokenId) external whenNotPaused nonReentrant {
ITraitForgeNft traitForgeNft = _getTraitForgeNft();
if (!(traitForgeNft.ownerOf(tokenId) == msg.sender || msg.sender == address(traitForgeNft))) {
revert EntityForging__TokenNotOwnedByCaller();
}
if (!listings[listedTokenIds[tokenId]].isListed) revert EntityForging__TokenNotListedForForging(tokenId);
_cancelListingForForging(tokenId);
}
//////////////////////////// read functions ////////////////////////////
function fetchListings(uint256 offset, uint256 limit) external view returns (Listing[] memory _listings) {
// L07
if (offset >= listingCount) {
revert EntityForging__OffsetOutOfBounds();
}
uint256 end = offset + limit;
if (end > listingCount) end = listingCount;
uint256 resultCount = end - offset;
_listings = new Listing[](resultCount);
for (uint256 i = 0; i < resultCount; ++i) {
_listings[i] = listings[offset + i + 1];
}
}
function getListedTokenIds(uint256 tokenId_) external view override returns (uint256) {
return listedTokenIds[tokenId_];
}
function getListings(uint256 id) external view override returns (Listing memory) {
return listings[id];
}
function getListingForTokenId(uint256 tokenId) external view returns (Listing memory) {
return listings[listedTokenIds[tokenId]];
}
/**
* internal & private *******************************************
*/
function _getTraitForgeNft() private view returns (ITraitForgeNft) {
return ITraitForgeNft(_addressProvider.getTraitForgeNft());
}
function _cancelListingForForging(uint256 tokenId) internal {
delete listings[listedTokenIds[tokenId]];
emit CancelledListingForForging(tokenId); // Emitting with 0 fee to denote cancellation
}
function _resetForgingCountIfNeeded(uint256 tokenId) private {
uint256 oneYear = oneYearInDays;
if (lastForgeResetTimestamp[tokenId] == 0) {
lastForgeResetTimestamp[tokenId] = block.timestamp;
} else if (block.timestamp >= lastForgeResetTimestamp[tokenId] + oneYear) {
forgingCounts[tokenId] = 0; // Reset to the forge potential
lastForgeResetTimestamp[tokenId] = block.timestamp;
}
}
function _processTransferShares(uint256 forgerId, uint256 forgingFee, ITraitForgeNft traitForgeNft) private {
uint256 devShare = (forgingFee * taxCut) / BPS;
uint256 forgerShare = forgingFee - devShare;
(bool success,) = nukeFundAddress.call{ value: devShare }("");
require(success, "Failed to send to NukeFund");
address payable forgerOwner = payable(traitForgeNft.ownerOf(forgerId));
(bool success_forge,) = forgerOwner.call{ value: forgerShare }("");
require(success_forge, "Failed to send to Forge Owner");
}
function _forgePreGuards(uint256 forgerTokenId, uint256 mergerTokenId, bool forgerTokenIdIsListed) private view {
ITraitForgeNft traitForgeNft = _getTraitForgeNft();
if (!forgerTokenIdIsListed) revert EntityForging__ForgerTokenIdNotListed();
if (mergerTokenId == 0) revert EntityForging__MergerTokenIdIsZero();
if (traitForgeNft.ownerOf(mergerTokenId) != msg.sender) revert EntityForging__MergerTokenNotOwnedByCaller();
if (traitForgeNft.getTokenGeneration(mergerTokenId) != traitForgeNft.getTokenGeneration(forgerTokenId)) {
revert EntityForging__TokensNotSameGeneration();
}
}
function _lowerId(uint256 tokenId1, uint256 tokenId2) private pure returns (uint256) {
return tokenId1 < tokenId2 ? tokenId1 : tokenId2;
}
function _higherId(uint256 tokenId1, uint256 tokenId2) private pure returns (uint256) {
return tokenId1 < tokenId2 ? tokenId2 : tokenId1;
}
}