Skip to content

Instantly share code, notes, and snippets.

@casweeney
Created September 16, 2022 15:58
Show Gist options
  • Select an option

  • Save casweeney/87ada9ed15650854f75b06747c1fde11 to your computer and use it in GitHub Desktop.

Select an option

Save casweeney/87ada9ed15650854f75b06747c1fde11 to your computer and use it in GitHub Desktop.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
import "./Proxiable.sol";
contract ImplementationA is Proxiable {
address public owner;
uint public count;
bool public initalized = false;
function initialize() public {
require(owner == address(0), "Already initalized");
require(!initalized, "Already initalized");
owner = msg.sender;
initalized = true;
}
function encode() public pure returns(bytes memory ){
return abi.encodeWithSignature("initialize()");
}
function increment() public {
count++;
}
function updateCode(address newCode) onlyOwner public {
updateCodeAddress(newCode);
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner is allowed to perform this action");
_;
}
}
@casweeney

Copy link
Copy Markdown
Author

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.1;

contract Proxy {
// Code position in storage is keccak256("PROXIABLE") = "0xc5f16f0fcc639fa48a6947836d9850f504798523bf8c9a3a87d5876cf622bcf7"
constructor( address contractLogic) {
// save the code address
assembly { // solium-disable-line
sstore(0xc5f16f0fcc639fa48a6947836d9850f504798523bf8c9a3a87d5876cf622bcf7, contractLogic)
}
}

fallback() external payable {
    assembly { // solium-disable-line
        let contractLogic := sload(0xc5f16f0fcc639fa48a6947836d9850f504798523bf8c9a3a87d5876cf622bcf7)
        calldatacopy(0x0, 0x0, calldatasize())
        let success := delegatecall(sub(gas(), 10000), contractLogic, 0x0, calldatasize(), 0, 0)
        let retSz := returndatasize()
        returndatacopy(0, 0, retSz)
        switch success
        case 0 {
            revert(0, retSz)
        }
        default {
            return(0, retSz)
        }
    }
}

}

@casweeney

Copy link
Copy Markdown
Author

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.1;

contract Proxiable {
// Code position in storage is keccak256("PROXIABLE") = "0xc5f16f0fcc639fa48a6947836d9850f504798523bf8c9a3a87d5876cf622bcf7"

function updateCodeAddress(address newAddress) internal {
    require(
        bytes32(0xc5f16f0fcc639fa48a6947836d9850f504798523bf8c9a3a87d5876cf622bcf7) == Proxiable(newAddress).proxiableUUID(),
        "Not compatible"
    );
    assembly { // solium-disable-line
        sstore(0xc5f16f0fcc639fa48a6947836d9850f504798523bf8c9a3a87d5876cf622bcf7, newAddress)
    }
}

function proxiableUUID() public pure returns (bytes32) {
    return 0xc5f16f0fcc639fa48a6947836d9850f504798523bf8c9a3a87d5876cf622bcf7;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment