Skip to content

Commit

Permalink
Safe 1.5.0 migration library
Browse files Browse the repository at this point in the history
  • Loading branch information
mmv08 committed Aug 31, 2023
1 parent 94cb9c8 commit 4ed42f1
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 295 deletions.
2 changes: 1 addition & 1 deletion contracts/Safe.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract Safe is
{
using SafeMath for uint256;

string public constant VERSION = "1.4.1";
string public constant VERSION = "1.5.0";

// keccak256(
// "EIP712Domain(uint256 chainId,address verifyingContract)"
Expand Down
84 changes: 0 additions & 84 deletions contracts/libraries/Migrate_to_1_5_0.sol

This file was deleted.

117 changes: 117 additions & 0 deletions contracts/libraries/Safe150Migration.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.7.0 <0.9.0;

import {SafeStorage} from "../libraries/SafeStorage.sol";
import {Guard} from "../base/GuardManager.sol";
import "hardhat/console.sol";

Check failure on line 6 in contracts/libraries/Safe150Migration.sol

View workflow job for this annotation

GitHub Actions / lint

Unexpected import of console file

interface ISafe {
function setFallbackHandler(address handler) external;

function setGuard(address guard) external;
}

/**
* @title Migration Contract for Safe Upgrade
* @notice This contract facilitates the migration of a Safe contract from version 1.3.0/1.4.1 to 1.5.0.
*/
contract Safe150Migration is SafeStorage {
// Address of this contract
address public immutable MIGRATION_SINGLETON;

// Address of Safe contract version 1.5.0 Singleton
address public constant SAFE_150_SINGLETON = address(0x88627c8904eCd9DF96A572Ef32A7ff13b199Ed8D);

// Address of Safe contract version 1.5.0 Singleton (L2)
address public constant SAFE_150_SINGLETON_L2 = address(0x0Ee37514644683f7EB9745a5726C722DeBa77e52);

// Address of Safe contract version 1.5.0 Compatibility Fallback Handler
address public constant SAFE_150_FALLBACK_HANDLER = address(0x8aa755cB169991fEDC3E306751dCb71964A041c7);

bytes32 internal constant GUARD_STORAGE_SLOT = 0x4a204f620c8c5ccdca3fd54d003badd85ba500436a431f0cbda4f558c93c34c8;

/**
* @notice Constructor
* @dev Initializes the migrationSingleton with the contract's own address.
*/
constructor() {
MIGRATION_SINGLETON = address(this);
}

/**
* @notice Event indicating a change of master copy address.
* @param singleton New master copy address
*/
event ChangedMasterCopy(address singleton);

function checkGuard() private view {
address guard = getGuard();
console.log("guard: %s", guard);

Check failure on line 49 in contracts/libraries/Safe150Migration.sol

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
if (guard != address(0)) {
require(Guard(guard).supportsInterface(type(Guard).interfaceId), "GS300");
}
}

/**
* @notice Migrate to Safe 1.5.0 Singleton (L1) at `safe150Singleton`
* @dev This function should only be called via a delegatecall to perform the upgrade.
*/
function migrate() public {
require(address(this) != MIGRATION_SINGLETON, "Migration should only be called via delegatecall");

checkGuard();

singleton = SAFE_150_SINGLETON;
ISafe(address(this)).setFallbackHandler(SAFE_150_FALLBACK_HANDLER);
emit ChangedMasterCopy(singleton);
}

function migrateWithSetGuard(address guard) public {
require(address(this) != MIGRATION_SINGLETON, "Migration should only be called via delegatecall");

ISafe safe = ISafe(address(this));

singleton = SAFE_150_SINGLETON;

safe.setGuard(guard);
safe.setFallbackHandler(SAFE_150_FALLBACK_HANDLER);

emit ChangedMasterCopy(singleton);
}

/**
* @notice Migrate to Safe 1.5.0 Singleton (L2) at `safe150SingletonL2`
* @dev This function should only be called via a delegatecall to perform the upgrade.
*/
function migrateL2() public {
require(address(this) != MIGRATION_SINGLETON, "Migration should only be called via delegatecall");

checkGuard();

singleton = SAFE_150_SINGLETON_L2;
ISafe(address(this)).setFallbackHandler(SAFE_150_FALLBACK_HANDLER);
emit ChangedMasterCopy(singleton);
}

function migrateL2WithSetGuard(address guard) public {
require(address(this) != MIGRATION_SINGLETON, "Migration should only be called via delegatecall");

ISafe safe = ISafe(address(this));

singleton = SAFE_150_SINGLETON_L2;

safe.setGuard(guard);
safe.setFallbackHandler(SAFE_150_FALLBACK_HANDLER);

emit ChangedMasterCopy(singleton);
}

function getGuard() internal view returns (address guard) {
bytes32 slot = GUARD_STORAGE_SLOT;
// solhint-disable-next-line no-inline-assembly
/// @solidity memory-safe-assembly
assembly {
guard := sload(slot)
}
}
}

0 comments on commit 4ed42f1

Please sign in to comment.