Transaction Hash:
Block:
19522468 at Mar-27-2024 01:34:35 AM +UTC
Transaction Fee:
0.003379700444930939 ETH
$8.18
Gas Used:
132,733 Gas / 25.462397783 Gwei
Emitted Events:
205 |
ElementGenesis.Approval( owner=0x71bcc0d2e3ade7bd8041d518125b29510fefbf79, approved=0x00000000...000000000, tokenId=785 )
|
206 |
ElementGenesis.Transfer( from=0x71bcc0d2e3ade7bd8041d518125b29510fefbf79, to=[Sender] 0x34159a18d87930ecc87916264f18fb785260ccea, tokenId=785 )
|
207 |
ElementEx.0x9c248aa1a265aa616f707b979d57f4529bb63a4fc34dc7fc61fdddc18410f74e( 0x9c248aa1a265aa616f707b979d57f4529bb63a4fc34dc7fc61fdddc18410f74e, 33d36fcb6e1336f040793cab24cb5729715e52c5fad84c3164c72b3702579ec6, 00000000000000000000000071bcc0d2e3ade7bd8041d518125b29510fefbf79, 00000000000000000000000034159a18d87930ecc87916264f18fb785260ccea, 0000000000000000000000000000000000000000000000000000000000000010, 000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee, 0000000000000000000000000000000000000000000000000482a1c730008000, 0000000000000000000000000000000000000000000000000000000000000120, 00000000000000000000000067405cadb820c2d89b8d6da3e42ef6a39089c831, 0000000000000000000000000000000000000000000000000000000000000311, 0000000000000000000000000000000000000000000000000000000000000001, 000000000000000000000000482b2150889bfc8cad0548ebb006cfd643647b69, 000000000000000000000000000000000000000000000000001cdda4faccd000 )
|
208 |
GnosisSafeProxy.0x3d0ce9bfc3ed7d6862dbb28b2dea94561fe714a1b4d019aa8af39730d1ad7c3d( 0x3d0ce9bfc3ed7d6862dbb28b2dea94561fe714a1b4d019aa8af39730d1ad7c3d, 0x00000000000000000000000020f780a973856b93f63670377900c1d2a50a77c4, 000000000000000000000000000000000000000000000000001cdda4faccd000 )
|
Account State Difference:
Address | Before | After | State Difference | ||
---|---|---|---|---|---|
0x20F780A9...2a50a77c4 | (Element: ElementEx) | ||||
0x34159a18...85260CCea |
0.413161916999899142 Eth
Nonce: 392
|
0.084782216554968203 Eth
Nonce: 393
| 0.328379700444930939 | ||
0x482b2150...643647B69 | 4.536750578592394283 Eth | 4.544875578592394283 Eth | 0.008125 | ||
0x67405cad...39089C831 | |||||
0x71bcc0d2...10FEfbF79 | 0.012324794573249318 Eth | 0.329199794573249318 Eth | 0.316875 | ||
0xf1568963...B3DF818e1
Miner
| (MEV Builder: 0xf156...8e1) | 5.687063686874574256 Eth | 5.687065966398862137 Eth | 0.000002279524287881 |
Execution Trace
ETH 0.325
ElementEx.a4d73041( )
ETH 0.325
BatchSignedERC721OrdersFeature.fillBatchSignedERC721Order( [{name:data1, type:uint256, order:1, indexed:false, value:25889268860157347192399996164640633845010848577870041894076281, valueString:25889268860157347192399996164640633845010848577870041894076281}, {name:data2, type:uint256, order:2, indexed:false, value:23558435453463634154967270117456378580530233056698688291531363440499448803054, valueString:23558435453463634154967270117456378580530233056698688291531363440499448803054}, {name:data3, type:uint256, order:3, indexed:false, value:90676511082719013088553214826266303866795021777801390446660337072912440754176, valueString:90676511082719013088553214826266303866795021777801390446660337072912440754176}, {name:r, type:bytes32, order:4, indexed:false, value:B969BD4DB97AEA27F4031D97BFF73A1AA9CE2953A71E9365840CF6080DEC76AB, valueString:B969BD4DB97AEA27F4031D97BFF73A1AA9CE2953A71E9365840CF6080DEC76AB}, {name:s, type:bytes32, order:5, indexed:false, value:43C2D2F3276FE2946F8A3B5394E1A13C397EA83EE57FA0AD5A2EAD10B163C92C, valueString:43C2D2F3276FE2946F8A3B5394E1A13C397EA83EE57FA0AD5A2EAD10B163C92C}], collections=0x00000000000000000000000067405CADB820C2D89B8D6DA3E42EF6A39089C8310001010000000000000000FA482B2150889BFC8CAD0548EBB006CFD643647B69000000000482A1C7300080000000000000000000000000000000000000000311 )
-
Null: 0x000...001.33d36fcb( )
ElementGenesis.transferFrom( from=0x71bcc0d2E3AdE7bD8041d518125B29510FEfbF79, to=0x34159a18d87930eCC87916264F18FB785260CCea, tokenId=785 )
-
OperatorFilterRegistry.isOperatorAllowed( registrant=0x67405cadb820c2D89B8d6dA3E42ef6A39089C831, operator=0x20F780A973856B93f63670377900C1d2a50a77c4 ) => ( True )
-
ETH 0.008125
GnosisSafeProxy.CALL( )
- ETH 0.008125
GnosisSafe.DELEGATECALL( )
- ETH 0.008125
- ETH 0.316875
0x71bcc0d2e3ade7bd8041d518125b29510fefbf79.CALL( )
-
fillBatchSignedERC721Order[BatchSignedERC721OrdersFeature (ln:143)]
_validateOrder[BatchSignedERC721OrdersFeature (ln:150)]
_getEIP712Hash[BatchSignedERC721OrdersFeature (ln:1074)]
_getStructHash[BatchSignedERC721OrdersFeature (ln:1074)]
_storeCollectionsHashToMemory[BatchSignedERC721OrdersFeature (ln:1180)]
ecrecover[BatchSignedERC721OrdersFeature (ln:1086)]
_fillBatchSignedERC721Order[BatchSignedERC721OrdersFeature (ln:163)]
File 1 of 6: ElementEx
File 2 of 6: ElementGenesis
File 3 of 6: GnosisSafeProxy
File 4 of 6: BatchSignedERC721OrdersFeature
File 5 of 6: OperatorFilterRegistry
File 6 of 6: GnosisSafe
// SPDX-License-Identifier: Apache-2.0 /* Modifications Copyright 2022 Element.Market Copyright 2020 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.13; import "./migrations/LibBootstrap.sol"; import "./features/BootstrapFeature.sol"; import "./storage/LibProxyStorage.sol"; /// @dev An extensible proxy contract that serves as a universal entry point for /// interacting with the 0x protocol. contract ElementEx { /// @dev Construct this contract and register the `BootstrapFeature` feature. /// After constructing this contract, `bootstrap()` should be called /// by `bootstrap()` to seed the initial feature set. /// @param bootstrapper Who can call `bootstrap()`. constructor(address bootstrapper) { // Temporarily create and register the bootstrap feature. // It will deregister itself after `bootstrap()` has been called. BootstrapFeature bootstrap = new BootstrapFeature(bootstrapper); LibProxyStorage.getStorage().impls[bootstrap.bootstrap.selector] = address(bootstrap); } // solhint-disable state-visibility /// @dev Forwards calls to the appropriate implementation contract. fallback() external payable { bytes memory b = msg.data; bytes4 selector; assembly { selector := mload(add(b, 32)) // Solidity does not require us to clean the trailing bytes. // We do it anyway selector := and(selector, 0xFFFFFFFF00000000000000000000000000000000000000000000000000000000) } address impl = LibProxyStorage.getStorage().impls[selector]; if (impl == address(0)) { revert("METHOD_NOT_IMPLEMENTED"); } (bool success, bytes memory resultData) = impl.delegatecall(msg.data); if (!success) { _revertWithData(resultData); } _returnWithData(resultData); } /// @dev Fallback for just receiving ether. receive() external payable {} // solhint-enable state-visibility /// @dev Get the implementation contract of a registered function. /// @param selector The function selector. /// @return impl The implementation contract address. function getFunctionImplementation(bytes4 selector) public view returns (address impl) { return LibProxyStorage.getStorage().impls[selector]; } /// @dev Revert with arbitrary bytes. /// @param data Revert data. function _revertWithData(bytes memory data) private pure { assembly { revert(add(data, 32), mload(data)) } } /// @dev Return with arbitrary bytes. /// @param data Return data. function _returnWithData(bytes memory data) private pure { assembly { return(add(data, 32), mload(data)) } } } // SPDX-License-Identifier: Apache-2.0 /* Modifications Copyright 2022 Element.Market Copyright 2020 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.13; library LibBootstrap { /// @dev Magic bytes returned by the bootstrapper to indicate success. /// This is `keccack('BOOTSTRAP_SUCCESS')`. bytes4 internal constant BOOTSTRAP_SUCCESS = 0xd150751b; /// @dev Perform a delegatecall and ensure it returns the magic bytes. /// @param target The call target. /// @param data The call data. function delegatecallBootstrapFunction(address target, bytes memory data) internal { (bool success, bytes memory resultData) = target.delegatecall(data); if (!success || resultData.length != 32 || abi.decode(resultData, (bytes4)) != BOOTSTRAP_SUCCESS) { revert("BOOTSTRAP_CALL_FAILED"); } } } // SPDX-License-Identifier: Apache-2.0 /* Modifications Copyright 2022 Element.Market Copyright 2020 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.13; import "../migrations/LibBootstrap.sol"; import "../storage/LibProxyStorage.sol"; import "./interfaces/IBootstrapFeature.sol"; /// @dev Detachable `bootstrap()` feature. contract BootstrapFeature is IBootstrapFeature { // solhint-disable state-visibility,indent /// @dev The ZeroEx contract. /// This has to be immutable to persist across delegatecalls. address immutable private _deployer; /// @dev The implementation address of this contract. /// This has to be immutable to persist across delegatecalls. address immutable private _implementation; /// @dev The deployer. /// This has to be immutable to persist across delegatecalls. address immutable private _bootstrapCaller; // solhint-enable state-visibility,indent /// @dev Construct this contract and set the bootstrap migration contract. /// After constructing this contract, `bootstrap()` should be called /// to seed the initial feature set. /// @param bootstrapCaller The allowed caller of `bootstrap()`. constructor(address bootstrapCaller) { _deployer = msg.sender; _implementation = address(this); _bootstrapCaller = bootstrapCaller; } /// @dev Bootstrap the initial feature set of this contract by delegatecalling /// into `target`. Before exiting the `bootstrap()` function will /// deregister itself from the proxy to prevent being called again. /// @param target The bootstrapper contract address. /// @param callData The call data to execute on `target`. function bootstrap(address target, bytes calldata callData) external override { // Only the bootstrap caller can call this function. if (msg.sender != _bootstrapCaller) { revert("INVALID_BOOTSTRAP_CALLER"); } // Deregister. LibProxyStorage.getStorage().impls[this.bootstrap.selector] = address(0); // Self-destruct. BootstrapFeature(_implementation).die(); // Call the bootstrapper. LibBootstrap.delegatecallBootstrapFunction(target, callData); } /// @dev Self-destructs this contract. /// Can only be called by the deployer. function die() external { require(address(this) == _implementation); if (msg.sender != _deployer) { revert("INVALID_DIE_CALLER"); } selfdestruct(payable(msg.sender)); } } // SPDX-License-Identifier: Apache-2.0 /* Modifications Copyright 2022 Element.Market Copyright 2020 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.13; import "./LibStorage.sol"; /// @dev Storage helpers for the proxy contract. library LibProxyStorage { /// @dev Storage bucket for proxy contract. struct Storage { // Mapping of function selector -> function implementation mapping(bytes4 => address) impls; } /// @dev Get the storage bucket for this contract. function getStorage() internal pure returns (Storage storage stor) { uint256 storageSlot = LibStorage.STORAGE_ID_PROXY; // Dip into assembly to change the slot pointed to by the local // variable `stor`. // See https://solidity.readthedocs.io/en/v0.6.8/assembly.html?highlight=slot#access-to-external-variables-functions-and-libraries assembly { stor.slot := storageSlot } } } // SPDX-License-Identifier: Apache-2.0 /* Modifications Copyright 2022 Element.Market Copyright 2020 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.13; /// @dev Detachable `bootstrap()` feature. interface IBootstrapFeature { /// @dev Bootstrap the initial feature set of this contract by delegatecalling /// into `target`. Before exiting the `bootstrap()` function will /// deregister itself from the proxy to prevent being called again. /// @param target The bootstrapper contract address. /// @param callData The call data to execute on `target`. function bootstrap(address target, bytes calldata callData) external; } // SPDX-License-Identifier: Apache-2.0 /* Modifications Copyright 2022 Element.Market Copyright 2020 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.13; /// @dev Common storage helpers library LibStorage { /// @dev What to bit-shift a storage ID by to get its slot. /// This gives us a maximum of 2**128 inline fields in each bucket. uint256 constant STORAGE_ID_PROXY = 1 << 128; uint256 constant STORAGE_ID_SIMPLE_FUNCTION_REGISTRY = 2 << 128; uint256 constant STORAGE_ID_OWNABLE = 3 << 128; uint256 constant STORAGE_ID_COMMON_NFT_ORDERS = 4 << 128; uint256 constant STORAGE_ID_ERC721_ORDERS = 5 << 128; uint256 constant STORAGE_ID_ERC1155_ORDERS = 6 << 128; }
File 2 of 6: ElementGenesis
// SPDX-License-Identifier: MIT pragma solidity ^0.8.17; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; import "operator-filter-registry/src/DefaultOperatorFilterer.sol"; contract ElementGenesis is ERC721("Element Genesis", "ELEG"), Ownable, DefaultOperatorFilterer { address public immutable MINTER; string private baseURI; uint256 public constant MAX_SUPPLY = 2500; uint256 public maxMintAmountPerUser; mapping(address => uint256) public userMinted; // 140bits(unused) + 100bits(rareTokenIdFlags) + 16bits(rareTokensSupply) uint256 private rareTokens; uint256 private otherTokensSupply; uint256 public constant minLockingPeriod = 7 days; uint256 public constant maxLockingPeriod = 365 days; event Locked( uint256 indexed tokenId, address owner, uint256 startedAt, uint256 period ); struct Locking { uint64 startedAt; uint64 period; } // Mapping tokenId to Locking information mapping(uint256 => Locking) public lockingTokens; bool public lockingOpen; constructor(address minter) { MINTER = minter; maxMintAmountPerUser = 2; baseURI = ""; } function safeMint(address to, uint256 amount) external payable { unchecked { require(msg.sender == MINTER, "Illegal minter"); require(tx.origin == to, "Contract mint is not supported"); require(totalSupply() + amount <= MAX_SUPPLY, "Already sold out"); require(userMinted[to] + amount <= maxMintAmountPerUser, "Exceeded the maximum mint limit for this account"); userMinted[to] += amount; for (uint256 i; i < amount; i++) { _mint(to, _generateTokenId()); } } } function _generateTokenId() internal returns(uint256) { unchecked { uint256 otherSupply = otherTokensSupply; // 140bits(unused) + 100bits(rareTokenIdFlags) + 16bits(rareTokensSupply) uint256 rares = rareTokens; uint256 rareSupply = (rares & 0xffff); uint256 rareLeft = 100 - rareSupply; if (rareLeft == 0) { otherTokensSupply = otherSupply + 1; return 101 + otherSupply; } uint256 totalLeft = MAX_SUPPLY - rareSupply - otherSupply; uint256 index = uint256(keccak256(abi.encodePacked( blockhash(block.number - rareSupply - 1), block.coinbase, uint96(block.basefee), uint32(block.number), uint32(block.timestamp), uint32(gasleft()), uint32(totalLeft) ))) % totalLeft; if (index >= rareLeft) { otherTokensSupply = otherSupply + 1; return 101 + otherSupply; } uint256 tokenIdFlags = rares >> 16; uint256 j; for (uint256 i; i < 100; i++) { if (tokenIdFlags & (1 << i) == 0) { if (j == index) { // 140bits(unused) + 100bits(rareTokenIdFlags) + 16bits(rareTokensSupply) rareTokens = ((tokenIdFlags | (1 << i)) << 16) + (rareSupply + 1); return i + 1; } j++; } } revert("Mint error"); } } function totalSupply() public view returns(uint256) { unchecked { return (rareTokens & 0xffff) + otherTokensSupply; } } function withdrawETH(address recipient) external onlyOwner { require(recipient != address(0), "Recipient error"); require(address(this).balance > 0, "The balance is zero"); (bool success, ) = recipient.call{value: address(this).balance}(""); require(success); } function setMaxMintAmountPerUser(uint256 amount) external onlyOwner { maxMintAmountPerUser = amount; } function setBaseURI(string memory uri) external onlyOwner { baseURI = uri; } function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { uint256 length = bytes(baseURI).length; if (length > 0 && bytes(baseURI)[length - 1] == 0x2f) { return super.tokenURI(tokenId); } require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); return baseURI; } function _baseURI() internal override view returns (string memory) { return baseURI; } function setLockingOpen(bool open) external onlyOwner { lockingOpen = open; } function lockTokens(uint256[] calldata tokenIds, uint256[] calldata periods) external { require(lockingOpen, "Locking closed"); require(tokenIds.length == periods.length, "Array length mismatch"); for (uint256 i; i < tokenIds.length; ) { _lockToken(tokenIds[i], periods[i]); unchecked { i++; } } } function _lockToken(uint256 tokenId, uint256 period) internal { require(period >= minLockingPeriod, "Locking period should gte 7 days"); require(period <= maxLockingPeriod, "Locking period should lte 365 days"); require(ERC721.ownerOf(tokenId) == msg.sender, "Only owner can lock token"); if (isLocking(tokenId)) { revert("The token is already locked"); } lockingTokens[tokenId].startedAt = uint64(block.timestamp); lockingTokens[tokenId].period = uint64(period); emit Locked(tokenId, msg.sender, block.timestamp, period); } function isLocking(uint256 tokenId) public view returns(bool) { unchecked { Locking memory info = lockingTokens[tokenId]; return block.timestamp < (info.startedAt + info.period); } } function _transfer( address from, address to, uint256 tokenId ) internal virtual override { if (isLocking(tokenId)) { revert("Token is locking"); } super._transfer(from, to, tokenId); } function setApprovalForAll(address operator, bool approved) public override onlyAllowedOperatorApproval(operator) { super.setApprovalForAll(operator, approved); } function approve(address operator, uint256 tokenId) public override onlyAllowedOperatorApproval(operator) { super.approve(operator, tokenId); } function transferFrom(address from, address to, uint256 tokenId) public override onlyAllowedOperator(from) { super.transferFrom(from, to, tokenId); } function safeTransferFrom(address from, address to, uint256 tokenId) public override onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId); } function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public override onlyAllowedOperator(from) { super.safeTransferFrom(from, to, tokenId, data); } } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (token/ERC721/ERC721.sol) pragma solidity ^0.8.0; import "./IERC721.sol"; import "./IERC721Receiver.sol"; import "./extensions/IERC721Metadata.sol"; import "../../utils/Address.sol"; import "../../utils/Context.sol"; import "../../utils/Strings.sol"; import "../../utils/introspection/ERC165.sol"; /** * @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including * the Metadata extension, but not including the Enumerable extension, which is available separately as * {ERC721Enumerable}. */ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata { using Address for address; using Strings for uint256; // Token name string private _name; // Token symbol string private _symbol; // Mapping from token ID to owner address mapping(uint256 => address) private _owners; // Mapping owner address to token count mapping(address => uint256) private _balances; // Mapping from token ID to approved address mapping(uint256 => address) private _tokenApprovals; // Mapping from owner to operator approvals mapping(address => mapping(address => bool)) private _operatorApprovals; /** * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection. */ constructor(string memory name_, string memory symbol_) { _name = name_; _symbol = symbol_; } /** * @dev See {IERC165-supportsInterface}. */ function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) { return interfaceId == type(IERC721).interfaceId || interfaceId == type(IERC721Metadata).interfaceId || super.supportsInterface(interfaceId); } /** * @dev See {IERC721-balanceOf}. */ function balanceOf(address owner) public view virtual override returns (uint256) { require(owner != address(0), "ERC721: balance query for the zero address"); return _balances[owner]; } /** * @dev See {IERC721-ownerOf}. */ function ownerOf(uint256 tokenId) public view virtual override returns (address) { address owner = _owners[tokenId]; require(owner != address(0), "ERC721: owner query for nonexistent token"); return owner; } /** * @dev See {IERC721Metadata-name}. */ function name() public view virtual override returns (string memory) { return _name; } /** * @dev See {IERC721Metadata-symbol}. */ function symbol() public view virtual override returns (string memory) { return _symbol; } /** * @dev See {IERC721Metadata-tokenURI}. */ function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); string memory baseURI = _baseURI(); return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : ""; } /** * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each * token will be the concatenation of the `baseURI` and the `tokenId`. Empty * by default, can be overriden in child contracts. */ function _baseURI() internal view virtual returns (string memory) { return ""; } /** * @dev See {IERC721-approve}. */ function approve(address to, uint256 tokenId) public virtual override { address owner = ERC721.ownerOf(tokenId); require(to != owner, "ERC721: approval to current owner"); require( _msgSender() == owner || isApprovedForAll(owner, _msgSender()), "ERC721: approve caller is not owner nor approved for all" ); _approve(to, tokenId); } /** * @dev See {IERC721-getApproved}. */ function getApproved(uint256 tokenId) public view virtual override returns (address) { require(_exists(tokenId), "ERC721: approved query for nonexistent token"); return _tokenApprovals[tokenId]; } /** * @dev See {IERC721-setApprovalForAll}. */ function setApprovalForAll(address operator, bool approved) public virtual override { _setApprovalForAll(_msgSender(), operator, approved); } /** * @dev See {IERC721-isApprovedForAll}. */ function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) { return _operatorApprovals[owner][operator]; } /** * @dev See {IERC721-transferFrom}. */ function transferFrom( address from, address to, uint256 tokenId ) public virtual override { //solhint-disable-next-line max-line-length require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); _transfer(from, to, tokenId); } /** * @dev See {IERC721-safeTransferFrom}. */ function safeTransferFrom( address from, address to, uint256 tokenId ) public virtual override { safeTransferFrom(from, to, tokenId, ""); } /** * @dev See {IERC721-safeTransferFrom}. */ function safeTransferFrom( address from, address to, uint256 tokenId, bytes memory _data ) public virtual override { require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); _safeTransfer(from, to, tokenId, _data); } /** * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients * are aware of the ERC721 protocol to prevent tokens from being forever locked. * * `_data` is additional data, it has no specified format and it is sent in call to `to`. * * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g. * implement alternative mechanisms to perform token transfer, such as signature-based. * * Requirements: * * - `from` cannot be the zero address. * - `to` cannot be the zero address. * - `tokenId` token must exist and be owned by `from`. * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. * * Emits a {Transfer} event. */ function _safeTransfer( address from, address to, uint256 tokenId, bytes memory _data ) internal virtual { _transfer(from, to, tokenId); require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer"); } /** * @dev Returns whether `tokenId` exists. * * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}. * * Tokens start existing when they are minted (`_mint`), * and stop existing when they are burned (`_burn`). */ function _exists(uint256 tokenId) internal view virtual returns (bool) { return _owners[tokenId] != address(0); } /** * @dev Returns whether `spender` is allowed to manage `tokenId`. * * Requirements: * * - `tokenId` must exist. */ function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) { require(_exists(tokenId), "ERC721: operator query for nonexistent token"); address owner = ERC721.ownerOf(tokenId); return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender)); } /** * @dev Safely mints `tokenId` and transfers it to `to`. * * Requirements: * * - `tokenId` must not exist. * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. * * Emits a {Transfer} event. */ function _safeMint(address to, uint256 tokenId) internal virtual { _safeMint(to, tokenId, ""); } /** * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is * forwarded in {IERC721Receiver-onERC721Received} to contract recipients. */ function _safeMint( address to, uint256 tokenId, bytes memory _data ) internal virtual { _mint(to, tokenId); require( _checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer" ); } /** * @dev Mints `tokenId` and transfers it to `to`. * * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible * * Requirements: * * - `tokenId` must not exist. * - `to` cannot be the zero address. * * Emits a {Transfer} event. */ function _mint(address to, uint256 tokenId) internal virtual { require(to != address(0), "ERC721: mint to the zero address"); require(!_exists(tokenId), "ERC721: token already minted"); _beforeTokenTransfer(address(0), to, tokenId); _balances[to] += 1; _owners[tokenId] = to; emit Transfer(address(0), to, tokenId); } /** * @dev Destroys `tokenId`. * The approval is cleared when the token is burned. * * Requirements: * * - `tokenId` must exist. * * Emits a {Transfer} event. */ function _burn(uint256 tokenId) internal virtual { address owner = ERC721.ownerOf(tokenId); _beforeTokenTransfer(owner, address(0), tokenId); // Clear approvals _approve(address(0), tokenId); _balances[owner] -= 1; delete _owners[tokenId]; emit Transfer(owner, address(0), tokenId); } /** * @dev Transfers `tokenId` from `from` to `to`. * As opposed to {transferFrom}, this imposes no restrictions on msg.sender. * * Requirements: * * - `to` cannot be the zero address. * - `tokenId` token must be owned by `from`. * * Emits a {Transfer} event. */ function _transfer( address from, address to, uint256 tokenId ) internal virtual { require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); require(to != address(0), "ERC721: transfer to the zero address"); _beforeTokenTransfer(from, to, tokenId); // Clear approvals from the previous owner _approve(address(0), tokenId); _balances[from] -= 1; _balances[to] += 1; _owners[tokenId] = to; emit Transfer(from, to, tokenId); } /** * @dev Approve `to` to operate on `tokenId` * * Emits a {Approval} event. */ function _approve(address to, uint256 tokenId) internal virtual { _tokenApprovals[tokenId] = to; emit Approval(ERC721.ownerOf(tokenId), to, tokenId); } /** * @dev Approve `operator` to operate on all of `owner` tokens * * Emits a {ApprovalForAll} event. */ function _setApprovalForAll( address owner, address operator, bool approved ) internal virtual { require(owner != operator, "ERC721: approve to caller"); _operatorApprovals[owner][operator] = approved; emit ApprovalForAll(owner, operator, approved); } /** * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address. * The call is not executed if the target address is not a contract. * * @param from address representing the previous owner of the given token ID * @param to target address that will receive the tokens * @param tokenId uint256 ID of the token to be transferred * @param _data bytes optional data to send along with the call * @return bool whether the call correctly returned the expected magic value */ function _checkOnERC721Received( address from, address to, uint256 tokenId, bytes memory _data ) private returns (bool) { if (to.isContract()) { try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) { return retval == IERC721Receiver.onERC721Received.selector; } catch (bytes memory reason) { if (reason.length == 0) { revert("ERC721: transfer to non ERC721Receiver implementer"); } else { assembly { revert(add(32, reason), mload(reason)) } } } } else { return true; } } /** * @dev Hook that is called before any token transfer. This includes minting * and burning. * * Calling conditions: * * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be * transferred to `to`. * - When `from` is zero, `tokenId` will be minted for `to`. * - When `to` is zero, ``from``'s `tokenId` will be burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _beforeTokenTransfer( address from, address to, uint256 tokenId ) internal virtual {} } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (access/Ownable.sol) pragma solidity ^0.8.0; import "../utils/Context.sol"; /** * @dev Contract module which provides a basic access control mechanism, where * there is an account (an owner) that can be granted exclusive access to * specific functions. * * By default, the owner account will be the one that deploys the contract. This * can later be changed with {transferOwnership}. * * This module is used through inheritance. It will make available the modifier * `onlyOwner`, which can be applied to your functions to restrict their use to * the owner. */ abstract contract Ownable is Context { address private _owner; event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); /** * @dev Initializes the contract setting the deployer as the initial owner. */ constructor() { _transferOwnership(_msgSender()); } /** * @dev Returns the address of the current owner. */ function owner() public view virtual returns (address) { return _owner; } /** * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { require(owner() == _msgSender(), "Ownable: caller is not the owner"); _; } /** * @dev Leaves the contract without owner. It will not be possible to call * `onlyOwner` functions anymore. Can only be called by the current owner. * * NOTE: Renouncing ownership will leave the contract without an owner, * thereby removing any functionality that is only available to the owner. */ function renounceOwnership() public virtual onlyOwner { _transferOwnership(address(0)); } /** * @dev Transfers ownership of the contract to a new account (`newOwner`). * Can only be called by the current owner. */ function transferOwnership(address newOwner) public virtual onlyOwner { require(newOwner != address(0), "Ownable: new owner is the zero address"); _transferOwnership(newOwner); } /** * @dev Transfers ownership of the contract to a new account (`newOwner`). * Internal function without access restriction. */ function _transferOwnership(address newOwner) internal virtual { address oldOwner = _owner; _owner = newOwner; emit OwnershipTransferred(oldOwner, newOwner); } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; import {OperatorFilterer} from "./OperatorFilterer.sol"; import {CANONICAL_CORI_SUBSCRIPTION} from "./lib/Constants.sol"; /** * @title DefaultOperatorFilterer * @notice Inherits from OperatorFilterer and automatically subscribes to the default OpenSea subscription. * @dev Please note that if your token contract does not provide an owner with EIP-173, it must provide * administration methods on the contract itself to interact with the registry otherwise the subscription * will be locked to the options set during construction. */ abstract contract DefaultOperatorFilterer is OperatorFilterer { /// @dev The constructor that is called when the contract is being deployed. constructor() OperatorFilterer(CANONICAL_CORI_SUBSCRIPTION, true) {} } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (token/ERC721/IERC721.sol) pragma solidity ^0.8.0; import "../../utils/introspection/IERC165.sol"; /** * @dev Required interface of an ERC721 compliant contract. */ interface IERC721 is IERC165 { /** * @dev Emitted when `tokenId` token is transferred from `from` to `to`. */ event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); /** * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token. */ event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); /** * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets. */ event ApprovalForAll(address indexed owner, address indexed operator, bool approved); /** * @dev Returns the number of tokens in ``owner``'s account. */ function balanceOf(address owner) external view returns (uint256 balance); /** * @dev Returns the owner of the `tokenId` token. * * Requirements: * * - `tokenId` must exist. */ function ownerOf(uint256 tokenId) external view returns (address owner); /** * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients * are aware of the ERC721 protocol to prevent tokens from being forever locked. * * Requirements: * * - `from` cannot be the zero address. * - `to` cannot be the zero address. * - `tokenId` token must exist and be owned by `from`. * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}. * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. * * Emits a {Transfer} event. */ function safeTransferFrom( address from, address to, uint256 tokenId ) external; /** * @dev Transfers `tokenId` token from `from` to `to`. * * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible. * * Requirements: * * - `from` cannot be the zero address. * - `to` cannot be the zero address. * - `tokenId` token must be owned by `from`. * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}. * * Emits a {Transfer} event. */ function transferFrom( address from, address to, uint256 tokenId ) external; /** * @dev Gives permission to `to` to transfer `tokenId` token to another account. * The approval is cleared when the token is transferred. * * Only a single account can be approved at a time, so approving the zero address clears previous approvals. * * Requirements: * * - The caller must own the token or be an approved operator. * - `tokenId` must exist. * * Emits an {Approval} event. */ function approve(address to, uint256 tokenId) external; /** * @dev Returns the account approved for `tokenId` token. * * Requirements: * * - `tokenId` must exist. */ function getApproved(uint256 tokenId) external view returns (address operator); /** * @dev Approve or remove `operator` as an operator for the caller. * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller. * * Requirements: * * - The `operator` cannot be the caller. * * Emits an {ApprovalForAll} event. */ function setApprovalForAll(address operator, bool _approved) external; /** * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`. * * See {setApprovalForAll} */ function isApprovedForAll(address owner, address operator) external view returns (bool); /** * @dev Safely transfers `tokenId` token from `from` to `to`. * * Requirements: * * - `from` cannot be the zero address. * - `to` cannot be the zero address. * - `tokenId` token must exist and be owned by `from`. * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}. * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. * * Emits a {Transfer} event. */ function safeTransferFrom( address from, address to, uint256 tokenId, bytes calldata data ) external; } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (token/ERC721/IERC721Receiver.sol) pragma solidity ^0.8.0; /** * @title ERC721 token receiver interface * @dev Interface for any contract that wants to support safeTransfers * from ERC721 asset contracts. */ interface IERC721Receiver { /** * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom} * by `operator` from `from`, this function is called. * * It must return its Solidity selector to confirm the token transfer. * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted. * * The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`. */ function onERC721Received( address operator, address from, uint256 tokenId, bytes calldata data ) external returns (bytes4); } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (token/ERC721/extensions/IERC721Metadata.sol) pragma solidity ^0.8.0; import "../IERC721.sol"; /** * @title ERC-721 Non-Fungible Token Standard, optional metadata extension * @dev See https://eips.ethereum.org/EIPS/eip-721 */ interface IERC721Metadata is IERC721 { /** * @dev Returns the token collection name. */ function name() external view returns (string memory); /** * @dev Returns the token collection symbol. */ function symbol() external view returns (string memory); /** * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token. */ function tokenURI(uint256 tokenId) external view returns (string memory); } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (utils/Address.sol) pragma solidity ^0.8.0; /** * @dev Collection of functions related to the address type */ library Address { /** * @dev Returns true if `account` is a contract. * * [IMPORTANT] * ==== * It is unsafe to assume that an address for which this function returns * false is an externally-owned account (EOA) and not a contract. * * Among others, `isContract` will return false for the following * types of addresses: * * - an externally-owned account * - a contract in construction * - an address where a contract will be created * - an address where a contract lived, but was destroyed * ==== */ function isContract(address account) internal view returns (bool) { // This method relies on extcodesize, which returns 0 for contracts in // construction, since the code is only stored at the end of the // constructor execution. uint256 size; assembly { size := extcodesize(account) } return size > 0; } /** * @dev Replacement for Solidity's `transfer`: sends `amount` wei to * `recipient`, forwarding all available gas and reverting on errors. * * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost * of certain opcodes, possibly making contracts go over the 2300 gas limit * imposed by `transfer`, making them unable to receive funds via * `transfer`. {sendValue} removes this limitation. * * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more]. * * IMPORTANT: because control is transferred to `recipient`, care must be * taken to not create reentrancy vulnerabilities. Consider using * {ReentrancyGuard} or the * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. */ function sendValue(address payable recipient, uint256 amount) internal { require(address(this).balance >= amount, "Address: insufficient balance"); (bool success, ) = recipient.call{value: amount}(""); require(success, "Address: unable to send value, recipient may have reverted"); } /** * @dev Performs a Solidity function call using a low level `call`. A * plain `call` is an unsafe replacement for a function call: use this * function instead. * * If `target` reverts with a revert reason, it is bubbled up by this * function (like regular Solidity function calls). * * Returns the raw returned data. To convert to the expected return value, * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`]. * * Requirements: * * - `target` must be a contract. * - calling `target` with `data` must not revert. * * _Available since v3.1._ */ function functionCall(address target, bytes memory data) internal returns (bytes memory) { return functionCall(target, data, "Address: low-level call failed"); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with * `errorMessage` as a fallback revert reason when `target` reverts. * * _Available since v3.1._ */ function functionCall( address target, bytes memory data, string memory errorMessage ) internal returns (bytes memory) { return functionCallWithValue(target, data, 0, errorMessage); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but also transferring `value` wei to `target`. * * Requirements: * * - the calling contract must have an ETH balance of at least `value`. * - the called Solidity function must be `payable`. * * _Available since v3.1._ */ function functionCallWithValue( address target, bytes memory data, uint256 value ) internal returns (bytes memory) { return functionCallWithValue(target, data, value, "Address: low-level call with value failed"); } /** * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but * with `errorMessage` as a fallback revert reason when `target` reverts. * * _Available since v3.1._ */ function functionCallWithValue( address target, bytes memory data, uint256 value, string memory errorMessage ) internal returns (bytes memory) { require(address(this).balance >= value, "Address: insufficient balance for call"); require(isContract(target), "Address: call to non-contract"); (bool success, bytes memory returndata) = target.call{value: value}(data); return verifyCallResult(success, returndata, errorMessage); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but performing a static call. * * _Available since v3.3._ */ function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) { return functionStaticCall(target, data, "Address: low-level static call failed"); } /** * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`], * but performing a static call. * * _Available since v3.3._ */ function functionStaticCall( address target, bytes memory data, string memory errorMessage ) internal view returns (bytes memory) { require(isContract(target), "Address: static call to non-contract"); (bool success, bytes memory returndata) = target.staticcall(data); return verifyCallResult(success, returndata, errorMessage); } /** * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], * but performing a delegate call. * * _Available since v3.4._ */ function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) { return functionDelegateCall(target, data, "Address: low-level delegate call failed"); } /** * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`], * but performing a delegate call. * * _Available since v3.4._ */ function functionDelegateCall( address target, bytes memory data, string memory errorMessage ) internal returns (bytes memory) { require(isContract(target), "Address: delegate call to non-contract"); (bool success, bytes memory returndata) = target.delegatecall(data); return verifyCallResult(success, returndata, errorMessage); } /** * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the * revert reason using the provided one. * * _Available since v4.3._ */ function verifyCallResult( bool success, bytes memory returndata, string memory errorMessage ) internal pure returns (bytes memory) { if (success) { return returndata; } else { // Look for revert reason and bubble it up if present if (returndata.length > 0) { // The easiest way to bubble the revert reason is using memory via assembly assembly { let returndata_size := mload(returndata) revert(add(32, returndata), returndata_size) } } else { revert(errorMessage); } } } } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (utils/Context.sol) pragma solidity ^0.8.0; /** * @dev Provides information about the current execution context, including the * sender of the transaction and its data. While these are generally available * via msg.sender and msg.data, they should not be accessed in such a direct * manner, since when dealing with meta-transactions the account sending and * paying for execution may not be the actual sender (as far as an application * is concerned). * * This contract is only required for intermediate, library-like contracts. */ abstract contract Context { function _msgSender() internal view virtual returns (address) { return msg.sender; } function _msgData() internal view virtual returns (bytes calldata) { return msg.data; } } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (utils/Strings.sol) pragma solidity ^0.8.0; /** * @dev String operations. */ library Strings { bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef"; /** * @dev Converts a `uint256` to its ASCII `string` decimal representation. */ function toString(uint256 value) internal pure returns (string memory) { // Inspired by OraclizeAPI's implementation - MIT licence // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol if (value == 0) { return "0"; } uint256 temp = value; uint256 digits; while (temp != 0) { digits++; temp /= 10; } bytes memory buffer = new bytes(digits); while (value != 0) { digits -= 1; buffer[digits] = bytes1(uint8(48 + uint256(value % 10))); value /= 10; } return string(buffer); } /** * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation. */ function toHexString(uint256 value) internal pure returns (string memory) { if (value == 0) { return "0x00"; } uint256 temp = value; uint256 length = 0; while (temp != 0) { length++; temp >>= 8; } return toHexString(value, length); } /** * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length. */ function toHexString(uint256 value, uint256 length) internal pure returns (string memory) { bytes memory buffer = new bytes(2 * length + 2); buffer[0] = "0"; buffer[1] = "x"; for (uint256 i = 2 * length + 1; i > 1; --i) { buffer[i] = _HEX_SYMBOLS[value & 0xf]; value >>= 4; } require(value == 0, "Strings: hex length insufficient"); return string(buffer); } } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol) pragma solidity ^0.8.0; import "./IERC165.sol"; /** * @dev Implementation of the {IERC165} interface. * * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check * for the additional interface id that will be supported. For example: * * ```solidity * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { * return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId); * } * ``` * * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation. */ abstract contract ERC165 is IERC165 { /** * @dev See {IERC165-supportsInterface}. */ function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { return interfaceId == type(IERC165).interfaceId; } } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol) pragma solidity ^0.8.0; /** * @dev Interface of the ERC165 standard, as defined in the * https://eips.ethereum.org/EIPS/eip-165[EIP]. * * Implementers can declare support of contract interfaces, which can then be * queried by others ({ERC165Checker}). * * For an implementation, see {ERC165}. */ interface IERC165 { /** * @dev Returns true if this contract implements the interface defined by * `interfaceId`. See the corresponding * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section] * to learn more about how these ids are created. * * This function call must use less than 30 000 gas. */ function supportsInterface(bytes4 interfaceId) external view returns (bool); } // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; import {IOperatorFilterRegistry} from "./IOperatorFilterRegistry.sol"; import {CANONICAL_OPERATOR_FILTER_REGISTRY_ADDRESS} from "./lib/Constants.sol"; /** * @title OperatorFilterer * @notice Abstract contract whose constructor automatically registers and optionally subscribes to or copies another * registrant's entries in the OperatorFilterRegistry. * @dev This smart contract is meant to be inherited by token contracts so they can use the following: * - `onlyAllowedOperator` modifier for `transferFrom` and `safeTransferFrom` methods. * - `onlyAllowedOperatorApproval` modifier for `approve` and `setApprovalForAll` methods. * Please note that if your token contract does not provide an owner with EIP-173, it must provide * administration methods on the contract itself to interact with the registry otherwise the subscription * will be locked to the options set during construction. */ abstract contract OperatorFilterer { /// @dev Emitted when an operator is not allowed. error OperatorNotAllowed(address operator); IOperatorFilterRegistry public constant OPERATOR_FILTER_REGISTRY = IOperatorFilterRegistry(CANONICAL_OPERATOR_FILTER_REGISTRY_ADDRESS); /// @dev The constructor that is called when the contract is being deployed. constructor(address subscriptionOrRegistrantToCopy, bool subscribe) { // If an inheriting token contract is deployed to a network without the registry deployed, the modifier // will not revert, but the contract will need to be registered with the registry once it is deployed in // order for the modifier to filter addresses. if (address(OPERATOR_FILTER_REGISTRY).code.length > 0) { if (subscribe) { OPERATOR_FILTER_REGISTRY.registerAndSubscribe(address(this), subscriptionOrRegistrantToCopy); } else { if (subscriptionOrRegistrantToCopy != address(0)) { OPERATOR_FILTER_REGISTRY.registerAndCopyEntries(address(this), subscriptionOrRegistrantToCopy); } else { OPERATOR_FILTER_REGISTRY.register(address(this)); } } } } /** * @dev A helper function to check if an operator is allowed. */ modifier onlyAllowedOperator(address from) virtual { // Allow spending tokens from addresses with balance // Note that this still allows listings and marketplaces with escrow to transfer tokens if transferred // from an EOA. if (from != msg.sender) { _checkFilterOperator(msg.sender); } _; } /** * @dev A helper function to check if an operator approval is allowed. */ modifier onlyAllowedOperatorApproval(address operator) virtual { _checkFilterOperator(operator); _; } /** * @dev A helper function to check if an operator is allowed. */ function _checkFilterOperator(address operator) internal view virtual { // Check registry code length to facilitate testing in environments without a deployed registry. if (address(OPERATOR_FILTER_REGISTRY).code.length > 0) { // under normal circumstances, this function will revert rather than return false, but inheriting contracts // may specify their own OperatorFilterRegistry implementations, which may behave differently if (!OPERATOR_FILTER_REGISTRY.isOperatorAllowed(address(this), operator)) { revert OperatorNotAllowed(operator); } } } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; address constant CANONICAL_OPERATOR_FILTER_REGISTRY_ADDRESS = 0x000000000000AAeB6D7670E522A718067333cd4E; address constant CANONICAL_CORI_SUBSCRIPTION = 0x3cc6CddA760b79bAfa08dF41ECFA224f810dCeB6; // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; interface IOperatorFilterRegistry { /** * @notice Returns true if operator is not filtered for a given token, either by address or codeHash. Also returns * true if supplied registrant address is not registered. */ function isOperatorAllowed(address registrant, address operator) external view returns (bool); /** * @notice Registers an address with the registry. May be called by address itself or by EIP-173 owner. */ function register(address registrant) external; /** * @notice Registers an address with the registry and "subscribes" to another address's filtered operators and codeHashes. */ function registerAndSubscribe(address registrant, address subscription) external; /** * @notice Registers an address with the registry and copies the filtered operators and codeHashes from another * address without subscribing. */ function registerAndCopyEntries(address registrant, address registrantToCopy) external; /** * @notice Unregisters an address with the registry and removes its subscription. May be called by address itself or by EIP-173 owner. * Note that this does not remove any filtered addresses or codeHashes. * Also note that any subscriptions to this registrant will still be active and follow the existing filtered addresses and codehashes. */ function unregister(address addr) external; /** * @notice Update an operator address for a registered address - when filtered is true, the operator is filtered. */ function updateOperator(address registrant, address operator, bool filtered) external; /** * @notice Update multiple operators for a registered address - when filtered is true, the operators will be filtered. Reverts on duplicates. */ function updateOperators(address registrant, address[] calldata operators, bool filtered) external; /** * @notice Update a codeHash for a registered address - when filtered is true, the codeHash is filtered. */ function updateCodeHash(address registrant, bytes32 codehash, bool filtered) external; /** * @notice Update multiple codeHashes for a registered address - when filtered is true, the codeHashes will be filtered. Reverts on duplicates. */ function updateCodeHashes(address registrant, bytes32[] calldata codeHashes, bool filtered) external; /** * @notice Subscribe an address to another registrant's filtered operators and codeHashes. Will remove previous * subscription if present. * Note that accounts with subscriptions may go on to subscribe to other accounts - in this case, * subscriptions will not be forwarded. Instead the former subscription's existing entries will still be * used. */ function subscribe(address registrant, address registrantToSubscribe) external; /** * @notice Unsubscribe an address from its current subscribed registrant, and optionally copy its filtered operators and codeHashes. */ function unsubscribe(address registrant, bool copyExistingEntries) external; /** * @notice Get the subscription address of a given registrant, if any. */ function subscriptionOf(address addr) external returns (address registrant); /** * @notice Get the set of addresses subscribed to a given registrant. * Note that order is not guaranteed as updates are made. */ function subscribers(address registrant) external returns (address[] memory); /** * @notice Get the subscriber at a given index in the set of addresses subscribed to a given registrant. * Note that order is not guaranteed as updates are made. */ function subscriberAt(address registrant, uint256 index) external returns (address); /** * @notice Copy filtered operators and codeHashes from a different registrantToCopy to addr. */ function copyEntriesOf(address registrant, address registrantToCopy) external; /** * @notice Returns true if operator is filtered by a given address or its subscription. */ function isOperatorFiltered(address registrant, address operator) external returns (bool); /** * @notice Returns true if the hash of an address's code is filtered by a given address or its subscription. */ function isCodeHashOfFiltered(address registrant, address operatorWithCode) external returns (bool); /** * @notice Returns true if a codeHash is filtered by a given address or its subscription. */ function isCodeHashFiltered(address registrant, bytes32 codeHash) external returns (bool); /** * @notice Returns a list of filtered operators for a given address or its subscription. */ function filteredOperators(address addr) external returns (address[] memory); /** * @notice Returns the set of filtered codeHashes for a given address or its subscription. * Note that order is not guaranteed as updates are made. */ function filteredCodeHashes(address addr) external returns (bytes32[] memory); /** * @notice Returns the filtered operator at the given index of the set of filtered operators for a given address or * its subscription. * Note that order is not guaranteed as updates are made. */ function filteredOperatorAt(address registrant, uint256 index) external returns (address); /** * @notice Returns the filtered codeHash at the given index of the list of filtered codeHashes for a given address or * its subscription. * Note that order is not guaranteed as updates are made. */ function filteredCodeHashAt(address registrant, uint256 index) external returns (bytes32); /** * @notice Returns true if an address has registered */ function isRegistered(address addr) external returns (bool); /** * @dev Convenience method to compute the code hash of an arbitrary contract */ function codeHashOf(address addr) external returns (bytes32); }
File 3 of 6: GnosisSafeProxy
// SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /// @title IProxy - Helper interface to access masterCopy of the Proxy on-chain /// @author Richard Meissner - <[email protected]> interface IProxy { function masterCopy() external view returns (address); } /// @title GnosisSafeProxy - Generic proxy contract allows to execute all transactions applying the code of a master contract. /// @author Stefan George - <[email protected]> /// @author Richard Meissner - <[email protected]> contract GnosisSafeProxy { // singleton always needs to be first declared variable, to ensure that it is at the same location in the contracts to which calls are delegated. // To reduce deployment costs this variable is internal and needs to be retrieved via `getStorageAt` address internal singleton; /// @dev Constructor function sets address of singleton contract. /// @param _singleton Singleton address. constructor(address _singleton) { require(_singleton != address(0), "Invalid singleton address provided"); singleton = _singleton; } /// @dev Fallback function forwards all transactions and returns all received return data. fallback() external payable { // solhint-disable-next-line no-inline-assembly assembly { let _singleton := and(sload(0), 0xffffffffffffffffffffffffffffffffffffffff) // 0xa619486e == keccak("masterCopy()"). The value is right padded to 32-bytes with 0s if eq(calldataload(0), 0xa619486e00000000000000000000000000000000000000000000000000000000) { mstore(0, _singleton) return(0, 0x20) } calldatacopy(0, 0, calldatasize()) let success := delegatecall(gas(), _singleton, 0, calldatasize(), 0, 0) returndatacopy(0, 0, returndatasize()) if eq(success, 0) { revert(0, returndatasize()) } return(0, returndatasize()) } } } /// @title Proxy Factory - Allows to create new proxy contact and execute a message call to the new proxy within one transaction. /// @author Stefan George - <[email protected]> contract GnosisSafeProxyFactory { event ProxyCreation(GnosisSafeProxy proxy, address singleton); /// @dev Allows to create new proxy contact and execute a message call to the new proxy within one transaction. /// @param singleton Address of singleton contract. /// @param data Payload for message call sent to new proxy contract. function createProxy(address singleton, bytes memory data) public returns (GnosisSafeProxy proxy) { proxy = new GnosisSafeProxy(singleton); if (data.length > 0) // solhint-disable-next-line no-inline-assembly assembly { if eq(call(gas(), proxy, 0, add(data, 0x20), mload(data), 0, 0), 0) { revert(0, 0) } } emit ProxyCreation(proxy, singleton); } /// @dev Allows to retrieve the runtime code of a deployed Proxy. This can be used to check that the expected Proxy was deployed. function proxyRuntimeCode() public pure returns (bytes memory) { return type(GnosisSafeProxy).runtimeCode; } /// @dev Allows to retrieve the creation code used for the Proxy deployment. With this it is easily possible to calculate predicted address. function proxyCreationCode() public pure returns (bytes memory) { return type(GnosisSafeProxy).creationCode; } /// @dev Allows to create new proxy contact using CREATE2 but it doesn't run the initializer. /// This method is only meant as an utility to be called from other methods /// @param _singleton Address of singleton contract. /// @param initializer Payload for message call sent to new proxy contract. /// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. function deployProxyWithNonce( address _singleton, bytes memory initializer, uint256 saltNonce ) internal returns (GnosisSafeProxy proxy) { // If the initializer changes the proxy address should change too. Hashing the initializer data is cheaper than just concatinating it bytes32 salt = keccak256(abi.encodePacked(keccak256(initializer), saltNonce)); bytes memory deploymentData = abi.encodePacked(type(GnosisSafeProxy).creationCode, uint256(uint160(_singleton))); // solhint-disable-next-line no-inline-assembly assembly { proxy := create2(0x0, add(0x20, deploymentData), mload(deploymentData), salt) } require(address(proxy) != address(0), "Create2 call failed"); } /// @dev Allows to create new proxy contact and execute a message call to the new proxy within one transaction. /// @param _singleton Address of singleton contract. /// @param initializer Payload for message call sent to new proxy contract. /// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. function createProxyWithNonce( address _singleton, bytes memory initializer, uint256 saltNonce ) public returns (GnosisSafeProxy proxy) { proxy = deployProxyWithNonce(_singleton, initializer, saltNonce); if (initializer.length > 0) // solhint-disable-next-line no-inline-assembly assembly { if eq(call(gas(), proxy, 0, add(initializer, 0x20), mload(initializer), 0, 0), 0) { revert(0, 0) } } emit ProxyCreation(proxy, _singleton); } /// @dev Allows to create new proxy contact, execute a message call to the new proxy and call a specified callback within one transaction /// @param _singleton Address of singleton contract. /// @param initializer Payload for message call sent to new proxy contract. /// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. /// @param callback Callback that will be invoced after the new proxy contract has been successfully deployed and initialized. function createProxyWithCallback( address _singleton, bytes memory initializer, uint256 saltNonce, IProxyCreationCallback callback ) public returns (GnosisSafeProxy proxy) { uint256 saltNonceWithCallback = uint256(keccak256(abi.encodePacked(saltNonce, callback))); proxy = createProxyWithNonce(_singleton, initializer, saltNonceWithCallback); if (address(callback) != address(0)) callback.proxyCreated(proxy, _singleton, initializer, saltNonce); } /// @dev Allows to get the address for a new proxy contact created via `createProxyWithNonce` /// This method is only meant for address calculation purpose when you use an initializer that would revert, /// therefore the response is returned with a revert. When calling this method set `from` to the address of the proxy factory. /// @param _singleton Address of singleton contract. /// @param initializer Payload for message call sent to new proxy contract. /// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. function calculateCreateProxyWithNonceAddress( address _singleton, bytes calldata initializer, uint256 saltNonce ) external returns (GnosisSafeProxy proxy) { proxy = deployProxyWithNonce(_singleton, initializer, saltNonce); revert(string(abi.encodePacked(proxy))); } } interface IProxyCreationCallback { function proxyCreated( GnosisSafeProxy proxy, address _singleton, bytes calldata initializer, uint256 saltNonce ) external; }
File 4 of 6: BatchSignedERC721OrdersFeature
// SPDX-License-Identifier: Apache-2.0 /* Copyright 2022 Element.Market Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.17; interface IBatchSignedERC721OrdersFeature { /// @param fee [16 bits(platformFeePercentage) + 16 bits(royaltyFeePercentage) + 160 bits(royaltyFeeRecipient)]. /// @param items [96 bits(erc20TokenAmount) + 160 bits(nftId)]. /// struct BasicCollection { /// address nftAddress; /// bytes32 fee; /// bytes32[] items; /// } /// /// struct OrderItem { /// uint256 erc20TokenAmount; /// uint256 nftId; /// } /// /// @param fee [16 bits(platformFeePercentage) + 16 bits(royaltyFeePercentage) + 160 bits(royaltyFeeRecipient)]. /// struct Collection { /// address nftAddress; /// bytes32 fee; /// OrderItem[] items; /// } /// /// struct BatchSignedERC721Orders { /// address maker; /// uint256 listingTime; /// uint256 expiryTime; /// uint256 startNonce; /// address erc20Token; /// address platformFeeRecipient; /// BasicCollection[] basicCollections; /// Collection[] collections; /// uint256 hashNonce; /// } /// @param data1 [8 bits(signatureType) + 8 bits(reserved) + 40 bits(startNonce) + 8 bits(v) + 32 bits(listingTime) + 160 bits(maker)] /// @param data2 [64 bits(taker part1) + 32 bits(expiryTime) + 160 bits(erc20Token)] /// @param data3 [96 bits(taker part2) + 160 bits(platformFeeRecipient)] struct BatchSignedERC721OrderParameter { uint256 data1; uint256 data2; uint256 data3; bytes32 r; bytes32 s; } function fillBatchSignedERC721Order(BatchSignedERC721OrderParameter calldata parameter, bytes calldata collections) external payable; /// @param data1 [8 bits(signatureType) + 8 bits(reserved) + 40 bits(startNonce) + 8 bits(v) + 32 bits(listingTime) + 160 bits(maker)] /// @param data2 [64 bits(taker part1) + 32 bits(expiryTime) + 160 bits(erc20Token)] /// @param data3 [96 bits(taker part2) + 160 bits(platformFeeRecipient)] struct BatchSignedERC721OrderParameters { uint256 data1; uint256 data2; uint256 data3; bytes32 r; bytes32 s; bytes collections; } /// @param additional1 [96 bits(withdrawETHAmount) + 160 bits(erc20Token)] /// @param additional2 [8 bits(revertIfIncomplete) + 88 bits(unused) + 160 bits(royaltyFeeRecipient)] function fillBatchSignedERC721Orders( BatchSignedERC721OrderParameters[] calldata parameters, uint256 additional1, uint256 additional2 ) external payable; } // SPDX-License-Identifier: Apache-2.0 /* Copyright 2022 Element.Market Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.17; import "../../storage/LibCommonNftOrdersStorage.sol"; import "../../storage/LibERC721OrdersStorage.sol"; import "../interfaces/IBatchSignedERC721OrdersFeature.sol"; contract BatchSignedERC721OrdersFeature is IBatchSignedERC721OrdersFeature { uint256 internal constant MASK_192 = (1 << 192) - 1; uint256 internal constant MASK_160 = (1 << 160) - 1; uint256 internal constant MASK_64 = (1 << 64) - 1; uint256 internal constant MASK_40 = (1 << 40) - 1; uint256 internal constant MASK_32 = (1 << 32) - 1; uint256 internal constant MASK_16 = (1 << 16) - 1; uint256 internal constant MASK_INDEX_LIST_PART1 = ((1 << 96) - 1) << 160; uint256 internal constant MASK_INDEX_LIST_PART2 = ((1 << 32) - 1) << 128; uint256 internal constant NONCE_RANGE_LIMIT = 1 << 248; uint256 internal constant MAX_ERC20_AMOUNT = (1 << 224) - 1; // Storage ID. uint256 constant STORAGE_ID_COMMON_NFT_ORDERS = 4 << 128; uint256 constant STORAGE_ID_ERC721_ORDERS = 5 << 128; // Topic for ERC721SellOrderFilled. bytes32 internal constant _TOPIC_SELL_ORDER_FILLED = 0x9c248aa1a265aa616f707b979d57f4529bb63a4fc34dc7fc61fdddc18410f74e; // keccak256("") bytes32 internal constant _EMPTY_ARRAY_KECCAK256 = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470; // keccak256(abi.encodePacked( // "BatchSignedERC721Orders(address maker,uint256 listingTime,uint256 expiryTime,uint256 startNonce,address erc20Token,address platformFeeRecipient,BasicCollection[] basicCollections,Collection[] collections,uint256 hashNonce)", // "BasicCollection(address nftAddress,bytes32 fee,bytes32[] items)", // "Collection(address nftAddress,bytes32 fee,OrderItem[] items)", // "OrderItem(uint256 erc20TokenAmount,uint256 nftId)" // )) bytes32 internal constant _BATCH_SIGNED_ERC721_ORDERS_TYPE_HASH = 0x2d8cbbbc696e7292c3b5beb38e1363d34ff11beb8c3456c14cb938854597b9ed; // keccak256("BasicCollection(address nftAddress,bytes32 fee,bytes32[] items)") bytes32 internal constant _BASIC_COLLECTION_TYPE_HASH = 0x12ad29288fd70022f26997a9958d9eceb6e840ceaa79b72ea5945ba87e4d33b0; // keccak256(abi.encodePacked( // "Collection(address nftAddress,bytes32 fee,OrderItem[] items)", // "OrderItem(uint256 erc20TokenAmount,uint256 nftId)" // )) bytes32 internal constant _COLLECTION_TYPE_HASH = 0xb9f488d48cec782be9ecdb74330c9c6a33c236a8022d8a91a4e4df4e81b51620; // keccak256("OrderItem(uint256 erc20TokenAmount,uint256 nftId)") bytes32 internal constant _ORDER_ITEM_TYPE_HASH = 0x5f93394997caa49a9382d44a75e3ce6a460f32b39870464866ac994f8be97afe; // keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)") bytes32 internal constant DOMAIN = 0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f; // keccak256("ElementEx") bytes32 internal constant NAME = 0x27b14c20196091d9cd90ca9c473d3ad1523b00ddf487a9b7452a8a119a16b98c; // keccak256("1.0.0") bytes32 internal constant VERSION = 0x06c015bd22b4c69690933c1058878ebdfef31f9aaae40bbe86d8a09fe1b2972c; /// @dev The implementation address of this feature. address internal immutable _IMPL; /// @dev The WETH token contract. address internal immutable _WETH; constructor(address weth) { require(address(weth) != address(0), "INVALID_WETH_ADDRESS"); _WETH = weth; _IMPL = address(this); } function fillBatchSignedERC721Order(BatchSignedERC721OrderParameter calldata /* parameter */, bytes calldata collections) external override payable { uint256 ethBalanceBefore = address(this).balance - msg.value; uint256 offsetCollectionsBytes; assembly { offsetCollectionsBytes := collections.offset } // Validate order. bytes32 orderHash = _validateOrder(offsetCollectionsBytes); assembly { // memory[0x0 - 0x20] orderHash mstore(0, orderHash) /////////////////////////// memory[0x380 - 0x420] for delegateCall /////////// // memory[0x380 - 0x3a0] erc20TokenFromDelegateCall // memory[0x3a0 - 0x3c0] platformFeeRecipientFromDelegateCall // memory[0x3c0 - 0x3e0] royaltyFeeRecipientFromDelegateCall mstore(0x380, 0) mstore(0x3a0, 0) mstore(0x3c0, 0) } // Fill order. _fillBatchSignedERC721Order(offsetCollectionsBytes); // Refund ETH. assembly { if eq(selfbalance(), ethBalanceBefore) { return(0, 0) } if gt(selfbalance(), ethBalanceBefore) { if iszero(call(gas(), caller(), sub(selfbalance(), ethBalanceBefore), 0, 0, 0, 0)) { _revertRefundETHFailed() } return(0, 0) } _revertRefundETHFailed() function _revertRefundETHFailed() { // revert("fillBatchSignedERC721Order: failed to refund ETH.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003166696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x6661696c656420746f20726566756e64204554482e0000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } } } /// @param additional1 [96 bits(withdrawETHAmount) + 160 bits(erc20Token)] /// @param additional2 [8 bits(revertIfIncomplete) + 88 bits(unused) + 160 bits(royaltyFeeRecipient)] function fillBatchSignedERC721Orders( BatchSignedERC721OrderParameters[] calldata parameters, uint256 additional1, uint256 additional2 ) external override payable { require(parameters.length > 0, "fillBatchSignedERC721Orders: invalid parameters."); uint256 ethBalanceBefore = address(this).balance - msg.value; uint256 platformFeeRecipient = parameters[0].data3 & MASK_160; address impl = _IMPL; address weth = _WETH; assembly { let withdrawETHAmount := shr(160, additional1) let erc20Token := and(additional1, MASK_160) let royaltyFeeRecipient := and(additional2, MASK_160) let platformFeeGlobalAccum let royaltyFeeGlobalAccum let someSuccess // Withdraw ETH if needed. if withdrawETHAmount { // Step1: transfer WETH from msg.sender to address(this). _transferERC20(weth, address(), withdrawETHAmount) // Step2: withdraw ETH. // selector for `withdraw(uint256)`. mstore(0, 0x2e1a7d4d) mstore(0x20, withdrawETHAmount) if iszero(call(gas(), weth, 0, 0x1c, 0x24, 0, 0)) { _revertWithdrawETHFailed() } } /////////////////////////// memory[0 - 0x40] for delegatecall output ///////////// // memory[0 - 0x20] output [platformFeeGlobal] // memory[0x20 - 0x40] output [royaltyFeeGlobal] /////////////////////////// memory[0x40 - ] for delegatecall input ///////////// // memory[0x40 - 0x60] selector for `delegateCallFillBatchSignedERC721Order(BatchSignedERC721OrderParameter,address,address,address,bytes)` mstore(0x40, 0xdc055ecc) // memory[0x60 - 0x100] parameter // memory[0x100 - 0x120] erc20Token mstore(0x100, erc20Token) // memory[0x120 - 0x140] platformFeeRecipient mstore(0x120, platformFeeRecipient) // memory[0x140 - 0x160] royaltyFeeRecipient mstore(0x140, royaltyFeeRecipient) // memory[0x160 - 0x180] collections.offset mstore(0x160, 0x120) // memory[0x180 - 0x1a0] collections.length // memory[0x1a0 - ] collections.data let ptrEnd := add(parameters.offset, mul(parameters.length, 0x20)) for { let ptr := parameters.offset } lt(ptr, ptrEnd) { ptr := add(ptr, 0x20) } { let ptrData := add(parameters.offset, calldataload(ptr)) // memory[0x40 - 0x60] selector for `delegateCallFillBatchSignedERC721Order` // memory[0x60 - 0x100] parameter calldatacopy(0x60, ptrData, 0xa0 /* 5 * 32*/) // memory[0x100 - 0x120] erc20Token // memory[0x120 - 0x140] platformFeeRecipient // memory[0x140 - 0x160] royaltyFeeRecipient // memory[0x160 - 0x180] collections.offset // memory[0x180 - 0x1a0] collections.length let collectionsLength := calldataload(add(ptrData, 0xc0)) if mod(collectionsLength, 0x20) { _revertInvalidCollectionsBytes() } mstore(0x180, collectionsLength) // memory[0x1a0 - ] collections.data calldatacopy(0x1a0, add(ptrData, 0xe0), collectionsLength) // 0x144 = 0x4(selector) + 0xa0(parameter) + 0x20(erc20Token) + 0x20(platformFeeRecipient) + 0x20(royaltyFeeRecipient) + 0x20(collections.offset) + 0x20(collections.length) switch delegatecall(gas(), impl, 0x5c, add(0x144, collectionsLength), 0, 0x40) case 0 { // Check revertIfIncomplete flag if failed. if byte(0, additional2) { returndatacopy(0, 0, returndatasize()) revert(0, returndatasize()) } } default { // Success. someSuccess := 1 // memory[0 - 0x20] output [platformFeeGlobal] // memory[0x20 - 0x40] output [royaltyFeeGlobal] platformFeeGlobalAccum := add(platformFeeGlobalAccum, mload(0)) royaltyFeeGlobalAccum := add(royaltyFeeGlobalAccum, mload(0x20)) } } // end for if platformFeeGlobalAccum { _transferERC20(erc20Token, platformFeeRecipient, platformFeeGlobalAccum) } if royaltyFeeGlobalAccum { _transferERC20(erc20Token, royaltyFeeRecipient, royaltyFeeGlobalAccum) } if iszero(someSuccess) { _revertNoOrderFilled() } // Refund ETH. if eq(selfbalance(), ethBalanceBefore) { return(0, 0) } if gt(selfbalance(), ethBalanceBefore) { if iszero(call(gas(), caller(), sub(selfbalance(), ethBalanceBefore), 0, 0, 0, 0)) { _revertRefundETHFailed() } return(0, 0) } _revertRefundETHFailed() ///////////////////////////////// functions ///////////////////////////////// function _transferERC20(_erc20Token, _to, _amount) { switch _erc20Token case 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE { if iszero(call(gas(), _to, _amount, 0, 0, 0, 0)) { _revertTransferETHFailed() } } default { if iszero(extcodesize(_erc20Token)) { _revertInvalidERC20Token() } // selector for `transferFrom(address,address,uint256)` mstore(0, 0x23b872dd) mstore(0x20, caller()) mstore(0x40, _to) mstore(0x60, _amount) if iszero(call(gas(), _erc20Token, 0, 0x1c, 0x64, 0, 0x20)) { _revertTransferERC20Failed() } // Check for ERC20 success. ERC20 tokens should return a boolean, but some don't. // We accept 0-length return data as success, or at least 32 bytes that starts with // a 32-byte boolean true. if returndatasize() { if lt(returndatasize(), 0x20) { _revertTransferERC20Failed() } if iszero(eq(mload(0), 1)) { _revertTransferERC20Failed() } } } } function _revertTransferETHFailed() { // revert("fillBatchSignedERC721Orders: failed to transfer ETH.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003466696c6c42617463685369676e65644552433732314f72646572733a) mstore(0x60, 0x206661696c656420746f207472616e73666572204554482e0000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertTransferERC20Failed() { // revert("fillBatchSignedERC721Orders: failed to transfer ERC20.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003666696c6c42617463685369676e65644552433732314f72646572733a) mstore(0x60, 0x206661696c656420746f207472616e736665722045524332302e000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertWithdrawETHFailed() { // revert("fillBatchSignedERC721Orders: failed to withdraw ETH.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003466696c6c42617463685369676e65644552433732314f72646572733a) mstore(0x60, 0x206661696c656420746f207769746864726177204554482e0000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertInvalidCollectionsBytes() { // revert("fillBatchSignedERC721Orders: invalid collectionsBytes.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003666696c6c42617463685369676e65644552433732314f72646572733a) mstore(0x60, 0x20696e76616c696420636f6c6c656374696f6e7342797465732e000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertNoOrderFilled() { // revert("fillBatchSignedERC721Orders: no order filled.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000002d66696c6c42617463685369676e65644552433732314f72646572733a) mstore(0x60, 0x206e6f206f726465722066696c6c65642e000000000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertRefundETHFailed() { // revert("fillBatchSignedERC721Orders: failed to refund ETH.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003266696c6c42617463685369676e65644552433732314f72646572733a) mstore(0x60, 0x206661696c656420746f20726566756e64204554482e00000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertInvalidERC20Token() { // revert("invalid erc20 token") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x00000013696e76616c696420657263323020746f6b656e000000000000000000) mstore(0x60, 0) revert(0, 0x64) } } } // @Note `delegateCallFillBatchSignedERC721Order` is a external function, but must delegatecall from an external exchange, // and should not be registered in the external exchange. function delegateCallFillBatchSignedERC721Order( BatchSignedERC721OrderParameter calldata /* parameter */, address erc20TokenFromDelegateCall, address platformFeeRecipientFromDelegateCall, address royaltyFeeRecipientFromDelegateCall, bytes calldata collections ) external payable { address impl = _IMPL; uint256 offsetCollectionsBytes; assembly { if eq(impl, address()) { // revert("delegateCallFillBatchSignedERC721Order: must delegateCall from an external exchange.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000005464656c656761746543616c6c46696c6c42617463685369676e656445) mstore(0x60, 0x52433732314f726465723a206d7573742064656c656761746543616c6c206672) mstore(0x80, 0x6f6d20616e2065787465726e616c2065786368616e67652e0000000000000000) mstore(0xa0, 0) revert(0, 0xa4) } offsetCollectionsBytes := collections.offset } // Validate order. bytes32 orderHash = _validateOrder(offsetCollectionsBytes); assembly { // memory[0x0 - 0x20] orderHash mstore(0, orderHash) /////////////////////////// memory[0x380 - 0x420] for delegateCall /////////// // memory[0x380 - 0x3a0] erc20TokenFromDelegateCall // memory[0x3a0 - 0x3c0] platformFeeRecipientFromDelegateCall // memory[0x3c0 - 0x3e0] royaltyFeeRecipientFromDelegateCall mstore(0x380, erc20TokenFromDelegateCall) mstore(0x3a0, platformFeeRecipientFromDelegateCall) mstore(0x3c0, royaltyFeeRecipientFromDelegateCall) } // Fill order. _fillBatchSignedERC721Order(offsetCollectionsBytes); assembly { // Return platformFeeGlobal and royaltyFeeGlobal. // memory[0x3e0 - 0x400] platformFeeGlobal // memory[0x400 - 0x420] royaltyFeeGlobal return(0x3e0, 0x40) } } /// data1 [[8 bits(signatureType) + 8 bits(reserved) + 40 bits(startNonce) + 8 bits(v) + 32 bits(listingTime) + 160 bits(maker)] /// data2 [64 bits(taker part1) + 32 bits(expiryTime) + 160 bits(erc20Token)] /// data3 [96 bits(taker part2) + 160 bits(platformFeeRecipient)] function _fillBatchSignedERC721Order(uint256 offsetCollectionsBytes) internal { assembly { /////////////////////////// memory[0x0 - 0x1c0] for emitEvent data //////////////////////// // memory[0x0 - 0x20] orderHash // memory[0x20 - 0x40] maker mstore(0x20, and(calldataload(0x4), MASK_160)) // maker = data1 & MASK_160 // memory[0x40 - 0x60] taker mstore(0x40, or(shl(96, shr(192, calldataload(0x24))), shr(160, calldataload(0x44)))) // taker = ((data2 >> 192) << 96) | (data3 >> 160) if iszero(mload(0x40)) { mstore(0x40, caller()) } // memory[0x60 - 0x80] nonce // memory[0x80 - 0xa0] erc20Token mstore(0x80, and(calldataload(0x24), MASK_160)) // erc20Token = data2 & MASK_160 // memory[0xa0 - 0xc0] erc20TokenAmount // memory[0xc0 - 0xe0] fees.offset mstore(0xc0, 0x120 /* 9 * 32 */) // memory[0xe0 - 0x100] nftAddress // memory[0x100 - 0x120] nftId // memory[0x120 - 0x140] fees.length // memory[0x140 - 0x1c0] fees.data /////////////////////////// memory[0x1c0 - 0x240] for transferERC721 ////////// // memory[0x1c0 - 0x1e0] selector for `transferFrom(address,address,uint256)` mstore(0x1c0, 0x23b872dd) // memory[0x1e0 - 0x200] maker mstore(0x1e0, mload(0x20)) // memory[0x200 - 0x220] taker mstore(0x200, mload(0x40)) // memory[0x220 - 0x240] nftId /////////////////////////// memory[0x240 - 0x300] for nonceVector ///////////// // Note: nonceRange = nonce >> 8 // Note: nonceVector = LibERC721OrdersStorage.orderStatusByMaker[maker][nonceRange] // Note: nonceVector.slot = keccak256(nonceRange, keccak256(maker, LibERC721OrdersStorage.storageId)) // memory[0x240 - 0x260] shouldStoreNonceVectorToStorage flag mstore(0x240, 0) // memory[0x260 - 0x280] nonceMask // memory[0x280 - 0x2a0] nonceVector.slot // memory[0x2a0 - 0x2c0] nonceVector // memory[0x2c0 - 0x2e0] nonceRange mstore(0x2c0, NONCE_RANGE_LIMIT) // memory[0x2e0 - 0x300] keccak256(maker, LibERC721OrdersStorage.storageId) mstore(0x2e0, mload(0x20)) mstore(0x300, STORAGE_ID_ERC721_ORDERS) mstore(0x2e0, keccak256(0x2e0, 0x40)) /////////////////////////// memory[0x300 - 0x380] for collection ////////////// // memory[0x300 - 0x320] collection.head2 // memory[0x320 - 0x340] collection.platformFeePercentage // memory[0x340 - 0x360] collection.royaltyFeePercentage // memory[0x360 - 0x380] someSuccess flag mstore(0x360, 0) /////////////////////////// memory[0x380 - 0x420] for delegateCall /////////// // memory[0x380 - 0x3a0] erc20TokenFromDelegateCall // memory[0x3a0 - 0x3c0] platformFeeRecipientFromDelegateCall // memory[0x3c0 - 0x3e0] royaltyFeeRecipientFromDelegateCall // memory[0x3e0 - 0x400] platformFeeGlobal mstore(0x3e0, 0) // memory[0x400 - 0x420] royaltyFeeGlobal mstore(0x400, 0) /////////////////////////// memory[0x420 - 0x4a0] for transferERC20 /////////// // memory[0x420 - 0x440] selector for `transferFrom(address,address,uint256)` mstore(0x420, 0x23b872dd) // memory[0x440 - 0x460] msg.sender mstore(0x440, caller()) // memory[0x460 - 0x480] to // memory[0x480 - 0x4a0] amount /////////////////////////// global variables ///////////////////////////////// let nonceVectorForCheckingNonReentrant // collectionStartNonce = (data1 >> 200) & MASK_40 let collectionStartNonce := and(shr(200, calldataload(0x4)), MASK_40) // platformFeeRecipient = data3 & MASK_160 let platformFeeRecipient := and(calldataload(0x44), MASK_160) // Total erc20 amount. let totalERC20AmountToPlatform let totalERC20AmountToMaker let ptrEnd := add(offsetCollectionsBytes, calldataload(sub(offsetCollectionsBytes, 0x20))) for { let offsetCollection := offsetCollectionsBytes } lt(offsetCollection, ptrEnd) {} { // memory[0xe0 - 0x100] nftAddress // head1 [96 bits(filledIndexList part1) + 160 bits(nftAddress)] mstore(0xe0, and(calldataload(offsetCollection), MASK_160)) // nftAddress = head1 & MASK_160 // memory[0x300 - 0x320] collection.head2 // collectionType: 0 - basicCollection, 1 - collection // head2 [8 bits(collectionType) + 8 bits(itemsCount) + 8 bits(filledCount) + 8 bits(unused) + 32 bits(filledIndexList part2) // + 16 bits(platformFeePercentage) + 16 bits(royaltyFeePercentage) + 160 bits(royaltyFeeRecipient)] mstore(0x300, calldataload(add(offsetCollection, 0x20))) // filledIndexList [96 bits(filledIndexList part1) + 32 bits(filledIndexList part2) + 128 bits(unused)] // filledIndexList = (head1 & MASK_INDEX_LIST_PART1) | ((head2 >> 64) & MASK_INDEX_LIST_PART2) let filledIndexList := or(and(calldataload(offsetCollection), MASK_INDEX_LIST_PART1), and(shr(64, mload(0x300)), MASK_INDEX_LIST_PART2)) let filledCount := byte(2, mload(0x300)) let itemsCount := byte(1, mload(0x300)) if filledCount { if gt(filledCount, itemsCount) { _revertInvalidFilledIndex() } if gt(filledCount, 128) { _revertInvalidFilledIndex() } if iszero(extcodesize(mload(0xe0))) { _revertInvalidERC721Token() } } // memory[0x140 - 0x160] platformFeeRecipient mstore(0x140, platformFeeRecipient) // memory[0x320 - 0x340] collection.platformFeePercentage switch platformFeeRecipient // if (platformFeeRecipient == address(0) platformFeePercentage = 0 case 0 { mstore(0x320, 0) } // else platformFeePercentage = collection.platformFeePercentage default { mstore(0x320, and(shr(176, mload(0x300)), MASK_16)) } // memory[0x180 - 0x1a0] royaltyFeeRecipient mstore(0x180, and(mload(0x300), MASK_160)) // memory[0x340 - 0x360] collection.royaltyFeePercentage switch mload(0x180) // if (royaltyFeeRecipient == address(0) royaltyFeePercentage = 0 case 0 { mstore(0x340, 0) } // else royaltyFeePercentage = collection.royaltyFeePercentage default { mstore(0x340, and(shr(160, mload(0x300)), MASK_16)) } // Check fees. if gt(add(mload(0x320), mload(0x340)), 10000) { revertFeesPercentageExceedsLimit() } let totalERC20AmountToRoyalty // switch collectionType switch byte(0, mload(0x300)) // basicCollection case 0 { for { } filledCount { } { filledCount := sub(filledCount, 1) let filledIndex := byte(filledCount, filledIndexList) if iszero(lt(filledIndex, itemsCount)) { _revertInvalidFilledIndex() } // memory[0x60 - 0x80] nonce // memory[0x240 - 0x260] shouldStoreNonceVectorToStorage flag // memory[0x260 - 0x280] nonceMask // memory[0x280 - 0x2a0] nonceVector.slot // memory[0x2a0 - 0x2c0] nonceVector // memory[0x2c0 - 0x2e0] nonceRange // memory[0x2e0 - 0x300] keccak256(maker, LibERC721OrdersStorage.storageId) // nonce = add(collectionStartNonce, filledIndex) mstore(0x60, add(collectionStartNonce, filledIndex)) // if (nonceRange != newNonceRange) if iszero(eq(mload(0x2c0), shr(8, mload(0x60)))) { // Store nonce to storage if needed. if mload(0x240) { // Revert if reentrant. if iszero(eq(nonceVectorForCheckingNonReentrant, sload(mload(0x280)))) { _revertReentrantCall() } // Store nonce to storage at one time. sstore(mload(0x280), mload(0x2a0)) // Clear store nonceVector flag. mstore(0x240, 0) } // nonceRange = nonce >> 8 mstore(0x2c0, shr(8, mload(0x60))) // Calculate nonceVector.slot and store to memory. mstore(0x280, keccak256(0x2c0, 0x40)) // Load nonceVector from storage. nonceVectorForCheckingNonReentrant := sload(mload(0x280)) // Store nonceVector to memory. mstore(0x2a0, nonceVectorForCheckingNonReentrant) } // memory[0x260 - 0x280] nonceMask // nonceMask = 1 << (nonce & 0xff) mstore(0x260, shl(and(mload(0x60), 0xff), 1)) // if order is not filled. // if (nonceVector & nonceMask == 0) if iszero(and(mload(0x2a0), mload(0x260))) { // orderItem [96 bits(erc20TokenAmount) + 160 bits(nftId)] let orderItem := calldataload(add(add(offsetCollection, 0x40), mul(filledIndex, 0x20))) // memory[0xe0 - 0x100] nftAddress // memory[0x1c0 - 0x1e0] selector for `transferFrom(address,address,uint256)` // memory[0x1e0 - 0x200] maker // memory[0x200 - 0x220] taker // memory[0x220 - 0x240] nftId mstore(0x220, and(orderItem, MASK_160)) // transferERC721 // 0x1dc = 0x1c0 + 28 if call(gas(), mload(0xe0), 0, 0x1dc, 0x64, 0, 0) { // Set store nonceVector flag. mstore(0x240, 1) // Update nonceVector. // nonceVector |= nonceMask mstore(0x2a0, or(mload(0x2a0), mload(0x260))) // Calculate fees. // memory[0xa0 - 0xc0] erc20TokenAmount mstore(0xa0, shr(160, orderItem)) // erc20TokenAmount = orderItem >> 160 // memory[0x140 - 0x1c0] fees.data // memory[0x140 - 0x160] platformFeeRecipient // memory[0x160 - 0x180] platformFeeAmount // memory[0x180 - 0x1a0] royaltyFeeRecipient // memory[0x1a0 - 0x1c0] royaltyFeeAmount // memory[0x320 - 0x340] platformFeePercentage // platformFeeAmount = erc20TokenAmount * platformFeePercentage / 10000 mstore(0x160, div(mul(mload(0xa0), mload(0x320)), 10000)) // memory[0x340 - 0x360] royaltyFeePercentage // royaltyFeeAmount = erc20TokenAmount * royaltyFeePercentage / 10000 mstore(0x1a0, div(mul(mload(0xa0), mload(0x340)), 10000)) // Update total erc20 amount. // totalERC20AmountToMaker += erc20TokenAmount - (platformFeeAmount + royaltyFeeAmount) totalERC20AmountToMaker := add(totalERC20AmountToMaker, sub(mload(0xa0), add(mload(0x160), mload(0x1a0)))) // totalERC20AmountToPlatform += platformFeeAmount totalERC20AmountToPlatform := add(totalERC20AmountToPlatform, mload(0x160)) // totalERC20AmountToRoyalty += royaltyFeeAmount totalERC20AmountToRoyalty := add(totalERC20AmountToRoyalty, mload(0x1a0)) // Emit event // memory[0 - 0x20] orderHash // memory[0x20 - 0x40] maker // memory[0x40 - 0x60] taker // memory[0x60 - 0x80] nonce // memory[0x80 - 0xa0] erc20Token // memory[0xa0 - 0xc0] erc20TokenAmount // memory[0xc0 - 0xe0] fees.offset // memory[0xe0 - 0x100] nftAddress // memory[0x100 - 0x120] nftId mstore(0x100, mload(0x220)) // fees switch platformFeeRecipient case 0 { // memory[0x180 - 0x1a0] royaltyFeeRecipient switch mload(0x180) case 0 { // memory[0x120 - 0x140] fees.length mstore(0x120, 0) // emit event log1(0, 320 /* 10 * 32 */, _TOPIC_SELL_ORDER_FILLED) } default { // memory[0x120 - 0x140] fees.length mstore(0x120, 1) // Copy royaltyFeeRecipient to memory[0x140 - 0x160] mstore(0x140, mload(0x180)) // Copy royaltyFeeAmount to memory[0x160 - 0x180] mstore(0x160, mload(0x1a0)) // emit event log1(0, 384 /* 12 * 32 */, _TOPIC_SELL_ORDER_FILLED) } } default { // memory[0x180 - 0x1a0] royaltyFeeRecipient switch mload(0x180) case 0 { // memory[0x120 - 0x140] fees.length mstore(0x120, 1) // emit event log1(0, 384 /* 12 * 32 */, _TOPIC_SELL_ORDER_FILLED) } default { // memory[0x120 - 0x140] fees.length mstore(0x120, 2) // emit event log1(0, 448 /* 14 * 32 */, _TOPIC_SELL_ORDER_FILLED) } } // Set someSuccess flag. mstore(0x360, 1) } } } // end for // Update offsetCollection. offsetCollection := add(add(offsetCollection, 0x40), mul(itemsCount, 0x20)) } // end basicCollection // collection default { for { } filledCount { } { filledCount := sub(filledCount, 1) let filledIndex := byte(filledCount, filledIndexList) if iszero(lt(filledIndex, itemsCount)) { _revertInvalidFilledIndex() } // memory[0x60 - 0x80] nonce // memory[0x240 - 0x260] shouldStoreNonceVectorToStorage flag // memory[0x260 - 0x280] nonceMask // memory[0x280 - 0x2a0] nonceVector.slot // memory[0x2a0 - 0x2c0] nonceVector // memory[0x2c0 - 0x2e0] nonceRange // memory[0x2e0 - 0x300] keccak256(maker, LibERC721OrdersStorage.storageId) // nonce = add(collectionStartNonce, filledIndex) mstore(0x60, add(collectionStartNonce, filledIndex)) // if (nonceRange != newNonceRange) if iszero(eq(mload(0x2c0), shr(8, mload(0x60)))) { // Store nonce to storage if needed. if mload(0x240) { // Revert if reentrant. if iszero(eq(nonceVectorForCheckingNonReentrant, sload(mload(0x280)))) { _revertReentrantCall() } // Store nonce to storage at one time. sstore(mload(0x280), mload(0x2a0)) // Clear store nonceVector flag. mstore(0x240, 0) } // nonceRange = nonce >> 8 mstore(0x2c0, shr(8, mload(0x60))) // Calculate nonceVector.slot and store to memory. mstore(0x280, keccak256(0x2c0, 0x40)) // Load nonceVector from storage. nonceVectorForCheckingNonReentrant := sload(mload(0x280)) // Store nonceVector to memory. mstore(0x2a0, nonceVectorForCheckingNonReentrant) } // memory[0x260 - 0x280] nonceMask // nonceMask = 1 << (nonce & 0xff) mstore(0x260, shl(and(mload(0x60), 0xff), 1)) // if order is not filled. // if (nonceVector & nonceMask == 0) if iszero(and(mload(0x2a0), mload(0x260))) { // struct OrderItem { // uint256 erc20TokenAmount; // uint256 nftId; // } let offsetOrderItem := add(add(offsetCollection, 0x40), mul(filledIndex, 0x40)) if gt(calldataload(offsetOrderItem), MAX_ERC20_AMOUNT) { _revertERC20AmountExceedsLimit() } // memory[0xe0 - 0x100] nftAddress // memory[0x1c0 - 0x1e0] selector for `transferFrom(address,address,uint256)` // memory[0x1e0 - 0x200] maker // memory[0x200 - 0x220] taker // memory[0x220 - 0x240] nftId mstore(0x220, calldataload(add(offsetOrderItem, 0x20))) // transferERC721 // 0x1dc = 0x1c0 + 28 if call(gas(), mload(0xe0), 0, 0x1dc, 0x64, 0, 0) { // Set store nonceVector flag. mstore(0x240, 1) // Update nonceVector. // nonceVector |= nonceMask mstore(0x2a0, or(mload(0x2a0), mload(0x260))) // Calculate fees. // memory[0xa0 - 0xc0] erc20TokenAmount mstore(0xa0, calldataload(offsetOrderItem)) // memory[0x140 - 0x1c0] fees.data // memory[0x140 - 0x160] platformFeeRecipient // memory[0x160 - 0x180] platformFeeAmount // memory[0x180 - 0x1a0] royaltyFeeRecipient // memory[0x1a0 - 0x1c0] royaltyFeeAmount // memory[0x320 - 0x340] platformFeePercentage // platformFeeAmount = erc20TokenAmount * platformFeePercentage / 10000 mstore(0x160, div(mul(mload(0xa0), mload(0x320)), 10000)) // memory[0x340 - 0x360] royaltyFeePercentage // royaltyFeeAmount = erc20TokenAmount * royaltyFeePercentage / 10000 mstore(0x1a0, div(mul(mload(0xa0), mload(0x340)), 10000)) // Update total erc20 amount. // totalERC20AmountToMaker += erc20TokenAmount - (platformFeeAmount + royaltyFeeAmount) totalERC20AmountToMaker := add(totalERC20AmountToMaker, sub(mload(0xa0), add(mload(0x160), mload(0x1a0)))) // totalERC20AmountToPlatform += platformFeeAmount totalERC20AmountToPlatform := add(totalERC20AmountToPlatform, mload(0x160)) // totalERC20AmountToRoyalty += royaltyFeeAmount totalERC20AmountToRoyalty := add(totalERC20AmountToRoyalty, mload(0x1a0)) // Emit event // memory[0 - 0x20] orderHash // memory[0x20 - 0x40] maker // memory[0x40 - 0x60] taker // memory[0x60 - 0x80] nonce // memory[0x80 - 0xa0] erc20Token // memory[0xa0 - 0xc0] erc20TokenAmount // memory[0xc0 - 0xe0] fees.offset // memory[0xe0 - 0x100] nftAddress // memory[0x100 - 0x120] nftId mstore(0x100, mload(0x220)) // fees switch platformFeeRecipient case 0 { // memory[0x180 - 0x1a0] royaltyFeeRecipient switch mload(0x180) case 0 { // memory[0x120 - 0x140] fees.length mstore(0x120, 0) // emit event log1(0, 320 /* 10 * 32 */, _TOPIC_SELL_ORDER_FILLED) } default { // memory[0x120 - 0x140] fees.length mstore(0x120, 1) // Copy royaltyFeeRecipient to memory[0x140 - 0x160] mstore(0x140, mload(0x180)) // Copy royaltyFeeAmount to memory[0x160 - 0x180] mstore(0x160, mload(0x1a0)) // emit event log1(0, 384 /* 12 * 32 */, _TOPIC_SELL_ORDER_FILLED) } } default { // memory[0x180 - 0x1a0] royaltyFeeRecipient switch mload(0x180) case 0 { // memory[0x120 - 0x140] fees.length mstore(0x120, 1) // emit event log1(0, 384 /* 12 * 32 */, _TOPIC_SELL_ORDER_FILLED) } default { // memory[0x120 - 0x140] fees.length mstore(0x120, 2) // emit event log1(0, 448 /* 14 * 32 */, _TOPIC_SELL_ORDER_FILLED) } } // Set someSuccess flag. mstore(0x360, 1) } } } // end for // Update offsetCollection. offsetCollection := add(add(offsetCollection, 0x40), mul(itemsCount, 0x40)) } // end collection // Update collectionStartNonce. // collectionStartNonce += itemsCount // memory[0x300 - 0x320] collection.head2 collectionStartNonce := add(collectionStartNonce, itemsCount) // Pay royaltyFee together. if totalERC20AmountToRoyalty { // memory[0x80 - 0xa0] erc20Token // memory[0x180 - 0x1a0] royaltyFeeRecipient // memory[0x380 - 0x3a0] erc20TokenFromDelegateCall // memory[0x3c0 - 0x3e0] royaltyFeeRecipientFromDelegateCall switch and(eq(mload(0x80), mload(0x380)), eq(mload(0x180), mload(0x3c0))) case 1 { // memory[0x400 - 0x420] royaltyFeeGlobal mstore(0x400, add(mload(0x400), totalERC20AmountToRoyalty)) } default { _transferERC20(mload(0x180), totalERC20AmountToRoyalty) } } } // end for // Store nonce to storage if needed. if mload(0x240) { // Revert if reentrant. if iszero(eq(nonceVectorForCheckingNonReentrant, sload(mload(0x280)))) { _revertReentrantCall() } // Store nonce to storage at one time. // memory[0x280 - 0x2a0] nonceVector.slot // memory[0x2a0 - 0x2c0] nonceVector sstore(mload(0x280), mload(0x2a0)) } // Pay to maker at one time. if totalERC20AmountToMaker { // memory[0x20 - 0x40] maker _transferERC20(mload(0x20), totalERC20AmountToMaker) } // Pay to platform at one time. if totalERC20AmountToPlatform { // memory[0x80 - 0xa0] erc20Token // memory[0x380 - 0x3a0] erc20TokenFromDelegateCall // memory[0x3a0 - 0x3c0] platformFeeRecipientFromDelegateCall switch and(eq(mload(0x80), mload(0x380)), eq(platformFeeRecipient, mload(0x3a0))) case 1 { // memory[0x3e0 - 0x400] platformFeeGlobal mstore(0x3e0, add(mload(0x3e0), totalERC20AmountToPlatform)) } default { _transferERC20(platformFeeRecipient, totalERC20AmountToPlatform) } } // Revert if none of the orders is filled. // memory[0x360 - 0x380] someSuccess flag if iszero(mload(0x360)) { _revertNoOrderFilled() } ///////////////////////////////// functions ///////////////////////////////// function _transferERC20(_to, _amount) { // memory[0x80 - 0xa0] erc20Token switch mload(0x80) case 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE { if iszero(call(gas(), _to, _amount, 0, 0, 0, 0)) { _revertTransferETHFailed() } } default { if iszero(extcodesize(mload(0x80))) { _revertInvalidERC20Token() } // memory[0x420 - 0x440] selector for `transferFrom(address,address,uint256)` // memory[0x440 - 0x460] msg.sender // memory[0x460 - 0x480] to mstore(0x460, _to) // memory[0x480 - 0x4a0] amount mstore(0x480, _amount) // memory[0x80 - 0xa0] erc20Token // 0x43c = 0x420 + 28 if iszero(call(gas(), mload(0x80), 0, 0x43c, 0x64, 0x480, 0x20)) { _revertTransferERC20Failed() } // Check for ERC20 success. ERC20 tokens should return a boolean, but some don't. // We accept 0-length return data as success, or at least 32 bytes that starts with // a 32-byte boolean true. if returndatasize() { if lt(returndatasize(), 0x20) { _revertTransferERC20Failed() } if iszero(eq(mload(0x480), 1)) { _revertTransferERC20Failed() } } } } function revertFeesPercentageExceedsLimit() { // revert("fillBatchSignedERC721Order: total fees percentage exceeds the limit.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000004466696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x746f74616c20666565732070657263656e746167652065786365656473207468) mstore(0x80, 0x65206c696d69742e000000000000000000000000000000000000000000000000) mstore(0xa0, 0) revert(0, 0xa4) } function _revertInvalidFilledIndex() { // revert("fillBatchSignedERC721Order: invalid filledIndex.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003066696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c69642066696c6c6564496e6465782e000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertReentrantCall() { // revert("fillBatchSignedERC721Order: reentrant call.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000002b66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x7265656e7472616e742063616c6c2e0000000000000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertERC20AmountExceedsLimit() { // revert("fillBatchSignedERC721Order: erc20TokenAmount exceeds limit.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003b66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x6572633230546f6b656e416d6f756e742065786365656473206c696d69742e00) mstore(0x80, 0) revert(0, 0x84) } function _revertNoOrderFilled() { // revert("fillBatchSignedERC721Order: no order filled.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000002c66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x6e6f206f726465722066696c6c65642e00000000000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertTransferETHFailed() { // revert("fillBatchSignedERC721Order: failed to transfer ETH.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003366696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x6661696c656420746f207472616e73666572204554482e000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertTransferERC20Failed() { // revert("fillBatchSignedERC721Order: failed to transfer ERC20.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003566696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x6661696c656420746f207472616e736665722045524332302e00000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertInvalidERC20Token() { // revert("invalid erc20 token") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x00000013696e76616c696420657263323020746f6b656e000000000000000000) mstore(0x60, 0) revert(0, 0x64) } function _revertInvalidERC721Token() { // revert("invalid erc271 token") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x00000014696e76616c69642065726337323120746f6b656e0000000000000000) mstore(0x60, 0) revert(0, 0x64) } } } /// data1 [8 bits(signatureType) + 8 bits(reserved) + 40 bits(startNonce) + 8 bits(v) + 32 bits(listingTime) + 160 bits(maker)] /// data2 [64 bits(taker part1) + 32 bits(expiryTime) + 160 bits(erc20Token)] function _validateOrder(uint256 offsetCollectionsBytes) internal view returns (bytes32 orderHash) { address maker; uint8 signatureType; uint8 v; assembly { let data1 := calldataload(0x4) let data2 := calldataload(0x24) signatureType := byte(0, data1) v := byte(7, data1) // Check for listingTime. // if ((data1 >> 160) & MASK_32 > block.timestamp) if gt(and(shr(160, data1), MASK_32), timestamp()) { // revert("fillBatchSignedERC721Order: failed to check for listingTime.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003c66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x6661696c656420746f20636865636b20666f72206c697374696e6754696d652e) mstore(0x80, 0) revert(0, 0x84) } // Check for expiryTime. // if ((data2 >> 160) & MASK_32 <= block.timestamp) if iszero(gt(and(shr(160, data2), MASK_32), timestamp())) { // revert("fillBatchSignedERC721Order: failed to check for expiryTime.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003b66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x6661696c656420746f20636865636b20666f722065787069727954696d652e00) mstore(0x80, 0) revert(0, 0x84) } // Check for erc20Token. if iszero(and(data2, MASK_160)) { // revert("fillBatchSignedERC721Order: invalid erc20Token.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000002f66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c6964206572633230546f6b656e2e00000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } // Check maker. maker := and(data1, MASK_160) if iszero(maker) { // revert("fillBatchSignedERC721Order: invalid maker.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000002a66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c6964206d616b65722e000000000000000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } } // Get order hash. orderHash = _getEIP712Hash(_getStructHash(offsetCollectionsBytes)); if (signatureType == 0) { bytes32 r; bytes32 s; assembly { // Must reset memory status before the `require` sentence. mstore(0x40, 0x80) mstore(0x60, 0) // Get r and v. r := calldataload(0x64) s := calldataload(0x84) } require(maker == ecrecover(orderHash, v, r, s), "fillBatchSignedERC721Order: failed to validate signature."); } else if (signatureType == 3) { assembly { // selector for `isValidSignature(bytes32,bytes)` mstore(0, 0x1626ba7e) mstore(0x20, orderHash) mstore(0x40, 0x40) mstore(0x60, 0x41) calldatacopy(0x80, 0x64, 64) mstore(0xc0, shl(248, v)) if iszero(extcodesize(maker)) { _revertInvalidMaker() } // Call signer with `isValidSignature` to validate signature. if iszero(staticcall(gas(), maker, 0x1c, 0xa5, 0, 0x20)) { _revertInvalidSignature() } // Check for returnData. if iszero(eq(mload(0), 0x1626ba7e00000000000000000000000000000000000000000000000000000000)) { _revertInvalidSignature() } function _revertInvalidMaker() { // revert("fillBatchSignedERC721Order: invalid maker.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000002a66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c6964206d616b65722e000000000000000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertInvalidSignature() { // revert("fillBatchSignedERC721Order: invalid signature.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003266696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c6964207369676e61747572652e0000000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } } } else { assembly { // revert("fillBatchSignedERC721Order: invalid signatureType.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003266696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c6964207369676e6174757265547970652e00000000000000000000) mstore(0x80, 0) revert(0, 0x84) } } } function _getEIP712Hash(bytes32 structHash) internal view returns (bytes32 eip712Hash) { assembly { // EIP712_DOMAIN_SEPARATOR = keccak256(abi.encode( // DOMAIN, // NAME, // VERSION, // block.chainid, // address(this) // )); mstore(0, DOMAIN) mstore(0x20, NAME) mstore(0x40, VERSION) mstore(0x60, chainid()) mstore(0x80, address()) // eip712Hash = keccak256(abi.encodePacked( // hex"1901", // EIP712_DOMAIN_SEPARATOR, // structHash // )); mstore(0xa0, 0x1901000000000000000000000000000000000000000000000000000000000000) mstore(0xa2, keccak256(0, 0xa0)) mstore(0xc2, structHash) eip712Hash := keccak256(0xa0, 0x42) } } /// data1 [[8 bits(signatureType) + 8 bits(reserved) + 40 bits(startNonce) + 8 bits(v) + 32 bits(listingTime) + 160 bits(maker)] /// data2 [64 bits(taker part1) + 32 bits(expiryTime) + 160 bits(erc20Token)] /// data3 [96 bits(taker part2) + 160 bits(platformFeeRecipient)] function _getStructHash(uint256 offsetCollectionsBytes) internal view returns (bytes32 structHash) { // structHash = keccak256(abi.encode( // _BATCH_SIGNED_ERC721_ORDERS_TYPE_HASH, // offset: 0x0 // maker, // offset: 0x20 // listingTime, // offset: 0x40 // expiryTime, // offset: 0x60 // startNonce, // offset: 0x80 // erc20Token, // offset: 0xa0 // platformFeeRecipient, // offset: 0xc0 // basicCollectionsHash, // offset: 0xe0 // collectionsHash, // offset: 0x100 // hashNonce // offset: 0x120 // )); // Store basicCollectionsHash to memory[0xe0] and store collectionsHash to memory[0x100]. _storeCollectionsHashToMemory(offsetCollectionsBytes); assembly { let data1 := calldataload(0x4) let data2 := calldataload(0x24) let data3 := calldataload(0x44) let maker := and(data1, MASK_160) // _BATCH_SIGNED_ERC721_ORDERS_TYPE_HASH mstore(0, _BATCH_SIGNED_ERC721_ORDERS_TYPE_HASH) // maker mstore(0x20, maker) // listingTime = (data1 >> 160) & MASK_32 mstore(0x40, and(shr(160, data1), MASK_32)) // expiryTime = (data2 >> 160) & MASK_32 mstore(0x60, and(shr(160, data2), MASK_32)) // startNonce = (data1 >> 200) & MASK_40 mstore(0x80, and(shr(200, data1), MASK_40)) // erc20Token = data2 & MASK_160 mstore(0xa0, and(data2, MASK_160)) // platformFeeRecipient = data3 & MASK_160 mstore(0xc0, and(data3, MASK_160)) // 0xe0 basicCollectionsHash // 0x100 collectionsHash // 0x120 hashNonce // hashNonce.slot = keccak256(abi.encode(maker, STORAGE_ID_COMMON_NFT_ORDERS)) // hashNonce = sload(hashNonce.slot) mstore(0x120, maker) mstore(0x140, STORAGE_ID_COMMON_NFT_ORDERS) mstore(0x120, sload(keccak256(0x120, 0x40))) structHash := keccak256(0, 0x140 /* 10 * 32 */) } } function _storeCollectionsHashToMemory(uint256 offsetCollectionsBytes) internal pure { assembly { let isBasicCollectionsEnded let basicCollectionsHash let ptrCollectionHash let offsetCollection := offsetCollectionsBytes let ptrEnd := add(offsetCollectionsBytes, calldataload(sub(offsetCollectionsBytes, 0x20))) for {} lt(offsetCollection, ptrEnd) {} { // head1 [96 bits(filledIndexList part1) + 160 bits(nftAddress)] // nftAddress = head1 & MASK_160 let nftAddress := and(calldataload(offsetCollection), MASK_160) if iszero(nftAddress) { _revertInvalidNftAddress() } // collectionType: 0 - basicCollection, 1 - collection // head2 [8 bits(collectionType) + 8 bits(itemsCount) + 8 bits(filledCount) + 8 bits(unused) + 32 bits(filledIndexList part2) // + 16 bits(platformFeePercentage) + 16 bits(royaltyFeePercentage) + 160 bits(royaltyFeeRecipient)] let head2 := calldataload(add(offsetCollection, 0x20)) let itemsCount := byte(1, head2) if iszero(itemsCount) { _revertInvalidItemCount() } let filledCount := byte(2, head2) if or(gt(filledCount, itemsCount), gt(filledCount, 16)) { _revertInvalidFilledCount() } // basicCollection if iszero(byte(0, head2)) { if isBasicCollectionsEnded { _revertInvalidCollectionsBytes() } // typeHash = _BASIC_COLLECTION_TYPE_HASH mstore(ptrCollectionHash, _BASIC_COLLECTION_TYPE_HASH) // nftAddress mstore(add(ptrCollectionHash, 0x20), nftAddress) // fee = head2 & MASK_192 mstore(add(ptrCollectionHash, 0x40), and(head2, MASK_192)) // itemsHash let ptrItemsHash := add(ptrCollectionHash, 0x60) let itemsBytesLength := mul(itemsCount, 0x20) // offset: 0x0 - head1 // 0x20 - head2 // 0x40 - items.data let offsetItems := add(offsetCollection, 0x40) calldatacopy(ptrItemsHash, offsetItems, itemsBytesLength) // Calculate and store itemsHash. mstore(ptrItemsHash, keccak256(ptrItemsHash, itemsBytesLength)) // keccak256(abi.encode(_BASIC_COLLECTION_TYPE_HASH, nftAddress, fee, itemsHash)) mstore(ptrCollectionHash, keccak256(ptrCollectionHash, 0x80)) // Update offset. ptrCollectionHash := add(ptrCollectionHash, 0x20) offsetCollection := add(offsetItems, itemsBytesLength) continue } // Get basicCollectionsHash. if iszero(isBasicCollectionsEnded) { // Set flag. isBasicCollectionsEnded := 1 switch ptrCollectionHash case 0 { // basicCollections is empty. basicCollectionsHash := _EMPTY_ARRAY_KECCAK256 } default { // Calculate basicCollectionsHash. basicCollectionsHash := keccak256(0, ptrCollectionHash) ptrCollectionHash := 0 } } // collection // typeHash = _COLLECTION_TYPE_HASH mstore(ptrCollectionHash, _COLLECTION_TYPE_HASH) // nftAddress mstore(add(ptrCollectionHash, 0x20), nftAddress) // fee = head2 & MASK_192 mstore(add(ptrCollectionHash, 0x40), and(head2, MASK_192)) // itemsHash let ptrItemsHash := add(ptrCollectionHash, 0x60) let itemsBytesLength := mul(itemsCount, 0x40) // offset: 0x0 - head1 // 0x20 - head2 // 0x40 - items.data let offsetItems := add(offsetCollection, 0x40) // Copy items to memory [ptrItemsHash + 0x20]. // Reserve a slot(0x20) to store _ORDER_ITEM_TYPE_HASH. calldatacopy(add(ptrItemsHash, 0x20), offsetItems, itemsBytesLength) let ptrItemHashData := ptrItemsHash let ptrItemEnd := add(ptrItemsHash, mul(itemsCount, 0x20)) for { let ptrItem := ptrItemsHash } lt(ptrItem, ptrItemEnd) {} { mstore(ptrItemHashData, _ORDER_ITEM_TYPE_HASH) mstore(ptrItem, keccak256(ptrItemHashData, 0x60)) ptrItem := add(ptrItem, 0x20) ptrItemHashData := add(ptrItemHashData, 0x40) } // Calculate and store itemsHash. mstore(ptrItemsHash, keccak256(ptrItemsHash, mul(itemsCount, 0x20))) // keccak256(abi.encode(_COLLECTION_TYPE_HASH, nftAddress, fee, itemsHash)) mstore(ptrCollectionHash, keccak256(ptrCollectionHash, 0x80)) // Update offset. ptrCollectionHash := add(ptrCollectionHash, 0x20) offsetCollection := add(offsetItems, itemsBytesLength) } // if (offsetCollection != ptrEnd) revert() if iszero(eq(offsetCollection, ptrEnd)) { _revertInvalidCollectionsBytes() } switch isBasicCollectionsEnded // Order.collections is empty. case 0 { // Order.basicCollections is empty. if iszero(ptrCollectionHash) { _revertInvalidCollectionsBytes() } // Store basicCollectionsHash to memory[0xe0]. mstore(0xe0, keccak256(0, ptrCollectionHash)) // Store collectionsHash to memory[0x100]. mstore(0x100, _EMPTY_ARRAY_KECCAK256) } // Order.collections is not empty. default { // Store basicCollectionsHash to memory[0xe0]. mstore(0xe0, basicCollectionsHash) // Store collectionsHash to memory[0x100]. mstore(0x100, keccak256(0, ptrCollectionHash)) } ///////////////////////////////// functions ///////////////////////////////// function _revertInvalidNftAddress() { // revert("fillBatchSignedERC721Order: invalid nftAddress.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000002f66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c6964206e6674416464726573732e00000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertInvalidItemCount() { // revert("fillBatchSignedERC721Order: invalid itemCount.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000002e66696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c6964206974656d436f756e742e0000000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertInvalidFilledCount() { // revert("fillBatchSignedERC721Order: invalid filledCount.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003066696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c69642066696c6c6564436f756e742e000000000000000000000000) mstore(0x80, 0) revert(0, 0x84) } function _revertInvalidCollectionsBytes() { // revert("fillBatchSignedERC721Order: invalid collectionsBytes.") mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000) mstore(0x20, 0x0000002000000000000000000000000000000000000000000000000000000000) mstore(0x40, 0x0000003566696c6c42617463685369676e65644552433732314f726465723a20) mstore(0x60, 0x696e76616c696420636f6c6c656374696f6e7342797465732e00000000000000) mstore(0x80, 0) revert(0, 0x84) } } } } // SPDX-License-Identifier: Apache-2.0 /* Copyright 2022 Element.Market Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.17; import "./LibStorage.sol"; library LibCommonNftOrdersStorage { /// @dev Storage bucket for this feature. struct Storage { /* Track per-maker nonces that can be incremented by the maker to cancel orders in bulk. */ // The current nonce for the maker represents the only valid nonce that can be signed by the maker // If a signature was signed with a nonce that's different from the one stored in nonces, it // will fail validation. mapping(address => uint256) hashNonces; } /// @dev Get the storage bucket for this contract. function getStorage() internal pure returns (Storage storage stor) { uint256 storageSlot = LibStorage.STORAGE_ID_COMMON_NFT_ORDERS; // Dip into assembly to change the slot pointed to by the local // variable `stor`. // See https://solidity.readthedocs.io/en/v0.6.8/assembly.html?highlight=slot#access-to-external-variables-functions-and-libraries assembly { stor.slot := storageSlot } } } // SPDX-License-Identifier: Apache-2.0 /* Modifications Copyright 2022 Element.Market Copyright 2020 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.17; import "./LibStorage.sol"; /// @dev Storage helpers for `ERC721OrdersFeature`. library LibERC721OrdersStorage { /// @dev Storage bucket for this feature. struct Storage { // maker => nonce range => order status bit vector mapping(address => mapping(uint248 => uint256)) orderStatusByMaker; // order hash => preSigned mapping(bytes32 => uint256) preSigned; // order hash => filledAmount mapping(bytes32 => uint128) filledAmount; } /// @dev Get the storage bucket for this contract. function getStorage() internal pure returns (Storage storage stor) { uint256 storageSlot = LibStorage.STORAGE_ID_ERC721_ORDERS; // Dip into assembly to change the slot pointed to by the local // variable `stor`. // See https://solidity.readthedocs.io/en/v0.6.8/assembly.html?highlight=slot#access-to-external-variables-functions-and-libraries assembly { stor.slot := storageSlot } } } // SPDX-License-Identifier: Apache-2.0 /* Modifications Copyright 2022 Element.Market Copyright 2020 ZeroEx Intl. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ pragma solidity ^0.8.17; /// @dev Common storage helpers library LibStorage { /// @dev What to bit-shift a storage ID by to get its slot. /// This gives us a maximum of 2**128 inline fields in each bucket. uint256 constant STORAGE_ID_PROXY = 1 << 128; uint256 constant STORAGE_ID_SIMPLE_FUNCTION_REGISTRY = 2 << 128; uint256 constant STORAGE_ID_OWNABLE = 3 << 128; uint256 constant STORAGE_ID_COMMON_NFT_ORDERS = 4 << 128; uint256 constant STORAGE_ID_ERC721_ORDERS = 5 << 128; uint256 constant STORAGE_ID_ERC1155_ORDERS = 6 << 128; uint256 constant STORAGE_ID_REENTRANCY_GUARD = 7 << 128; }
File 5 of 6: OperatorFilterRegistry
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol) pragma solidity ^0.8.0; import "../utils/Context.sol"; /** * @dev Contract module which provides a basic access control mechanism, where * there is an account (an owner) that can be granted exclusive access to * specific functions. * * By default, the owner account will be the one that deploys the contract. This * can later be changed with {transferOwnership}. * * This module is used through inheritance. It will make available the modifier * `onlyOwner`, which can be applied to your functions to restrict their use to * the owner. */ abstract contract Ownable is Context { address private _owner; event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); /** * @dev Initializes the contract setting the deployer as the initial owner. */ constructor() { _transferOwnership(_msgSender()); } /** * @dev Throws if called by any account other than the owner. */ modifier onlyOwner() { _checkOwner(); _; } /** * @dev Returns the address of the current owner. */ function owner() public view virtual returns (address) { return _owner; } /** * @dev Throws if the sender is not the owner. */ function _checkOwner() internal view virtual { require(owner() == _msgSender(), "Ownable: caller is not the owner"); } /** * @dev Leaves the contract without owner. It will not be possible to call * `onlyOwner` functions anymore. Can only be called by the current owner. * * NOTE: Renouncing ownership will leave the contract without an owner, * thereby removing any functionality that is only available to the owner. */ function renounceOwnership() public virtual onlyOwner { _transferOwnership(address(0)); } /** * @dev Transfers ownership of the contract to a new account (`newOwner`). * Can only be called by the current owner. */ function transferOwnership(address newOwner) public virtual onlyOwner { require(newOwner != address(0), "Ownable: new owner is the zero address"); _transferOwnership(newOwner); } /** * @dev Transfers ownership of the contract to a new account (`newOwner`). * Internal function without access restriction. */ function _transferOwnership(address newOwner) internal virtual { address oldOwner = _owner; _owner = newOwner; emit OwnershipTransferred(oldOwner, newOwner); } } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (utils/Context.sol) pragma solidity ^0.8.0; /** * @dev Provides information about the current execution context, including the * sender of the transaction and its data. While these are generally available * via msg.sender and msg.data, they should not be accessed in such a direct * manner, since when dealing with meta-transactions the account sending and * paying for execution may not be the actual sender (as far as an application * is concerned). * * This contract is only required for intermediate, library-like contracts. */ abstract contract Context { function _msgSender() internal view virtual returns (address) { return msg.sender; } function _msgData() internal view virtual returns (bytes calldata) { return msg.data; } } // SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.7.0) (utils/structs/EnumerableSet.sol) // This file was procedurally generated from scripts/generate/templates/EnumerableSet.js. pragma solidity ^0.8.0; /** * @dev Library for managing * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive * types. * * Sets have the following properties: * * - Elements are added, removed, and checked for existence in constant time * (O(1)). * - Elements are enumerated in O(n). No guarantees are made on the ordering. * * ``` * contract Example { * // Add the library methods * using EnumerableSet for EnumerableSet.AddressSet; * * // Declare a set state variable * EnumerableSet.AddressSet private mySet; * } * ``` * * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`) * and `uint256` (`UintSet`) are supported. * * [WARNING] * ==== * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure * unusable. * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info. * * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an * array of EnumerableSet. * ==== */ library EnumerableSet { // To implement this library for multiple types with as little code // repetition as possible, we write it in terms of a generic Set type with // bytes32 values. // The Set implementation uses private functions, and user-facing // implementations (such as AddressSet) are just wrappers around the // underlying Set. // This means that we can only create new EnumerableSets for types that fit // in bytes32. struct Set { // Storage of set values bytes32[] _values; // Position of the value in the `values` array, plus 1 because index 0 // means a value is not in the set. mapping(bytes32 => uint256) _indexes; } /** * @dev Add a value to a set. O(1). * * Returns true if the value was added to the set, that is if it was not * already present. */ function _add(Set storage set, bytes32 value) private returns (bool) { if (!_contains(set, value)) { set._values.push(value); // The value is stored at length-1, but we add 1 to all indexes // and use 0 as a sentinel value set._indexes[value] = set._values.length; return true; } else { return false; } } /** * @dev Removes a value from a set. O(1). * * Returns true if the value was removed from the set, that is if it was * present. */ function _remove(Set storage set, bytes32 value) private returns (bool) { // We read and store the value's index to prevent multiple reads from the same storage slot uint256 valueIndex = set._indexes[value]; if (valueIndex != 0) { // Equivalent to contains(set, value) // To delete an element from the _values array in O(1), we swap the element to delete with the last one in // the array, and then remove the last element (sometimes called as 'swap and pop'). // This modifies the order of the array, as noted in {at}. uint256 toDeleteIndex = valueIndex - 1; uint256 lastIndex = set._values.length - 1; if (lastIndex != toDeleteIndex) { bytes32 lastValue = set._values[lastIndex]; // Move the last value to the index where the value to delete is set._values[toDeleteIndex] = lastValue; // Update the index for the moved value set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex } // Delete the slot where the moved value was stored set._values.pop(); // Delete the index for the deleted slot delete set._indexes[value]; return true; } else { return false; } } /** * @dev Returns true if the value is in the set. O(1). */ function _contains(Set storage set, bytes32 value) private view returns (bool) { return set._indexes[value] != 0; } /** * @dev Returns the number of values on the set. O(1). */ function _length(Set storage set) private view returns (uint256) { return set._values.length; } /** * @dev Returns the value stored at position `index` in the set. O(1). * * Note that there are no guarantees on the ordering of values inside the * array, and it may change when more values are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function _at(Set storage set, uint256 index) private view returns (bytes32) { return set._values[index]; } /** * @dev Return the entire set in an array * * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that * this function has an unbounded cost, and using it as part of a state-changing function may render the function * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. */ function _values(Set storage set) private view returns (bytes32[] memory) { return set._values; } // Bytes32Set struct Bytes32Set { Set _inner; } /** * @dev Add a value to a set. O(1). * * Returns true if the value was added to the set, that is if it was not * already present. */ function add(Bytes32Set storage set, bytes32 value) internal returns (bool) { return _add(set._inner, value); } /** * @dev Removes a value from a set. O(1). * * Returns true if the value was removed from the set, that is if it was * present. */ function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) { return _remove(set._inner, value); } /** * @dev Returns true if the value is in the set. O(1). */ function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) { return _contains(set._inner, value); } /** * @dev Returns the number of values in the set. O(1). */ function length(Bytes32Set storage set) internal view returns (uint256) { return _length(set._inner); } /** * @dev Returns the value stored at position `index` in the set. O(1). * * Note that there are no guarantees on the ordering of values inside the * array, and it may change when more values are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) { return _at(set._inner, index); } /** * @dev Return the entire set in an array * * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that * this function has an unbounded cost, and using it as part of a state-changing function may render the function * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. */ function values(Bytes32Set storage set) internal view returns (bytes32[] memory) { bytes32[] memory store = _values(set._inner); bytes32[] memory result; /// @solidity memory-safe-assembly assembly { result := store } return result; } // AddressSet struct AddressSet { Set _inner; } /** * @dev Add a value to a set. O(1). * * Returns true if the value was added to the set, that is if it was not * already present. */ function add(AddressSet storage set, address value) internal returns (bool) { return _add(set._inner, bytes32(uint256(uint160(value)))); } /** * @dev Removes a value from a set. O(1). * * Returns true if the value was removed from the set, that is if it was * present. */ function remove(AddressSet storage set, address value) internal returns (bool) { return _remove(set._inner, bytes32(uint256(uint160(value)))); } /** * @dev Returns true if the value is in the set. O(1). */ function contains(AddressSet storage set, address value) internal view returns (bool) { return _contains(set._inner, bytes32(uint256(uint160(value)))); } /** * @dev Returns the number of values in the set. O(1). */ function length(AddressSet storage set) internal view returns (uint256) { return _length(set._inner); } /** * @dev Returns the value stored at position `index` in the set. O(1). * * Note that there are no guarantees on the ordering of values inside the * array, and it may change when more values are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function at(AddressSet storage set, uint256 index) internal view returns (address) { return address(uint160(uint256(_at(set._inner, index)))); } /** * @dev Return the entire set in an array * * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that * this function has an unbounded cost, and using it as part of a state-changing function may render the function * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. */ function values(AddressSet storage set) internal view returns (address[] memory) { bytes32[] memory store = _values(set._inner); address[] memory result; /// @solidity memory-safe-assembly assembly { result := store } return result; } // UintSet struct UintSet { Set _inner; } /** * @dev Add a value to a set. O(1). * * Returns true if the value was added to the set, that is if it was not * already present. */ function add(UintSet storage set, uint256 value) internal returns (bool) { return _add(set._inner, bytes32(value)); } /** * @dev Removes a value from a set. O(1). * * Returns true if the value was removed from the set, that is if it was * present. */ function remove(UintSet storage set, uint256 value) internal returns (bool) { return _remove(set._inner, bytes32(value)); } /** * @dev Returns true if the value is in the set. O(1). */ function contains(UintSet storage set, uint256 value) internal view returns (bool) { return _contains(set._inner, bytes32(value)); } /** * @dev Returns the number of values in the set. O(1). */ function length(UintSet storage set) internal view returns (uint256) { return _length(set._inner); } /** * @dev Returns the value stored at position `index` in the set. O(1). * * Note that there are no guarantees on the ordering of values inside the * array, and it may change when more values are added or removed. * * Requirements: * * - `index` must be strictly less than {length}. */ function at(UintSet storage set, uint256 index) internal view returns (uint256) { return uint256(_at(set._inner, index)); } /** * @dev Return the entire set in an array * * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that * this function has an unbounded cost, and using it as part of a state-changing function may render the function * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. */ function values(UintSet storage set) internal view returns (uint256[] memory) { bytes32[] memory store = _values(set._inner); uint256[] memory result; /// @solidity memory-safe-assembly assembly { result := store } return result; } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; import {EnumerableSet} from "openzeppelin-contracts/utils/structs/EnumerableSet.sol"; interface IOperatorFilterRegistry { function isOperatorAllowed(address registrant, address operator) external returns (bool); function register(address registrant) external; function registerAndSubscribe(address registrant, address subscription) external; function registerAndCopyEntries(address registrant, address registrantToCopy) external; function updateOperator(address registrant, address operator, bool filtered) external; function updateOperators(address registrant, address[] calldata operators, bool filtered) external; function updateCodeHash(address registrant, bytes32 codehash, bool filtered) external; function updateCodeHashes(address registrant, bytes32[] calldata codeHashes, bool filtered) external; function subscribe(address registrant, address registrantToSubscribe) external; function unsubscribe(address registrant, bool copyExistingEntries) external; function subscriptionOf(address addr) external returns (address registrant); function subscribers(address registrant) external returns (address[] memory); function subscriberAt(address registrant, uint256 index) external returns (address); function copyEntriesOf(address registrant, address registrantToCopy) external; function isOperatorFiltered(address registrant, address operator) external returns (bool); function isCodeHashOfFiltered(address registrant, address operatorWithCode) external returns (bool); function isCodeHashFiltered(address registrant, bytes32 codeHash) external returns (bool); function filteredOperators(address addr) external returns (address[] memory); function filteredCodeHashes(address addr) external returns (bytes32[] memory); function filteredOperatorAt(address registrant, uint256 index) external returns (address); function filteredCodeHashAt(address registrant, uint256 index) external returns (bytes32); function isRegistered(address addr) external returns (bool); function codeHashOf(address addr) external returns (bytes32); } // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; import {IOperatorFilterRegistry} from "./IOperatorFilterRegistry.sol"; import {Ownable} from "openzeppelin-contracts/access/Ownable.sol"; import {EnumerableSet} from "openzeppelin-contracts/utils/structs/EnumerableSet.sol"; import {OperatorFilterRegistryErrorsAndEvents} from "./OperatorFilterRegistryErrorsAndEvents.sol"; /** * @title OperatorFilterRegistry * @notice Borrows heavily from the QQL BlacklistOperatorFilter contract: * https://github.com/qql-art/contracts/blob/main/contracts/BlacklistOperatorFilter.sol * @notice This contracts allows tokens or token owners to register specific addresses or codeHashes that may be * * restricted according to the isOperatorAllowed function. */ contract OperatorFilterRegistry is IOperatorFilterRegistry, OperatorFilterRegistryErrorsAndEvents { using EnumerableSet for EnumerableSet.AddressSet; using EnumerableSet for EnumerableSet.Bytes32Set; /// @dev initialized accounts have a nonzero codehash (see https://eips.ethereum.org/EIPS/eip-1052) /// Note that this will also be a smart contract's codehash when making calls from its constructor. bytes32 constant EOA_CODEHASH = keccak256(""); mapping(address => EnumerableSet.AddressSet) private _filteredOperators; mapping(address => EnumerableSet.Bytes32Set) private _filteredCodeHashes; mapping(address => address) private _registrations; mapping(address => EnumerableSet.AddressSet) private _subscribers; /** * @notice restricts method caller to the address or EIP-173 "owner()" */ modifier onlyAddressOrOwner(address addr) { if (msg.sender != addr) { try Ownable(addr).owner() returns (address owner) { if (msg.sender != owner) { revert OnlyAddressOrOwner(); } } catch (bytes memory reason) { if (reason.length == 0) { revert NotOwnable(); } else { /// @solidity memory-safe-assembly assembly { revert(add(32, reason), mload(reason)) } } } } _; } /** * @notice Returns true if operator is not filtered for a given token, either by address or codeHash. Also returns * true if supplied registrant address is not registered. */ function isOperatorAllowed(address registrant, address operator) external view returns (bool) { address registration = _registrations[registrant]; if (registration != address(0)) { EnumerableSet.AddressSet storage filteredOperatorsRef; EnumerableSet.Bytes32Set storage filteredCodeHashesRef; filteredOperatorsRef = _filteredOperators[registration]; filteredCodeHashesRef = _filteredCodeHashes[registration]; if (filteredOperatorsRef.contains(operator)) { revert AddressFiltered(operator); } if (operator.code.length > 0) { bytes32 codeHash = operator.codehash; if (filteredCodeHashesRef.contains(codeHash)) { revert CodeHashFiltered(operator, codeHash); } } } return true; } ////////////////// // AUTH METHODS // ////////////////// /** * @notice Registers an address with the registry. May be called by address itself or by EIP-173 owner. */ function register(address registrant) external onlyAddressOrOwner(registrant) { if (_registrations[registrant] != address(0)) { revert AlreadyRegistered(); } _registrations[registrant] = registrant; emit RegistrationUpdated(registrant, true); } /** * @notice Unregisters an address with the registry and removes its subscription. May be called by address itself or by EIP-173 owner. * Note that this does not remove any filtered addresses or codeHashes. * Also note that any subscriptions to this registrant will still be active and follow the existing filtered addresses and codehashes. */ function unregister(address registrant) external onlyAddressOrOwner(registrant) { address registration = _registrations[registrant]; if (registration == address(0)) { revert NotRegistered(registrant); } if (registration != registrant) { _subscribers[registration].remove(registrant); emit SubscriptionUpdated(registrant, registration, false); } _registrations[registrant] = address(0); emit RegistrationUpdated(registrant, false); } /** * @notice Registers an address with the registry and "subscribes" to another address's filtered operators and codeHashes. */ function registerAndSubscribe(address registrant, address subscription) external onlyAddressOrOwner(registrant) { address registration = _registrations[registrant]; if (registration != address(0)) { revert AlreadyRegistered(); } if (registrant == subscription) { revert CannotSubscribeToSelf(); } address subscriptionRegistration = _registrations[subscription]; if (subscriptionRegistration == address(0)) { revert NotRegistered(subscription); } if (subscriptionRegistration != subscription) { revert CannotSubscribeToRegistrantWithSubscription(subscription); } _registrations[registrant] = subscription; _subscribers[subscription].add(registrant); emit RegistrationUpdated(registrant, true); emit SubscriptionUpdated(registrant, subscription, true); } /** * @notice Registers an address with the registry and copies the filtered operators and codeHashes from another * address without subscribing. */ function registerAndCopyEntries(address registrant, address registrantToCopy) external onlyAddressOrOwner(registrant) { if (registrantToCopy == registrant) { revert CannotCopyFromSelf(); } address registration = _registrations[registrant]; if (registration != address(0)) { revert AlreadyRegistered(); } address registrantRegistration = _registrations[registrantToCopy]; if (registrantRegistration == address(0)) { revert NotRegistered(registrantToCopy); } _registrations[registrant] = registrant; emit RegistrationUpdated(registrant, true); _copyEntries(registrant, registrantToCopy); } /** * @notice Update an operator address for a registered address - when filtered is true, the operator is filtered. */ function updateOperator(address registrant, address operator, bool filtered) external onlyAddressOrOwner(registrant) { address registration = _registrations[registrant]; if (registration == address(0)) { revert NotRegistered(registrant); } if (registration != registrant) { revert CannotUpdateWhileSubscribed(registration); } EnumerableSet.AddressSet storage filteredOperatorsRef = _filteredOperators[registrant]; if (!filtered) { bool removed = filteredOperatorsRef.remove(operator); if (!removed) { revert AddressNotFiltered(operator); } } else { bool added = filteredOperatorsRef.add(operator); if (!added) { revert AddressAlreadyFiltered(operator); } } emit OperatorUpdated(registrant, operator, filtered); } /** * @notice Update a codeHash for a registered address - when filtered is true, the codeHash is filtered. */ function updateCodeHash(address registrant, bytes32 codeHash, bool filtered) external onlyAddressOrOwner(registrant) { if (codeHash == EOA_CODEHASH) { revert CannotFilterEOAs(); } address registration = _registrations[registrant]; if (registration == address(0)) { revert NotRegistered(registrant); } if (registration != registrant) { revert CannotUpdateWhileSubscribed(registration); } EnumerableSet.Bytes32Set storage filteredCodeHashesRef = _filteredCodeHashes[registrant]; if (!filtered) { bool removed = filteredCodeHashesRef.remove(codeHash); if (!removed) { revert CodeHashNotFiltered(codeHash); } } else { bool added = filteredCodeHashesRef.add(codeHash); if (!added) { revert CodeHashAlreadyFiltered(codeHash); } } emit CodeHashUpdated(registrant, codeHash, filtered); } /** * @notice Update multiple operators for a registered address - when filtered is true, the operators will be filtered. Reverts on duplicates. */ function updateOperators(address registrant, address[] calldata operators, bool filtered) external onlyAddressOrOwner(registrant) { address registration = _registrations[registrant]; if (registration == address(0)) { revert NotRegistered(registrant); } if (registration != registrant) { revert CannotUpdateWhileSubscribed(registration); } EnumerableSet.AddressSet storage filteredOperatorsRef = _filteredOperators[registrant]; uint256 operatorsLength = operators.length; unchecked { if (!filtered) { for (uint256 i = 0; i < operatorsLength; ++i) { address operator = operators[i]; bool removed = filteredOperatorsRef.remove(operator); if (!removed) { revert AddressNotFiltered(operator); } } } else { for (uint256 i = 0; i < operatorsLength; ++i) { address operator = operators[i]; bool added = filteredOperatorsRef.add(operator); if (!added) { revert AddressAlreadyFiltered(operator); } } } } emit OperatorsUpdated(registrant, operators, filtered); } /** * @notice Update multiple codeHashes for a registered address - when filtered is true, the codeHashes will be filtered. Reverts on duplicates. */ function updateCodeHashes(address registrant, bytes32[] calldata codeHashes, bool filtered) external onlyAddressOrOwner(registrant) { address registration = _registrations[registrant]; if (registration == address(0)) { revert NotRegistered(registrant); } if (registration != registrant) { revert CannotUpdateWhileSubscribed(registration); } EnumerableSet.Bytes32Set storage filteredCodeHashesRef = _filteredCodeHashes[registrant]; uint256 codeHashesLength = codeHashes.length; unchecked { if (!filtered) { for (uint256 i = 0; i < codeHashesLength; ++i) { bytes32 codeHash = codeHashes[i]; bool removed = filteredCodeHashesRef.remove(codeHash); if (!removed) { revert CodeHashNotFiltered(codeHash); } } } else { for (uint256 i = 0; i < codeHashesLength; ++i) { bytes32 codeHash = codeHashes[i]; if (codeHash == EOA_CODEHASH) { revert CannotFilterEOAs(); } bool added = filteredCodeHashesRef.add(codeHash); if (!added) { revert CodeHashAlreadyFiltered(codeHash); } } } } emit CodeHashesUpdated(registrant, codeHashes, filtered); } /** * @notice Subscribe an address to another registrant's filtered operators and codeHashes. Will remove previous * subscription if present. * Note that accounts with subscriptions may go on to subscribe to other accounts - in this case, * subscriptions will not be forwarded. Instead the former subscription's existing entries will still be * used. */ function subscribe(address registrant, address newSubscription) external onlyAddressOrOwner(registrant) { if (registrant == newSubscription) { revert CannotSubscribeToSelf(); } if (newSubscription == address(0)) { revert CannotSubscribeToZeroAddress(); } address registration = _registrations[registrant]; if (registration == address(0)) { revert NotRegistered(registrant); } if (registration == newSubscription) { revert AlreadySubscribed(newSubscription); } address newSubscriptionRegistration = _registrations[newSubscription]; if (newSubscriptionRegistration == address(0)) { revert NotRegistered(newSubscription); } if (newSubscriptionRegistration != newSubscription) { revert CannotSubscribeToRegistrantWithSubscription(newSubscription); } if (registration != registrant) { _subscribers[registration].remove(registrant); emit SubscriptionUpdated(registrant, registration, false); } _registrations[registrant] = newSubscription; _subscribers[newSubscription].add(registrant); emit SubscriptionUpdated(registrant, newSubscription, true); } /** * @notice Unsubscribe an address from its current subscribed registrant, and optionally copy its filtered operators and codeHashes. */ function unsubscribe(address registrant, bool copyExistingEntries) external onlyAddressOrOwner(registrant) { address registration = _registrations[registrant]; if (registration == address(0)) { revert NotRegistered(registrant); } if (registration == registrant) { revert NotSubscribed(); } _subscribers[registration].remove(registrant); _registrations[registrant] = registrant; emit SubscriptionUpdated(registrant, registration, false); if (copyExistingEntries) { _copyEntries(registrant, registration); } } /** * @notice Copy filtered operators and codeHashes from a different registrantToCopy to addr. */ function copyEntriesOf(address registrant, address registrantToCopy) external onlyAddressOrOwner(registrant) { if (registrant == registrantToCopy) { revert CannotCopyFromSelf(); } address registration = _registrations[registrant]; if (registration == address(0)) { revert NotRegistered(registrant); } if (registration != registrant) { revert CannotUpdateWhileSubscribed(registration); } address registrantRegistration = _registrations[registrantToCopy]; if (registrantRegistration == address(0)) { revert NotRegistered(registrantToCopy); } _copyEntries(registrant, registrantToCopy); } /// @dev helper to copy entries from registrantToCopy to registrant and emit events function _copyEntries(address registrant, address registrantToCopy) private { EnumerableSet.AddressSet storage filteredOperatorsRef = _filteredOperators[registrantToCopy]; EnumerableSet.Bytes32Set storage filteredCodeHashesRef = _filteredCodeHashes[registrantToCopy]; uint256 filteredOperatorsLength = filteredOperatorsRef.length(); uint256 filteredCodeHashesLength = filteredCodeHashesRef.length(); unchecked { for (uint256 i = 0; i < filteredOperatorsLength; ++i) { address operator = filteredOperatorsRef.at(i); bool added = _filteredOperators[registrant].add(operator); if (added) { emit OperatorUpdated(registrant, operator, true); } } for (uint256 i = 0; i < filteredCodeHashesLength; ++i) { bytes32 codehash = filteredCodeHashesRef.at(i); bool added = _filteredCodeHashes[registrant].add(codehash); if (added) { emit CodeHashUpdated(registrant, codehash, true); } } } } ////////////////// // VIEW METHODS // ////////////////// /** * @notice Get the subscription address of a given registrant, if any. */ function subscriptionOf(address registrant) external view returns (address subscription) { subscription = _registrations[registrant]; if (subscription == address(0)) { revert NotRegistered(registrant); } else if (subscription == registrant) { subscription = address(0); } } /** * @notice Get the set of addresses subscribed to a given registrant. * Note that order is not guaranteed as updates are made. */ function subscribers(address registrant) external view returns (address[] memory) { return _subscribers[registrant].values(); } /** * @notice Get the subscriber at a given index in the set of addresses subscribed to a given registrant. * Note that order is not guaranteed as updates are made. */ function subscriberAt(address registrant, uint256 index) external view returns (address) { return _subscribers[registrant].at(index); } /** * @notice Returns true if operator is filtered by a given address or its subscription. */ function isOperatorFiltered(address registrant, address operator) external view returns (bool) { address registration = _registrations[registrant]; if (registration != registrant) { return _filteredOperators[registration].contains(operator); } return _filteredOperators[registrant].contains(operator); } /** * @notice Returns true if a codeHash is filtered by a given address or its subscription. */ function isCodeHashFiltered(address registrant, bytes32 codeHash) external view returns (bool) { address registration = _registrations[registrant]; if (registration != registrant) { return _filteredCodeHashes[registration].contains(codeHash); } return _filteredCodeHashes[registrant].contains(codeHash); } /** * @notice Returns true if the hash of an address's code is filtered by a given address or its subscription. */ function isCodeHashOfFiltered(address registrant, address operatorWithCode) external view returns (bool) { bytes32 codeHash = operatorWithCode.codehash; address registration = _registrations[registrant]; if (registration != registrant) { return _filteredCodeHashes[registration].contains(codeHash); } return _filteredCodeHashes[registrant].contains(codeHash); } /** * @notice Returns true if an address has registered */ function isRegistered(address registrant) external view returns (bool) { return _registrations[registrant] != address(0); } /** * @notice Returns a list of filtered operators for a given address or its subscription. */ function filteredOperators(address registrant) external view returns (address[] memory) { address registration = _registrations[registrant]; if (registration != registrant) { return _filteredOperators[registration].values(); } return _filteredOperators[registrant].values(); } /** * @notice Returns the set of filtered codeHashes for a given address or its subscription. * Note that order is not guaranteed as updates are made. */ function filteredCodeHashes(address registrant) external view returns (bytes32[] memory) { address registration = _registrations[registrant]; if (registration != registrant) { return _filteredCodeHashes[registration].values(); } return _filteredCodeHashes[registrant].values(); } /** * @notice Returns the filtered operator at the given index of the set of filtered operators for a given address or * its subscription. * Note that order is not guaranteed as updates are made. */ function filteredOperatorAt(address registrant, uint256 index) external view returns (address) { address registration = _registrations[registrant]; if (registration != registrant) { return _filteredOperators[registration].at(index); } return _filteredOperators[registrant].at(index); } /** * @notice Returns the filtered codeHash at the given index of the list of filtered codeHashes for a given address or * its subscription. * Note that order is not guaranteed as updates are made. */ function filteredCodeHashAt(address registrant, uint256 index) external view returns (bytes32) { address registration = _registrations[registrant]; if (registration != registrant) { return _filteredCodeHashes[registration].at(index); } return _filteredCodeHashes[registrant].at(index); } /// @dev Convenience method to compute the code hash of an arbitrary contract function codeHashOf(address a) external view returns (bytes32) { return a.codehash; } } // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; contract OperatorFilterRegistryErrorsAndEvents { error CannotFilterEOAs(); error AddressAlreadyFiltered(address operator); error AddressNotFiltered(address operator); error CodeHashAlreadyFiltered(bytes32 codeHash); error CodeHashNotFiltered(bytes32 codeHash); error OnlyAddressOrOwner(); error NotRegistered(address registrant); error AlreadyRegistered(); error AlreadySubscribed(address subscription); error NotSubscribed(); error CannotUpdateWhileSubscribed(address subscription); error CannotSubscribeToSelf(); error CannotSubscribeToZeroAddress(); error NotOwnable(); error AddressFiltered(address filtered); error CodeHashFiltered(address account, bytes32 codeHash); error CannotSubscribeToRegistrantWithSubscription(address registrant); error CannotCopyFromSelf(); event RegistrationUpdated(address indexed registrant, bool indexed registered); event OperatorUpdated(address indexed registrant, address indexed operator, bool indexed filtered); event OperatorsUpdated(address indexed registrant, address[] operators, bool indexed filtered); event CodeHashUpdated(address indexed registrant, bytes32 indexed codeHash, bool indexed filtered); event CodeHashesUpdated(address indexed registrant, bytes32[] codeHashes, bool indexed filtered); event SubscriptionUpdated(address indexed registrant, address indexed subscription, bool indexed subscribed); }
File 6 of 6: GnosisSafe
// SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; import "./base/ModuleManager.sol"; import "./base/OwnerManager.sol"; import "./base/FallbackManager.sol"; import "./base/GuardManager.sol"; import "./common/EtherPaymentFallback.sol"; import "./common/Singleton.sol"; import "./common/SignatureDecoder.sol"; import "./common/SecuredTokenTransfer.sol"; import "./common/StorageAccessible.sol"; import "./interfaces/ISignatureValidator.sol"; import "./external/GnosisSafeMath.sol"; /// @title Gnosis Safe - A multisignature wallet with support for confirmations using signed messages based on ERC191. /// @author Stefan George - <[email protected]> /// @author Richard Meissner - <[email protected]> contract GnosisSafe is EtherPaymentFallback, Singleton, ModuleManager, OwnerManager, SignatureDecoder, SecuredTokenTransfer, ISignatureValidatorConstants, FallbackManager, StorageAccessible, GuardManager { using GnosisSafeMath for uint256; string public constant VERSION = "1.3.0"; // keccak256( // "EIP712Domain(uint256 chainId,address verifyingContract)" // ); bytes32 private constant DOMAIN_SEPARATOR_TYPEHASH = 0x47e79534a245952e8b16893a336b85a3d9ea9fa8c573f3d803afb92a79469218; // keccak256( // "SafeTx(address to,uint256 value,bytes data,uint8 operation,uint256 safeTxGas,uint256 baseGas,uint256 gasPrice,address gasToken,address refundReceiver,uint256 nonce)" // ); bytes32 private constant SAFE_TX_TYPEHASH = 0xbb8310d486368db6bd6f849402fdd73ad53d316b5a4b2644ad6efe0f941286d8; event SafeSetup(address indexed initiator, address[] owners, uint256 threshold, address initializer, address fallbackHandler); event ApproveHash(bytes32 indexed approvedHash, address indexed owner); event SignMsg(bytes32 indexed msgHash); event ExecutionFailure(bytes32 txHash, uint256 payment); event ExecutionSuccess(bytes32 txHash, uint256 payment); uint256 public nonce; bytes32 private _deprecatedDomainSeparator; // Mapping to keep track of all message hashes that have been approve by ALL REQUIRED owners mapping(bytes32 => uint256) public signedMessages; // Mapping to keep track of all hashes (message or transaction) that have been approve by ANY owners mapping(address => mapping(bytes32 => uint256)) public approvedHashes; // This constructor ensures that this contract can only be used as a master copy for Proxy contracts constructor() { // By setting the threshold it is not possible to call setup anymore, // so we create a Safe with 0 owners and threshold 1. // This is an unusable Safe, perfect for the singleton threshold = 1; } /// @dev Setup function sets initial storage of contract. /// @param _owners List of Safe owners. /// @param _threshold Number of required confirmations for a Safe transaction. /// @param to Contract address for optional delegate call. /// @param data Data payload for optional delegate call. /// @param fallbackHandler Handler for fallback calls to this contract /// @param paymentToken Token that should be used for the payment (0 is ETH) /// @param payment Value that should be paid /// @param paymentReceiver Adddress that should receive the payment (or 0 if tx.origin) function setup( address[] calldata _owners, uint256 _threshold, address to, bytes calldata data, address fallbackHandler, address paymentToken, uint256 payment, address payable paymentReceiver ) external { // setupOwners checks if the Threshold is already set, therefore preventing that this method is called twice setupOwners(_owners, _threshold); if (fallbackHandler != address(0)) internalSetFallbackHandler(fallbackHandler); // As setupOwners can only be called if the contract has not been initialized we don't need a check for setupModules setupModules(to, data); if (payment > 0) { // To avoid running into issues with EIP-170 we reuse the handlePayment function (to avoid adjusting code of that has been verified we do not adjust the method itself) // baseGas = 0, gasPrice = 1 and gas = payment => amount = (payment + 0) * 1 = payment handlePayment(payment, 0, 1, paymentToken, paymentReceiver); } emit SafeSetup(msg.sender, _owners, _threshold, to, fallbackHandler); } /// @dev Allows to execute a Safe transaction confirmed by required number of owners and then pays the account that submitted the transaction. /// Note: The fees are always transferred, even if the user transaction fails. /// @param to Destination address of Safe transaction. /// @param value Ether value of Safe transaction. /// @param data Data payload of Safe transaction. /// @param operation Operation type of Safe transaction. /// @param safeTxGas Gas that should be used for the Safe transaction. /// @param baseGas Gas costs that are independent of the transaction execution(e.g. base transaction fee, signature check, payment of the refund) /// @param gasPrice Gas price that should be used for the payment calculation. /// @param gasToken Token address (or 0 if ETH) that is used for the payment. /// @param refundReceiver Address of receiver of gas payment (or 0 if tx.origin). /// @param signatures Packed signature data ({bytes32 r}{bytes32 s}{uint8 v}) function execTransaction( address to, uint256 value, bytes calldata data, Enum.Operation operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address payable refundReceiver, bytes memory signatures ) public payable virtual returns (bool success) { bytes32 txHash; // Use scope here to limit variable lifetime and prevent `stack too deep` errors { bytes memory txHashData = encodeTransactionData( // Transaction info to, value, data, operation, safeTxGas, // Payment info baseGas, gasPrice, gasToken, refundReceiver, // Signature info nonce ); // Increase nonce and execute transaction. nonce++; txHash = keccak256(txHashData); checkSignatures(txHash, txHashData, signatures); } address guard = getGuard(); { if (guard != address(0)) { Guard(guard).checkTransaction( // Transaction info to, value, data, operation, safeTxGas, // Payment info baseGas, gasPrice, gasToken, refundReceiver, // Signature info signatures, msg.sender ); } } // We require some gas to emit the events (at least 2500) after the execution and some to perform code until the execution (500) // We also include the 1/64 in the check that is not send along with a call to counteract potential shortings because of EIP-150 require(gasleft() >= ((safeTxGas * 64) / 63).max(safeTxGas + 2500) + 500, "GS010"); // Use scope here to limit variable lifetime and prevent `stack too deep` errors { uint256 gasUsed = gasleft(); // If the gasPrice is 0 we assume that nearly all available gas can be used (it is always more than safeTxGas) // We only substract 2500 (compared to the 3000 before) to ensure that the amount passed is still higher than safeTxGas success = execute(to, value, data, operation, gasPrice == 0 ? (gasleft() - 2500) : safeTxGas); gasUsed = gasUsed.sub(gasleft()); // If no safeTxGas and no gasPrice was set (e.g. both are 0), then the internal tx is required to be successful // This makes it possible to use `estimateGas` without issues, as it searches for the minimum gas where the tx doesn't revert require(success || safeTxGas != 0 || gasPrice != 0, "GS013"); // We transfer the calculated tx costs to the tx.origin to avoid sending it to intermediate contracts that have made calls uint256 payment = 0; if (gasPrice > 0) { payment = handlePayment(gasUsed, baseGas, gasPrice, gasToken, refundReceiver); } if (success) emit ExecutionSuccess(txHash, payment); else emit ExecutionFailure(txHash, payment); } { if (guard != address(0)) { Guard(guard).checkAfterExecution(txHash, success); } } } function handlePayment( uint256 gasUsed, uint256 baseGas, uint256 gasPrice, address gasToken, address payable refundReceiver ) private returns (uint256 payment) { // solhint-disable-next-line avoid-tx-origin address payable receiver = refundReceiver == address(0) ? payable(tx.origin) : refundReceiver; if (gasToken == address(0)) { // For ETH we will only adjust the gas price to not be higher than the actual used gas price payment = gasUsed.add(baseGas).mul(gasPrice < tx.gasprice ? gasPrice : tx.gasprice); require(receiver.send(payment), "GS011"); } else { payment = gasUsed.add(baseGas).mul(gasPrice); require(transferToken(gasToken, receiver, payment), "GS012"); } } /** * @dev Checks whether the signature provided is valid for the provided data, hash. Will revert otherwise. * @param dataHash Hash of the data (could be either a message hash or transaction hash) * @param data That should be signed (this is passed to an external validator contract) * @param signatures Signature data that should be verified. Can be ECDSA signature, contract signature (EIP-1271) or approved hash. */ function checkSignatures( bytes32 dataHash, bytes memory data, bytes memory signatures ) public view { // Load threshold to avoid multiple storage loads uint256 _threshold = threshold; // Check that a threshold is set require(_threshold > 0, "GS001"); checkNSignatures(dataHash, data, signatures, _threshold); } /** * @dev Checks whether the signature provided is valid for the provided data, hash. Will revert otherwise. * @param dataHash Hash of the data (could be either a message hash or transaction hash) * @param data That should be signed (this is passed to an external validator contract) * @param signatures Signature data that should be verified. Can be ECDSA signature, contract signature (EIP-1271) or approved hash. * @param requiredSignatures Amount of required valid signatures. */ function checkNSignatures( bytes32 dataHash, bytes memory data, bytes memory signatures, uint256 requiredSignatures ) public view { // Check that the provided signature data is not too short require(signatures.length >= requiredSignatures.mul(65), "GS020"); // There cannot be an owner with address 0. address lastOwner = address(0); address currentOwner; uint8 v; bytes32 r; bytes32 s; uint256 i; for (i = 0; i < requiredSignatures; i++) { (v, r, s) = signatureSplit(signatures, i); if (v == 0) { // If v is 0 then it is a contract signature // When handling contract signatures the address of the contract is encoded into r currentOwner = address(uint160(uint256(r))); // Check that signature data pointer (s) is not pointing inside the static part of the signatures bytes // This check is not completely accurate, since it is possible that more signatures than the threshold are send. // Here we only check that the pointer is not pointing inside the part that is being processed require(uint256(s) >= requiredSignatures.mul(65), "GS021"); // Check that signature data pointer (s) is in bounds (points to the length of data -> 32 bytes) require(uint256(s).add(32) <= signatures.length, "GS022"); // Check if the contract signature is in bounds: start of data is s + 32 and end is start + signature length uint256 contractSignatureLen; // solhint-disable-next-line no-inline-assembly assembly { contractSignatureLen := mload(add(add(signatures, s), 0x20)) } require(uint256(s).add(32).add(contractSignatureLen) <= signatures.length, "GS023"); // Check signature bytes memory contractSignature; // solhint-disable-next-line no-inline-assembly assembly { // The signature data for contract signatures is appended to the concatenated signatures and the offset is stored in s contractSignature := add(add(signatures, s), 0x20) } require(ISignatureValidator(currentOwner).isValidSignature(data, contractSignature) == EIP1271_MAGIC_VALUE, "GS024"); } else if (v == 1) { // If v is 1 then it is an approved hash // When handling approved hashes the address of the approver is encoded into r currentOwner = address(uint160(uint256(r))); // Hashes are automatically approved by the sender of the message or when they have been pre-approved via a separate transaction require(msg.sender == currentOwner || approvedHashes[currentOwner][dataHash] != 0, "GS025"); } else if (v > 30) { // If v > 30 then default va (27,28) has been adjusted for eth_sign flow // To support eth_sign and similar we adjust v and hash the messageHash with the Ethereum message prefix before applying ecrecover currentOwner = ecrecover(keccak256(abi.encodePacked("\\x19Ethereum Signed Message:\ 32", dataHash)), v - 4, r, s); } else { // Default is the ecrecover flow with the provided data hash // Use ecrecover with the messageHash for EOA signatures currentOwner = ecrecover(dataHash, v, r, s); } require(currentOwner > lastOwner && owners[currentOwner] != address(0) && currentOwner != SENTINEL_OWNERS, "GS026"); lastOwner = currentOwner; } } /// @dev Allows to estimate a Safe transaction. /// This method is only meant for estimation purpose, therefore the call will always revert and encode the result in the revert data. /// Since the `estimateGas` function includes refunds, call this method to get an estimated of the costs that are deducted from the safe with `execTransaction` /// @param to Destination address of Safe transaction. /// @param value Ether value of Safe transaction. /// @param data Data payload of Safe transaction. /// @param operation Operation type of Safe transaction. /// @return Estimate without refunds and overhead fees (base transaction and payload data gas costs). /// @notice Deprecated in favor of common/StorageAccessible.sol and will be removed in next version. function requiredTxGas( address to, uint256 value, bytes calldata data, Enum.Operation operation ) external returns (uint256) { uint256 startGas = gasleft(); // We don't provide an error message here, as we use it to return the estimate require(execute(to, value, data, operation, gasleft())); uint256 requiredGas = startGas - gasleft(); // Convert response to string and return via error message revert(string(abi.encodePacked(requiredGas))); } /** * @dev Marks a hash as approved. This can be used to validate a hash that is used by a signature. * @param hashToApprove The hash that should be marked as approved for signatures that are verified by this contract. */ function approveHash(bytes32 hashToApprove) external { require(owners[msg.sender] != address(0), "GS030"); approvedHashes[msg.sender][hashToApprove] = 1; emit ApproveHash(hashToApprove, msg.sender); } /// @dev Returns the chain id used by this contract. function getChainId() public view returns (uint256) { uint256 id; // solhint-disable-next-line no-inline-assembly assembly { id := chainid() } return id; } function domainSeparator() public view returns (bytes32) { return keccak256(abi.encode(DOMAIN_SEPARATOR_TYPEHASH, getChainId(), this)); } /// @dev Returns the bytes that are hashed to be signed by owners. /// @param to Destination address. /// @param value Ether value. /// @param data Data payload. /// @param operation Operation type. /// @param safeTxGas Gas that should be used for the safe transaction. /// @param baseGas Gas costs for that are independent of the transaction execution(e.g. base transaction fee, signature check, payment of the refund) /// @param gasPrice Maximum gas price that should be used for this transaction. /// @param gasToken Token address (or 0 if ETH) that is used for the payment. /// @param refundReceiver Address of receiver of gas payment (or 0 if tx.origin). /// @param _nonce Transaction nonce. /// @return Transaction hash bytes. function encodeTransactionData( address to, uint256 value, bytes calldata data, Enum.Operation operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address refundReceiver, uint256 _nonce ) public view returns (bytes memory) { bytes32 safeTxHash = keccak256( abi.encode( SAFE_TX_TYPEHASH, to, value, keccak256(data), operation, safeTxGas, baseGas, gasPrice, gasToken, refundReceiver, _nonce ) ); return abi.encodePacked(bytes1(0x19), bytes1(0x01), domainSeparator(), safeTxHash); } /// @dev Returns hash to be signed by owners. /// @param to Destination address. /// @param value Ether value. /// @param data Data payload. /// @param operation Operation type. /// @param safeTxGas Fas that should be used for the safe transaction. /// @param baseGas Gas costs for data used to trigger the safe transaction. /// @param gasPrice Maximum gas price that should be used for this transaction. /// @param gasToken Token address (or 0 if ETH) that is used for the payment. /// @param refundReceiver Address of receiver of gas payment (or 0 if tx.origin). /// @param _nonce Transaction nonce. /// @return Transaction hash. function getTransactionHash( address to, uint256 value, bytes calldata data, Enum.Operation operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address refundReceiver, uint256 _nonce ) public view returns (bytes32) { return keccak256(encodeTransactionData(to, value, data, operation, safeTxGas, baseGas, gasPrice, gasToken, refundReceiver, _nonce)); } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; import "../common/Enum.sol"; /// @title Executor - A contract that can execute transactions /// @author Richard Meissner - <[email protected]> contract Executor { function execute( address to, uint256 value, bytes memory data, Enum.Operation operation, uint256 txGas ) internal returns (bool success) { if (operation == Enum.Operation.DelegateCall) { // solhint-disable-next-line no-inline-assembly assembly { success := delegatecall(txGas, to, add(data, 0x20), mload(data), 0, 0) } } else { // solhint-disable-next-line no-inline-assembly assembly { success := call(txGas, to, value, add(data, 0x20), mload(data), 0, 0) } } } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; import "../common/SelfAuthorized.sol"; /// @title Fallback Manager - A contract that manages fallback calls made to this contract /// @author Richard Meissner - <[email protected]> contract FallbackManager is SelfAuthorized { event ChangedFallbackHandler(address handler); // keccak256("fallback_manager.handler.address") bytes32 internal constant FALLBACK_HANDLER_STORAGE_SLOT = 0x6c9a6c4a39284e37ed1cf53d337577d14212a4870fb976a4366c693b939918d5; function internalSetFallbackHandler(address handler) internal { bytes32 slot = FALLBACK_HANDLER_STORAGE_SLOT; // solhint-disable-next-line no-inline-assembly assembly { sstore(slot, handler) } } /// @dev Allows to add a contract to handle fallback calls. /// Only fallback calls without value and with data will be forwarded. /// This can only be done via a Safe transaction. /// @param handler contract to handle fallbacks calls. function setFallbackHandler(address handler) public authorized { internalSetFallbackHandler(handler); emit ChangedFallbackHandler(handler); } // solhint-disable-next-line payable-fallback,no-complex-fallback fallback() external { bytes32 slot = FALLBACK_HANDLER_STORAGE_SLOT; // solhint-disable-next-line no-inline-assembly assembly { let handler := sload(slot) if iszero(handler) { return(0, 0) } calldatacopy(0, 0, calldatasize()) // The msg.sender address is shifted to the left by 12 bytes to remove the padding // Then the address without padding is stored right after the calldata mstore(calldatasize(), shl(96, caller())) // Add 20 bytes for the address appended add the end let success := call(gas(), handler, 0, 0, add(calldatasize(), 20), 0, 0) returndatacopy(0, 0, returndatasize()) if iszero(success) { revert(0, returndatasize()) } return(0, returndatasize()) } } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; import "../common/Enum.sol"; import "../common/SelfAuthorized.sol"; interface Guard { function checkTransaction( address to, uint256 value, bytes memory data, Enum.Operation operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address payable refundReceiver, bytes memory signatures, address msgSender ) external; function checkAfterExecution(bytes32 txHash, bool success) external; } /// @title Fallback Manager - A contract that manages fallback calls made to this contract /// @author Richard Meissner - <[email protected]> contract GuardManager is SelfAuthorized { event ChangedGuard(address guard); // keccak256("guard_manager.guard.address") bytes32 internal constant GUARD_STORAGE_SLOT = 0x4a204f620c8c5ccdca3fd54d003badd85ba500436a431f0cbda4f558c93c34c8; /// @dev Set a guard that checks transactions before execution /// @param guard The address of the guard to be used or the 0 address to disable the guard function setGuard(address guard) external authorized { bytes32 slot = GUARD_STORAGE_SLOT; // solhint-disable-next-line no-inline-assembly assembly { sstore(slot, guard) } emit ChangedGuard(guard); } function getGuard() internal view returns (address guard) { bytes32 slot = GUARD_STORAGE_SLOT; // solhint-disable-next-line no-inline-assembly assembly { guard := sload(slot) } } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; import "../common/Enum.sol"; import "../common/SelfAuthorized.sol"; import "./Executor.sol"; /// @title Module Manager - A contract that manages modules that can execute transactions via this contract /// @author Stefan George - <[email protected]> /// @author Richard Meissner - <[email protected]> contract ModuleManager is SelfAuthorized, Executor { event EnabledModule(address module); event DisabledModule(address module); event ExecutionFromModuleSuccess(address indexed module); event ExecutionFromModuleFailure(address indexed module); address internal constant SENTINEL_MODULES = address(0x1); mapping(address => address) internal modules; function setupModules(address to, bytes memory data) internal { require(modules[SENTINEL_MODULES] == address(0), "GS100"); modules[SENTINEL_MODULES] = SENTINEL_MODULES; if (to != address(0)) // Setup has to complete successfully or transaction fails. require(execute(to, 0, data, Enum.Operation.DelegateCall, gasleft()), "GS000"); } /// @dev Allows to add a module to the whitelist. /// This can only be done via a Safe transaction. /// @notice Enables the module `module` for the Safe. /// @param module Module to be whitelisted. function enableModule(address module) public authorized { // Module address cannot be null or sentinel. require(module != address(0) && module != SENTINEL_MODULES, "GS101"); // Module cannot be added twice. require(modules[module] == address(0), "GS102"); modules[module] = modules[SENTINEL_MODULES]; modules[SENTINEL_MODULES] = module; emit EnabledModule(module); } /// @dev Allows to remove a module from the whitelist. /// This can only be done via a Safe transaction. /// @notice Disables the module `module` for the Safe. /// @param prevModule Module that pointed to the module to be removed in the linked list /// @param module Module to be removed. function disableModule(address prevModule, address module) public authorized { // Validate module address and check that it corresponds to module index. require(module != address(0) && module != SENTINEL_MODULES, "GS101"); require(modules[prevModule] == module, "GS103"); modules[prevModule] = modules[module]; modules[module] = address(0); emit DisabledModule(module); } /// @dev Allows a Module to execute a Safe transaction without any further confirmations. /// @param to Destination address of module transaction. /// @param value Ether value of module transaction. /// @param data Data payload of module transaction. /// @param operation Operation type of module transaction. function execTransactionFromModule( address to, uint256 value, bytes memory data, Enum.Operation operation ) public virtual returns (bool success) { // Only whitelisted modules are allowed. require(msg.sender != SENTINEL_MODULES && modules[msg.sender] != address(0), "GS104"); // Execute transaction without further confirmations. success = execute(to, value, data, operation, gasleft()); if (success) emit ExecutionFromModuleSuccess(msg.sender); else emit ExecutionFromModuleFailure(msg.sender); } /// @dev Allows a Module to execute a Safe transaction without any further confirmations and return data /// @param to Destination address of module transaction. /// @param value Ether value of module transaction. /// @param data Data payload of module transaction. /// @param operation Operation type of module transaction. function execTransactionFromModuleReturnData( address to, uint256 value, bytes memory data, Enum.Operation operation ) public returns (bool success, bytes memory returnData) { success = execTransactionFromModule(to, value, data, operation); // solhint-disable-next-line no-inline-assembly assembly { // Load free memory location let ptr := mload(0x40) // We allocate memory for the return data by setting the free memory location to // current free memory location + data size + 32 bytes for data size value mstore(0x40, add(ptr, add(returndatasize(), 0x20))) // Store the size mstore(ptr, returndatasize()) // Store the data returndatacopy(add(ptr, 0x20), 0, returndatasize()) // Point the return data to the correct memory location returnData := ptr } } /// @dev Returns if an module is enabled /// @return True if the module is enabled function isModuleEnabled(address module) public view returns (bool) { return SENTINEL_MODULES != module && modules[module] != address(0); } /// @dev Returns array of modules. /// @param start Start of the page. /// @param pageSize Maximum number of modules that should be returned. /// @return array Array of modules. /// @return next Start of the next page. function getModulesPaginated(address start, uint256 pageSize) external view returns (address[] memory array, address next) { // Init array with max page size array = new address[](pageSize); // Populate return array uint256 moduleCount = 0; address currentModule = modules[start]; while (currentModule != address(0x0) && currentModule != SENTINEL_MODULES && moduleCount < pageSize) { array[moduleCount] = currentModule; currentModule = modules[currentModule]; moduleCount++; } next = currentModule; // Set correct size of returned array // solhint-disable-next-line no-inline-assembly assembly { mstore(array, moduleCount) } } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; import "../common/SelfAuthorized.sol"; /// @title OwnerManager - Manages a set of owners and a threshold to perform actions. /// @author Stefan George - <[email protected]> /// @author Richard Meissner - <[email protected]> contract OwnerManager is SelfAuthorized { event AddedOwner(address owner); event RemovedOwner(address owner); event ChangedThreshold(uint256 threshold); address internal constant SENTINEL_OWNERS = address(0x1); mapping(address => address) internal owners; uint256 internal ownerCount; uint256 internal threshold; /// @dev Setup function sets initial storage of contract. /// @param _owners List of Safe owners. /// @param _threshold Number of required confirmations for a Safe transaction. function setupOwners(address[] memory _owners, uint256 _threshold) internal { // Threshold can only be 0 at initialization. // Check ensures that setup function can only be called once. require(threshold == 0, "GS200"); // Validate that threshold is smaller than number of added owners. require(_threshold <= _owners.length, "GS201"); // There has to be at least one Safe owner. require(_threshold >= 1, "GS202"); // Initializing Safe owners. address currentOwner = SENTINEL_OWNERS; for (uint256 i = 0; i < _owners.length; i++) { // Owner address cannot be null. address owner = _owners[i]; require(owner != address(0) && owner != SENTINEL_OWNERS && owner != address(this) && currentOwner != owner, "GS203"); // No duplicate owners allowed. require(owners[owner] == address(0), "GS204"); owners[currentOwner] = owner; currentOwner = owner; } owners[currentOwner] = SENTINEL_OWNERS; ownerCount = _owners.length; threshold = _threshold; } /// @dev Allows to add a new owner to the Safe and update the threshold at the same time. /// This can only be done via a Safe transaction. /// @notice Adds the owner `owner` to the Safe and updates the threshold to `_threshold`. /// @param owner New owner address. /// @param _threshold New threshold. function addOwnerWithThreshold(address owner, uint256 _threshold) public authorized { // Owner address cannot be null, the sentinel or the Safe itself. require(owner != address(0) && owner != SENTINEL_OWNERS && owner != address(this), "GS203"); // No duplicate owners allowed. require(owners[owner] == address(0), "GS204"); owners[owner] = owners[SENTINEL_OWNERS]; owners[SENTINEL_OWNERS] = owner; ownerCount++; emit AddedOwner(owner); // Change threshold if threshold was changed. if (threshold != _threshold) changeThreshold(_threshold); } /// @dev Allows to remove an owner from the Safe and update the threshold at the same time. /// This can only be done via a Safe transaction. /// @notice Removes the owner `owner` from the Safe and updates the threshold to `_threshold`. /// @param prevOwner Owner that pointed to the owner to be removed in the linked list /// @param owner Owner address to be removed. /// @param _threshold New threshold. function removeOwner( address prevOwner, address owner, uint256 _threshold ) public authorized { // Only allow to remove an owner, if threshold can still be reached. require(ownerCount - 1 >= _threshold, "GS201"); // Validate owner address and check that it corresponds to owner index. require(owner != address(0) && owner != SENTINEL_OWNERS, "GS203"); require(owners[prevOwner] == owner, "GS205"); owners[prevOwner] = owners[owner]; owners[owner] = address(0); ownerCount--; emit RemovedOwner(owner); // Change threshold if threshold was changed. if (threshold != _threshold) changeThreshold(_threshold); } /// @dev Allows to swap/replace an owner from the Safe with another address. /// This can only be done via a Safe transaction. /// @notice Replaces the owner `oldOwner` in the Safe with `newOwner`. /// @param prevOwner Owner that pointed to the owner to be replaced in the linked list /// @param oldOwner Owner address to be replaced. /// @param newOwner New owner address. function swapOwner( address prevOwner, address oldOwner, address newOwner ) public authorized { // Owner address cannot be null, the sentinel or the Safe itself. require(newOwner != address(0) && newOwner != SENTINEL_OWNERS && newOwner != address(this), "GS203"); // No duplicate owners allowed. require(owners[newOwner] == address(0), "GS204"); // Validate oldOwner address and check that it corresponds to owner index. require(oldOwner != address(0) && oldOwner != SENTINEL_OWNERS, "GS203"); require(owners[prevOwner] == oldOwner, "GS205"); owners[newOwner] = owners[oldOwner]; owners[prevOwner] = newOwner; owners[oldOwner] = address(0); emit RemovedOwner(oldOwner); emit AddedOwner(newOwner); } /// @dev Allows to update the number of required confirmations by Safe owners. /// This can only be done via a Safe transaction. /// @notice Changes the threshold of the Safe to `_threshold`. /// @param _threshold New threshold. function changeThreshold(uint256 _threshold) public authorized { // Validate that threshold is smaller than number of owners. require(_threshold <= ownerCount, "GS201"); // There has to be at least one Safe owner. require(_threshold >= 1, "GS202"); threshold = _threshold; emit ChangedThreshold(threshold); } function getThreshold() public view returns (uint256) { return threshold; } function isOwner(address owner) public view returns (bool) { return owner != SENTINEL_OWNERS && owners[owner] != address(0); } /// @dev Returns array of owners. /// @return Array of Safe owners. function getOwners() public view returns (address[] memory) { address[] memory array = new address[](ownerCount); // populate return array uint256 index = 0; address currentOwner = owners[SENTINEL_OWNERS]; while (currentOwner != SENTINEL_OWNERS) { array[index] = currentOwner; currentOwner = owners[currentOwner]; index++; } return array; } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /// @title Enum - Collection of enums /// @author Richard Meissner - <[email protected]> contract Enum { enum Operation {Call, DelegateCall} } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /// @title EtherPaymentFallback - A contract that has a fallback to accept ether payments /// @author Richard Meissner - <[email protected]> contract EtherPaymentFallback { event SafeReceived(address indexed sender, uint256 value); /// @dev Fallback function accepts Ether transactions. receive() external payable { emit SafeReceived(msg.sender, msg.value); } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /// @title SecuredTokenTransfer - Secure token transfer /// @author Richard Meissner - <[email protected]> contract SecuredTokenTransfer { /// @dev Transfers a token and returns if it was a success /// @param token Token that should be transferred /// @param receiver Receiver to whom the token should be transferred /// @param amount The amount of tokens that should be transferred function transferToken( address token, address receiver, uint256 amount ) internal returns (bool transferred) { // 0xa9059cbb - keccack("transfer(address,uint256)") bytes memory data = abi.encodeWithSelector(0xa9059cbb, receiver, amount); // solhint-disable-next-line no-inline-assembly assembly { // We write the return value to scratch space. // See https://docs.soliditylang.org/en/v0.7.6/internals/layout_in_memory.html#layout-in-memory let success := call(sub(gas(), 10000), token, 0, add(data, 0x20), mload(data), 0, 0x20) switch returndatasize() case 0 { transferred := success } case 0x20 { transferred := iszero(or(iszero(success), iszero(mload(0)))) } default { transferred := 0 } } } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /// @title SelfAuthorized - authorizes current contract to perform actions /// @author Richard Meissner - <[email protected]> contract SelfAuthorized { function requireSelfCall() private view { require(msg.sender == address(this), "GS031"); } modifier authorized() { // This is a function call as it minimized the bytecode size requireSelfCall(); _; } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /// @title SignatureDecoder - Decodes signatures that a encoded as bytes /// @author Richard Meissner - <[email protected]> contract SignatureDecoder { /// @dev divides bytes signature into `uint8 v, bytes32 r, bytes32 s`. /// @notice Make sure to peform a bounds check for @param pos, to avoid out of bounds access on @param signatures /// @param pos which signature to read. A prior bounds check of this parameter should be performed, to avoid out of bounds access /// @param signatures concatenated rsv signatures function signatureSplit(bytes memory signatures, uint256 pos) internal pure returns ( uint8 v, bytes32 r, bytes32 s ) { // The signature format is a compact form of: // {bytes32 r}{bytes32 s}{uint8 v} // Compact means, uint8 is not padded to 32 bytes. // solhint-disable-next-line no-inline-assembly assembly { let signaturePos := mul(0x41, pos) r := mload(add(signatures, add(signaturePos, 0x20))) s := mload(add(signatures, add(signaturePos, 0x40))) // Here we are loading the last 32 bytes, including 31 bytes // of 's'. There is no 'mload8' to do this. // // 'byte' is not working due to the Solidity parser, so lets // use the second best option, 'and' v := and(mload(add(signatures, add(signaturePos, 0x41))), 0xff) } } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /// @title Singleton - Base for singleton contracts (should always be first super contract) /// This contract is tightly coupled to our proxy contract (see `proxies/GnosisSafeProxy.sol`) /// @author Richard Meissner - <[email protected]> contract Singleton { // singleton always needs to be first declared variable, to ensure that it is at the same location as in the Proxy contract. // It should also always be ensured that the address is stored alone (uses a full word) address private singleton; } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /// @title StorageAccessible - generic base contract that allows callers to access all internal storage. /// @notice See https://github.com/gnosis/util-contracts/blob/bb5fe5fb5df6d8400998094fb1b32a178a47c3a1/contracts/StorageAccessible.sol contract StorageAccessible { /** * @dev Reads `length` bytes of storage in the currents contract * @param offset - the offset in the current contract's storage in words to start reading from * @param length - the number of words (32 bytes) of data to read * @return the bytes that were read. */ function getStorageAt(uint256 offset, uint256 length) public view returns (bytes memory) { bytes memory result = new bytes(length * 32); for (uint256 index = 0; index < length; index++) { // solhint-disable-next-line no-inline-assembly assembly { let word := sload(add(offset, index)) mstore(add(add(result, 0x20), mul(index, 0x20)), word) } } return result; } /** * @dev Performs a delegetecall on a targetContract in the context of self. * Internally reverts execution to avoid side effects (making it static). * * This method reverts with data equal to `abi.encode(bool(success), bytes(response))`. * Specifically, the `returndata` after a call to this method will be: * `success:bool || response.length:uint256 || response:bytes`. * * @param targetContract Address of the contract containing the code to execute. * @param calldataPayload Calldata that should be sent to the target contract (encoded method name and arguments). */ function simulateAndRevert(address targetContract, bytes memory calldataPayload) external { // solhint-disable-next-line no-inline-assembly assembly { let success := delegatecall(gas(), targetContract, add(calldataPayload, 0x20), mload(calldataPayload), 0, 0) mstore(0x00, success) mstore(0x20, returndatasize()) returndatacopy(0x40, 0, returndatasize()) revert(0, add(returndatasize(), 0x40)) } } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; /** * @title GnosisSafeMath * @dev Math operations with safety checks that revert on error * Renamed from SafeMath to GnosisSafeMath to avoid conflicts * TODO: remove once open zeppelin update to solc 0.5.0 */ library GnosisSafeMath { /** * @dev Multiplies two numbers, reverts on overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b); return c; } /** * @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend). */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { require(b <= a); uint256 c = a - b; return c; } /** * @dev Adds two numbers, reverts on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a); return c; } /** * @dev Returns the largest of two numbers. */ function max(uint256 a, uint256 b) internal pure returns (uint256) { return a >= b ? a : b; } } // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.7.0 <0.9.0; contract ISignatureValidatorConstants { // bytes4(keccak256("isValidSignature(bytes,bytes)") bytes4 internal constant EIP1271_MAGIC_VALUE = 0x20c13b0b; } abstract contract ISignatureValidator is ISignatureValidatorConstants { /** * @dev Should return whether the signature provided is valid for the provided data * @param _data Arbitrary length data signed on the behalf of address(this) * @param _signature Signature byte array associated with _data * * MUST return the bytes4 magic value 0x20c13b0b when function passes. * MUST NOT modify state (using STATICCALL for solc < 0.5, view modifier for solc > 0.5) * MUST allow external calls */ function isValidSignature(bytes memory _data, bytes memory _signature) public view virtual returns (bytes4); }