-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailNames.sol
More file actions
612 lines (524 loc) · 25.7 KB
/
Copy pathMailNames.sol
File metadata and controls
612 lines (524 loc) · 25.7 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// SPDX-License-Identifier: BSL 1.1 - Peng Protocol 2025
pragma solidity ^0.8.2;
// File Version: 0.0.35 (10/01/2026)
// Changelog:
// - 0.0.35 (10/01/2026): Ensured check-in processing skips names with outdated owners rather than reverting.
// - 20/11/2025: Fixed lock amount normalization.
interface IERC20 {
function decimals() external view returns (uint8);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
}
interface IERC721Receiver {
function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) external returns (bytes4);
}
interface IMailLocker {
function depositLock(uint256 amount, address user, uint256 unlockTime) external;
}
interface IMailMarket {
function settleBid(uint256 _nameHash, uint256 _bidIndex, bool _isETH, address _token) external;
function getBidDetails(uint256 _nameHash, uint256 _bidIndex, bool _isETH, address _token) external view returns (address bidder, uint256 amount);
function cancelBid(uint256 _nameHash, uint256 _bidIndex, bool _isETH, address _token) external;
}
contract MailNames {
struct NameRecord {
string name;
uint256 nameHash;
uint256 tokenId;
uint256 allowanceEnd;
uint256 graceEnd;
CustomRecord[5] customRecords;
}
struct SubnameRecord {
uint256 parentHash;
string subname;
uint256 subnameHash;
CustomRecord[5] customRecords;
}
struct CustomRecord {
string text;
string resolver;
string contentHash;
uint256 ttl;
address targetAddress;
}
struct SettlementData {
uint256 nameHash;
uint256 tokenId;
address oldOwner;
address newOwner;
uint256 amount;
bool postGrace;
}
struct PendingCheckin {
uint256 nameHash;
address user;
uint256 queuedTime;
uint256 waitDuration;
}
// New (0.0.14) struct for pending settlements
struct PendingSettlement {
uint256 nameHash;
uint256 bidIndex;
bool isETH;
address token;
uint256 queueTime;
}
mapping(uint256 => NameRecord) private nameRecords;
mapping(uint256 => SubnameRecord[]) private subnameRecords;
uint256[] private allNameHashes;
uint256 public constant ALLOWANCE_PERIOD = 365 days;
uint256 public constant GRACE_PERIOD = 7 days;
uint256 public constant MAX_NAME_LENGTH = 24;
uint256 public constant MAX_STRING_LENGTH = 1024;
uint256 public totalNames;
mapping(uint256 => uint256) public tokenIdToNameHash;
mapping(uint256 => uint256) public nameHashToTokenId;
mapping(address => uint256) private _balances;
mapping(uint256 => address) public ownerOf;
mapping(uint256 => address) public getApproved;
mapping(address => mapping(address => bool)) public isApprovedForAll;
address public owner;
address public mailToken;
address public mailLocker;
address public mailMarket;
uint256 public checkInCost = 5e17; // 0.5 MAIL (in wei, 18 decimals)
uint256 public currentTime;
bool public isWarped;
// New (0.0.14) array to store pending settlements
PendingSettlement[] public pendingSettlements;
PendingCheckin[] public pendingCheckins;
uint256 public nextProcessIndex;
event NameMinted(uint256 indexed nameHash, string name, address owner);
event NameCheckedIn(uint256 indexed nameHash, address owner);
event SubnameMinted(uint256 indexed parentHash, uint256 subnameHash, string subname, address owner);
event RecordsUpdated(uint256 indexed nameHash, address owner);
event GraceReset(uint256 indexed nameHash, address indexed newOwner);
event QueueCheckInQueued(uint256 indexed nameHash, address indexed user, uint256 minRequired, uint256 waitDuration);
event CheckInProcessed(uint256 indexed nameHash, address indexed user);
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
// New (0.0.14) events for settlement queueing and processing
event SettlementProcessed(uint256 indexed nameHash, address newOwner);
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function warp(uint256 newTimestamp) external onlyOwner {
currentTime = newTimestamp;
isWarped = true;
}
function unWarp() external onlyOwner {
isWarped = false;
}
function _now() internal view returns (uint256) {
return isWarped ? currentTime : block.timestamp;
}
function setCheckInCost(uint256 _cost) external onlyOwner {
checkInCost = _cost;
}
function transferOwnership(address _newOwner) external onlyOwner {
require(_newOwner != address(0), "Invalid owner");
address oldOwner = owner;
owner = _newOwner;
emit OwnershipTransferred(oldOwner, _newOwner);
}
function setMailToken(address _mailToken) external onlyOwner {
mailToken = _mailToken;
}
function setMailLocker(address _mailLocker) external onlyOwner {
mailLocker = _mailLocker;
// Grant infinite approval to the locker
IERC20(mailToken).approve(_mailLocker, type(uint256).max);
}
function setMailMarket(address _mailMarket) external onlyOwner {
mailMarket = _mailMarket;
}
function _stringToHash(string memory _str) private pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(_str)));
}
function _validateName(string memory _name) private pure returns (bool) {
bytes memory nameBytes = bytes(_name);
if (nameBytes.length > MAX_NAME_LENGTH || nameBytes.length == 0) return false;
for (uint256 i = 0; i < nameBytes.length; i++) {
if (nameBytes[i] == " ") return false;
}
return true;
}
function _validateRecord(CustomRecord memory _record) private pure {
require(bytes(_record.text).length <= MAX_STRING_LENGTH, "Text too long");
require(bytes(_record.resolver).length <= MAX_STRING_LENGTH, "Resolver too long");
require(bytes(_record.contentHash).length <= MAX_STRING_LENGTH, "Content hash too long");
}
function _calculateWaitDuration(uint256 _queueLen) private pure returns (uint256) {
uint256 wait = 10 minutes + 6 hours * _queueLen;
return wait > 2 weeks ? 2 weeks : wait;
}
function _transfer(uint256 _tokenId, address _to) internal {
require(_to != address(0), "Invalid to");
address from = ownerOf[_tokenId];
require(from != address(0), "Token not minted");
require(from == msg.sender || getApproved[_tokenId] == msg.sender || isApprovedForAll[from][msg.sender], "Unauthorized");
_balances[from]--;
_balances[_to]++;
ownerOf[_tokenId] = _to;
delete getApproved[_tokenId];
emit Transfer(from, _to, _tokenId);
}
function _isContract(address _account) private view returns (bool) {
uint256 size;
assembly {
size := extcodesize(_account)
}
return size > 0;
}
function _checkOnERC721Received(address _to, address _from, uint256 _tokenId, bytes memory _data) private {
if (_isContract(_to)) {
try IERC721Receiver(_to).onERC721Received(msg.sender, _from, _tokenId, _data) returns (bytes4 retval) {
require(retval == IERC721Receiver.onERC721Received.selector, "ERC721: transfer to non ERC721Receiver");
} catch {
revert("ERC721: transfer to non ERC721Receiver");
}
}
}
function _processQueueRequirements() private returns (uint256 queueLen, uint256 minRequired) {
queueLen = pendingCheckins.length - nextProcessIndex;
minRequired = checkInCost; // FIX: Use raw cost (e.g. 5e17)
IERC20(mailToken).transferFrom(msg.sender, address(this), minRequired);
// FIX (0.0.34): Do not divide by decimals. Pass raw amount to Locker.
IMailLocker(mailLocker).depositLock(minRequired, msg.sender, _now() + 365 days * 10);
}
// Changelog: 0.0.31 (17/10/2025) - Adjusted pre-grace and post-grace settlement logic.
function _settleBid(uint256 _nameHash, uint256 _bidIndex, bool _isETH, address _token) private {
SettlementData memory settlement;
settlement.nameHash = _nameHash;
settlement.tokenId = nameHashToTokenId[_nameHash];
settlement.oldOwner = ownerOf[settlement.tokenId];
settlement.postGrace = _now() > nameRecords[_nameHash].graceEnd;
if (settlement.postGrace) {
// --- FIX ---
// This block should ONLY queue the settlement.
// The rest of the logic (grace reset, check-in)
// is moved to processSettlement().
pendingSettlements.push(PendingSettlement(_nameHash, _bidIndex, _isETH, _token, _now()));
} else {
// Pre-grace: Settle immediately
IMailMarket(mailMarket).settleBid(_nameHash, _bidIndex, _isETH, _token);
}
// --- BUGGY LOGIC REMOVED FROM HERE
}
// New helpers for processSettlement
function _calculateSettlementRequirements() private view returns (uint256 queueLen, uint256 minReq, uint256 fullMinReq) {
queueLen = pendingCheckins.length - nextProcessIndex;
fullMinReq = checkInCost;
minReq = fullMinReq; // FIX (0.0.34): No normalization/division. minReq equals fullMinReq.
}
function _validateBidderMAILBalance(address bidder, uint256 requiredAmount) private view returns (bool) {
return IERC20(mailToken).balanceOf(bidder) >= requiredAmount;
}
function _executeSettlement(PendingSettlement memory settlement) private returns (address newOwner) {
IMailMarket(mailMarket).settleBid(settlement.nameHash, settlement.bidIndex, settlement.isETH, settlement.token);
newOwner = ownerOf[nameHashToTokenId[settlement.nameHash]];
}
function _updateNameRecordTimestamps(uint256 _nameHash) private {
NameRecord storage record = nameRecords[_nameHash];
record.graceEnd = _now() + GRACE_PERIOD;
record.allowanceEnd = _now() + ALLOWANCE_PERIOD;
}
function _lockBidderMAIL(address bidder, uint256 nameHash, uint256 minReq, uint256 fullMinReq) private {
IERC20(mailToken).transferFrom(bidder, address(this), fullMinReq);
IMailLocker(mailLocker).depositLock(minReq, bidder, _now() + 365 days * 10);
uint256 queueLen = pendingCheckins.length - nextProcessIndex;
uint256 waitDuration = _calculateWaitDuration(queueLen);
pendingCheckins.push(PendingCheckin(nameHash, bidder, _now(), waitDuration));
emit QueueCheckInQueued(nameHash, bidder, fullMinReq, waitDuration);
}
function _removePendingSettlement(uint256 _index) private {
pendingSettlements[_index] = pendingSettlements[pendingSettlements.length - 1];
pendingSettlements.pop();
}
// Added $MAIL balance check and penalty.
function processSettlement(uint256 _index) external {
require(_index < pendingSettlements.length, "Invalid index");
PendingSettlement memory settlement = pendingSettlements[_index];
require(_now() >= settlement.queueTime + 3 weeks, "Settlement not ready");
// Get bidder details and calculate requirements
(address bidder, ) = IMailMarket(mailMarket).getBidDetails(
settlement.nameHash,
settlement.bidIndex,
settlement.isETH,
settlement.token
);
(uint256 queueLen, uint256 minReq, uint256 fullMinReq) = _calculateSettlementRequirements();
// Check if bidder has sufficient MAIL
if (!_validateBidderMAILBalance(bidder, fullMinReq)) {
// Cancel bid with 1% penalty instead of reverting
IMailMarket(mailMarket).cancelBid(settlement.nameHash, settlement.bidIndex, settlement.isETH, settlement.token);
_removePendingSettlement(_index);
return; // Exit gracefully without settling
}
// Execute settlement
address newOwner = _executeSettlement(settlement);
_updateNameRecordTimestamps(settlement.nameHash);
emit SettlementProcessed(settlement.nameHash, newOwner);
// Lock bidder's MAIL and queue check-in
_lockBidderMAIL(newOwner, settlement.nameHash, minReq, fullMinReq);
// Remove processed settlement
_removePendingSettlement(_index);
}
// Changelog: 0.0.30 (17/11/2025) - Allowed post-grace settlement.
function acceptMarketBid(uint256 _nameHash, bool _isETH, address _token, uint256 _bidIndex) external {
// 1. Check for existence first
require(nameRecords[_nameHash].nameHash != 0, "Name not minted");
NameRecord storage record = nameRecords[_nameHash];
uint256 tokenId = nameHashToTokenId[_nameHash];
// --- LOGIC FIX ---
// Check if the name is still within its allowance period
if (_now() <= record.allowanceEnd) {
// PRE-EXPIRATION: Only the owner can accept a bid.
require(ownerOf[tokenId] == msg.sender, "Not owner");
} else {
// POST-EXPIRATION: Anyone can trigger settlement,
// but only after the grace period has ended.
require(_now() > record.graceEnd, "Settlement still in grace period");
}
// 3. Proceed with settlement logic
_settleBid(_nameHash, _bidIndex, _isETH, _token);
}
function mintName(string memory _name) external {
require(_validateName(_name), "Invalid name");
uint256 nameHash = _stringToHash(_name);
require(nameRecords[nameHash].nameHash == 0, "Name already minted");
uint256 tokenId = totalNames++;
NameRecord storage record = nameRecords[nameHash];
record.name = _name;
record.nameHash = nameHash;
record.tokenId = tokenId;
record.allowanceEnd = _now() + ALLOWANCE_PERIOD;
record.graceEnd = _now() + ALLOWANCE_PERIOD + GRACE_PERIOD;
for (uint256 i = 0; i < 5; i++) {
record.customRecords[i] = CustomRecord("", "", "", 0, address(0));
}
tokenIdToNameHash[tokenId] = nameHash;
nameHashToTokenId[nameHash] = tokenId;
ownerOf[tokenId] = msg.sender;
_balances[msg.sender]++;
allNameHashes.push(nameHash);
emit NameMinted(nameHash, _name, msg.sender);
emit Transfer(address(0), msg.sender, tokenId);
}
function mintSubname(string memory _parentName, string memory _subname) external {
require(_validateName(_subname), "Invalid subname");
uint256 parentHash = _stringToHash(_parentName);
uint256 tokenId = nameHashToTokenId[parentHash];
require(ownerOf[tokenId] == msg.sender, "Not parent owner");
uint256 subnameHash = _stringToHash(_subname);
subnameRecords[parentHash].push();
uint256 newIndex = subnameRecords[parentHash].length - 1;
SubnameRecord storage subname = subnameRecords[parentHash][newIndex];
subname.parentHash = parentHash;
subname.subname = _subname;
subname.subnameHash = subnameHash;
for (uint256 i = 0; i < 5; i++) {
subname.customRecords[i] = CustomRecord("", "", "", 0, address(0));
}
emit SubnameMinted(parentHash, subnameHash, _subname, msg.sender);
}
// Changelog: 0.0.16 (06/10/2025) - Added helper to clear pending settlements for a name
function _clearPendingSettlements(uint256 _nameHash) private {
for (uint256 i = 0; i < pendingSettlements.length; i++) {
if (pendingSettlements[i].nameHash == _nameHash) {
pendingSettlements[i] = pendingSettlements[pendingSettlements.length - 1];
pendingSettlements.pop();
i--; // Adjust index after pop
}
}
}
// Changelog: 0.0.35 -
function _processNextCheckin() private {
if (nextProcessIndex >= pendingCheckins.length) return;
PendingCheckin memory nextCheck = pendingCheckins[nextProcessIndex];
// Wait until the specific wait duration has passed
if (_now() < nextCheck.queuedTime + nextCheck.waitDuration) return;
// Ensure name exists
require(nameRecords[nextCheck.nameHash].nameHash != 0, "Name not minted");
// --- PATCH START ---
// Instead of reverting with require(), we check ownership conditionally.
// If the user lost ownership (e.g. via settlement), we simply skip the update.
// The index increments regardless, preventing the queue from stalling.
uint256 tokenId = nameHashToTokenId[nextCheck.nameHash];
if (ownerOf[tokenId] == nextCheck.user) {
NameRecord storage record = nameRecords[nextCheck.nameHash];
record.allowanceEnd = _now() + ALLOWANCE_PERIOD;
record.graceEnd = _now() + ALLOWANCE_PERIOD + GRACE_PERIOD;
_clearPendingSettlements(nextCheck.nameHash); // Clear pending settlements
emit NameCheckedIn(nextCheck.nameHash, nextCheck.user);
emit CheckInProcessed(nextCheck.nameHash, nextCheck.user);
}
// --- PATCH END ---
// Always increment to process the next item in the next call
nextProcessIndex++;
}
function advance() external {
_processNextCheckin();
}
function queueCheckIn(uint256 _nameHash) external {
// 1. Check for existence *first*
require(nameRecords[_nameHash].nameHash != 0, "Name not minted");
// 2. Check for ownership (buggy 'tokenId != 0' is removed)
uint256 tokenId = nameHashToTokenId[_nameHash];
require(ownerOf[tokenId] == msg.sender, "Not owner");
// 3. Proceed with check-in
NameRecord storage record = nameRecords[_nameHash];
require(_now() > record.allowanceEnd, "Not expired");
(uint256 queueLen, uint256 minRequired) = _processQueueRequirements();
uint256 waitDuration = _calculateWaitDuration(queueLen);
pendingCheckins.push(PendingCheckin(_nameHash, msg.sender, _now(), waitDuration));
emit QueueCheckInQueued(_nameHash, msg.sender, minRequired, waitDuration);
this.advance();
}
// This function is called by MailMarket.settleBid()
// It bypasses normal transfer auth
// Owner already authorized the action via acceptMarketBid()
function transfer(uint256 _tokenId, address _to) external {
require(msg.sender == mailMarket, "Only MailMarket");
require(_to != address(0), "Invalid to");
address from = ownerOf[_tokenId];
require(from != address(0), "Token not minted");
// bypass because the market is trusted
_balances[from]--;
_balances[_to]++;
ownerOf[_tokenId] = _to;
delete getApproved[_tokenId];
emit Transfer(from, _to, _tokenId);
}
function transferName(uint256 _nameHash, address _newOwner) external {
require(nameRecords[_nameHash].nameHash != 0, "Name not minted"); //This ensures the name hash is not zero.
uint256 tokenId = nameHashToTokenId[_nameHash];
// Removed unnecessary token ID zero check
_transfer(tokenId, _newOwner);
}
function transferFrom(address _from, address _to, uint256 _tokenId) external {
require(_from == ownerOf[_tokenId], "Wrong from");
uint256 nameHash = tokenIdToNameHash[_tokenId];
require(nameHash != 0, "Invalid tokenId");
_transfer(_tokenId, _to);
}
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes memory _data) external {
require(_from == ownerOf[_tokenId], "Wrong from");
uint256 nameHash = tokenIdToNameHash[_tokenId];
require(nameHash != 0, "Invalid tokenId");
_transfer(_tokenId, _to);
_checkOnERC721Received(_to, _from, _tokenId, _data);
}
function setCustomRecord(uint256 _nameHash, uint256 _index, CustomRecord memory _record) external {
_validateRecord(_record);
uint256 tokenId = nameHashToTokenId[_nameHash];
require(ownerOf[tokenId] == msg.sender, "Not owner");
require(_index < 5, "Invalid index");
nameRecords[_nameHash].customRecords[_index] = _record;
emit RecordsUpdated(_nameHash, msg.sender);
}
function setSubnameRecord(uint256 _parentHash, uint256 _subnameIndex, uint256 _recordIndex, CustomRecord memory _record) external {
_validateRecord(_record);
uint256 tokenId = nameHashToTokenId[_parentHash];
require(ownerOf[tokenId] == msg.sender, "Not parent owner");
require(_subnameIndex < subnameRecords[_parentHash].length, "Invalid subname index");
require(_recordIndex < 5, "Invalid record index");
subnameRecords[_parentHash][_subnameIndex].customRecords[_recordIndex] = _record;
emit RecordsUpdated(_parentHash, msg.sender);
}
// Changelog: 0.0.15 (06/10/2025) - Returns name string for a given token ID
function getNameByTokenId(uint256 _tokenId) external view returns (string memory name) {
uint256 nameHash = tokenIdToNameHash[_tokenId];
require(nameHash != 0, "Token ID not minted");
name = nameRecords[nameHash].name;
}
function getName(string memory _name) external view returns (address _owner) {
uint256 nameHash = _stringToHash(_name);
require(nameRecords[nameHash].nameHash != 0, "Name not registered!"); // Checks name hash is non-zero
uint256 tokenId = nameHashToTokenId[nameHash];
// removed non-zero token ID check 'require(tokenId != 0, ...)'
return ownerOf[tokenId];
}
// Changelog: 0.0.20 (06/10/2025) - Returns single NameRecord for name string
function getNameRecords(string memory _name) external view returns (NameRecord memory record) {
uint256 nameHash = _stringToHash(_name);
require(nameRecords[nameHash].nameHash != 0, "Name not found");
record = nameRecords[nameHash];
}
// Changelog: 0.0.20 (06/10/2025) - Returns PendingSettlement for given index
function getSettlementById(uint256 _index) external view returns (PendingSettlement memory settlement) {
require(_index < pendingSettlements.length, "Invalid index");
settlement = pendingSettlements[_index];
}
// Changelog: 0.0.19 (06/10/2025) - Uses name string, returns paginated settlements
function getPendingSettlements(string memory _name, uint256 _step, uint256 _maxIterations) external view returns (PendingSettlement[] memory settlements) {
uint256 nameHash = _stringToHash(_name);
uint256 count = 0;
for (uint256 i = _step; i < pendingSettlements.length && count < _maxIterations; i++) {
if (pendingSettlements[i].nameHash == nameHash) {
count++;
}
}
settlements = new PendingSettlement[](count);
uint256 index = 0;
for (uint256 i = _step; i < pendingSettlements.length && index < count; i++) {
if (pendingSettlements[i].nameHash == nameHash) {
settlements[index] = pendingSettlements[i];
index++;
}
}
}
function getSubnameID(string memory _parentName, string memory _subname) external view returns (uint256 subnameIndex, bool found) {
uint256 parentHash = _stringToHash(_parentName);
uint256 subnameHash = _stringToHash(_subname);
SubnameRecord[] storage subnames = subnameRecords[parentHash];
for (uint256 i = 0; i < subnames.length; i++) {
if (subnames[i].subnameHash == subnameHash) {
return (i, true);
}
}
return (0, false);
}
function getSubRecords(string memory _parentName, string memory _subname) external view returns (CustomRecord[5] memory customRecords) {
uint256 parentHash = _stringToHash(_parentName);
(uint256 subnameIndex, bool found) = this.getSubnameID(_parentName, _subname);
require(found, "Subname not found");
return subnameRecords[parentHash][subnameIndex].customRecords;
}
function getSubnames(string memory _parentName, uint256 step, uint256 maxIterations) external view returns (string[] memory subnames) {
uint256 parentHash = _stringToHash(_parentName);
SubnameRecord[] storage subs = subnameRecords[parentHash];
uint256 length = subs.length;
uint256 end = step + maxIterations > length ? length : step + maxIterations;
uint256 count = end > step ? end - step : 0;
subnames = new string[](count);
for (uint256 i = 0; i < count; i++) {
subnames[i] = subs[step + i].subname;
}
}
function approve(address _to, uint256 _tokenId) external {
uint256 nameHash = tokenIdToNameHash[_tokenId];
require(nameHash != 0, "Invalid tokenId");
address tokenOwner = ownerOf[_tokenId];
require(msg.sender == tokenOwner || isApprovedForAll[tokenOwner][msg.sender], "Unauthorized");
getApproved[_tokenId] = _to;
emit Approval(tokenOwner, _to, _tokenId);
}
function setApprovalForAll(address _operator, bool _approved) external {
isApprovedForAll[msg.sender][_operator] = _approved;
emit ApprovalForAll(msg.sender, _operator, _approved);
}
function balanceOf(address _owner) external view returns (uint256) {
require(_owner != address(0), "Zero address");
return _balances[_owner];
}
}