Created
July 14, 2022 09:33
-
-
Save cassc/41cfb69b15a37ad42fded872f8db3c2e to your computer and use it in GitHub Desktop.
beacon contract
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; | |
import "@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol"; | |
import "@openzeppelin/contracts/proxy/beacon/UpgradeableBeacon.sol"; | |
contract Vaultv1 is Initializable { | |
string public name; | |
uint256 public vaLue; | |
error Down(string reason); | |
function initialize(string memory _name, uint256 _vaLue) public initializer { | |
name = _name; | |
vaLue = _vaLue; | |
} | |
function down() public { | |
if (vaLue == 0) revert Down("!vaLue"); | |
vaLue--; | |
} | |
} | |
contract Vaultv2 is Initializable { | |
string public name; | |
uint256 public vaLue; | |
error Down(string reason); | |
function initialize(string memory _name, uint256 _vaLue) public initializer { | |
name = _name; | |
vaLue = _vaLue; | |
} | |
function down() public { | |
if (vaLue == 0) revert Down("!vaLue"); | |
vaLue--; | |
} | |
function up() public { | |
vaLue++; | |
} | |
} | |
contract VaultBeacon { | |
UpgradeableBeacon immutable beacon; | |
address public vLogic; | |
constructor(address _vLogic) { | |
beacon = new UpgradeableBeacon(_vLogic); | |
vLogic = _vLogic; | |
} | |
function update(address _vLogic) public { | |
beacon.upgradeTo(_vLogic); | |
vLogic = _vLogic; | |
} | |
function implementation() public view returns(address) { | |
return beacon.implementation(); | |
} | |
} | |
contract Factory { | |
mapping(uint256 => address) private vaults; | |
VaultBeacon immutable beacon; | |
constructor(address _vLogic) { | |
beacon = new VaultBeacon(_vLogic); | |
} | |
function create(string calldata _name, uint256 _vaLue, uint256 x) external returns (address) { | |
BeaconProxy proxy = new BeaconProxy(address(beacon), | |
abi.encodeWithSelector(Vaultv1(address(0)).initialize.selector, _name, _vaLue) | |
); | |
vaults[x] = address(proxy); | |
return address(proxy); | |
} | |
function getImplementation() public view returns (address) { | |
return beacon.implementation(); | |
} | |
function getBeacon() public view returns (address) { | |
return address(beacon); | |
} | |
function getVault(uint256 x) public view returns (address) { | |
return vaults[x]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=2oUHr8hxzBA
A simple demo showing upgrading contract with beacon
Vaultv1
and get address, e.g.,0xvault1
Factory
and use0xvault1
as constructor argumentFactory#create(vaultname, value, idx)
to set name and value and index of the vaultVaultv1
, callFactory#getVault(idx)
to get the vault address and invoke the method inVaultv1
from this addressVaultv2
, first deployVaultv2
to0xvault2
Factory#getBeacon()
to get the beacon address and invoke theVaultBeacon#update(v2Address)
to useVaultv2