Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce LSP1Delegate deployment cost by 8,678 gas #485

Merged
merged 3 commits into from
Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,26 @@ contract LSP1UniversalReceiverDelegateUP is ERC165, ILSP1UniversalReceiver {
if (!ownerIsKeyManager) return "LSP1: account owner is not a LSP6KeyManager";

// if the contract being transferred doesn't support LSP9, do not register it as a received vault
if (mapPrefix == _LSP10_VAULTS_MAP_KEY_PREFIX) {
if (notifier.code.length > 0) {
if (!notifier.supportsERC165InterfaceUnchecked(_INTERFACEID_LSP9))
return "LSP1: not an LSP9Vault ownership transfer";
}
if (
mapPrefix == _LSP10_VAULTS_MAP_KEY_PREFIX &&
notifier.code.length > 0 &&
!notifier.supportsERC165InterfaceUnchecked(_INTERFACEID_LSP9)
) {
return "LSP1: not an LSP9Vault ownership transfer";
}

bytes32 notifierMapKey = LSP2Utils.generateMappingKey(mapPrefix, bytes20(notifier));
bytes memory notifierMapValue = IERC725Y(msg.sender).getData(notifierMapKey);
bool isMapValueSet = bytes12(notifierMapValue) != bytes12(0);

if (isReceiving) {
// if the map value is already set, then do nothing
if (bytes12(notifierMapValue) != bytes12(0))
return "LSP1: asset received is already registered";
if (isMapValueSet) return "LSP1: asset received is already registered";

result = _whenReceiving(typeId, notifier, keyManager, notifierMapKey, interfaceID);
return _whenReceiving(typeId, notifier, keyManager, notifierMapKey, interfaceID);
} else {
// if there is no map value for the asset/vault to remove, then do nothing
if (bytes12(notifierMapValue) == bytes12(0))
return "LSP1: asset sent is not registered";
if (!isMapValueSet) return "LSP1: asset sent is not registered";

result = _whenSending(typeId, notifier, keyManager, notifierMapKey, notifierMapValue);
return _whenSending(typeId, notifier, keyManager, notifierMapKey, notifierMapValue);
}
}

Expand All @@ -109,7 +107,7 @@ contract LSP1UniversalReceiverDelegateUP is ERC165, ILSP1UniversalReceiver {
address keyManager,
bytes32 notifierMapKey,
bytes4 interfaceID
) internal virtual returns (bytes memory result) {
) internal virtual returns (bytes memory) {
// if it's a token transfer (LSP7/LSP8)
if (typeId != _TYPEID_LSP9_OwnershipTransferred_RecipientNotification) {
// if the amount sent is 0, then do not update the keys
Expand All @@ -119,12 +117,12 @@ contract LSP1UniversalReceiverDelegateUP is ERC165, ILSP1UniversalReceiver {
(bytes32[] memory dataKeys, bytes[] memory dataValues) = LSP5Utils
.generateReceivedAssetKeys(msg.sender, notifier, notifierMapKey, interfaceID);

result = LSP6Utils.setDataViaKeyManager(keyManager, dataKeys, dataValues);
return LSP6Utils.setDataViaKeyManager(keyManager, dataKeys, dataValues);
} else {
(bytes32[] memory dataKeys, bytes[] memory dataValues) = LSP10Utils
.generateReceivedVaultKeys(msg.sender, notifier, notifierMapKey);

result = LSP6Utils.setDataViaKeyManager(keyManager, dataKeys, dataValues);
return LSP6Utils.setDataViaKeyManager(keyManager, dataKeys, dataValues);
}
}

Expand All @@ -139,7 +137,7 @@ contract LSP1UniversalReceiverDelegateUP is ERC165, ILSP1UniversalReceiver {
address keyManager,
bytes32 notifierMapKey,
bytes memory notifierMapValue
) internal virtual returns (bytes memory result) {
) internal virtual returns (bytes memory) {
// if it's a token transfer (LSP7/LSP8)
if (typeId != _TYPEID_LSP9_OwnershipTransferred_SenderNotification) {
// if the amount sent is not the full balance, then do not update the keys
Expand All @@ -149,12 +147,12 @@ contract LSP1UniversalReceiverDelegateUP is ERC165, ILSP1UniversalReceiver {
(bytes32[] memory dataKeys, bytes[] memory dataValues) = LSP5Utils
.generateSentAssetKeys(msg.sender, notifierMapKey, notifierMapValue);

result = LSP6Utils.setDataViaKeyManager(keyManager, dataKeys, dataValues);
return LSP6Utils.setDataViaKeyManager(keyManager, dataKeys, dataValues);
} else {
(bytes32[] memory dataKeys, bytes[] memory dataValues) = LSP10Utils
.generateSentVaultKeys(msg.sender, notifierMapKey, notifierMapValue);

result = LSP6Utils.setDataViaKeyManager(keyManager, dataKeys, dataValues);
return LSP6Utils.setDataViaKeyManager(keyManager, dataKeys, dataValues);
}
}

Expand All @@ -169,14 +167,16 @@ contract LSP1UniversalReceiverDelegateUP is ERC165, ILSP1UniversalReceiver {
returns (address accountOwner, bool ownerIsKeyManager)
{
accountOwner = ERC725Y(msg.sender).owner();
if (accountOwner.supportsERC165InterfaceUnchecked(_INTERFACEID_LSP6))
ownerIsKeyManager = true;

if (ownerIsKeyManager) {
if (accountOwner.supportsERC165InterfaceUnchecked(_INTERFACEID_LSP6)) {
address target = ILSP6KeyManager(accountOwner).target();
// check if the caller is the same account controlled by the keyManager
if (target != msg.sender) revert CallerNotLSP6LinkedTarget(msg.sender, target);

return (accountOwner, true);
}

return (accountOwner, false);
}

// --- Overrides
Expand Down