升级合约
import '@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol';
import '@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol';
contract XXX is UUPSUpgradeable, OwnableUpgradeable {
using SafeERC20Upgradeable for IERC20Upgradeable;
// ======================== State Variables ========================
// --------------- pool
// --------------- user
// ======================== Events ========================
// ======================== Constructor ========================
function initialize() external initializer {
__Ownable_init();
}
function _authorizeUpgrade(address) internal override onlyOwner {}
// ======================== Functions: Limit ========================
// ======================== Functions: Main ========================
// ======================== Functions: View ========================
// ======================== Functions: Private ========================
function _transfer(address _from, address _to, uint256 _amount) private {
IERC20Upgradeable(address(WETH)).safeTransferFrom(_from, address(this), _amount);
WETH.withdraw(_amount);
(bool success, ) = payable(_to).call{ value: _amount }('');
require(success, 'transfer failed');
}
}
其中 initialize
和 _authorizeUpgrade
是固定格式。
升级合约的部署
const XXXX = await ethers.getContractFactory("XXXX");
const xxxx = await upgrades.deployProxy(XXXX);
await xxxx.waitForDeployment();