|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import "@thirdweb-dev/contracts/smart-wallet/non-upgradeable/Account.sol"; |
| 5 | +import "@thirdweb-dev/contracts/eip/interface/IERC721.sol"; |
| 6 | + |
| 7 | +contract TokenGatedAccount is Account { |
| 8 | + uint256 public chainId; |
| 9 | + address public tokenContract; |
| 10 | + uint256 public tokenId; |
| 11 | + |
| 12 | + event TokenGatedAccountCreated(address indexed account, bytes indexed data); |
| 13 | + |
| 14 | + /** |
| 15 | + * @notice Executes once when a contract is created to initialize state variables |
| 16 | + * |
| 17 | + * @param _entrypoint - 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 |
| 18 | + * @param _factory - The factory contract address to issue token Gated accounts |
| 19 | + * |
| 20 | + */ |
| 21 | + constructor( |
| 22 | + IEntryPoint _entrypoint, |
| 23 | + address _factory |
| 24 | + ) Account(_entrypoint, _factory) { |
| 25 | + _disableInitializers(); |
| 26 | + } |
| 27 | + |
| 28 | + function isValidSigner( |
| 29 | + address _signer |
| 30 | + ) public view override returns (bool) { |
| 31 | + return (owner() == _signer); |
| 32 | + } |
| 33 | + |
| 34 | + function owner() public view returns (address) { |
| 35 | + if (chainId != block.chainid) { |
| 36 | + revert("Invalid chainId"); |
| 37 | + } |
| 38 | + return IERC721(tokenContract).ownerOf(tokenId); |
| 39 | + } |
| 40 | + |
| 41 | + function initialize( |
| 42 | + address _admin, |
| 43 | + bytes calldata _data |
| 44 | + ) public override initializer { |
| 45 | + (chainId, tokenContract, tokenId) = abi.decode( |
| 46 | + _data, |
| 47 | + (uint256, address, uint256) |
| 48 | + ); |
| 49 | + require(owner() == _admin, "Account: not token owner."); |
| 50 | + emit TokenGatedAccountCreated(_admin, _data); |
| 51 | + } |
| 52 | + |
| 53 | + /// @notice Executes a transaction (called directly from the token owner, or by entryPoint) |
| 54 | + function execute( |
| 55 | + address _target, |
| 56 | + uint256 _value, |
| 57 | + bytes calldata _calldata |
| 58 | + ) external virtual override onlyOwnerOrEntrypoint { |
| 59 | + _call(_target, _value, _calldata); |
| 60 | + } |
| 61 | + |
| 62 | + /// @notice Executes a sequence transaction (called directly from the token owner, or by entryPoint) |
| 63 | + function executeBatch( |
| 64 | + address[] calldata _target, |
| 65 | + uint256[] calldata _value, |
| 66 | + bytes[] calldata _calldata |
| 67 | + ) external virtual override onlyOwnerOrEntrypoint { |
| 68 | + require( |
| 69 | + _target.length == _calldata.length && |
| 70 | + _target.length == _value.length, |
| 71 | + "Account: wrong array lengths." |
| 72 | + ); |
| 73 | + for (uint256 i = 0; i < _target.length; i++) { |
| 74 | + _call(_target[i], _value[i], _calldata[i]); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// @notice Withdraw funds for this account from Entrypoint. |
| 79 | + function withdrawDepositTo( |
| 80 | + address payable withdrawAddress, |
| 81 | + uint256 amount |
| 82 | + ) public virtual override { |
| 83 | + require(owner() == msg.sender, "Account: not NFT owner"); |
| 84 | + entryPoint().withdrawTo(withdrawAddress, amount); |
| 85 | + } |
| 86 | + |
| 87 | + function token() |
| 88 | + external |
| 89 | + view |
| 90 | + returns (uint256 chainId, address tokenContract, uint256 tokenId) |
| 91 | + {} |
| 92 | + |
| 93 | + /// @notice Checks whether the caller is the EntryPoint contract or the token owner. |
| 94 | + modifier onlyOwnerOrEntrypoint() { |
| 95 | + require( |
| 96 | + msg.sender == address(entryPoint()) || owner() == msg.sender, |
| 97 | + "Account: not admin or EntryPoint." |
| 98 | + ); |
| 99 | + _; |
| 100 | + } |
| 101 | +} |
0 commit comments