refactor: Refactored libraries for a cleaner directory, also pulled content URI getter to GeneralLib.

This commit is contained in:
Peter Michael
2022-07-05 14:18:14 -04:00
parent c45581cc08
commit 0ba6d06734
5 changed files with 105 additions and 53 deletions

View File

@@ -6,7 +6,6 @@ import {ILensNFTBase} from '../interfaces/ILensNFTBase.sol';
import {ILensHub} from '../interfaces/ILensHub.sol';
import {Events} from '../libraries/Events.sol';
import {Helpers} from '../libraries/Helpers.sol';
import {DataTypes} from '../libraries/DataTypes.sol';
import {Errors} from '../libraries/Errors.sol';
import {GeneralLib} from '../libraries/GeneralLib.sol';
@@ -533,8 +532,7 @@ contract LensHub is LensNFTBase, VersionedInitializable, LensMultiState, LensHub
override
returns (string memory)
{
(uint256 rootProfileId, uint256 rootPubId) = Helpers.getPointedIfMirror(profileId, pubId);
return _pubByIdByProfile[rootProfileId][rootPubId].contentURI;
return GeneralLib.getContentURI(profileId, pubId);
}
/// @inheritdoc ILensHub

View File

@@ -2,9 +2,10 @@
pragma solidity 0.8.15;
import {Helpers} from './Helpers.sol';
import {GeneralHelpers} from './helpers/GeneralHelpers.sol';
import {MetaTxHelpers} from './helpers/MetaTxHelpers.sol';
import {InteractionHelpers} from './helpers/InteractionHelpers.sol';
import {DataTypes} from './DataTypes.sol';
import {Helpers} from './Helpers.sol';
import {Errors} from './Errors.sol';
import {Events} from './Events.sol';
import {IFollowModule} from '../interfaces/IFollowModule.sol';
@@ -12,8 +13,6 @@ import {ICollectModule} from '../interfaces/ICollectModule.sol';
import {IReferenceModule} from '../interfaces/IReferenceModule.sol';
import './Constants.sol';
import {MetaTxHelpers} from './MetaTxHelpers.sol';
import {InteractionHelpers} from './InteractionHelpers.sol';
/**
* @title GeneralLib
@@ -546,8 +545,69 @@ library GeneralLib {
return MetaTxHelpers.getDomainSeparator();
}
function getContentURI(uint256 profileId, uint256 pubId) external view returns (string memory) {
(uint256 rootProfileId, uint256 rootPubId) = GeneralHelpers.getPointedIfMirror(
profileId,
pubId
);
string memory ptr;
assembly {
// Load the free memory pointer, where we'll return the value
ptr := mload(64)
// Load the slot, which either contains the content URI + 2*length if length < 32 or
// 2*length+1 if length >= 32, and the actual string starts at slot keccak256(slot)
mstore(0, rootProfileId)
mstore(32, PUB_BY_ID_BY_PROFILE_MAPPING_SLOT)
mstore(32, keccak256(0, 64))
mstore(0, rootPubId)
let slot := add(keccak256(0, 64), PUBLICATION_CONTENT_URI_OFFSET)
let slotLoad := sload(slot)
let size
// Determine if the length > 32 by checking the lowest order bit, meaning the string
// itself is stored at keccak256(slot)
switch and(slotLoad, 1)
case 0 {
// The content URI is in the same slot
// Determine the size by dividing the last byte's value by 2
size := shr(1, and(slotLoad, 255))
// Store the size in the first slot
mstore(ptr, size)
// Store the actual string in the second slot (without the size)
mstore(add(ptr, 32), and(slotLoad, not(255)))
}
case 1 {
// The content URI is not in the same slot
// Determine the size by dividing the value in the whole slot minus 1 by 2
size := shr(1, sub(slotLoad, 1))
// Store the size in the first slot
mstore(ptr, size)
// Compute the total memory slots we need, this is (size + 31) / 32
let totalMemorySlots := shr(5, add(size, 31))
mstore(0, slot)
let uriSlot := keccak256(0, 32)
// Iterate through the words in memory and store the string word by word
// prettier-ignore
for { let i := 0 } lt(i, totalMemorySlots) { i := add(i, 1) } {
mstore(add(add(ptr, 32), mul(32, i)), sload(add(uriSlot, i)))
}
}
// Store the new memory pointer in the free memory pointer slot
mstore(64, add(add(ptr, 32), size))
}
return ptr;
}
function _setDefaultProfile(address wallet, uint256 profileId) private {
if (profileId > 0 && wallet != Helpers.unsafeOwnerOf(profileId))
if (profileId > 0 && wallet != GeneralHelpers.unsafeOwnerOf(profileId))
revert Errors.NotProfileOwner();
// Store the default profile in the appropriate slot for the given wallet.
@@ -888,10 +948,8 @@ library GeneralLib {
* @param pubId The publication ID to associate with this publication.
*/
function _createMirror(DataTypes.MirrorData calldata vars, uint256 pubId) private {
(uint256 rootProfileIdPointed, uint256 rootPubIdPointed) = Helpers.getPointedIfMirror(
vars.profileIdPointed,
vars.pubIdPointed
);
(uint256 rootProfileIdPointed, uint256 rootPubIdPointed) = GeneralHelpers
.getPointedIfMirror(vars.profileIdPointed, vars.pubIdPointed);
_setPublicationPointer(vars.profileId, pubId, rootProfileIdPointed, rootPubIdPointed);
@@ -930,10 +988,8 @@ library GeneralLib {
function _createMirrorWithSigStruct(DataTypes.MirrorWithSigData calldata vars, uint256 pubId)
private
{
(uint256 rootProfileIdPointed, uint256 rootPubIdPointed) = Helpers.getPointedIfMirror(
vars.profileIdPointed,
vars.pubIdPointed
);
(uint256 rootProfileIdPointed, uint256 rootPubIdPointed) = GeneralHelpers
.getPointedIfMirror(vars.profileIdPointed, vars.pubIdPointed);
_setPublicationPointer(vars.profileId, pubId, rootProfileIdPointed, rootPubIdPointed);
@@ -1086,11 +1142,11 @@ library GeneralLib {
function _validateCallerIsProfileOwnerOrDispatcher(uint256 profileId) private view {
// It's safe to use the `unsafeOwnerOf()` function here because the sender cannot be
// the zero address.
if (msg.sender == Helpers.unsafeOwnerOf(profileId)) {
if (msg.sender == GeneralHelpers.unsafeOwnerOf(profileId)) {
return;
} else {
address dispatcher;
// Load the dispatcher for the given profile.
assembly {
mstore(0, profileId)

View File

@@ -2,10 +2,10 @@
pragma solidity 0.8.15;
import {DataTypes} from './DataTypes.sol';
import {Errors} from './Errors.sol';
import {DataTypes} from '../DataTypes.sol';
import {Errors} from '../Errors.sol';
import './Constants.sol';
import '../Constants.sol';
/**
* @title Helpers
@@ -13,7 +13,7 @@ import './Constants.sol';
*
* @notice This is a library that contains helper internal functions used by both the Hub and the GeneralLib.
*/
library Helpers {
library GeneralHelpers {
/**
* @notice This helper function just returns the pointed publication if the passed publication is a mirror,
* otherwise it returns the passed publication.

View File

@@ -2,19 +2,19 @@
pragma solidity 0.8.15;
import {FollowNFTProxy} from '../upgradeability/FollowNFTProxy.sol';
import {Helpers} from './Helpers.sol';
import {DataTypes} from './DataTypes.sol';
import {Errors} from './Errors.sol';
import {Events} from './Events.sol';
import {IFollowNFT} from '../interfaces/IFollowNFT.sol';
import {ICollectNFT} from '../interfaces/ICollectNFT.sol';
import {IFollowModule} from '../interfaces/IFollowModule.sol';
import {ICollectModule} from '../interfaces/ICollectModule.sol';
import {FollowNFTProxy} from '../../upgradeability/FollowNFTProxy.sol';
import {GeneralHelpers} from './GeneralHelpers.sol';
import {DataTypes} from '../DataTypes.sol';
import {Errors} from '../Errors.sol';
import {Events} from '../Events.sol';
import {IFollowNFT} from '../../interfaces/IFollowNFT.sol';
import {ICollectNFT} from '../../interfaces/ICollectNFT.sol';
import {IFollowModule} from '../../interfaces/IFollowModule.sol';
import {ICollectModule} from '../../interfaces/ICollectModule.sol';
import {Clones} from '@openzeppelin/contracts/proxy/Clones.sol';
import {Strings} from '@openzeppelin/contracts/utils/Strings.sol';
import './Constants.sol';
import '../Constants.sol';
/**
* @title InteractionHelpers
@@ -88,7 +88,7 @@ library InteractionHelpers {
bytes calldata collectModuleData,
address collectNFTImpl
) internal returns (uint256) {
(uint256 rootProfileId, uint256 rootPubId, address rootCollectModule) = Helpers
(uint256 rootProfileId, uint256 rootPubId, address rootCollectModule) = GeneralHelpers
.getPointedIfMirrorWithCollectModule(profileId, pubId);
uint256 collectNFTSlot;
@@ -227,7 +227,7 @@ library InteractionHelpers {
// Load the free memory pointer, where we'll return the value
ptr := mload(64)
// Load the slot, which either contains the name + 2*length if length < 32 or
// Load the slot, which either contains the handle + 2*length if length < 32 or
// 2*length+1 if length >= 32, and the actual string starts at slot keccak256(slot)
mstore(0, profileId)
mstore(32, PROFILE_BY_ID_MAPPING_SLOT)
@@ -239,7 +239,7 @@ library InteractionHelpers {
// itself is stored at keccak256(slot)
switch and(slotLoad, 1)
case 0 {
// The name is in the same slot
// The handle is in the same slot
// Determine the size by dividing the last byte's value by 2
size := shr(1, and(slotLoad, 255))
@@ -281,7 +281,7 @@ library InteractionHelpers {
// Load the free memory pointer, where we'll return the value
let ptr := mload(64)
// Compute the profile slot, which either contains the name + 2*length if length < 32
// Compute the profile slot, which either contains the handle + 2*length if length < 32
// or 2*length+1 if length >= 32, and the actual string starts at slot keccak256(slot)
mstore(0, profileId)
mstore(32, PROFILE_BY_ID_MAPPING_SLOT)
@@ -295,7 +295,7 @@ library InteractionHelpers {
// itself is stored at keccak256(slot)
switch and(slotLoad, 1)
case 0 {
// The name is in the same slot
// The handle is in the same slot
// Determine the size by dividing the last byte's value by 2
size := shr(1, and(slotLoad, 255))

View File

@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import {DataTypes} from './DataTypes.sol';
import {Errors} from './Errors.sol';
import {DataTypes} from './DataTypes.sol';
import {Helpers} from './Helpers.sol';
import {DataTypes} from '../DataTypes.sol';
import {Errors} from '../Errors.sol';
import {DataTypes} from '../DataTypes.sol';
import {GeneralHelpers} from './GeneralHelpers.sol';
import './Constants.sol';
import '../Constants.sol';
/**
* @title MetaTxHelpers
@@ -37,7 +37,7 @@ library MetaTxHelpers {
DataTypes.EIP712Signature calldata sig
) internal {
if (spender == address(0)) revert Errors.ZeroSpender();
address owner = Helpers.unsafeOwnerOf(tokenId);
address owner = GeneralHelpers.unsafeOwnerOf(tokenId);
_validateRecoveredAddress(
_calculateDigest(
keccak256(
@@ -99,7 +99,7 @@ library MetaTxHelpers {
function baseSetFollowModuleWithSig(DataTypes.SetFollowModuleWithSigData calldata vars)
internal
{
address owner = Helpers.unsafeOwnerOf(vars.profileId);
address owner = GeneralHelpers.unsafeOwnerOf(vars.profileId);
_validateRecoveredAddress(
_calculateDigest(
keccak256(
@@ -119,7 +119,7 @@ library MetaTxHelpers {
}
function baseSetDispatcherWithSig(DataTypes.SetDispatcherWithSigData calldata vars) internal {
address owner = Helpers.unsafeOwnerOf(vars.profileId);
address owner = GeneralHelpers.unsafeOwnerOf(vars.profileId);
_validateRecoveredAddress(
_calculateDigest(
keccak256(
@@ -140,7 +140,7 @@ library MetaTxHelpers {
function baseSetProfileImageURIWithSig(DataTypes.SetProfileImageURIWithSigData calldata vars)
internal
{
address owner = Helpers.unsafeOwnerOf(vars.profileId);
address owner = GeneralHelpers.unsafeOwnerOf(vars.profileId);
_validateRecoveredAddress(
_calculateDigest(
keccak256(
@@ -161,7 +161,7 @@ library MetaTxHelpers {
function baseSetFollowNFTURIWithSig(DataTypes.SetFollowNFTURIWithSigData calldata vars)
internal
{
address owner = Helpers.unsafeOwnerOf(vars.profileId);
address owner = GeneralHelpers.unsafeOwnerOf(vars.profileId);
_validateRecoveredAddress(
_calculateDigest(
keccak256(
@@ -180,7 +180,7 @@ library MetaTxHelpers {
}
function basePostWithSig(DataTypes.PostWithSigData calldata vars) internal {
address owner = Helpers.unsafeOwnerOf(vars.profileId);
address owner = GeneralHelpers.unsafeOwnerOf(vars.profileId);
unchecked {
_validateRecoveredAddress(
_calculateDigest(
@@ -205,8 +205,7 @@ library MetaTxHelpers {
}
function baseCommentWithSig(DataTypes.CommentWithSigData calldata vars) internal {
address owner = Helpers.unsafeOwnerOf(vars.profileId);
address owner = GeneralHelpers.unsafeOwnerOf(vars.profileId);
_validateRecoveredAddress(
_calculateDigest(
keccak256(
@@ -232,7 +231,7 @@ library MetaTxHelpers {
}
function baseMirrorWithSig(DataTypes.MirrorWithSigData calldata vars) internal {
address owner = Helpers.unsafeOwnerOf(vars.profileId);
address owner = GeneralHelpers.unsafeOwnerOf(vars.profileId);
_validateRecoveredAddress(
_calculateDigest(
keccak256(
@@ -255,7 +254,7 @@ library MetaTxHelpers {
}
function baseBurnWithSig(uint256 tokenId, DataTypes.EIP712Signature calldata sig) internal {
address owner = Helpers.unsafeOwnerOf(tokenId);
address owner = GeneralHelpers.unsafeOwnerOf(tokenId);
_validateRecoveredAddress(
_calculateDigest(
keccak256(
@@ -432,7 +431,6 @@ library MetaTxHelpers {
// Store the new memory pointer in the free memory pointer slot
mstore(64, add(add(ptr, 32), size))
}
// Return a memory pointer to the name (which always starts with the size at the first slot)
return ptr;
}
}