generated from m0-foundation/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05230c0
commit 0d82736
Showing
6 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
pragma solidity 0.8.23; | ||
|
||
import { IMigratable } from "./interfaces/IMigratable.sol"; | ||
|
||
abstract contract Migratable is IMigratable { | ||
/* ============ Variables ============ */ | ||
|
||
/// @dev Storage slot with the address of the current factory. `keccak256('eip1967.proxy.implementation') - 1`. | ||
bytes32 private constant _IMPLEMENTATION_SLOT = | ||
bytes32(0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc); | ||
|
||
/* ============ Interactive Functions ============ */ | ||
|
||
function migrate() external { | ||
address migrator_ = _getMigrator(); | ||
|
||
if (migrator_ == address(0)) revert ZeroMigrator(); | ||
|
||
address oldImplementation_ = implementation(); | ||
|
||
migrator_.delegatecall(""); | ||
|
||
emit Migrate(migrator_, oldImplementation_, implementation()); | ||
} | ||
|
||
/* ============ View/Pure Functions ============ */ | ||
|
||
function implementation() public view returns (address implementation_) { | ||
bytes32 slot_ = _IMPLEMENTATION_SLOT; | ||
|
||
assembly { | ||
implementation_ := sload(slot_) | ||
} | ||
} | ||
|
||
/* ============ Internal View/Pure Functions ============ */ | ||
|
||
function _getMigrator() internal view virtual returns (address migrator_); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
pragma solidity 0.8.23; | ||
|
||
contract Proxy { | ||
/// @dev Storage slot with the address of the current factory. `keccak256('eip1967.proxy.implementation') - 1`. | ||
bytes32 private constant _IMPLEMENTATION_SLOT = | ||
bytes32(0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc); | ||
|
||
constructor(address implementation_) { | ||
if (implementation_ == address(0)) revert(); | ||
|
||
bytes32 slot_ = _IMPLEMENTATION_SLOT; | ||
|
||
assembly { | ||
sstore(slot_, implementation_) | ||
} | ||
} | ||
|
||
fallback() external payable virtual { | ||
bytes32 slot_ = _IMPLEMENTATION_SLOT; | ||
bytes32 implementation_; | ||
|
||
assembly { | ||
implementation_ := sload(slot_) | ||
} | ||
|
||
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()) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
pragma solidity 0.8.23; | ||
|
||
interface IMigratable { | ||
event Migrate(address indexed migrator, address indexed oldImplementation, address indexed newImplementation); | ||
|
||
error ZeroMigrator(); | ||
|
||
function migrate() external; | ||
|
||
function implementation() external view returns (address implementation); | ||
} |
This file contains 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
This file contains 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