This program shows how a proxy contract can be used to upgrade the logic contract without losing stored data.
// Solidity example showing upgrade via proxy
pragma solidity ^0.8.0;
contract ImplementationV1 {
uint public number;
function setNumber(uint _num) public {
number = _num;
}
}
contract Proxy {
address public implementation;
constructor(address _impl) {
implementation = _impl;
}
function upgrade(address _newImpl) public {
implementation = _newImpl;
}
fallback() external payable {
(bool success, ) = implementation.delegatecall(msg.data);
require(success);
}
}
// Usage:
// 1. Deploy ImplementationV1
// 2. Deploy Proxy with ImplementationV1 address
// 3. Call setNumber via Proxy
// 4. Deploy ImplementationV2 with new features
// 5. Call upgrade on Proxy to new ImplementationV2 address
// 6. Continue using Proxy with new logic