使用UUPS升级智能合约
pragma solidity >=0.4.22 <0.9.0;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
contract Box is Initializable, OwnableUpgradeable, UUPSUpgradeable {
function initialize() public initializer {
__Ownable_init();
__UUPSUpgradeable_init();
}
uint256 private value;
event ValueChanged(uint256 value);
function store(uint256 _value) public {
value = _value;
emit ValueChanged(_value);
}
function retrieve() public view returns (uint256) {
return value;
}
function _authorizeUpgrade(address newImplementation)
internal
onlyOwner
override
{}
}
智能合约地址: 0x5e17b14ADd6c386305A32928F985b29bbA34Eff5 代理合约地址: 0xe2899bddFD890e320e643044c6b95B9B0b84157A
调用代理合约的store方法, 设置value=10 调用代理合约的retrieve方法, value=10
function retrieve() public view returns (uint256) {
// 修改方法
return value + 100;
}
智能合约地址: 0x93f8dddd876c7dBE3323723500e83E202A7C96CC 代理合约地址: 0xe2899bddFD890e320e643044c6b95B9B0b84157A
调用代理合约的retrieve方法, value=110, 证明升级成功
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!