test: refactor internal token registry functions

This commit is contained in:
juancito.eth
2023-10-24 21:26:57 -03:00
parent 221e547ee7
commit 6e2085ba4f
2 changed files with 62 additions and 40 deletions

View File

@@ -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);
}
}

View File

@@ -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);