Skip to content

Commit

Permalink
Deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohann committed Oct 24, 2024
1 parent 280df54 commit 2801af7
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 15 deletions.
1 change: 1 addition & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ forge-std/=lib/forge-std/src/
ds-test/=lib/solmate/lib/ds-test/src/
@analog-gmp/=src/
@analog-gmp-testing/=test/
@universal-factory/=lib/universal-factory/src
52 changes: 37 additions & 15 deletions scripts/Deploy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

pragma solidity ^0.8.0;

import {IUniversalFactory} from "@universal-factory/IUniversalFactory.sol";
import {Script} from "forge-std/Script.sol";
import {console} from "forge-std/console.sol";
import {IGateway} from "../src/interfaces/IGateway.sol";
Expand Down Expand Up @@ -33,7 +34,17 @@ contract MigrateGateway is Script {
*/
uint256 internal constant MINIMAL_DEPLOYER_BALANCE = 0.5 ether;

address internal constant PROXY_CODEHASH = 0x0000000000001C4Bf962dF86e38F0c10c7972C6E
/**
* @dev Universal Factory used to deploy the implementation contract
* see https://github.com/Analog-Labs/universal-factory/tree/main for mode details.
*/
IUniversalFactory internal constant FACTORY = IUniversalFactory(0x0000000000001C4Bf962dF86e38F0c10c7972C6E);

/**
* @dev Bytecode hash of the Universal Factory, used to verify if the contract is deployed.
*/
bytes32 internal constant FACTORY_CODEHASH = 0x0dac89b851eaa2369ef725788f1aa9e2094bc7819f5951e3eeaa28420f202b50;


/**
* Information about the current state of the migration
Expand Down Expand Up @@ -72,20 +83,26 @@ contract MigrateGateway is Script {
private
view
returns (UpdateNetworkInfo memory info, bool hasProxy)
{
// Verify if the provided proxy address is valid
{
if (proxyAddress.code.length > 0) {
bytes32 codehash;
assembly {
codehash := extcodehash(proxyAddress)
}
require(codehash == PROXY_CODEHASH, "UpgradeGateway: invalid proxy codehash");
} else {
console.log("PROXY NOT DEPLOYED");
}
}
{
//////////////////////////////////////////////////
// Verify if the proxy is deployed and is valid //
//////////////////////////////////////////////////
hasProxy = proxyAddress.code.length > 0;
if (hasProxy) {
require(proxyAddress.codehash == PROXY_CODEHASH, "invalid proxy codehash");
} else {
console.log("PROXY NOT DEPLOYED");
}

////////////////////////////////////////////////
// Verify if the UNIVERSAL FACORY is deployed //
////////////////////////////////////////////////
require(address(FACTORY).code.length > 0, "universal factory not deployed");
require(address(FACTORY).codehash == FACTORY_CODEHASH, "invalid universal factory codehash");

/////////////////////////
// Retrieve Chain Info //
/////////////////////////

// Allocate the network information
info = UpdateNetworkInfo({
Expand Down Expand Up @@ -285,7 +302,12 @@ contract MigrateGateway is Script {
// Switch the network
vm.selectFork(forks[i]);


// Deploy the implementation contract
bytes memory initCode = abi.concat(
type(Gateway).creationCode,
abi.encode(Gateway, (uint16, address))
);
FACTORY.create2();
}


Expand Down
92 changes: 92 additions & 0 deletions src/NetworkID.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: MIT
// Analog's Contracts (last updated v0.1.0) (scripts/Upgrade.sol)

pragma solidity ^0.8.0;

import {BranchlessMath} from "./utils/BranchlessMath.sol";

type NetworkID is uint16;

library NetworkIDHelpers {
NetworkID internal constant MAINNET = NetworkID.wrap(0);
NetworkID internal constant ASTAR = NetworkID.wrap(1);
NetworkID internal constant POLYGON_POS = NetworkID.wrap(2);
NetworkID internal constant ETHEREUM_LOCAL_DEV = NetworkID.wrap(3);
NetworkID internal constant GOERLI = NetworkID.wrap(4);
NetworkID internal constant SEPOLIA = NetworkID.wrap(5);
NetworkID internal constant ASTAR_LOCAL_DEV = NetworkID.wrap(6);
NetworkID internal constant SHIBUYA = NetworkID.wrap(7);
NetworkID internal constant BINANCE_SMART_CHAIN_TESTNET = NetworkID.wrap(9);
NetworkID internal constant ARBITRUM_SEPOLIA = NetworkID.wrap(10);

function asUint(NetworkID networkId) internal pure returns (uint16) {
return NetworkID.unwrap(networkId);
}

function chainId(NetworkID networkId) internal pure returns (uint64) {
uint256 id = NetworkID.unwrap(networkId);
uint256 chainid = type(uint256).max;

// Ethereum Mainnet
chainid = BranchlessMath.ternary(id == asUint(MAINNET), 0, chainid);
// Astar
chainid = BranchlessMath.ternary(id == asUint(ASTAR), 592, chainid);
// Polygon PoS
chainid = BranchlessMath.ternary(id == asUint(POLYGON_POS), 137, chainid);
// Ethereum local testnet
chainid = BranchlessMath.ternary(id == asUint(ETHEREUM_LOCAL_DEV), 1337, chainid);
// Goerli
chainid = BranchlessMath.ternary(id == asUint(GOERLI), 5, chainid);
// Sepolia
chainid = BranchlessMath.ternary(id == asUint(SEPOLIA), 11155111, chainid);
// Astar local testnet
chainid = BranchlessMath.ternary(id == asUint(ASTAR_LOCAL_DEV), 592, chainid);
// Shibuya
chainid = BranchlessMath.ternary(id == asUint(SHIBUYA), 81, chainid);
// Binance Smart Chain
chainid = BranchlessMath.ternary(id == asUint(BINANCE_SMART_CHAIN_TESTNET), 97, chainid);
// Arbitrum Sepolia
chainid = BranchlessMath.ternary(id == asUint(ARBITRUM_SEPOLIA), 421614, chainid);

require(chainid != type(uint256).max, "the provided network id doesn't exists");

return uint64(chainid);
}

/**
* @dev Try to get the network id from the chain id.
*/
function tryFromChainID(uint256 chainid) internal pure returns (bool, NetworkID) {
uint256 networkId = type(uint256).max;

// Ethereum Mainnet
networkId = BranchlessMath.ternary(chainid == 0, asUint(MAINNET), networkId);
// Astar
networkId = BranchlessMath.ternary(chainid == 592, asUint(ASTAR), networkId);
// Polygon PoS
networkId = BranchlessMath.ternary(chainid == 137, asUint(POLYGON_POS), networkId);
// Ethereum local testnet
networkId = BranchlessMath.ternary(chainid == 1337, asUint(ETHEREUM_LOCAL_DEV), networkId);
// Goerli
networkId = BranchlessMath.ternary(chainid == 5, asUint(GOERLI), networkId);
// Sepolia
networkId = BranchlessMath.ternary(chainid == 11155111, asUint(SEPOLIA), networkId);
// Astar local testnet
networkId = BranchlessMath.ternary(chainid == 592, asUint(ASTAR_LOCAL_DEV), networkId);
// Shibuya
networkId = BranchlessMath.ternary(chainid == 81, asUint(SHIBUYA), networkId);
// Binance Smart Chain
networkId = BranchlessMath.ternary(chainid == 97, asUint(BINANCE_SMART_CHAIN_TESTNET), networkId);
// Arbitrum Sepolia
networkId = BranchlessMath.ternary(chainid == 421614, asUint(ARBITRUM_SEPOLIA), networkId);

bool exists = networkId != type(uint256).max;
return (exists, NetworkID.wrap(uint16(networkId)));
}

function fromChainID(uint256 chainid) internal pure returns (NetworkID) {
(bool exists, NetworkID networkId) = tryFromChainID(chainid);
require(exists, "network id doesn't exists for the given chain id");
return networkId;
}
}

0 comments on commit 2801af7

Please sign in to comment.