Skip to content

Commit

Permalink
wip (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfoobar committed Aug 31, 2023
1 parent 80ec70a commit 0e0ae90
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 131 deletions.
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ via_ir = false


[fmt]
line_length = 170
line_length = 180
wrap_comments = true # Increases readability of comments

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
30 changes: 15 additions & 15 deletions src/DelegateRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ contract DelegateRegistry is IDelegateRegistry {

/// @dev Transfer native token out
function sweep() external {
// TODO: Replace this with correct address
// TODO: Replace this with correct address at deployment time
// This hardcoded address is a CREATE2 factory counterfactual smart contract wallet that will always accept native token transfers
uint256 sc = uint256(uint160(0x0000000000000000000000000000000000000000));
assembly ("memory-safe") {
Expand All @@ -169,8 +169,8 @@ contract DelegateRegistry is IDelegateRegistry {
}
assembly ("memory-safe") {
// Only first 32 bytes of scratch space is accessed
mstore(0, iszero(iszero(valid))) // Compiler cleans dirty booleans on the stack to 1, so we're doing the same here
return(0, 32) // Direct return. Skips Solidity's redundant copying to save gas.
mstore(0, iszero(iszero(valid))) // Compiler cleans dirty booleans on the stack to 1, so do the same here
return(0, 32) // Direct return, skips Solidity's redundant copying to save gas
}
}

Expand All @@ -184,8 +184,8 @@ contract DelegateRegistry is IDelegateRegistry {
}
assembly ("memory-safe") {
// Only first 32 bytes of scratch space is accessed
mstore(0, iszero(iszero(valid))) // Compiler cleans dirty booleans on the stack to 1, so we're doing the same here
return(0, 32) // Direct return. Skips Solidity's redundant copying to save gas.
mstore(0, iszero(iszero(valid))) // Compiler cleans dirty booleans on the stack to 1, so do the same here
return(0, 32) // Direct return, skips Solidity's redundant copying to save gas
}
}

Expand All @@ -201,8 +201,8 @@ contract DelegateRegistry is IDelegateRegistry {
}
assembly ("memory-safe") {
// Only first 32 bytes of scratch space is accessed
mstore(0, iszero(iszero(valid))) // Compiler cleans dirty booleans on the stack to 1, so we're doing the same here
return(0, 32) // Direct return. Skips Solidity's redundant copying to save gas.
mstore(0, iszero(iszero(valid))) // Compiler cleans dirty booleans on the stack to 1, so do the same here
return(0, 32) // Direct return, skips Solidity's redundant copying to save gas
}
}

Expand All @@ -213,15 +213,15 @@ contract DelegateRegistry is IDelegateRegistry {
? type(uint256).max
: _loadDelegationUint(Hashes.erc20Location(from, "", to, contract_), Storage.POSITIONS_AMOUNT);
if (!Ops.or(rights == "", amount == type(uint256).max)) {
uint256 rightsBalance = (
_validateFrom(Hashes.allLocation(from, rights, to), from) || _validateFrom(Hashes.contractLocation(from, rights, to, contract_), from)
) ? type(uint256).max : _loadDelegationUint(Hashes.erc20Location(from, rights, to, contract_), Storage.POSITIONS_AMOUNT);
uint256 rightsBalance = (_validateFrom(Hashes.allLocation(from, rights, to), from) || _validateFrom(Hashes.contractLocation(from, rights, to, contract_), from))
? type(uint256).max
: _loadDelegationUint(Hashes.erc20Location(from, rights, to, contract_), Storage.POSITIONS_AMOUNT);
amount = Ops.max(rightsBalance, amount);
}
}
assembly ("memory-safe") {
mstore(0, amount) // Only first 32 bytes of scratch space being accessed
return(0, 32) // Direct return. Skips Solidity's redundant copying to save gas.
return(0, 32) // Direct return, skips Solidity's redundant copying to save gas
}
}

Expand All @@ -232,15 +232,15 @@ contract DelegateRegistry is IDelegateRegistry {
? type(uint256).max
: _loadDelegationUint(Hashes.erc1155Location(from, "", to, tokenId, contract_), Storage.POSITIONS_AMOUNT);
if (!Ops.or(rights == "", amount == type(uint256).max)) {
uint256 rightsBalance = (
_validateFrom(Hashes.allLocation(from, rights, to), from) || _validateFrom(Hashes.contractLocation(from, rights, to, contract_), from)
) ? type(uint256).max : _loadDelegationUint(Hashes.erc1155Location(from, rights, to, tokenId, contract_), Storage.POSITIONS_AMOUNT);
uint256 rightsBalance = (_validateFrom(Hashes.allLocation(from, rights, to), from) || _validateFrom(Hashes.contractLocation(from, rights, to, contract_), from))
? type(uint256).max
: _loadDelegationUint(Hashes.erc1155Location(from, rights, to, tokenId, contract_), Storage.POSITIONS_AMOUNT);
amount = Ops.max(rightsBalance, amount);
}
}
assembly ("memory-safe") {
mstore(0, amount) // Only first 32 bytes of scratch space is accessed
return(0, 32) // Direct return. Skips Solidity's redundant copying to save gas.
return(0, 32) // Direct return, skips Solidity's redundant copying to save gas
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/examples/IPLicenseCheck.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ contract IPLicenseCheck {
* @param ipLicense is a bytes32 representation of the IP license to be check for
* @return valid is returned true if the wallet has rights to the IP license for the NFT
*/
function checkForIPLicenseFromLicensor(address wallet, address licensor, address vault, address nftContract, uint256 tokenId, bytes32 ipLicense)
external
view
returns (bool)
{
function checkForIPLicenseFromLicensor(address wallet, address licensor, address vault, address nftContract, uint256 tokenId, bytes32 ipLicense) external view returns (bool) {
// Return false if the vault does not own the NFT
if (IERC721(nftContract).ownerOf(tokenId) != vault) return false;
// Call the v2 registry, and return false if vault has not granted "ip licensor" rights to the licensor for the nft
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/RegistryOps.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ library RegistryOps {
/// @dev `x & y`.
function and(bool x, bool y) internal pure returns (bool z) {
assembly {
z := and(iszero(iszero(x)), iszero(iszero(y))) // Compiler cleans dirty booleans on the stack to 1, so we're doing that here
z := and(iszero(iszero(x)), iszero(iszero(y))) // Compiler cleans dirty booleans on the stack to 1, so do the same here
}
}

/// @dev `x | y`.
function or(bool x, bool y) internal pure returns (bool z) {
assembly {
z := or(iszero(iszero(x)), iszero(iszero(y))) // Compiler cleans dirty booleans on the stack to 1, so we're doing that here
z := or(iszero(iszero(x)), iszero(iszero(y))) // Compiler cleans dirty booleans on the stack to 1, so do the same here
}
}
}
30 changes: 15 additions & 15 deletions src/libraries/RegistryStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ library RegistryStorage {

/**
* @notice Helper function that packs from, to, and contract_ address to into the two slot configuration
* @param from is the address making the delegation
* @param to is the address receiving the delegation
* @param contract_ is the contract address associated with the delegation (optional)
* @return firstPacked is the firstPacked storage configured with the parameters
* @return secondPacked is the secondPacked storage configured with the parameters
* @dev will not revert if from, to, and contract_ are > uint160, any inputs with dirty bits outside the last 20 bytes will be cleaned
* @param from The address making the delegation
* @param to The address receiving the delegation
* @param contract_ The contract address associated with the delegation (optional)
* @return firstPacked The firstPacked storage configured with the parameters
* @return secondPacked The secondPacked storage configured with the parameters
* @dev Will not revert if from, to, and contract_ are > uint160, any inputs with dirty bits outside the last 20 bytes will be cleaned
*/
function packAddresses(address from, address to, address contract_) internal pure returns (bytes32 firstPacked, bytes32 secondPacked) {
assembly {
Expand All @@ -40,12 +40,12 @@ library RegistryStorage {

/**
* @notice Helper function that unpacks from, to, and contract_ address inside the firstPacked secondPacked storage configuration
* @param firstPacked is the firstPacked storage to be decoded
* @param secondPacked is the secondPacked storage to be decoded
* @return from is the address making the delegation
* @return to is the address receiving the delegation
* @return contract_ is the contract address associated with the delegation
* @dev will not revert if from, to, and contract_ are > uint160, any inputs with dirty bits outside the last 20 bytes will be cleaned
* @param firstPacked The firstPacked storage to be decoded
* @param secondPacked The secondPacked storage to be decoded
* @return from The address making the delegation
* @return to The address receiving the delegation
* @return contract_ The contract address associated with the delegation
* @dev Will not revert if from, to, and contract_ are > uint160, any inputs with dirty bits outside the last 20 bytes will be cleaned
*/
function unpackAddresses(bytes32 firstPacked, bytes32 secondPacked) internal pure returns (address from, address to, address contract_) {
assembly {
Expand All @@ -57,9 +57,9 @@ library RegistryStorage {

/**
* @notice Helper function that can unpack the from or to address from their respective packed slots in the registry
* @param packedSlot is the slot containing the from or to address
* @return unpacked from or to address
* @dev will not work if you want to obtain the contract address, use unpackAddresses
* @param packedSlot The slot containing the from or to address
* @return unpacked The `from` or `to` address
* @dev Will not work if you want to obtain the contract address, use unpackAddresses
*/
function unpackAddress(bytes32 packedSlot) internal pure returns (address unpacked) {
assembly {
Expand Down
2 changes: 1 addition & 1 deletion src/singlesig/Singlesig.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

/// @dev This does not include receiver callbacks for "safe" transfer methods of ERC721, ERC1155, etc
/// @dev Does not include receiver callbacks for "safe" transfer methods of ERC721, ERC1155, etc
contract Singlesig {
address public owner;
address public pendingOwner;
Expand Down
3 changes: 1 addition & 2 deletions test/DelegateRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.21;

import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
import {DelegateRegistry} from "src/DelegateRegistry.sol";
import {IDelegateRegistry} from "src/IDelegateRegistry.sol";

Expand Down Expand Up @@ -107,7 +106,7 @@ contract DelegateRegistryTest is Test {
assertEq(info[0].to, delegate0);
assertEq(info[1].from, vault);
assertEq(info[1].to, delegate1);
// Remove
// Revoke
reg.delegateAll(delegate0, rights, false);
info = reg.getOutgoingDelegations(vault);
assertEq(info.length, 1);
Expand Down
18 changes: 3 additions & 15 deletions test/GasBenchmark.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,12 @@ contract GasBenchmark is Test {
multicallDelegations = _createDelegations(keccak256(abi.encode(seed, "multicall")));
bytes[] memory data = new bytes[](5);
data[0] = abi.encodeWithSelector(IRegistry.delegateAll.selector, multicallDelegations[0].to, multicallDelegations[0].rights, true);
data[1] = abi.encodeWithSelector(
IRegistry.delegateContract.selector, multicallDelegations[1].to, multicallDelegations[1].contract_, multicallDelegations[1].rights, true
);
data[1] = abi.encodeWithSelector(IRegistry.delegateContract.selector, multicallDelegations[1].to, multicallDelegations[1].contract_, multicallDelegations[1].rights, true);
data[2] = abi.encodeWithSelector(
IRegistry.delegateERC721.selector,
multicallDelegations[2].to,
multicallDelegations[2].contract_,
multicallDelegations[2].tokenId,
multicallDelegations[2].rights,
true
IRegistry.delegateERC721.selector, multicallDelegations[2].to, multicallDelegations[2].contract_, multicallDelegations[2].tokenId, multicallDelegations[2].rights, true
);
data[3] = abi.encodeWithSelector(
IRegistry.delegateERC20.selector,
multicallDelegations[3].to,
multicallDelegations[3].contract_,
multicallDelegations[3].amount,
multicallDelegations[3].rights,
true
IRegistry.delegateERC20.selector, multicallDelegations[3].to, multicallDelegations[3].contract_, multicallDelegations[3].amount, multicallDelegations[3].rights, true
);
data[4] = abi.encodeWithSelector(
IRegistry.delegateERC1155.selector,
Expand Down
9 changes: 2 additions & 7 deletions test/RegistryHashTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.21;

import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
import {IDelegateRegistry as IRegistry} from "src/IDelegateRegistry.sol";
import {RegistryHashes as Hashes} from "src/libraries/RegistryHashes.sol";

Expand Down Expand Up @@ -147,15 +146,11 @@ contract RegistryHashTests is Test {
assertEq(Hashes.contractHash(largeFrom, rights, largeTo, largeContract), Hashes.contractHash(cleanedFrom, rights, cleanedTo, cleanedContract));
assertEq(Hashes.contractLocation(largeFrom, rights, largeTo, largeContract), Hashes.contractLocation(cleanedFrom, rights, cleanedTo, cleanedContract));
assertEq(Hashes.erc721Hash(largeFrom, rights, largeTo, tokenId, largeContract), Hashes.erc721Hash(cleanedFrom, rights, cleanedTo, tokenId, cleanedContract));
assertEq(
Hashes.erc721Location(largeFrom, rights, largeTo, tokenId, largeContract), Hashes.erc721Location(cleanedFrom, rights, cleanedTo, tokenId, cleanedContract)
);
assertEq(Hashes.erc721Location(largeFrom, rights, largeTo, tokenId, largeContract), Hashes.erc721Location(cleanedFrom, rights, cleanedTo, tokenId, cleanedContract));
assertEq(Hashes.erc20Hash(largeFrom, rights, largeTo, largeContract), Hashes.erc20Hash(cleanedFrom, rights, cleanedTo, cleanedContract));
assertEq(Hashes.erc20Location(largeFrom, rights, largeTo, largeContract), Hashes.erc20Location(cleanedFrom, rights, cleanedTo, cleanedContract));
assertEq(Hashes.erc1155Hash(largeFrom, rights, largeTo, tokenId, largeContract), Hashes.erc1155Hash(cleanedFrom, rights, cleanedTo, tokenId, cleanedContract));
assertEq(
Hashes.erc1155Location(largeFrom, rights, largeTo, tokenId, largeContract), Hashes.erc1155Location(cleanedFrom, rights, cleanedTo, tokenId, cleanedContract)
);
assertEq(Hashes.erc1155Location(largeFrom, rights, largeTo, tokenId, largeContract), Hashes.erc1155Location(cleanedFrom, rights, cleanedTo, tokenId, cleanedContract));
}

/// @dev internal functions of the original registry hash specification to test optimized methods work as intended
Expand Down
2 changes: 0 additions & 2 deletions test/RegistryOpsTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
pragma solidity ^0.8.21;

import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
import {IDelegateRegistry as IRegistry} from "src/IDelegateRegistry.sol";
import {RegistryOps as Ops} from "src/libraries/RegistryOps.sol";

contract RegistryOpsTests is Test {
Expand Down
13 changes: 3 additions & 10 deletions test/RegistrySingularIntegrations.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,9 @@ contract DelegateSingularIntegrations is Test {
}

// Tests delegateAll case with non-default rights
function testDelegateAllSpecificRights(
address vault,
address fVault,
address delegate,
address fDelegate,
bytes32 rights,
bytes32 fRights,
address fContract,
uint256 fTokenId
) public {
function testDelegateAllSpecificRights(address vault, address fVault, address delegate, address fDelegate, bytes32 rights, bytes32 fRights, address fContract, uint256 fTokenId)
public
{
vm.assume(rights != "");
_setParameters(vault, fVault, delegate, fDelegate, address(0), fContract, 0, fTokenId, rights, fRights, 0, IRegistry.DelegationType.ALL);
_testDelegateAll();
Expand Down
2 changes: 0 additions & 2 deletions test/RegistryStorageTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
pragma solidity ^0.8.21;

import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
import {IDelegateRegistry as IRegistry} from "src/IDelegateRegistry.sol";
import {RegistryStorage as Storage} from "src/libraries/RegistryStorage.sol";
import {RegistryHarness as Harness} from "./tools/RegistryHarness.sol";

Expand Down
Loading

0 comments on commit 0e0ae90

Please sign in to comment.