From 6e2085ba4fb25a8cb6feb559996d7917a2cfe181 Mon Sep 17 00:00:00 2001 From: "juancito.eth" Date: Tue, 24 Oct 2023 21:26:57 -0300 Subject: [PATCH] test: refactor internal token registry functions --- test/base/BaseTest.t.sol | 54 +++++++++++++++++++ test/namespaces/TokenHandleRegistryTest.t.sol | 48 +++-------------- 2 files changed, 62 insertions(+), 40 deletions(-) diff --git a/test/base/BaseTest.t.sol b/test/base/BaseTest.t.sol index 7dd34a8..56ff03b 100644 --- a/test/base/BaseTest.t.sol +++ b/test/base/BaseTest.t.sol @@ -340,6 +340,46 @@ contract BaseTest is TestSetup { return _calculateDigest(structHash); } + function _getLinkSigStruct( + uint256 signerPk, + uint256 handleId, + uint256 profileId + ) internal view returns (Types.EIP712Signature memory) { + address signer = vm.addr(signerPk); + return _getSigStruct({ + signer: signer, + pKey: signerPk, + digest: _getLinkTypedDataHash({ + handleId: handleId, + profileId: profileId, + signer: signer, + nonce: hub.nonces(signer), + deadline: type(uint256).max + }), + deadline: type(uint256).max + }); + } + + function _getUnlinkSigStruct( + uint256 signerPk, + uint256 handleId, + uint256 profileId + ) internal view returns (Types.EIP712Signature memory) { + address signer = vm.addr(signerPk); + return _getSigStruct({ + signer: signer, + pKey: signerPk, + digest: _getUnlinkTypedDataHash({ + handleId: handleId, + profileId: profileId, + signer: signer, + nonce: hub.nonces(signer), + deadline: type(uint256).max + }), + deadline: type(uint256).max + }); + } + function _calculateDigest(bytes32 structHash) internal view returns (bytes32) { return keccak256(abi.encodePacked('\x19\x01', domainSeparator, structHash)); } @@ -485,4 +525,18 @@ contract BaseTest is TestSetup { function _encodeUsingEip712Rules(bytes memory bytesValue) internal pure returns (bytes32) { return keccak256(bytesValue); } + + function _transferHandle(address to, uint256 handleId) internal { + address initialHandleHolder = lensHandles.ownerOf(handleId); + _effectivelyDisableGuardian(address(lensHandles), initialHandleHolder); + vm.prank(initialHandleHolder); + lensHandles.transferFrom(initialHandleHolder, to, handleId); + } + + function _transferProfile(address to, uint256 profileId) internal { + address initialProfileHolder = hub.ownerOf(profileId); + _effectivelyDisableProfileGuardian(initialProfileHolder); + vm.prank(initialProfileHolder); + hub.transferFrom(initialProfileHolder, to, profileId); + } } diff --git a/test/namespaces/TokenHandleRegistryTest.t.sol b/test/namespaces/TokenHandleRegistryTest.t.sol index df06e5b..4fe47ff 100644 --- a/test/namespaces/TokenHandleRegistryTest.t.sol +++ b/test/namespaces/TokenHandleRegistryTest.t.sol @@ -278,33 +278,17 @@ contract TokenHandleRegistryTest is BaseTest { uint256 holderPk = 0x401DE8; address holder = vm.addr(holderPk); - _effectivelyDisableGuardian(address(lensHandles), initialHandleHolder); - vm.prank(initialHandleHolder); - lensHandles.transferFrom(initialHandleHolder, holder, handleId); - - _effectivelyDisableProfileGuardian(initialProfileHolder); - vm.prank(initialProfileHolder); - hub.transferFrom(initialProfileHolder, holder, profileId); + _transferHandle(holder, handleId); + _transferProfile(holder, profileId); RegistryTypes.Handle memory handle = RegistryTypes.Handle({collection: address(lensHandles), id: handleId}); RegistryTypes.Token memory token = RegistryTypes.Token({collection: address(hub), id: profileId}); + Types.EIP712Signature memory sig = _getLinkSigStruct(holderPk, handleId, profileId); + vm.expectEmit(true, true, true, true, address(tokenHandleRegistry)); emit RegistryEvents.HandleLinked(handle, token, holder, block.timestamp); - Types.EIP712Signature memory sig = _getSigStruct({ - signer: holder, - pKey: holderPk, - digest: _getLinkTypedDataHash({ - handleId: handleId, - profileId: profileId, - signer: holder, - nonce: hub.nonces(holder), - deadline: type(uint256).max - }), - deadline: type(uint256).max - }); - vm.prank(holder); tokenHandleRegistry.linkWithSig(handleId, profileId, sig); @@ -520,13 +504,8 @@ contract TokenHandleRegistryTest is BaseTest { uint256 holderPk = 0x401DE8; address holder = vm.addr(holderPk); - _effectivelyDisableGuardian(address(lensHandles), initialHandleHolder); - vm.prank(initialHandleHolder); - lensHandles.transferFrom(initialHandleHolder, holder, handleId); - - _effectivelyDisableProfileGuardian(initialProfileHolder); - vm.prank(initialProfileHolder); - hub.transferFrom(initialProfileHolder, holder, profileId); + _transferHandle(holder, handleId); + _transferProfile(holder, profileId); vm.prank(holder); tokenHandleRegistry.link(handleId, profileId); @@ -537,22 +516,11 @@ contract TokenHandleRegistryTest is BaseTest { RegistryTypes.Handle memory handle = RegistryTypes.Handle({collection: address(lensHandles), id: handleId}); RegistryTypes.Token memory token = RegistryTypes.Token({collection: address(hub), id: profileId}); + Types.EIP712Signature memory sig = _getUnlinkSigStruct(holderPk, handleId, profileId); + vm.expectEmit(true, true, true, true, address(tokenHandleRegistry)); emit RegistryEvents.HandleUnlinked(handle, token, holder, block.timestamp); - Types.EIP712Signature memory sig = _getSigStruct({ - signer: holder, - pKey: holderPk, - digest: _getUnlinkTypedDataHash({ - handleId: handleId, - profileId: profileId, - signer: holder, - nonce: hub.nonces(holder), - deadline: type(uint256).max - }), - deadline: type(uint256).max - }); - vm.prank(holder); tokenHandleRegistry.unlinkWithSig(handleId, profileId, sig);