Skip to content

Instantly share code, notes, and snippets.

@maymax777
Last active March 1, 2022 09:58
Show Gist options
  • Save maymax777/ddf8c8c7f774ff9785527504bede869a to your computer and use it in GitHub Desktop.
Save maymax777/ddf8c8c7f774ff9785527504bede869a to your computer and use it in GitHub Desktop.
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "./IVestingFactory.sol";
contract VestingProxy is ERC1967Proxy {
/**
* @dev Storage slot with the factory of the contract.
* This is the keccak-256 hash of "co.example.factory" subtracted by 1, and is
* validated in the constructor.
*/
bytes32 private constant _FACTORY_SLOT = 0x686e...;
modifier onlyVestingFactory() {
require(msg.sender == _factory(), "ERR__UNAUTHORIZED");
_;
}
constructor(address _vesting, bytes memory _data)
ERC1967Proxy(_vesting, _data)
{
assert(
_FACTORY_SLOT ==
bytes32(uint256(keccak256("co.example.factory")) - 1)
);
require(IVestingFactory(msg.sender).vesting() != address(0));
_setFactory(msg.sender);
}
function vesting() public view returns (address) {
return _implementation();
}
function vestingFactory() public view returns (address factory_) {
factory_ = _factory();
}
/**
* @dev Returns the current factory.
*/
function _factory() internal view returns (address factory) {
bytes32 slot = _FACTORY_SLOT;
assembly {
factory := sload(slot)
}
}
/**
* @dev Stores a new address in the EIP1967 factory slot.
*/
function _setFactory(address factory) private {
bytes32 slot = _FACTORY_SLOT;
assembly {
sstore(slot, factory)
}
}
function upgradeVesting(address _vesting) public onlyVestingFactory {
_upgradeTo(_vesting);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment