Skip to content

Instantly share code, notes, and snippets.

@gentlyawesome
Created June 21, 2023 01:43
Show Gist options
  • Save gentlyawesome/0eae9143944c5590fe95d414ed82545a to your computer and use it in GitHub Desktop.
Save gentlyawesome/0eae9143944c5590fe95d414ed82545a to your computer and use it in GitHub Desktop.
pragma solidity ^0.8.0;
contract Proxy {
address private _implementation;
event Upgraded(address indexed implementation);
constructor(address implementation_) {
_implementation = implementation_;
}
fallback() external payable {
_delegate(_implementation);
}
function implementation() public view returns (address) {
return _implementation;
}
function upgradeTo(address newImplementation) public {
require(_implementation != newImplementation, "Proxy: Same implementation");
_implementation = newImplementation;
emit Upgraded(newImplementation);
}
function _delegate(address implementation_) private {
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), implementation_, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
}
contract MyContract {
uint public value;
function setValue(uint newValue) external {
value = newValue;
}
function getValue() external view returns (uint) {
return value;
}
}
contract MyContractProxy is Proxy {
constructor(address implementation) Proxy(implementation) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment