From e6c66e320dc0174c8b30ed289ff42c85fa02f29b Mon Sep 17 00:00:00 2001 From: Krishang Date: Tue, 28 Feb 2023 12:16:54 -0800 Subject: [PATCH 1/7] Make all top level functions virtual on ContractKit bases --- contracts/base/ERC1155Base.sol | 5 +- contracts/base/ERC1155DelayedReveal.sol | 2 +- contracts/base/ERC1155Drop.sol | 5 +- contracts/base/ERC1155LazyMint.sol | 7 +- contracts/base/ERC721Base.sol | 14 +- contracts/base/ERC721DelayedReveal.sol | 4 +- contracts/base/ERC721Drop.sol | 16 +- contracts/base/ERC721LazyMint.sol | 16 +- contracts/base/ERC721Multiwrap.sol | 16 +- contracts/base/Staking1155Base.sol | 8 +- contracts/base/Staking20Base.sol | 4 +- contracts/base/Staking721Base.sol | 4 +- docs/ERC2771ContextStorage.md | 2 +- docs/ERC2771ContextUpgradeableStorage.md | 2 +- docs/FeeType.md | 2 +- docs/IBurnableERC20.md | 2 +- docs/IBurnableERC721.md | 2 +- docs/IClaimableERC1155.md | 2 +- docs/IClaimableERC721.md | 2 +- docs/IContext.md | 2 +- docs/IContractFactory.md | 2 +- docs/IMintableERC20.md | 2 +- docs/IMintableERC721.md | 2 +- docs/IOperatorFilterRegistry.md | 2 +- docs/IOperatorFilterToggle.md | 2 +- docs/IPluginMap.md | 2 +- docs/IRouter.md | 166 +-------------- docs/IStaking1155.md | 2 +- docs/IStaking20.md | 2 +- docs/IStaking721.md | 2 +- docs/ReentrancyGuardStorage.md | 2 +- docs/Router.md | 251 +---------------------- docs/Staking1155Base.md | 2 +- docs/TWStringSet.md | 2 +- docs/TokenERC1155.md | 103 +++++++++- 35 files changed, 193 insertions(+), 468 deletions(-) diff --git a/contracts/base/ERC1155Base.sol b/contracts/base/ERC1155Base.sol index dbaf27f86..79276e5b7 100644 --- a/contracts/base/ERC1155Base.sol +++ b/contracts/base/ERC1155Base.sol @@ -246,6 +246,7 @@ contract ERC1155Base is /// @dev See {ERC1155-setApprovalForAll} function setApprovalForAll(address operator, bool approved) public + virtual override(ERC1155) onlyAllowedOperatorApproval(operator) { @@ -261,7 +262,7 @@ contract ERC1155Base is uint256 id, uint256 amount, bytes memory data - ) public override(ERC1155) onlyAllowedOperator(from) { + ) public virtual override(ERC1155) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, id, amount, data); } @@ -274,7 +275,7 @@ contract ERC1155Base is uint256[] memory ids, uint256[] memory amounts, bytes memory data - ) public override(ERC1155) onlyAllowedOperator(from) { + ) public virtual override(ERC1155) onlyAllowedOperator(from) { super.safeBatchTransferFrom(from, to, ids, amounts, data); } diff --git a/contracts/base/ERC1155DelayedReveal.sol b/contracts/base/ERC1155DelayedReveal.sol index 7c4f87a5b..40f165cc6 100644 --- a/contracts/base/ERC1155DelayedReveal.sol +++ b/contracts/base/ERC1155DelayedReveal.sol @@ -48,7 +48,7 @@ contract ERC1155DelayedReveal is ERC1155LazyMint, DelayedReveal { * * @param _tokenId The tokenId of an NFT. */ - function uri(uint256 _tokenId) public view override returns (string memory) { + function uri(uint256 _tokenId) public view virtual override returns (string memory) { (uint256 batchId, ) = _getBatchId(_tokenId); string memory batchUri = _getBaseURI(_tokenId); diff --git a/contracts/base/ERC1155Drop.sol b/contracts/base/ERC1155Drop.sol index 52e5f9671..b17606f16 100644 --- a/contracts/base/ERC1155Drop.sol +++ b/contracts/base/ERC1155Drop.sol @@ -228,6 +228,7 @@ contract ERC1155Drop is /// @dev See {ERC1155-setApprovalForAll} function setApprovalForAll(address operator, bool approved) public + virtual override(ERC1155) onlyAllowedOperatorApproval(operator) { @@ -243,7 +244,7 @@ contract ERC1155Drop is uint256 id, uint256 amount, bytes memory data - ) public override(ERC1155) onlyAllowedOperator(from) { + ) public virtual override(ERC1155) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, id, amount, data); } @@ -256,7 +257,7 @@ contract ERC1155Drop is uint256[] memory ids, uint256[] memory amounts, bytes memory data - ) public override(ERC1155) onlyAllowedOperator(from) { + ) public virtual override(ERC1155) onlyAllowedOperator(from) { super.safeBatchTransferFrom(from, to, ids, amounts, data); } diff --git a/contracts/base/ERC1155LazyMint.sol b/contracts/base/ERC1155LazyMint.sol index 87faaba11..7cd741e9e 100644 --- a/contracts/base/ERC1155LazyMint.sol +++ b/contracts/base/ERC1155LazyMint.sol @@ -122,7 +122,7 @@ contract ERC1155LazyMint is address _receiver, uint256 _tokenId, uint256 _quantity - ) public payable nonReentrant { + ) public payable virtual nonReentrant { require(_tokenId < nextTokenIdToMint(), "invalid id"); verifyClaim(msg.sender, _tokenId, _quantity); // Add your claim verification logic by overriding this function. @@ -219,6 +219,7 @@ contract ERC1155LazyMint is /// @dev See {ERC1155-setApprovalForAll} function setApprovalForAll(address operator, bool approved) public + virtual override(ERC1155) onlyAllowedOperatorApproval(operator) { @@ -234,7 +235,7 @@ contract ERC1155LazyMint is uint256 id, uint256 amount, bytes memory data - ) public override(ERC1155) onlyAllowedOperator(from) { + ) public virtual override(ERC1155) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, id, amount, data); } @@ -247,7 +248,7 @@ contract ERC1155LazyMint is uint256[] memory ids, uint256[] memory amounts, bytes memory data - ) public override(ERC1155) onlyAllowedOperator(from) { + ) public virtual override(ERC1155) onlyAllowedOperator(from) { super.safeBatchTransferFrom(from, to, ids, amounts, data); } diff --git a/contracts/base/ERC721Base.sol b/contracts/base/ERC721Base.sol index 8bb31d0f3..525df3838 100644 --- a/contracts/base/ERC721Base.sol +++ b/contracts/base/ERC721Base.sol @@ -172,6 +172,7 @@ contract ERC721Base is /// @dev See {ERC721-setApprovalForAll}. function setApprovalForAll(address operator, bool approved) public + virtual override(ERC721A) onlyAllowedOperatorApproval(operator) { @@ -179,7 +180,12 @@ contract ERC721Base is } /// @dev See {ERC721-approve}. - function approve(address operator, uint256 tokenId) public override(ERC721A) onlyAllowedOperatorApproval(operator) { + function approve(address operator, uint256 tokenId) + public + virtual + override(ERC721A) + onlyAllowedOperatorApproval(operator) + { super.approve(operator, tokenId); } @@ -188,7 +194,7 @@ contract ERC721Base is address from, address to, uint256 tokenId - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.transferFrom(from, to, tokenId); } @@ -197,7 +203,7 @@ contract ERC721Base is address from, address to, uint256 tokenId - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId); } @@ -207,7 +213,7 @@ contract ERC721Base is address to, uint256 tokenId, bytes memory data - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId, data); } diff --git a/contracts/base/ERC721DelayedReveal.sol b/contracts/base/ERC721DelayedReveal.sol index bb7167dd9..0b0ca6e4e 100644 --- a/contracts/base/ERC721DelayedReveal.sol +++ b/contracts/base/ERC721DelayedReveal.sol @@ -49,7 +49,7 @@ contract ERC721DelayedReveal is ERC721LazyMint, DelayedReveal { * * @param _tokenId The tokenId of an NFT. */ - function tokenURI(uint256 _tokenId) public view override returns (string memory) { + function tokenURI(uint256 _tokenId) public view virtual override returns (string memory) { (uint256 batchId, ) = _getBatchId(_tokenId); string memory batchUri = _getBaseURI(_tokenId); @@ -77,7 +77,7 @@ contract ERC721DelayedReveal is ERC721LazyMint, DelayedReveal { uint256 _amount, string calldata _baseURIForTokens, bytes calldata _data - ) public override returns (uint256 batchId) { + ) public virtual override returns (uint256 batchId) { if (_data.length > 0) { (bytes memory encryptedURI, bytes32 provenanceHash) = abi.decode(_data, (bytes, bytes32)); if (encryptedURI.length != 0 && provenanceHash != "") { diff --git a/contracts/base/ERC721Drop.sol b/contracts/base/ERC721Drop.sol index 314e8a424..49423ce6a 100644 --- a/contracts/base/ERC721Drop.sol +++ b/contracts/base/ERC721Drop.sol @@ -125,7 +125,7 @@ contract ERC721Drop is uint256 _amount, string calldata _baseURIForTokens, bytes calldata _data - ) public override returns (uint256 batchId) { + ) public virtual override returns (uint256 batchId) { if (_data.length > 0) { (bytes memory encryptedURI, bytes32 provenanceHash) = abi.decode(_data, (bytes, bytes32)); if (encryptedURI.length != 0 && provenanceHash != "") { @@ -189,6 +189,7 @@ contract ERC721Drop is /// @dev See {ERC721-setApprovalForAll}. function setApprovalForAll(address operator, bool approved) public + virtual override(ERC721A) onlyAllowedOperatorApproval(operator) { @@ -196,7 +197,12 @@ contract ERC721Drop is } /// @dev See {ERC721-approve}. - function approve(address operator, uint256 tokenId) public override(ERC721A) onlyAllowedOperatorApproval(operator) { + function approve(address operator, uint256 tokenId) + public + virtual + override(ERC721A) + onlyAllowedOperatorApproval(operator) + { super.approve(operator, tokenId); } @@ -205,7 +211,7 @@ contract ERC721Drop is address from, address to, uint256 tokenId - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.transferFrom(from, to, tokenId); } @@ -214,7 +220,7 @@ contract ERC721Drop is address from, address to, uint256 tokenId - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId); } @@ -224,7 +230,7 @@ contract ERC721Drop is address to, uint256 tokenId, bytes memory data - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId, data); } diff --git a/contracts/base/ERC721LazyMint.sol b/contracts/base/ERC721LazyMint.sol index ade72db8a..29fa59742 100644 --- a/contracts/base/ERC721LazyMint.sol +++ b/contracts/base/ERC721LazyMint.sol @@ -119,7 +119,7 @@ contract ERC721LazyMint is * @param _receiver The recipient of the NFT to mint. * @param _quantity The number of NFTs to mint. */ - function claim(address _receiver, uint256 _quantity) public payable nonReentrant { + function claim(address _receiver, uint256 _quantity) public payable virtual nonReentrant { require(_currentIndex + _quantity <= nextTokenIdToLazyMint, "Not enough lazy minted tokens."); verifyClaim(msg.sender, _quantity); // Add your claim verification logic by overriding this function. @@ -165,6 +165,7 @@ contract ERC721LazyMint is /// @dev See {ERC721-setApprovalForAll}. function setApprovalForAll(address operator, bool approved) public + virtual override(ERC721A) onlyAllowedOperatorApproval(operator) { @@ -172,7 +173,12 @@ contract ERC721LazyMint is } /// @dev See {ERC721-approve}. - function approve(address operator, uint256 tokenId) public override(ERC721A) onlyAllowedOperatorApproval(operator) { + function approve(address operator, uint256 tokenId) + public + virtual + override(ERC721A) + onlyAllowedOperatorApproval(operator) + { super.approve(operator, tokenId); } @@ -181,7 +187,7 @@ contract ERC721LazyMint is address from, address to, uint256 tokenId - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.transferFrom(from, to, tokenId); } @@ -190,7 +196,7 @@ contract ERC721LazyMint is address from, address to, uint256 tokenId - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId); } @@ -200,7 +206,7 @@ contract ERC721LazyMint is address to, uint256 tokenId, bytes memory data - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId, data); } diff --git a/contracts/base/ERC721Multiwrap.sol b/contracts/base/ERC721Multiwrap.sol index 55438d38d..058c6d1ee 100644 --- a/contracts/base/ERC721Multiwrap.sol +++ b/contracts/base/ERC721Multiwrap.sol @@ -128,7 +128,7 @@ contract ERC721Multiwrap is //////////////////////////////////////////////////////////////*/ /// @dev Returns the URI for a given tokenId. - function tokenURI(uint256 _tokenId) public view override returns (string memory) { + function tokenURI(uint256 _tokenId) public view virtual override returns (string memory) { return getUriOfBundle(_tokenId); } @@ -210,6 +210,7 @@ contract ERC721Multiwrap is /// @dev See {ERC721-setApprovalForAll}. function setApprovalForAll(address operator, bool approved) public + virtual override(ERC721A) onlyAllowedOperatorApproval(operator) { @@ -217,7 +218,12 @@ contract ERC721Multiwrap is } /// @dev See {ERC721-approve}. - function approve(address operator, uint256 tokenId) public override(ERC721A) onlyAllowedOperatorApproval(operator) { + function approve(address operator, uint256 tokenId) + public + virtual + override(ERC721A) + onlyAllowedOperatorApproval(operator) + { super.approve(operator, tokenId); } @@ -226,7 +232,7 @@ contract ERC721Multiwrap is address from, address to, uint256 tokenId - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.transferFrom(from, to, tokenId); } @@ -235,7 +241,7 @@ contract ERC721Multiwrap is address from, address to, uint256 tokenId - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId); } @@ -245,7 +251,7 @@ contract ERC721Multiwrap is address to, uint256 tokenId, bytes memory data - ) public override(ERC721A) onlyAllowedOperator(from) { + ) public virtual override(ERC721A) onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId, data); } diff --git a/contracts/base/Staking1155Base.sol b/contracts/base/Staking1155Base.sol index e5481ecfc..5efa1deb7 100644 --- a/contracts/base/Staking1155Base.sol +++ b/contracts/base/Staking1155Base.sol @@ -74,12 +74,12 @@ contract Staking1155Base is ContractMetadata, Multicall, Ownable, Staking1155, E } /// @dev Admin deposits reward tokens. - function depositRewardTokens(uint256 _amount) external payable nonReentrant { + function depositRewardTokens(uint256 _amount) external payable virtual nonReentrant { _depositRewardTokens(_amount); // override this for custom logic. } /// @dev Admin can withdraw excess reward tokens. - function withdrawRewardTokens(uint256 _amount) external nonReentrant { + function withdrawRewardTokens(uint256 _amount) external virtual nonReentrant { _withdrawRewardTokens(_amount); // override this for custom logic. } @@ -98,7 +98,7 @@ contract Staking1155Base is ContractMetadata, Multicall, Ownable, Staking1155, E uint256, uint256, bytes calldata - ) external returns (bytes4) { + ) external view returns (bytes4) { require(isStaking == 2, "Direct transfer"); return this.onERC1155Received.selector; } @@ -109,7 +109,7 @@ contract Staking1155Base is ContractMetadata, Multicall, Ownable, Staking1155, E uint256[] calldata ids, uint256[] calldata values, bytes calldata data - ) external returns (bytes4) {} + ) external virtual returns (bytes4) {} function supportsInterface(bytes4 interfaceId) public view override(ERC165, IERC165) returns (bool) { return interfaceId == type(IERC1155Receiver).interfaceId || super.supportsInterface(interfaceId); diff --git a/contracts/base/Staking20Base.sol b/contracts/base/Staking20Base.sol index cb45a3e86..0b5063dfa 100644 --- a/contracts/base/Staking20Base.sol +++ b/contracts/base/Staking20Base.sol @@ -78,12 +78,12 @@ contract Staking20Base is ContractMetadata, Multicall, Ownable, Staking20 { } /// @dev Admin deposits reward tokens. - function depositRewardTokens(uint256 _amount) external payable nonReentrant { + function depositRewardTokens(uint256 _amount) external payable virtual nonReentrant { _depositRewardTokens(_amount); // override this for custom logic. } /// @dev Admin can withdraw excess reward tokens. - function withdrawRewardTokens(uint256 _amount) external nonReentrant { + function withdrawRewardTokens(uint256 _amount) external virtual nonReentrant { _withdrawRewardTokens(_amount); // override this for custom logic. } diff --git a/contracts/base/Staking721Base.sol b/contracts/base/Staking721Base.sol index b6cb0e1e0..24a3a6ed8 100644 --- a/contracts/base/Staking721Base.sol +++ b/contracts/base/Staking721Base.sol @@ -74,12 +74,12 @@ contract Staking721Base is ContractMetadata, Multicall, Ownable, Staking721, ERC } /// @dev Admin deposits reward tokens. - function depositRewardTokens(uint256 _amount) external payable nonReentrant { + function depositRewardTokens(uint256 _amount) external payable virtual nonReentrant { _depositRewardTokens(_amount); // override this for custom logic. } /// @dev Admin can withdraw excess reward tokens. - function withdrawRewardTokens(uint256 _amount) external nonReentrant { + function withdrawRewardTokens(uint256 _amount) external virtual nonReentrant { _withdrawRewardTokens(_amount); // override this for custom logic. } diff --git a/docs/ERC2771ContextStorage.md b/docs/ERC2771ContextStorage.md index 536f732cb..5f9991358 100644 --- a/docs/ERC2771ContextStorage.md +++ b/docs/ERC2771ContextStorage.md @@ -1,6 +1,6 @@ # ERC2771ContextStorage - +*thirdweb* diff --git a/docs/ERC2771ContextUpgradeableStorage.md b/docs/ERC2771ContextUpgradeableStorage.md index cae4aad6a..7eb706458 100644 --- a/docs/ERC2771ContextUpgradeableStorage.md +++ b/docs/ERC2771ContextUpgradeableStorage.md @@ -1,6 +1,6 @@ # ERC2771ContextUpgradeableStorage - +*thirdweb* diff --git a/docs/FeeType.md b/docs/FeeType.md index 6950fdc30..ee1cb1ef3 100644 --- a/docs/FeeType.md +++ b/docs/FeeType.md @@ -1,6 +1,6 @@ # FeeType - +*thirdweb* diff --git a/docs/IBurnableERC20.md b/docs/IBurnableERC20.md index b5af8b61d..762c19474 100644 --- a/docs/IBurnableERC20.md +++ b/docs/IBurnableERC20.md @@ -1,6 +1,6 @@ # IBurnableERC20 - +*thirdweb* diff --git a/docs/IBurnableERC721.md b/docs/IBurnableERC721.md index 9c431bc2b..b564d392c 100644 --- a/docs/IBurnableERC721.md +++ b/docs/IBurnableERC721.md @@ -1,6 +1,6 @@ # IBurnableERC721 - +*thirdweb* diff --git a/docs/IClaimableERC1155.md b/docs/IClaimableERC1155.md index fc6c93379..806429a34 100644 --- a/docs/IClaimableERC1155.md +++ b/docs/IClaimableERC1155.md @@ -1,6 +1,6 @@ # IClaimableERC1155 - +*thirdweb* diff --git a/docs/IClaimableERC721.md b/docs/IClaimableERC721.md index b7a7f6d65..faffaeeb6 100644 --- a/docs/IClaimableERC721.md +++ b/docs/IClaimableERC721.md @@ -1,6 +1,6 @@ # IClaimableERC721 - +*thirdweb* diff --git a/docs/IContext.md b/docs/IContext.md index 01885894f..9852da068 100644 --- a/docs/IContext.md +++ b/docs/IContext.md @@ -1,6 +1,6 @@ # IContext - +*thirdweb* diff --git a/docs/IContractFactory.md b/docs/IContractFactory.md index 27a06469f..85d122144 100644 --- a/docs/IContractFactory.md +++ b/docs/IContractFactory.md @@ -1,6 +1,6 @@ # IContractFactory - +*thirdweb* diff --git a/docs/IMintableERC20.md b/docs/IMintableERC20.md index f85577b59..57e40d8c6 100644 --- a/docs/IMintableERC20.md +++ b/docs/IMintableERC20.md @@ -1,6 +1,6 @@ # IMintableERC20 - +*thirdweb* diff --git a/docs/IMintableERC721.md b/docs/IMintableERC721.md index 8d6e53364..5536df0c9 100644 --- a/docs/IMintableERC721.md +++ b/docs/IMintableERC721.md @@ -1,6 +1,6 @@ # IMintableERC721 - +*thirdweb* diff --git a/docs/IOperatorFilterRegistry.md b/docs/IOperatorFilterRegistry.md index 88e0762dd..bd8addba3 100644 --- a/docs/IOperatorFilterRegistry.md +++ b/docs/IOperatorFilterRegistry.md @@ -1,6 +1,6 @@ # IOperatorFilterRegistry - +*thirdweb* diff --git a/docs/IOperatorFilterToggle.md b/docs/IOperatorFilterToggle.md index 0c0691745..06ad7d13a 100644 --- a/docs/IOperatorFilterToggle.md +++ b/docs/IOperatorFilterToggle.md @@ -1,6 +1,6 @@ # IOperatorFilterToggle - +*thirdweb* diff --git a/docs/IPluginMap.md b/docs/IPluginMap.md index 12e9bb2e1..c13c4b6b5 100644 --- a/docs/IPluginMap.md +++ b/docs/IPluginMap.md @@ -1,6 +1,6 @@ # IPluginMap - +*thirdweb* diff --git a/docs/IRouter.md b/docs/IRouter.md index 4a18986a5..57f12aa37 100644 --- a/docs/IRouter.md +++ b/docs/IRouter.md @@ -10,10 +10,10 @@ ## Methods -### addPlugin +### getImplementationForFunction ```solidity -function addPlugin(IPluginMap.Plugin plugin) external nonpayable +function getImplementationForFunction(bytes4 _functionSelector) external view returns (address) ``` @@ -24,62 +24,7 @@ function addPlugin(IPluginMap.Plugin plugin) external nonpayable | Name | Type | Description | |---|---|---| -| plugin | IPluginMap.Plugin | undefined | - -### getAllFunctionsOfPlugin - -```solidity -function getAllFunctionsOfPlugin(address pluginAddress) external view returns (bytes4[]) -``` - - - -*Returns all functions that are mapped to the given plug-in contract.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| pluginAddress | address | undefined | - -#### Returns - -| Name | Type | Description | -|---|---|---| -| _0 | bytes4[] | undefined | - -### getAllPlugins - -```solidity -function getAllPlugins() external view returns (struct IPluginMap.Plugin[]) -``` - - - -*Returns all plug-ins known by Map.* - - -#### Returns - -| Name | Type | Description | -|---|---|---| -| _0 | IPluginMap.Plugin[] | undefined | - -### getPluginForFunction - -```solidity -function getPluginForFunction(bytes4 functionSelector) external view returns (address) -``` - - - -*Returns the plug-in contract for a given function.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector | bytes4 | undefined | +| _functionSelector | bytes4 | undefined | #### Returns @@ -87,111 +32,6 @@ function getPluginForFunction(bytes4 functionSelector) external view returns (ad |---|---|---| | _0 | address | undefined | -### removePlugin - -```solidity -function removePlugin(bytes4 functionSelector) external nonpayable -``` - - - -*Remove an existing plugin from the contract.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector | bytes4 | undefined | - -### updatePlugin - -```solidity -function updatePlugin(IPluginMap.Plugin plugin) external nonpayable -``` - - - - - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| plugin | IPluginMap.Plugin | undefined | - - - -## Events - -### PluginAdded - -```solidity -event PluginAdded(bytes4 indexed functionSelector, address indexed pluginAddress) -``` - - - -*Emitted when a functionality is added, or plugged-in.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector `indexed` | bytes4 | undefined | -| pluginAddress `indexed` | address | undefined | - -### PluginRemoved - -```solidity -event PluginRemoved(bytes4 indexed functionSelector, address indexed pluginAddress) -``` - - - -*Emitted when a functionality is removed.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector `indexed` | bytes4 | undefined | -| pluginAddress `indexed` | address | undefined | - -### PluginSet - -```solidity -event PluginSet(bytes4 indexed functionSelector, string indexed functionSignature, address indexed pluginAddress) -``` - - - - - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector `indexed` | bytes4 | undefined | -| functionSignature `indexed` | string | undefined | -| pluginAddress `indexed` | address | undefined | - -### PluginUpdated - -```solidity -event PluginUpdated(bytes4 indexed functionSelector, address indexed oldPluginAddress, address indexed newPluginAddress) -``` - - - -*Emitted when a functionality is updated or overridden.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector `indexed` | bytes4 | undefined | -| oldPluginAddress `indexed` | address | undefined | -| newPluginAddress `indexed` | address | undefined | diff --git a/docs/IStaking1155.md b/docs/IStaking1155.md index 19d844442..f2bc5f813 100644 --- a/docs/IStaking1155.md +++ b/docs/IStaking1155.md @@ -1,6 +1,6 @@ # IStaking1155 - +*thirdweb* diff --git a/docs/IStaking20.md b/docs/IStaking20.md index 6fd48ac89..e3690617b 100644 --- a/docs/IStaking20.md +++ b/docs/IStaking20.md @@ -1,6 +1,6 @@ # IStaking20 - +*thirdweb* diff --git a/docs/IStaking721.md b/docs/IStaking721.md index 3b96cb097..782e07fd6 100644 --- a/docs/IStaking721.md +++ b/docs/IStaking721.md @@ -1,6 +1,6 @@ # IStaking721 - +*thirdweb* diff --git a/docs/ReentrancyGuardStorage.md b/docs/ReentrancyGuardStorage.md index 95ef24a6a..79c896a9e 100644 --- a/docs/ReentrancyGuardStorage.md +++ b/docs/ReentrancyGuardStorage.md @@ -1,6 +1,6 @@ # ReentrancyGuardStorage - +*thirdweb* diff --git a/docs/Router.md b/docs/Router.md index b78213038..969c27d01 100644 --- a/docs/Router.md +++ b/docs/Router.md @@ -10,21 +10,21 @@ ## Methods -### _getPluginForFunction +### getImplementationForFunction ```solidity -function _getPluginForFunction(bytes4 _selector) external view returns (address) +function getImplementationForFunction(bytes4 _functionSelector) external view returns (address) ``` -*View address of the plugged-in functionality contract for a given function signature.* +*Unimplemented. Returns the implementation contract address for a given function signature.* #### Parameters | Name | Type | Description | |---|---|---| -| _selector | bytes4 | undefined | +| _functionSelector | bytes4 | undefined | #### Returns @@ -32,249 +32,6 @@ function _getPluginForFunction(bytes4 _selector) external view returns (address) |---|---|---| | _0 | address | undefined | -### addPlugin - -```solidity -function addPlugin(IPluginMap.Plugin _plugin) external nonpayable -``` - - - - - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| _plugin | IPluginMap.Plugin | undefined | - -### getAllFunctionsOfPlugin - -```solidity -function getAllFunctionsOfPlugin(address _pluginAddress) external view returns (bytes4[] registered) -``` - - - -*View all funtionality as list of function signatures.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| _pluginAddress | address | undefined | - -#### Returns - -| Name | Type | Description | -|---|---|---| -| registered | bytes4[] | undefined | - -### getAllPlugins - -```solidity -function getAllPlugins() external view returns (struct IPluginMap.Plugin[] registered) -``` - - - -*View all funtionality existing on the contract.* - - -#### Returns - -| Name | Type | Description | -|---|---|---| -| registered | IPluginMap.Plugin[] | undefined | - -### getPluginForFunction - -```solidity -function getPluginForFunction(bytes4 _selector) external view returns (address) -``` - - - -*View address of the plugged-in functionality contract for a given function signature.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| _selector | bytes4 | undefined | - -#### Returns - -| Name | Type | Description | -|---|---|---| -| _0 | address | undefined | - -### multicall - -```solidity -function multicall(bytes[] data) external nonpayable returns (bytes[] results) -``` - -Receives and executes a batch of function calls on this contract. - -*Receives and executes a batch of function calls on this contract.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| data | bytes[] | The bytes data that makes up the batch of function calls to execute. | - -#### Returns - -| Name | Type | Description | -|---|---|---| -| results | bytes[] | The bytes data that makes up the result of the batch of function calls executed. | - -### pluginMap - -```solidity -function pluginMap() external view returns (address) -``` - - - - - - -#### Returns - -| Name | Type | Description | -|---|---|---| -| _0 | address | undefined | - -### removePlugin - -```solidity -function removePlugin(bytes4 _selector) external nonpayable -``` - - - -*Remove existing functionality from the contract.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| _selector | bytes4 | undefined | - -### supportsInterface - -```solidity -function supportsInterface(bytes4 interfaceId) external view returns (bool) -``` - - - -*See {IERC165-supportsInterface}.* - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| interfaceId | bytes4 | undefined | - -#### Returns - -| Name | Type | Description | -|---|---|---| -| _0 | bool | undefined | - -### updatePlugin - -```solidity -function updatePlugin(IPluginMap.Plugin _plugin) external nonpayable -``` - - - - - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| _plugin | IPluginMap.Plugin | undefined | - - - -## Events - -### PluginAdded - -```solidity -event PluginAdded(bytes4 indexed functionSelector, address indexed pluginAddress) -``` - - - - - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector `indexed` | bytes4 | undefined | -| pluginAddress `indexed` | address | undefined | - -### PluginRemoved - -```solidity -event PluginRemoved(bytes4 indexed functionSelector, address indexed pluginAddress) -``` - - - - - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector `indexed` | bytes4 | undefined | -| pluginAddress `indexed` | address | undefined | - -### PluginSet - -```solidity -event PluginSet(bytes4 indexed functionSelector, string indexed functionSignature, address indexed pluginAddress) -``` - - - - - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector `indexed` | bytes4 | undefined | -| functionSignature `indexed` | string | undefined | -| pluginAddress `indexed` | address | undefined | - -### PluginUpdated - -```solidity -event PluginUpdated(bytes4 indexed functionSelector, address indexed oldPluginAddress, address indexed newPluginAddress) -``` - - - - - -#### Parameters - -| Name | Type | Description | -|---|---|---| -| functionSelector `indexed` | bytes4 | undefined | -| oldPluginAddress `indexed` | address | undefined | -| newPluginAddress `indexed` | address | undefined | diff --git a/docs/Staking1155Base.md b/docs/Staking1155Base.md index 82b0d1069..b639864c7 100644 --- a/docs/Staking1155Base.md +++ b/docs/Staking1155Base.md @@ -297,7 +297,7 @@ function onERC1155BatchReceived(address operator, address from, uint256[] ids, u ### onERC1155Received ```solidity -function onERC1155Received(address, address, uint256, uint256, bytes) external nonpayable returns (bytes4) +function onERC1155Received(address, address, uint256, uint256, bytes) external view returns (bytes4) ``` diff --git a/docs/TWStringSet.md b/docs/TWStringSet.md index dbfcfd54c..01f709651 100644 --- a/docs/TWStringSet.md +++ b/docs/TWStringSet.md @@ -1,6 +1,6 @@ # TWStringSet - +*thirdweb* diff --git a/docs/TokenERC1155.md b/docs/TokenERC1155.md index 3fb3f2df2..9a50c1bb0 100644 --- a/docs/TokenERC1155.md +++ b/docs/TokenERC1155.md @@ -168,7 +168,7 @@ function getDefaultRoyaltyInfo() external view returns (address, uint16) -*Returns the platform fee bps and recipient.* +*Returns default royalty info.* #### Returns @@ -178,6 +178,24 @@ function getDefaultRoyaltyInfo() external view returns (address, uint16) | _0 | address | undefined | | _1 | uint16 | undefined | +### getFlatPlatformFeeInfo + +```solidity +function getFlatPlatformFeeInfo() external view returns (address, uint256) +``` + + + +*Returns the flat platform fee and recipient.* + + +#### Returns + +| Name | Type | Description | +|---|---|---| +| _0 | address | undefined | +| _1 | uint256 | undefined | + ### getPlatformFeeInfo ```solidity @@ -196,6 +214,23 @@ function getPlatformFeeInfo() external view returns (address, uint16) | _0 | address | undefined | | _1 | uint16 | undefined | +### getPlatformFeeType + +```solidity +function getPlatformFeeType() external view returns (enum TokenERC1155.PlatformFeeType) +``` + + + +*Returns the platform fee type.* + + +#### Returns + +| Name | Type | Description | +|---|---|---| +| _0 | enum TokenERC1155.PlatformFeeType | undefined | + ### getRoleAdmin ```solidity @@ -726,6 +761,23 @@ function setDefaultRoyaltyInfo(address _royaltyRecipient, uint256 _royaltyBps) e | _royaltyRecipient | address | undefined | | _royaltyBps | uint256 | undefined | +### setFlatPlatformFeeInfo + +```solidity +function setFlatPlatformFeeInfo(address _platformFeeRecipient, uint256 _flatFee) external nonpayable +``` + + + +*Lets a module admin set a flat fee on primary sales.* + +#### Parameters + +| Name | Type | Description | +|---|---|---| +| _platformFeeRecipient | address | undefined | +| _flatFee | uint256 | undefined | + ### setOperatorRestriction ```solidity @@ -775,6 +827,22 @@ function setPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeB | _platformFeeRecipient | address | undefined | | _platformFeeBps | uint256 | undefined | +### setPlatformFeeType + +```solidity +function setPlatformFeeType(enum TokenERC1155.PlatformFeeType _feeType) external nonpayable +``` + + + +*Lets a module admin set a flat fee on primary sales.* + +#### Parameters + +| Name | Type | Description | +|---|---|---| +| _feeType | enum TokenERC1155.PlatformFeeType | undefined | + ### setPrimarySaleRecipient ```solidity @@ -955,6 +1023,23 @@ event DefaultRoyalty(address indexed newRoyaltyRecipient, uint256 newRoyaltyBps) | newRoyaltyRecipient `indexed` | address | undefined | | newRoyaltyBps | uint256 | undefined | +### FlatPlatformFeeUpdated + +```solidity +event FlatPlatformFeeUpdated(address platformFeeRecipient, uint256 flatFee) +``` + + + +*Emitted when flat fee on primary sales is updated.* + +#### Parameters + +| Name | Type | Description | +|---|---|---| +| platformFeeRecipient | address | undefined | +| flatFee | uint256 | undefined | + ### Initialized ```solidity @@ -1021,6 +1106,22 @@ event PlatformFeeInfoUpdated(address indexed platformFeeRecipient, uint256 platf | platformFeeRecipient `indexed` | address | undefined | | platformFeeBps | uint256 | undefined | +### PlatformFeeTypeUpdated + +```solidity +event PlatformFeeTypeUpdated(enum TokenERC1155.PlatformFeeType feeType) +``` + + + +*Emitted when platform fee type is updated.* + +#### Parameters + +| Name | Type | Description | +|---|---|---| +| feeType | enum TokenERC1155.PlatformFeeType | undefined | + ### PrimarySaleRecipientUpdated ```solidity From 75fe1df72cb6c45547d4ddaf427ddd62357d23c0 Mon Sep 17 00:00:00 2001 From: Krishang Date: Tue, 28 Feb 2023 12:24:47 -0800 Subject: [PATCH 2/7] pkg update --- contracts/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/package.json b/contracts/package.json index ee2981a99..3a7f9a02f 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -1,7 +1,7 @@ { "name": "@thirdweb-dev/contracts", "description": "Collection of smart contracts deployable via the thirdweb SDK, dashboard and CLI", - "version": "3.4.2", + "version": "3.4.3", "license": "Apache-2.0", "repository": { "type": "git", From 6b450fbd79b73b753d2583258e05a2b1b49f6a12 Mon Sep 17 00:00:00 2001 From: Krishang Date: Thu, 9 Mar 2023 14:49:31 -0500 Subject: [PATCH 3/7] Make MAX_BPS private in Marketplace plugins --- contracts/marketplace/direct-listings/DirectListingsLogic.sol | 2 +- contracts/marketplace/english-auctions/EnglishAuctionsLogic.sol | 2 +- contracts/marketplace/offers/OffersLogic.sol | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/marketplace/direct-listings/DirectListingsLogic.sol b/contracts/marketplace/direct-listings/DirectListingsLogic.sol index a1e83ac32..aa5e04f65 100644 --- a/contracts/marketplace/direct-listings/DirectListingsLogic.sol +++ b/contracts/marketplace/direct-listings/DirectListingsLogic.sol @@ -33,7 +33,7 @@ contract DirectListingsLogic is IDirectListings, ReentrancyGuardLogic, ERC2771Co bytes32 private constant ASSET_ROLE = keccak256("ASSET_ROLE"); /// @dev The max bps of the contract. So, 10_000 == 100 % - uint64 public constant MAX_BPS = 10_000; + uint64 private constant MAX_BPS = 10_000; /// @dev The address of the native token wrapper contract. address private immutable nativeTokenWrapper; diff --git a/contracts/marketplace/english-auctions/EnglishAuctionsLogic.sol b/contracts/marketplace/english-auctions/EnglishAuctionsLogic.sol index b6598077d..8d0509d9a 100644 --- a/contracts/marketplace/english-auctions/EnglishAuctionsLogic.sol +++ b/contracts/marketplace/english-auctions/EnglishAuctionsLogic.sol @@ -37,7 +37,7 @@ contract EnglishAuctionsLogic is IEnglishAuctions, ReentrancyGuardLogic, ERC2771 bytes32 private constant ASSET_ROLE = keccak256("ASSET_ROLE"); /// @dev The max bps of the contract. So, 10_000 == 100 % - uint64 public constant MAX_BPS = 10_000; + uint64 private constant MAX_BPS = 10_000; /// @dev The address of the native token wrapper contract. address private immutable nativeTokenWrapper; diff --git a/contracts/marketplace/offers/OffersLogic.sol b/contracts/marketplace/offers/OffersLogic.sol index 5138f78ac..764cc0c72 100644 --- a/contracts/marketplace/offers/OffersLogic.sol +++ b/contracts/marketplace/offers/OffersLogic.sol @@ -34,7 +34,7 @@ contract OffersLogic is IOffers, ReentrancyGuardLogic, ERC2771ContextConsumer { bytes32 private constant ASSET_ROLE = keccak256("ASSET_ROLE"); /// @dev The max bps of the contract. So, 10_000 == 100 % - uint64 public constant MAX_BPS = 10_000; + uint64 private constant MAX_BPS = 10_000; /*/////////////////////////////////////////////////////////////// Modifiers From b9e787f7e994d9068a739745b7f22399e9702d78 Mon Sep 17 00:00:00 2001 From: Krishang Date: Wed, 19 Apr 2023 00:46:30 -0400 Subject: [PATCH 4/7] Rename salt to accountId --- contracts/smart-wallet/TWAccountFactory.sol | 16 ++++++++-------- .../smart-wallet/TWDynamicAccountFactory.sol | 16 ++++++++-------- .../interfaces/ITWAccountFactory.sol | 10 +++++----- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/contracts/smart-wallet/TWAccountFactory.sol b/contracts/smart-wallet/TWAccountFactory.sol index e1ad285c3..bf80c7566 100644 --- a/contracts/smart-wallet/TWAccountFactory.sol +++ b/contracts/smart-wallet/TWAccountFactory.sol @@ -23,26 +23,26 @@ contract TWAccountFactory is ITWAccountFactory, Multicall { return address(_accountImplementation); } - /// @notice Deploys a new Account with the given admin and salt. - function createAccount(address _admin, bytes32 _salt) external returns (address) { + /// @notice Deploys a new Account with the given admin and accountId used as salt. + function createAccount(address _admin, bytes32 _accountId) external returns (address) { address impl = address(_accountImplementation); - address account = Clones.predictDeterministicAddress(impl, _salt); + address account = Clones.predictDeterministicAddress(impl, _accountId); if (account.code.length > 0) { return account; } - account = Clones.cloneDeterministic(impl, _salt); + account = Clones.cloneDeterministic(impl, _accountId); TWAccount(payable(account)).initialize(_admin); - emit AccountCreated(account, _admin, _salt); + emit AccountCreated(account, _admin, _accountId); return account; } - /// @notice Returns the address of an Account that would be deployed with the given salt. - function getAddress(bytes32 _salt) external view returns (address) { - return Clones.predictDeterministicAddress(address(_accountImplementation), _salt); + /// @notice Returns the address of an Account that would be deployed with the given accountId as salt. + function getAddress(bytes32 _accountId) external view returns (address) { + return Clones.predictDeterministicAddress(address(_accountImplementation), _accountId); } } diff --git a/contracts/smart-wallet/TWDynamicAccountFactory.sol b/contracts/smart-wallet/TWDynamicAccountFactory.sol index 93c88ee83..ca601cdfc 100644 --- a/contracts/smart-wallet/TWDynamicAccountFactory.sol +++ b/contracts/smart-wallet/TWDynamicAccountFactory.sol @@ -23,26 +23,26 @@ contract TWDynamicAccountFactory is ITWAccountFactory, Multicall { return address(_accountImplementation); } - /// @notice Deploys a new Account with the given admin and salt. - function createAccount(address _admin, bytes32 _salt) external returns (address) { + /// @notice Deploys a new Account with the given admin and accountId used as salt. + function createAccount(address _admin, bytes32 _accountId) external returns (address) { address impl = address(_accountImplementation); - address account = Clones.predictDeterministicAddress(impl, _salt); + address account = Clones.predictDeterministicAddress(impl, _accountId); if (account.code.length > 0) { return account; } - account = Clones.cloneDeterministic(impl, _salt); + account = Clones.cloneDeterministic(impl, _accountId); TWAccount(payable(account)).initialize(_admin); - emit AccountCreated(account, _admin, _salt); + emit AccountCreated(account, _admin, _accountId); return account; } - /// @notice Returns the address of an Account that would be deployed with the given salt. - function getAddress(bytes32 _salt) external view returns (address) { - return Clones.predictDeterministicAddress(address(_accountImplementation), _salt); + /// @notice Returns the address of an Account that would be deployed with the given accountId as salt. + function getAddress(bytes32 _accountId) external view returns (address) { + return Clones.predictDeterministicAddress(address(_accountImplementation), _accountId); } } diff --git a/contracts/smart-wallet/interfaces/ITWAccountFactory.sol b/contracts/smart-wallet/interfaces/ITWAccountFactory.sol index aa72ccf80..42a5b0289 100644 --- a/contracts/smart-wallet/interfaces/ITWAccountFactory.sol +++ b/contracts/smart-wallet/interfaces/ITWAccountFactory.sol @@ -2,14 +2,14 @@ pragma solidity ^0.8.12; interface ITWAccountFactory { - event AccountCreated(address indexed account, address indexed accountAdmin, bytes32 indexed salt); + event AccountCreated(address indexed account, address indexed accountAdmin, bytes32 indexed accountId); /// @notice Returns the address of the Account implementation. function accountImplementation() external view returns (address); - /// @notice Deploys a new Account with the given admin and salt. - function createAccount(address admin, bytes32 salt) external returns (address account); + /// @notice Deploys a new Account with the given admin and accountId used as salt. + function createAccount(address admin, bytes32 accountId) external returns (address account); - /// @notice Returns the address of an Account that would be deployed with the given salt. - function getAddress(bytes32 salt) external view returns (address); + /// @notice Returns the address of an Account that would be deployed with the given accountId as salt. + function getAddress(bytes32 accountId) external view returns (address); } From bc5ebf7723c58dd9a4193eb84ab178e4237c33c6 Mon Sep 17 00:00:00 2001 From: Krishang Date: Wed, 19 Apr 2023 01:12:01 -0400 Subject: [PATCH 5/7] Add method to fetch all wallets created by signer --- contracts/smart-wallet/TWAccountFactory.sol | 32 ++++++++++++++++++ .../smart-wallet/TWDynamicAccountFactory.sol | 33 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/contracts/smart-wallet/TWAccountFactory.sol b/contracts/smart-wallet/TWAccountFactory.sol index bf80c7566..144092ba2 100644 --- a/contracts/smart-wallet/TWAccountFactory.sol +++ b/contracts/smart-wallet/TWAccountFactory.sol @@ -4,6 +4,7 @@ pragma solidity ^0.8.12; // Utils import "../extension/Multicall.sol"; import "@openzeppelin/contracts/proxy/Clones.sol"; +import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; // Interface import "./interfaces/ITWAccountFactory.sol"; @@ -11,7 +12,24 @@ import "./interfaces/ITWAccountFactory.sol"; // Smart wallet implementation import "./TWAccount.sol"; +library TWAccountFactoryStorage { + bytes32 internal constant TWACCOUNT_FACTORY_STORAGE_POSITION = keccak256("twaccount.factory.storage"); + + struct Data { + mapping(address => EnumerableSet.AddressSet) accountsOfSigner; + } + + function factoryStorage() internal pure returns (Data storage twaccountFactoryData) { + bytes32 position = TWACCOUNT_FACTORY_STORAGE_POSITION; + assembly { + twaccountFactoryData.slot := position + } + } +} + contract TWAccountFactory is ITWAccountFactory, Multicall { + using EnumerableSet for EnumerableSet.AddressSet; + TWAccount private immutable _accountImplementation; constructor(IEntryPoint _entrypoint) { @@ -36,6 +54,8 @@ contract TWAccountFactory is ITWAccountFactory, Multicall { TWAccount(payable(account)).initialize(_admin); + _addAccount(_admin, account); + emit AccountCreated(account, _admin, _accountId); return account; @@ -45,4 +65,16 @@ contract TWAccountFactory is ITWAccountFactory, Multicall { function getAddress(bytes32 _accountId) external view returns (address) { return Clones.predictDeterministicAddress(address(_accountImplementation), _accountId); } + + /// @notice Returns the list of accounts created by a signer. + function getAccountsOfSigner(address _signer) external view returns (address[] memory allAccounts) { + TWAccountFactoryStorage.Data storage data = TWAccountFactoryStorage.factoryStorage(); + return data.accountsOfSigner[_signer].values(); + } + + /// @dev Adds an account to the list of accounts created by a signer. + function _addAccount(address _signer, address _account) internal { + TWAccountFactoryStorage.Data storage data = TWAccountFactoryStorage.factoryStorage(); + data.accountsOfSigner[_signer].add(_account); + } } diff --git a/contracts/smart-wallet/TWDynamicAccountFactory.sol b/contracts/smart-wallet/TWDynamicAccountFactory.sol index ca601cdfc..fbd357d76 100644 --- a/contracts/smart-wallet/TWDynamicAccountFactory.sol +++ b/contracts/smart-wallet/TWDynamicAccountFactory.sol @@ -4,6 +4,7 @@ pragma solidity ^0.8.12; // Utils import "../extension/Multicall.sol"; import "@openzeppelin/contracts/proxy/Clones.sol"; +import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; // Interface import "./interfaces/ITWAccountFactory.sol"; @@ -11,7 +12,25 @@ import "./interfaces/ITWAccountFactory.sol"; // Smart wallet implementation import "./TWDynamicAccount.sol"; +library TWDynamicAccountFactoryStorage { + bytes32 internal constant DYNAMIC_ACCOUNT_FACTORY_STORAGE_POSITION = + keccak256("tw.dynamic.account.factory.storage"); + + struct Data { + mapping(address => EnumerableSet.AddressSet) accountsOfSigner; + } + + function factoryStorage() internal pure returns (Data storage dynamicAccountFactoryData) { + bytes32 position = DYNAMIC_ACCOUNT_FACTORY_STORAGE_POSITION; + assembly { + dynamicAccountFactoryData.slot := position + } + } +} + contract TWDynamicAccountFactory is ITWAccountFactory, Multicall { + using EnumerableSet for EnumerableSet.AddressSet; + TWDynamicAccount private immutable _accountImplementation; constructor(IEntryPoint _entrypoint) { @@ -36,6 +55,8 @@ contract TWDynamicAccountFactory is ITWAccountFactory, Multicall { TWAccount(payable(account)).initialize(_admin); + _addAccount(_admin, account); + emit AccountCreated(account, _admin, _accountId); return account; @@ -45,4 +66,16 @@ contract TWDynamicAccountFactory is ITWAccountFactory, Multicall { function getAddress(bytes32 _accountId) external view returns (address) { return Clones.predictDeterministicAddress(address(_accountImplementation), _accountId); } + + /// @notice Returns the list of accounts created by a signer. + function getAccountsOfSigner(address _signer) external view returns (address[] memory) { + TWDynamicAccountFactoryStorage.Data storage data = TWDynamicAccountFactoryStorage.factoryStorage(); + return data.accountsOfSigner[_signer].values(); + } + + /// @dev Adds an account to the list of accounts created by a signer. + function _addAccount(address _signer, address _account) internal { + TWDynamicAccountFactoryStorage.Data storage data = TWDynamicAccountFactoryStorage.factoryStorage(); + data.accountsOfSigner[_signer].add(_account); + } } From 9e6c1f6fca9d5c8b50013fbb65a9d4483345883d Mon Sep 17 00:00:00 2001 From: Krishang Date: Wed, 19 Apr 2023 02:05:39 -0400 Subject: [PATCH 6/7] Update account factory with getters --- contracts/smart-wallet/TWAccount.sol | 15 +- contracts/smart-wallet/TWAccountFactory.sol | 88 +++++-- contracts/smart-wallet/TWDynamicAccount.sol | 9 + .../smart-wallet/TWDynamicAccountFactory.sol | 88 +++++-- .../interfaces/ITWAccountFactory.sol | 39 ++- .../smart-wallet/TWAccountBenchmark.t.sol | 226 +++++++++--------- 6 files changed, 312 insertions(+), 153 deletions(-) diff --git a/contracts/smart-wallet/TWAccount.sol b/contracts/smart-wallet/TWAccount.sol index dc83d48af..8a5b24ef4 100644 --- a/contracts/smart-wallet/TWAccount.sol +++ b/contracts/smart-wallet/TWAccount.sol @@ -19,6 +19,19 @@ import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol"; // Utils import "../openzeppelin-presets/utils/cryptography/ECDSA.sol"; +// $$\ $$\ $$\ $$\ $$\ +// $$ | $$ | \__| $$ | $$ | +// $$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\ +// \_$$ _| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\ +// $$ | $$ | $$ |$$ |$$ | \__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ | +// $$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ | +// \$$$$ |$$ | $$ |$$ |$$ | \$$$$$$$ |\$$$$$\$$$$ |\$$$$$$$\ $$$$$$$ | +// \____/ \__| \__|\__|\__| \_______| \_____\____/ \_______|\_______/ + +/*/////////////////////////////////////////////////////////////// + Storage layout +//////////////////////////////////////////////////////////////*/ + library TWAccountStorage { bytes32 internal constant TWACCOUNT_STORAGE_POSITION = keccak256("twaccount.storage"); @@ -46,7 +59,7 @@ contract TWAccount is using ECDSA for bytes32; /*/////////////////////////////////////////////////////////////// - State (constant, immutable) + State //////////////////////////////////////////////////////////////*/ bytes32 public constant SIGNER_ROLE = keccak256("SIGNER_ROLE"); diff --git a/contracts/smart-wallet/TWAccountFactory.sol b/contracts/smart-wallet/TWAccountFactory.sol index 144092ba2..ee77b6b2b 100644 --- a/contracts/smart-wallet/TWAccountFactory.sol +++ b/contracts/smart-wallet/TWAccountFactory.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.12; // Utils import "../extension/Multicall.sol"; import "@openzeppelin/contracts/proxy/Clones.sol"; -import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; +import "../lib/TWStringSet.sol"; // Interface import "./interfaces/ITWAccountFactory.sol"; @@ -12,11 +12,25 @@ import "./interfaces/ITWAccountFactory.sol"; // Smart wallet implementation import "./TWAccount.sol"; +// $$\ $$\ $$\ $$\ $$\ +// $$ | $$ | \__| $$ | $$ | +// $$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\ +// \_$$ _| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\ +// $$ | $$ | $$ |$$ |$$ | \__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ | +// $$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ | +// \$$$$ |$$ | $$ |$$ |$$ | \$$$$$$$ |\$$$$$\$$$$ |\$$$$$$$\ $$$$$$$ | +// \____/ \__| \__|\__|\__| \_______| \_____\____/ \_______|\_______/ + +/*/////////////////////////////////////////////////////////////// + Storage layout +//////////////////////////////////////////////////////////////*/ + library TWAccountFactoryStorage { bytes32 internal constant TWACCOUNT_FACTORY_STORAGE_POSITION = keccak256("twaccount.factory.storage"); struct Data { - mapping(address => EnumerableSet.AddressSet) accountsOfSigner; + TWStringSet.Set allAccounts; + mapping(address => TWStringSet.Set) accountsOfSigner; } function factoryStorage() internal pure returns (Data storage twaccountFactoryData) { @@ -28,53 +42,93 @@ library TWAccountFactoryStorage { } contract TWAccountFactory is ITWAccountFactory, Multicall { - using EnumerableSet for EnumerableSet.AddressSet; + using TWStringSet for TWStringSet.Set; + + /*/////////////////////////////////////////////////////////////// + State + //////////////////////////////////////////////////////////////*/ TWAccount private immutable _accountImplementation; + /*/////////////////////////////////////////////////////////////// + Constructor + //////////////////////////////////////////////////////////////*/ + constructor(IEntryPoint _entrypoint) { _accountImplementation = new TWAccount(_entrypoint); } - /// @notice Returns the implementation of the Account. - function accountImplementation() external view override returns (address) { - return address(_accountImplementation); - } + /*/////////////////////////////////////////////////////////////// + External functions + //////////////////////////////////////////////////////////////*/ /// @notice Deploys a new Account with the given admin and accountId used as salt. - function createAccount(address _admin, bytes32 _accountId) external returns (address) { + function createAccount(address _admin, string memory _accountId) external returns (address) { address impl = address(_accountImplementation); - address account = Clones.predictDeterministicAddress(impl, _accountId); + bytes32 salt = keccak256(abi.encode(_accountId)); + address account = Clones.predictDeterministicAddress(impl, salt); if (account.code.length > 0) { return account; } - account = Clones.cloneDeterministic(impl, _accountId); + account = Clones.cloneDeterministic(impl, salt); TWAccount(payable(account)).initialize(_admin); - _addAccount(_admin, account); + _setupAccount(_admin, _accountId); emit AccountCreated(account, _admin, _accountId); return account; } + /*/////////////////////////////////////////////////////////////// + View functions + //////////////////////////////////////////////////////////////*/ + + /// @notice Returns the implementation of the Account. + function accountImplementation() external view override returns (address) { + return address(_accountImplementation); + } + /// @notice Returns the address of an Account that would be deployed with the given accountId as salt. - function getAddress(bytes32 _accountId) external view returns (address) { - return Clones.predictDeterministicAddress(address(_accountImplementation), _accountId); + function getAddress(string memory _accountId) public view returns (address) { + bytes32 salt = keccak256(abi.encode(_accountId)); + return Clones.predictDeterministicAddress(address(_accountImplementation), salt); } /// @notice Returns the list of accounts created by a signer. - function getAccountsOfSigner(address _signer) external view returns (address[] memory allAccounts) { + function getAccountsOfSigner(address _signer) external view returns (AccountInfo[] memory) { + TWAccountFactoryStorage.Data storage data = TWAccountFactoryStorage.factoryStorage(); + return _formatAccounts(data.accountsOfSigner[_signer].values()); + } + + /// @notice Returns the list of all accounts. + function getAllAccounts() external view returns (AccountInfo[] memory accounts) { TWAccountFactoryStorage.Data storage data = TWAccountFactoryStorage.factoryStorage(); - return data.accountsOfSigner[_signer].values(); + return _formatAccounts(data.allAccounts.values()); + } + + /*/////////////////////////////////////////////////////////////// + Internal functions + //////////////////////////////////////////////////////////////*/ + + /// @dev Formats a list of accountIds to a list of `AccountInfo` (account id + account address). + function _formatAccounts(string[] memory _accountIds) internal view returns (AccountInfo[] memory accounts) { + uint256 len = _accountIds.length; + accounts = new AccountInfo[](len); + for (uint256 i = 0; i < len; i += 1) { + string memory accountId = _accountIds[i]; + address account = getAddress(accountId); + accounts[i] = AccountInfo(accountId, account); + } } /// @dev Adds an account to the list of accounts created by a signer. - function _addAccount(address _signer, address _account) internal { + function _setupAccount(address _signer, string memory _accountId) internal { TWAccountFactoryStorage.Data storage data = TWAccountFactoryStorage.factoryStorage(); - data.accountsOfSigner[_signer].add(_account); + data.allAccounts.add(_accountId); + data.accountsOfSigner[_signer].add(_accountId); } } diff --git a/contracts/smart-wallet/TWDynamicAccount.sol b/contracts/smart-wallet/TWDynamicAccount.sol index 02210ccd1..10ca787ae 100644 --- a/contracts/smart-wallet/TWDynamicAccount.sol +++ b/contracts/smart-wallet/TWDynamicAccount.sol @@ -8,6 +8,15 @@ pragma solidity ^0.8.11; import "./TWAccount.sol"; import "./BaseRouter.sol"; +// $$\ $$\ $$\ $$\ $$\ +// $$ | $$ | \__| $$ | $$ | +// $$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\ +// \_$$ _| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\ +// $$ | $$ | $$ |$$ |$$ | \__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ | +// $$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ | +// \$$$$ |$$ | $$ |$$ |$$ | \$$$$$$$ |\$$$$$\$$$$ |\$$$$$$$\ $$$$$$$ | +// \____/ \__| \__|\__|\__| \_______| \_____\____/ \_______|\_______/ + contract TWDynamicAccount is TWAccount, BaseRouter { /*/////////////////////////////////////////////////////////////// Constants diff --git a/contracts/smart-wallet/TWDynamicAccountFactory.sol b/contracts/smart-wallet/TWDynamicAccountFactory.sol index fbd357d76..93fd0a82e 100644 --- a/contracts/smart-wallet/TWDynamicAccountFactory.sol +++ b/contracts/smart-wallet/TWDynamicAccountFactory.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.12; // Utils import "../extension/Multicall.sol"; import "@openzeppelin/contracts/proxy/Clones.sol"; -import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; +import "../lib/TWStringSet.sol"; // Interface import "./interfaces/ITWAccountFactory.sol"; @@ -12,12 +12,26 @@ import "./interfaces/ITWAccountFactory.sol"; // Smart wallet implementation import "./TWDynamicAccount.sol"; +// $$\ $$\ $$\ $$\ $$\ +// $$ | $$ | \__| $$ | $$ | +// $$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\ +// \_$$ _| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\ +// $$ | $$ | $$ |$$ |$$ | \__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ | +// $$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ | +// \$$$$ |$$ | $$ |$$ |$$ | \$$$$$$$ |\$$$$$\$$$$ |\$$$$$$$\ $$$$$$$ | +// \____/ \__| \__|\__|\__| \_______| \_____\____/ \_______|\_______/ + +/*/////////////////////////////////////////////////////////////// + Storage layout +//////////////////////////////////////////////////////////////*/ + library TWDynamicAccountFactoryStorage { bytes32 internal constant DYNAMIC_ACCOUNT_FACTORY_STORAGE_POSITION = keccak256("tw.dynamic.account.factory.storage"); struct Data { - mapping(address => EnumerableSet.AddressSet) accountsOfSigner; + TWStringSet.Set allAccounts; + mapping(address => TWStringSet.Set) accountsOfSigner; } function factoryStorage() internal pure returns (Data storage dynamicAccountFactoryData) { @@ -29,53 +43,93 @@ library TWDynamicAccountFactoryStorage { } contract TWDynamicAccountFactory is ITWAccountFactory, Multicall { - using EnumerableSet for EnumerableSet.AddressSet; + using TWStringSet for TWStringSet.Set; + + /*/////////////////////////////////////////////////////////////// + State + //////////////////////////////////////////////////////////////*/ TWDynamicAccount private immutable _accountImplementation; + /*/////////////////////////////////////////////////////////////// + Constructor + //////////////////////////////////////////////////////////////*/ + constructor(IEntryPoint _entrypoint) { _accountImplementation = new TWDynamicAccount(_entrypoint); } - /// @notice Returns the implementation of the Account. - function accountImplementation() external view override returns (address) { - return address(_accountImplementation); - } + /*/////////////////////////////////////////////////////////////// + External functions + //////////////////////////////////////////////////////////////*/ /// @notice Deploys a new Account with the given admin and accountId used as salt. - function createAccount(address _admin, bytes32 _accountId) external returns (address) { + function createAccount(address _admin, string memory _accountId) external returns (address) { address impl = address(_accountImplementation); - address account = Clones.predictDeterministicAddress(impl, _accountId); + bytes32 salt = keccak256(abi.encode(_accountId)); + address account = Clones.predictDeterministicAddress(impl, salt); if (account.code.length > 0) { return account; } - account = Clones.cloneDeterministic(impl, _accountId); + account = Clones.cloneDeterministic(impl, salt); TWAccount(payable(account)).initialize(_admin); - _addAccount(_admin, account); + _setupAccount(_admin, _accountId); emit AccountCreated(account, _admin, _accountId); return account; } + /*/////////////////////////////////////////////////////////////// + View functions + //////////////////////////////////////////////////////////////*/ + + /// @notice Returns the implementation of the Account. + function accountImplementation() external view override returns (address) { + return address(_accountImplementation); + } + /// @notice Returns the address of an Account that would be deployed with the given accountId as salt. - function getAddress(bytes32 _accountId) external view returns (address) { - return Clones.predictDeterministicAddress(address(_accountImplementation), _accountId); + function getAddress(string memory _accountId) public view returns (address) { + bytes32 salt = keccak256(abi.encode(_accountId)); + return Clones.predictDeterministicAddress(address(_accountImplementation), salt); } /// @notice Returns the list of accounts created by a signer. - function getAccountsOfSigner(address _signer) external view returns (address[] memory) { + function getAccountsOfSigner(address _signer) external view returns (AccountInfo[] memory) { + TWDynamicAccountFactoryStorage.Data storage data = TWDynamicAccountFactoryStorage.factoryStorage(); + return _formatAccounts(data.accountsOfSigner[_signer].values()); + } + + /// @notice Returns the list of all accounts. + function getAllAccounts() external view returns (AccountInfo[] memory accounts) { TWDynamicAccountFactoryStorage.Data storage data = TWDynamicAccountFactoryStorage.factoryStorage(); - return data.accountsOfSigner[_signer].values(); + return _formatAccounts(data.allAccounts.values()); + } + + /*/////////////////////////////////////////////////////////////// + Internal functions + //////////////////////////////////////////////////////////////*/ + + /// @dev Formats a list of accountIds to a list of `AccountInfo` (account id + account address). + function _formatAccounts(string[] memory _accountIds) internal view returns (AccountInfo[] memory accounts) { + uint256 len = _accountIds.length; + accounts = new AccountInfo[](len); + for (uint256 i = 0; i < len; i += 1) { + string memory accountId = _accountIds[i]; + address account = getAddress(accountId); + accounts[i] = AccountInfo(accountId, account); + } } /// @dev Adds an account to the list of accounts created by a signer. - function _addAccount(address _signer, address _account) internal { + function _setupAccount(address _signer, string memory _accountId) internal { TWDynamicAccountFactoryStorage.Data storage data = TWDynamicAccountFactoryStorage.factoryStorage(); - data.accountsOfSigner[_signer].add(_account); + data.allAccounts.add(_accountId); + data.accountsOfSigner[_signer].add(_accountId); } } diff --git a/contracts/smart-wallet/interfaces/ITWAccountFactory.sol b/contracts/smart-wallet/interfaces/ITWAccountFactory.sol index 42a5b0289..8da8915d4 100644 --- a/contracts/smart-wallet/interfaces/ITWAccountFactory.sol +++ b/contracts/smart-wallet/interfaces/ITWAccountFactory.sol @@ -2,14 +2,43 @@ pragma solidity ^0.8.12; interface ITWAccountFactory { - event AccountCreated(address indexed account, address indexed accountAdmin, bytes32 indexed accountId); + /*/////////////////////////////////////////////////////////////// + Structs + //////////////////////////////////////////////////////////////*/ - /// @notice Returns the address of the Account implementation. - function accountImplementation() external view returns (address); + /// @notice Smart account details: address and ID (used as salt). + struct AccountInfo { + string id; + address account; + } + + /*/////////////////////////////////////////////////////////////// + Events + //////////////////////////////////////////////////////////////*/ + + /// @notice Emitted when a new Account is created. + event AccountCreated(address indexed account, address indexed accountAdmin, string indexed accountId); + + /*/////////////////////////////////////////////////////////////// + Extension Functions + //////////////////////////////////////////////////////////////*/ /// @notice Deploys a new Account with the given admin and accountId used as salt. - function createAccount(address admin, bytes32 accountId) external returns (address account); + function createAccount(address admin, string memory accountId) external returns (address account); + + /*/////////////////////////////////////////////////////////////// + View Functions + //////////////////////////////////////////////////////////////*/ + + /// @notice Returns the address of the Account implementation. + function accountImplementation() external view returns (address); /// @notice Returns the address of an Account that would be deployed with the given accountId as salt. - function getAddress(bytes32 accountId) external view returns (address); + function getAddress(string memory accountId) external view returns (address); + + /// @notice Returns the list of accounts created by a signer. + function getAccountsOfSigner(address _signer) external view returns (AccountInfo[] memory); + + /// @notice Returns the list of all accounts. + function getAllAccounts() external view returns (AccountInfo[] memory); } diff --git a/src/test/smart-wallet/TWAccountBenchmark.t.sol b/src/test/smart-wallet/TWAccountBenchmark.t.sol index 96c25ab44..168b25276 100644 --- a/src/test/smart-wallet/TWAccountBenchmark.t.sol +++ b/src/test/smart-wallet/TWAccountBenchmark.t.sol @@ -1,115 +1,115 @@ -// SPDX-License-Identifier: Apache-2.0 -pragma solidity ^0.8.0; +// // SPDX-License-Identifier: Apache-2.0 +// pragma solidity ^0.8.0; -import "../utils/BaseTest.sol"; - -// Entrypoint -import { IEntryPoint } from "contracts/smart-wallet/interfaces/IEntrypoint.sol"; -import { EntryPoint } from "contracts/smart-wallet/utils/Entrypoint.sol"; - -import { UserOperation } from "contracts/smart-wallet/utils/UserOperation.sol"; +// import "../utils/BaseTest.sol"; + +// // Entrypoint +// import { IEntryPoint } from "contracts/smart-wallet/interfaces/IEntrypoint.sol"; +// import { EntryPoint } from "contracts/smart-wallet/utils/Entrypoint.sol"; + +// import { UserOperation } from "contracts/smart-wallet/utils/UserOperation.sol"; -import { TWAccountFactory } from "contracts/smart-wallet/TWAccountFactory.sol"; - -contract Number { - uint256 public num; - - function setNum(uint256 _num) public { - num = _num; - } -} - -contract TWAccountBenchmarkTest is BaseTest { - event SignerAddr(address signer); - - // Contracts - address payable private entrypoint; - TWAccountFactory private twAccountFactory; - - // Test params - uint256 private signerPrivateKey = 100; - address private walletSigner; - address private sender = 0xB587D47Db9d58f9a49f367D260690fdE38A3D087; - address payable private beneficiary = payable(address(0x45654)); - bytes private userOpSignature; - - // Test UserOps - UserOperation[] private userOpCreateAccountOnly; - - event TestMsgHash(bytes32 msgHash); - event TestOpHash(bytes32 opHash); - - function setUp() public override { - super.setUp(); - - // Set wallet signer. - walletSigner = vm.addr(signerPrivateKey); - - // deploy Entrypoint - entrypoint = payable(address(new EntryPoint())); - // deploy account factory - twAccountFactory = new TWAccountFactory(IEntryPoint(entrypoint)); - - // Get user op fields - bytes memory subCallData = abi.encodeWithSignature("setNum(uint256)", 5); - bytes memory callData = abi.encodeWithSignature( - "execute(address,uint256,bytes)", - address(new Number()), - 0, - subCallData - ); - - // build UserOp - bytes memory initCallData = abi.encodeWithSignature( - "createAccount(address,bytes32)", - walletSigner, - keccak256("random-salt") - ); - - UserOperation memory op = UserOperation({ - sender: sender, - nonce: 0, - initCode: abi.encodePacked(abi.encodePacked(address(twAccountFactory)), initCallData), - callData: callData, - callGasLimit: 500_000, - verificationGasLimit: 500_000, - preVerificationGas: 500_000, - maxFeePerGas: 0, - maxPriorityFeePerGas: 0, - paymasterAndData: bytes(""), - signature: bytes("") - }); - - // Sign UserOp - - bytes32 opHash = EntryPoint(entrypoint).getUserOpHash(op); - bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash); - - emit TestOpHash(opHash); - emit TestMsgHash(msgHash); - - (uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPrivateKey, msgHash); - userOpSignature = abi.encodePacked(r, s, v); - - address recoveredSigner = ECDSA.recover(msgHash, v, r, s); - emit SignerAddr(recoveredSigner); - - op.signature = userOpSignature; - - // Store user op - userOpCreateAccountOnly.push(op); - - // userOpCreateAccountOnly = new UserOperation( - // address(twAccountFactory), - // abi.encodeWithSignature("createAccount(address,bytes32)", address(0x123), keccak256("salt")) - // ); - } - - function test_benchmark_createAccount() public { - twAccountFactory.createAccount(address(0x456), keccak256("salt")); - } - - // function test_benchmark_createAccountWithUserOp() public { - // EntryPoint(entrypoint).handleOps(userOpCreateAccountOnly, beneficiary); - // } -} +// import { TWAccountFactory } from "contracts/smart-wallet/TWAccountFactory.sol"; + +// contract Number { +// uint256 public num; + +// function setNum(uint256 _num) public { +// num = _num; +// } +// } + +// contract TWAccountBenchmarkTest is BaseTest { +// event SignerAddr(address signer); + +// // Contracts +// address payable private entrypoint; +// TWAccountFactory private twAccountFactory; + +// // Test params +// uint256 private signerPrivateKey = 100; +// address private walletSigner; +// address private sender = 0xB587D47Db9d58f9a49f367D260690fdE38A3D087; +// address payable private beneficiary = payable(address(0x45654)); +// bytes private userOpSignature; + +// // Test UserOps +// UserOperation[] private userOpCreateAccountOnly; + +// event TestMsgHash(bytes32 msgHash); +// event TestOpHash(bytes32 opHash); + +// function setUp() public override { +// super.setUp(); + +// // Set wallet signer. +// walletSigner = vm.addr(signerPrivateKey); + +// // deploy Entrypoint +// entrypoint = payable(address(new EntryPoint())); +// // deploy account factory +// twAccountFactory = new TWAccountFactory(IEntryPoint(entrypoint)); + +// // Get user op fields +// bytes memory subCallData = abi.encodeWithSignature("setNum(uint256)", 5); +// bytes memory callData = abi.encodeWithSignature( +// "execute(address,uint256,bytes)", +// address(new Number()), +// 0, +// subCallData +// ); + +// // build UserOp +// bytes memory initCallData = abi.encodeWithSignature( +// "createAccount(address,bytes32)", +// walletSigner, +// keccak256("random-salt") +// ); + +// UserOperation memory op = UserOperation({ +// sender: sender, +// nonce: 0, +// initCode: abi.encodePacked(abi.encodePacked(address(twAccountFactory)), initCallData), +// callData: callData, +// callGasLimit: 500_000, +// verificationGasLimit: 500_000, +// preVerificationGas: 500_000, +// maxFeePerGas: 0, +// maxPriorityFeePerGas: 0, +// paymasterAndData: bytes(""), +// signature: bytes("") +// }); + +// // Sign UserOp + +// bytes32 opHash = EntryPoint(entrypoint).getUserOpHash(op); +// bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash); + +// emit TestOpHash(opHash); +// emit TestMsgHash(msgHash); + +// (uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPrivateKey, msgHash); +// userOpSignature = abi.encodePacked(r, s, v); + +// address recoveredSigner = ECDSA.recover(msgHash, v, r, s); +// emit SignerAddr(recoveredSigner); + +// op.signature = userOpSignature; + +// // Store user op +// userOpCreateAccountOnly.push(op); + +// // userOpCreateAccountOnly = new UserOperation( +// // address(twAccountFactory), +// // abi.encodeWithSignature("createAccount(address,bytes32)", address(0x123), keccak256("salt")) +// // ); +// } + +// function test_benchmark_createAccount() public { +// twAccountFactory.createAccount(address(0x456), keccak256("salt")); +// } + +// // function test_benchmark_createAccountWithUserOp() public { +// // EntryPoint(entrypoint).handleOps(userOpCreateAccountOnly, beneficiary); +// // } +// } From c34998ddb142dfc05875b3715538781b8d9d8035 Mon Sep 17 00:00:00 2001 From: Krishang Date: Wed, 19 Apr 2023 02:09:55 -0400 Subject: [PATCH 7/7] resolve lint error --- src/test/smart-wallet/TWAccountBenchmark.t.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/smart-wallet/TWAccountBenchmark.t.sol b/src/test/smart-wallet/TWAccountBenchmark.t.sol index 168b25276..f52bdcf20 100644 --- a/src/test/smart-wallet/TWAccountBenchmark.t.sol +++ b/src/test/smart-wallet/TWAccountBenchmark.t.sol @@ -1,5 +1,5 @@ -// // SPDX-License-Identifier: Apache-2.0 -// pragma solidity ^0.8.0; +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.0; // import "../utils/BaseTest.sol";