solidity--可升级代理Upgradeable Proxy
https://solidity-by-example.org/app/upgradeable-proxy/
Example of upgradeable proxy contract. Never use this in production.
This example shows how to use
delegatecallfallback
可升级代理合约的示例。 永远不要在生产中使用它。
这个例子展示了如何在调用 fallback 时使用委托调用并返回数据。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Proxy {
address public implementation;
function setImplementation(address _imp) external {
implementation = _imp;
}
function _delegate(address _imp) internal virtual {
assembly {
// calldatacopy(t, f, s)
// copy s bytes from calldata at position f to mem at position t
calldatacopy(0, 0, calldatasize())
// delegatecall(g, a, in, insize, out, outsize)
// - call contract at address a
// - with input mem[in…(in+insize))
// - providing g gas
// - and output area mem[out…(out+outsize))
// - returning 0 on error and 1 on success
let result := delegatecall(gas(), _imp, 0, calldatasize(), 0, 0)
// returndatacopy(t, f, s)
// copy s bytes from returndata at position f to mem at position t
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
// revert(p, s)
// end execution, revert state changes, return data mem[p…(p+s))
revert(0, returndatasize())
}
default {
// return(p, s)
// end execution, return data mem[p…(p+s))
return(0, returndatasize())
}
}
}
fallback() external payable {
_delegate(implementation);
}
}
contract V1 {
address public implementation;
uint public x;
function inc() external {
x += 1;
}
}
contract V2 {
address public implementation;
uint public x;
function inc() external {
x += 1;
}
function dec() external {
x -= 1;
}
}
不能编译