Skip to content

Commit b6b00fc

Browse files
committed
Refactor register/renew into internal funcs, add parent reverts
1 parent 8051947 commit b6b00fc

File tree

2 files changed

+264
-28
lines changed

2 files changed

+264
-28
lines changed

contracts/subdomainregistrar/SubdomainRegistrar.sol

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "../wrapper/INameWrapper.sol";
55
import "@openzeppelin/contracts/utils/Address.sol";
66
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
77
import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
8+
import "hardhat/console.sol";
89

910
error Unavailable();
1011
error Unauthorised(bytes32 node);
@@ -13,6 +14,10 @@ error NameNotRegistered();
1314
error InvalidTokenAddress(address);
1415
error NameNotSetup(bytes32 node);
1516
error DataMissing();
17+
error ParentExpired(bytes32 node);
18+
error ParentNotWrapped(bytes32 node);
19+
error ParentWillHaveExpired(bytes32 node);
20+
error DurationTooLong(bytes32 node);
1621

1722
struct Name {
1823
uint256 registrationFee; // per second
@@ -41,6 +46,11 @@ contract SubdomainRegistrar is ERC1155Holder {
4146
_;
4247
}
4348

49+
modifier canBeRegistered(bytes32 parentNode, uint64 duration) {
50+
_checkParent(parentNode, duration);
51+
_;
52+
}
53+
4454
function setupDomain(
4555
bytes32 node,
4656
address token,
@@ -71,6 +81,8 @@ contract SubdomainRegistrar is ERC1155Holder {
7181
) public payable {
7282
uint256 fee = duration * names[parentNode].registrationFee;
7383

84+
_checkParent(parentNode, duration);
85+
7486
if (fee > 0) {
7587
if (IERC20(names[parentNode].token).balanceOf(msg.sender) < fee) {
7688
revert InsufficientFunds();
@@ -99,11 +111,7 @@ contract SubdomainRegistrar is ERC1155Holder {
99111
bytes32 labelhash,
100112
uint64 duration
101113
) external payable returns (uint64 newExpiry) {
102-
bytes32 node = _makeNode(parentNode, labelhash);
103-
(, uint64 expiry) = wrapper.getFuses(node);
104-
if (expiry < block.timestamp) {
105-
revert NameNotRegistered();
106-
}
114+
_checkParent(parentNode, duration);
107115

108116
uint256 fee = duration * names[parentNode].registrationFee;
109117

@@ -115,11 +123,7 @@ contract SubdomainRegistrar is ERC1155Holder {
115123
);
116124
}
117125

118-
newExpiry = expiry += duration;
119-
120-
wrapper.setChildFuses(parentNode, labelhash, 0, newExpiry);
121-
122-
emit NameRenewed(node, newExpiry);
126+
_renew(parentNode, labelhash, duration);
123127
}
124128

125129
function batchRegister(
@@ -137,6 +141,8 @@ contract SubdomainRegistrar is ERC1155Holder {
137141
revert DataMissing();
138142
}
139143

144+
_checkParent(parentNode, duration);
145+
140146
uint256 fee = duration *
141147
names[parentNode].registrationFee *
142148
labels.length;
@@ -166,8 +172,54 @@ contract SubdomainRegistrar is ERC1155Holder {
166172
}
167173
}
168174

175+
function batchRenew(
176+
bytes32 parentNode,
177+
bytes32[] calldata labelhashes,
178+
uint64 duration
179+
) external payable {
180+
if (labelhashes.length == 0) {
181+
revert DataMissing();
182+
}
183+
184+
_checkParent(parentNode, duration);
185+
186+
uint256 fee = duration *
187+
names[parentNode].registrationFee *
188+
labelhashes.length;
189+
190+
if (fee > 0) {
191+
if (IERC20(names[parentNode].token).balanceOf(msg.sender) < fee) {
192+
revert InsufficientFunds();
193+
}
194+
195+
IERC20(names[parentNode].token).transferFrom(
196+
msg.sender,
197+
address(names[parentNode].beneficiary),
198+
fee
199+
);
200+
}
201+
202+
for (uint256 i = 0; i < labelhashes.length; i++) {
203+
_renew(parentNode, labelhashes[i], duration);
204+
}
205+
}
206+
169207
/* Internal Functions */
170208

209+
function _checkParent(bytes32 node, uint256 duration) internal {
210+
try wrapper.getFuses(node) returns (uint32, uint64 expiry) {
211+
if (expiry < block.timestamp) {
212+
revert ParentExpired(node);
213+
}
214+
215+
if (duration + block.timestamp > expiry) {
216+
revert ParentWillHaveExpired(node);
217+
}
218+
} catch {
219+
revert ParentNotWrapped(node);
220+
}
221+
}
222+
171223
function _register(
172224
bytes32 parentNode,
173225
string calldata label,
@@ -209,6 +261,24 @@ contract SubdomainRegistrar is ERC1155Holder {
209261
emit NameRegistered(node, uint64(block.timestamp + duration));
210262
}
211263

264+
function _renew(
265+
bytes32 parentNode,
266+
bytes32 labelhash,
267+
uint64 duration
268+
) internal {
269+
bytes32 node = _makeNode(parentNode, labelhash);
270+
(, uint64 expiry) = wrapper.getFuses(node);
271+
if (expiry < block.timestamp) {
272+
revert NameNotRegistered();
273+
}
274+
275+
uint64 newExpiry = expiry += duration;
276+
277+
wrapper.setChildFuses(parentNode, labelhash, 0, newExpiry);
278+
279+
emit NameRenewed(node, newExpiry);
280+
}
281+
212282
function _setRecords(
213283
bytes32 node,
214284
address resolver,

0 commit comments

Comments
 (0)