|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.26; |
| 3 | + |
| 4 | +import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; |
| 5 | +import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; |
| 6 | +import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; |
| 7 | +import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; |
| 8 | + |
| 9 | +import {CAssetDTokenWrapUpgradeable} from "./CAssetDTokenWrapUpgradeable.sol"; |
| 10 | + |
| 11 | +contract CAssetDTokenWrapFactoryUpgradeable is Initializable, OwnableUpgradeable, UUPSUpgradeable { |
| 12 | + address public router; |
| 13 | + address public tokenTreasuryImplementation; |
| 14 | + address public cAssetDTokenWrapImplementation; |
| 15 | + |
| 16 | + address[] public allWraps; |
| 17 | + |
| 18 | + event CAssetDTokenWrapDeployed( |
| 19 | + address indexed wrap, |
| 20 | + address indexed dToken, |
| 21 | + address indexed cAsset, |
| 22 | + address deployer |
| 23 | + ); |
| 24 | + |
| 25 | + function initialize( |
| 26 | + address _tokenTreasuryImpl, |
| 27 | + address _cAssetDTokenWrapImpl, |
| 28 | + address _router, |
| 29 | + address owner_ |
| 30 | + ) public initializer { |
| 31 | + __Ownable_init(owner_); |
| 32 | + __UUPSUpgradeable_init(); |
| 33 | + |
| 34 | + router = _router; |
| 35 | + tokenTreasuryImplementation = _tokenTreasuryImpl; |
| 36 | + cAssetDTokenWrapImplementation = _cAssetDTokenWrapImpl; |
| 37 | + } |
| 38 | + |
| 39 | + function _authorizeUpgrade(address newImplementation) internal override onlyOwner {} |
| 40 | + |
| 41 | + function deployWrap( |
| 42 | + address _dTokenAddress, |
| 43 | + address _cAssetAddress, |
| 44 | + uint8 _dTokenDecimals, |
| 45 | + uint8 _cAssetDecimals, |
| 46 | + uint256 _dTokenInFeeBps, |
| 47 | + uint256 _dTokenOutFeeBps |
| 48 | + ) external returns (address) { |
| 49 | + // use struct to solve Stack too deep. |
| 50 | + CAssetDTokenWrapUpgradeable.WrapInfo memory info = CAssetDTokenWrapUpgradeable.WrapInfo({ |
| 51 | + dTokenAddress: _dTokenAddress, |
| 52 | + cAssetAddress: _cAssetAddress, |
| 53 | + dTokenDecimals: _dTokenDecimals, |
| 54 | + cAssetDecimals: _cAssetDecimals, |
| 55 | + dTokenInFeeBps: _dTokenInFeeBps, |
| 56 | + dTokenOutFeeBps: _dTokenOutFeeBps |
| 57 | + }); |
| 58 | + ERC1967Proxy proxy = new ERC1967Proxy( |
| 59 | + cAssetDTokenWrapImplementation, |
| 60 | + abi.encodeWithSelector( |
| 61 | + CAssetDTokenWrapUpgradeable.initialize.selector, |
| 62 | + owner(), // factory owner as admin |
| 63 | + router, // router is the only contract to call (un)wrap |
| 64 | + tokenTreasuryImplementation, |
| 65 | + info |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + address wrapAddr = address(proxy); |
| 70 | + allWraps.push(wrapAddr); |
| 71 | + |
| 72 | + emit CAssetDTokenWrapDeployed(wrapAddr, _dTokenAddress, _cAssetAddress, msg.sender); |
| 73 | + return wrapAddr; |
| 74 | + } |
| 75 | + |
| 76 | + function getAllWraps() external view returns (address[] memory) { |
| 77 | + return allWraps; |
| 78 | + } |
| 79 | +} |
0 commit comments