Skip to content

Commit 5bc21f5

Browse files
authored
✨ Automatic pullOwner (#146)
1 parent 008743c commit 5bc21f5

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/DN404.sol

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ abstract contract DN404 {
249249
mstore(0x00, 0xd125259c) // `LinkMirrorContractFailed()`.
250250
revert(0x1c, 0x04)
251251
}
252+
// Query `owner()` on this contract, and if it is non-zero, call `pullOwner()` on the mirror.
253+
// This allows for any Ownable (e.g. OpenZeppelin, Solady).
254+
mstore(0x00, 0x8da5cb5b6cef16e6) // `owner()` and `pullOwner()`.
255+
if and(
256+
lt(iszero(shl(96, mload(0x20))), gt(returndatasize(), 0x1f)),
257+
staticcall(gas(), address(), 0x18, 0x04, 0x20, 0x20)
258+
) { if iszero(call(gas(), mirror, 0, 0x1c, 0x04, 0x00, 0x00)) { revert(0x00, 0x00) } }
252259
}
253260

254261
$.nextTokenId = uint32(_toUint(_useOneIndexed()));

test/DN404Mirror.t.sol

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,19 +293,36 @@ contract DN404MirrorTest is SoladyTest {
293293
assertEq(mirror.owner(), address(0));
294294
}
295295

296-
function testPullOwnerWithOwnable() public {
297-
MockDN404Ownable dnOwnable = new MockDN404Ownable();
296+
function testAutomaticPullOwnerWithOwnable() public {
297+
MockDN404Ownable dnOwnable = new MockDN404Ownable(address(0));
298298
dnOwnable.initializeDN404(1000, address(this), address(mirror));
299+
assertEq(mirror.owner(), address(0));
300+
mirror.pullOwner();
301+
assertEq(mirror.owner(), address(0));
302+
303+
dnOwnable.initializeOwner(address(this));
304+
assertEq(mirror.owner(), address(0));
305+
mirror.pullOwner();
306+
assertEq(mirror.owner(), address(this));
307+
299308
address newOwner = address(123);
300309
dnOwnable.transferOwnership(newOwner);
301310

302-
assertEq(mirror.owner(), address(0));
303311
vm.expectEmit(true, true, true, true);
304-
emit OwnershipTransferred(address(0), newOwner);
312+
emit OwnershipTransferred(address(this), newOwner);
305313
mirror.pullOwner();
306314
assertEq(mirror.owner(), newOwner);
307315
}
308316

317+
function testAutomaticPullOwnerWithOwnable2() public {
318+
MockDN404Ownable dnOwnable = new MockDN404Ownable(address(this));
319+
assertEq(mirror.owner(), address(0));
320+
vm.expectEmit(true, true, true, true);
321+
emit OwnershipTransferred(address(0), address(this));
322+
dnOwnable.initializeDN404(1000, address(this), address(mirror));
323+
assertEq(mirror.owner(), address(this));
324+
}
325+
309326
function testFnSelectorNotRecognized() public {
310327
(bool success, bytes memory result) =
311328
address(dn).call(abi.encodeWithSignature("nonSupportedFunction123()"));

test/utils/mocks/MockDN404Ownable.sol

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import "./MockDN404.sol";
55
import {Ownable} from "solady/auth/Ownable.sol";
66

77
contract MockDN404Ownable is MockDN404, Ownable {
8-
constructor() {
9-
_initializeOwner(msg.sender);
8+
constructor(address initialOwner) {
9+
if (initialOwner != address(0)) {
10+
_initializeOwner(initialOwner);
11+
}
12+
}
13+
14+
function initializeOwner(address initialOwner) public {
15+
_initializeOwner(initialOwner);
1016
}
1117
}

0 commit comments

Comments
 (0)