Proxy Pattern
Delegatecall
delegatecall is a low level function similar to call.
When contract A executes delegatecall to contract B, B's code is executed
with contract A's storage, msg.sender and msg.value.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
// NOTE: Deploy this contract first
contract B {
// NOTE: storage layout must be the same as contract A
uint public num;
address public sender;
uint public value;
function setVars(uint _num) public payable {
num = _num;
sender = msg.sender;
value = msg.value;
}
}
contract A {
uint public num;
address public sender;
uint public value;
function setVars(address _contract, uint _num) public payable {
// A's storage is set, B is not modified.
(bool success, bytes memory data) = _contract.delegatecall(
abi.encodeWithSignature("setVars(uint256)", _num)
);
}
}
Proxy Admin, Upgradeability Proxy, Implementation Contract
Proxy contract will be the storage layer
Implementation contract will be the logic layer
In the case of an upgrade, existing storage variables cannot be changed or omitted. While upgrading the contract, the storage sequence has to be the same. Only new variables can be added.
It shows assembly and calldatacopy ... function
UUPS vs Transparent Proxy
Transparent Proxy
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (proxy/transparent/TransparentUpgradeableProxy.sol)
pragma solidity ^0.8.0;
import "../ERC1967/ERC1967Proxy.sol";
contract TransparentUpgradeableProxy is ERC1967Proxy {
/**
* @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin.
*/
modifier ifAdmin() {
if (msg.sender == _getAdmin()) {
_;
} else {
_fallback();
}
}
function admin() external ifAdmin returns (address admin_) {
admin_ = _getAdmin();
}
...
}
- Put the deploy function in proxy contract
- Have extra overload for transaction (Whenever you execute function, it need to ask ifadmin)
UUPS
- Put the deploy function in logic contract
- Can customize deploy function
- Higher deploy fee(because function put into logic contract), lower transaction gas fee(users will benefit from it)
- Not easy to deploy, often come with truffle or hardhat