|
| 1 | +// SPDX-License-Identifier: GPL-3.0-only |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import { Token, TokenType } from "../interfaces/IEnsoRouter.sol"; |
| 5 | + |
| 6 | +import { IEnsoWalletV2 } from "./interfaces/IEnsoWalletV2.sol"; |
| 7 | + |
| 8 | +import { IERC1155 } from "openzeppelin-contracts/token/ERC1155/IERC1155.sol"; |
| 9 | +import { IERC20, SafeERC20 } from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol"; |
| 10 | +import { IERC721 } from "openzeppelin-contracts/token/ERC721/IERC721.sol"; |
| 11 | +import { LibClone } from "solady/utils/LibClone.sol"; |
| 12 | + |
| 13 | +contract EnsoWalletV2Factory { |
| 14 | + using LibClone for address; |
| 15 | + using SafeERC20 for IERC20; |
| 16 | + |
| 17 | + address public immutable implementation; |
| 18 | + |
| 19 | + event EnsoWalletV2Deployed(address wallet, address indexed account); |
| 20 | + |
| 21 | + error WrongMsgValue(uint256 value, uint256 expectedAmount); |
| 22 | + error UnsupportedTokenType(TokenType tokenType); |
| 23 | + |
| 24 | + constructor(address implementation_) { |
| 25 | + implementation = implementation_; |
| 26 | + } |
| 27 | + |
| 28 | + function deploy(address account) external returns (address) { |
| 29 | + return _deploy(account); |
| 30 | + } |
| 31 | + |
| 32 | + function deployAndExecute(Token calldata tokenIn, bytes calldata data) external payable returns (address, bool) { |
| 33 | + return _deployAndExecute(tokenIn, data); |
| 34 | + } |
| 35 | + |
| 36 | + function getAddress(address account) external view returns (address) { |
| 37 | + bytes32 salt = _getSalt(account); |
| 38 | + return implementation.predictDeterministicAddress(salt, address(this)); |
| 39 | + } |
| 40 | + |
| 41 | + function _deployAndExecute( |
| 42 | + Token calldata tokenIn, |
| 43 | + bytes calldata data |
| 44 | + ) |
| 45 | + private |
| 46 | + returns (address wallet, bool success) |
| 47 | + { |
| 48 | + // strictly only msg.sender can deploy and execute |
| 49 | + wallet = _deploy(msg.sender); |
| 50 | + bool isNativeAsset = _transfer(tokenIn, wallet); |
| 51 | + if (!isNativeAsset && msg.value != 0) revert WrongMsgValue(msg.value, 0); |
| 52 | + (success,) = wallet.call{ value: msg.value }(data); |
| 53 | + } |
| 54 | + |
| 55 | + function _deploy(address account) private returns (address wallet) { |
| 56 | + bytes32 salt = _getSalt(account); |
| 57 | + wallet = implementation.predictDeterministicAddress(salt, address(this)); |
| 58 | + if (wallet.code.length == 0) { |
| 59 | + implementation.cloneDeterministic(salt); |
| 60 | + IEnsoWalletV2(wallet).initialize(account); |
| 61 | + emit EnsoWalletV2Deployed(wallet, account); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + function _transfer(Token calldata token, address receiver) private returns (bool isNativeAsset) { |
| 66 | + TokenType tokenType = token.tokenType; |
| 67 | + |
| 68 | + if (tokenType == TokenType.ERC20) { |
| 69 | + (IERC20 erc20, uint256 amount) = abi.decode(token.data, (IERC20, uint256)); |
| 70 | + erc20.safeTransferFrom(msg.sender, receiver, amount); |
| 71 | + } else if (tokenType == TokenType.Native) { |
| 72 | + // no need to get amount, it will come from msg.value |
| 73 | + isNativeAsset = true; |
| 74 | + } else if (tokenType == TokenType.ERC721) { |
| 75 | + (IERC721 erc721, uint256 tokenId) = abi.decode(token.data, (IERC721, uint256)); |
| 76 | + erc721.safeTransferFrom(msg.sender, receiver, tokenId); |
| 77 | + } else if (tokenType == TokenType.ERC1155) { |
| 78 | + (IERC1155 erc1155, uint256 tokenId, uint256 amount) = abi.decode(token.data, (IERC1155, uint256, uint256)); |
| 79 | + erc1155.safeTransferFrom(msg.sender, receiver, tokenId, amount, "0x"); |
| 80 | + } else { |
| 81 | + revert UnsupportedTokenType(tokenType); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + function _getSalt(address account) internal pure returns (bytes32) { |
| 86 | + return keccak256(abi.encode(account)); |
| 87 | + } |
| 88 | +} |
0 commit comments