misc: Fix namespace resolve functions, make them revert for non-existing tokens, remove unlinkIfBurnt functions as excessive

Co-authored-by: Alan <donosonaumczuk@gmail.com>
This commit is contained in:
vicnaum
2023-05-11 18:20:29 +02:00
parent 1aa227190a
commit 444d78cc53
3 changed files with 29 additions and 38 deletions

View File

@@ -70,10 +70,10 @@ interface ITokenHandleRegistry {
*
* @return tokenId ID of the Lens Protocol Profile NFT
*/
function resolveHandle(uint256 handleId) external view returns (uint256);
function resolve(uint256 handleId) external view returns (uint256);
/**
* @notice Resolves a profile NFT to a handle NFT.
* @notice Gets a default handle for a profile NFT (aka reverse resolution).
*
* @dev In the first version of the registry, the contracts are hard-coded:
* - Handle is hard-coded to be of the .lens namespace
@@ -85,5 +85,5 @@ interface ITokenHandleRegistry {
*
* @return handleId ID of the .lens namespace handle NFT
*/
function resolveToken(uint256 tokenId) external view returns (uint256);
function getDefaultHandle(uint256 tokenId) external view returns (uint256);
}

View File

@@ -90,53 +90,44 @@ contract TokenHandleRegistry is ITokenHandleRegistry {
_unlink(handle, tokenPointedByHandle);
}
function unlinkIfTokenBurnt(uint256 tokenId) external {
// In the first version of the registry linkage is one-to-one, so we can check only one side of it to save gas.
RegistryTypes.Token memory token = RegistryTypes.Token({collection: LENS_HUB, id: tokenId});
RegistryTypes.Handle memory handleThatTokenPointsTo = tokenToHandle[_tokenHash(token)];
if (ILensHub(LENS_HUB).exists(tokenId)) {
revert RegistryErrors.NotBurnt();
}
_unlink(handleThatTokenPointsTo, token);
}
function unlinkIfHandleBurnt(uint256 handleId) external {
// In the first version of the registry linkage is one-to-one, so we can check only one side of it to save gas.
RegistryTypes.Handle memory handle = RegistryTypes.Handle({collection: LENS_HANDLES, id: handleId});
RegistryTypes.Token memory tokenPointedByHandle = handleToToken[_handleHash(handle)];
if (ILensHub(LENS_HUB).exists(handleId)) {
revert RegistryErrors.NotBurnt();
}
_unlink(handle, tokenPointedByHandle);
}
/// @inheritdoc ITokenHandleRegistry
function resolveHandle(uint256 handleId) external view returns (uint256) {
uint256 resolvedHandleId = _resolveHandle(RegistryTypes.Handle({collection: LENS_HANDLES, id: handleId})).id;
if (resolvedHandleId != 0 && !ILensHandles(LENS_HANDLES).exists(resolvedHandleId)) {
return 0; // Handle was burned - unlinkIfBurnt should be called
function resolve(uint256 handleId) external view returns (uint256) {
if (!ILensHandles(LENS_HANDLES).exists(handleId)) {
revert RegistryErrors.DoesNotExist();
}
return resolvedHandleId;
}
/// @inheritdoc ITokenHandleRegistry
function resolveToken(uint256 tokenId) external view returns (uint256) {
uint256 resolvedTokenId = _resolveToken(RegistryTypes.Token({collection: LENS_HUB, id: tokenId})).id;
if (resolvedTokenId != 0 && !ILensHub(LENS_HUB).exists(resolvedTokenId)) {
return 0; // Token was burned - unlinkIfBurnt should be called
uint256 resolvedTokenId = _resolveHandleToToken(RegistryTypes.Handle({collection: LENS_HANDLES, id: handleId}))
.id;
if (resolvedTokenId == 0 || !ILensHub(LENS_HUB).exists(resolvedTokenId)) {
return 0;
}
return resolvedTokenId;
}
/// @inheritdoc ITokenHandleRegistry
function getDefaultHandle(uint256 tokenId) external view returns (uint256) {
if (!ILensHub(LENS_HUB).exists(tokenId)) {
revert RegistryErrors.DoesNotExist();
}
uint256 defaultHandleId = _resolveTokenToHandle(RegistryTypes.Token({collection: LENS_HUB, id: tokenId})).id;
if (defaultHandleId == 0 || !ILensHandles(LENS_HANDLES).exists(defaultHandleId)) {
return 0;
}
return defaultHandleId;
}
//////////////////////////////////////
/// INTERNAL FUNCTIONS ///
//////////////////////////////////////
function _resolveHandle(RegistryTypes.Handle memory handle) internal view returns (RegistryTypes.Token storage) {
function _resolveHandleToToken(
RegistryTypes.Handle memory handle
) internal view returns (RegistryTypes.Token storage) {
return handleToToken[_handleHash(handle)];
}
function _resolveToken(RegistryTypes.Token memory token) internal view returns (RegistryTypes.Handle storage) {
function _resolveTokenToHandle(
RegistryTypes.Token memory token
) internal view returns (RegistryTypes.Handle storage) {
return tokenToHandle[_tokenHash(token)];
}

View File

@@ -7,8 +7,8 @@ library RegistryErrors {
error NotTokenOwner();
error NotHandleNorTokenOwner();
error OnlyLensHub();
error NotBurnt();
error NotLinked();
error DoesNotExist();
}
library HandlesErrors {