Skip to content

Commit

Permalink
feat(pg): add local deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-goldman committed Oct 9, 2022
1 parent 967b529 commit 19cf359
Show file tree
Hide file tree
Showing 27 changed files with 1,873 additions and 884 deletions.
8 changes: 8 additions & 0 deletions .changeset/silver-kangaroos-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@chugsplash/contracts': minor
'@chugsplash/core': minor
'@chugsplash/demo': minor
'@chugsplash/plugins': minor
---

Adds local ChugSplash deployments for testing contracts on the Hardhat network.
28 changes: 16 additions & 12 deletions packages/contracts/contracts/ChugSplashManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ contract ChugSplashManager is Owned {
struct ChugSplashBundleState {
ChugSplashBundleStatus status;
bool[] executions;
bytes32 root;
uint256 total;
uint256 timeClaimed;
address selectedExecutor;
Expand Down Expand Up @@ -182,8 +183,8 @@ contract ChugSplashManager is Owned {
event ETHDeposited(address indexed from, uint256 indexed amount);

/**
* @notice "Magic" prefix. When prepended to some arbitrary bytecode and used to create a
* contract, the appended bytecode will be deployed as given.
* @notice "Magic" prefix. When prepended to some arbitrary runtime bytecode and used to create
* a contract, the appended bytecode will be deployed as given.
*/
bytes13 internal constant DEPLOY_CODE_PREFIX = 0x600D380380600D6000396000f3;

Expand Down Expand Up @@ -249,7 +250,6 @@ contract ChugSplashManager is Owned {
/**
* @param _registry Address of the ChugSplashRegistry.
* @param _name Name of the project this contract is managing.
* @param _owner Initial owner of this contract.
* @param _proxyUpdater Address of the ProxyUpdater.
* @param _executorBondAmount Executor bond amount in ETH.
* @param _executionLockTime Amount of time for an executor to completely execute a bundle
Expand All @@ -260,12 +260,11 @@ contract ChugSplashManager is Owned {
constructor(
ChugSplashRegistry _registry,
string memory _name,
address _owner,
address _proxyUpdater,
uint256 _executorBondAmount,
uint256 _executionLockTime,
uint256 _ownerBondAmount
) Owned(_owner) {
) Owned(msg.sender) {
registry = _registry;
proxyUpdater = _proxyUpdater;
name = _name;
Expand Down Expand Up @@ -313,6 +312,7 @@ contract ChugSplashManager is Owned {

bundle.status = ChugSplashBundleStatus.PROPOSED;
bundle.executions = new bool[](_bundleSize);
bundle.root = _bundleRoot;

emit ChugSplashBundleProposed(bundleId, _bundleRoot, _bundleSize, _configUri);
registry.announce("ChugSplashBundleProposed");
Expand Down Expand Up @@ -391,7 +391,7 @@ contract ChugSplashManager is Owned {

require(
MerkleTree.verify(
activeBundleId,
bundle.root,
keccak256(abi.encode(_action.target, _action.actionType, _action.data)),
_actionIndex,
_proof,
Expand Down Expand Up @@ -424,7 +424,7 @@ contract ChugSplashManager is Owned {
// standard OOG, then this would halt the entire contract.
// TODO: Make sure this cannot happen in any case other than OOG.
require(
address(created) != proxy,
address(created) == proxy,
"ChugSplashManager: Proxy was not created correctly"
);
}
Expand Down Expand Up @@ -625,7 +625,11 @@ contract ChugSplashManager is Owned {
function getProxyByName(string memory _name) public view returns (address payable) {
return (
payable(
Create2.compute(address(this), keccak256(bytes(_name)), type(Proxy).creationCode)
Create2.compute(
address(this),
keccak256(bytes(_name)),
abi.encodePacked(type(Proxy).creationCode, abi.encode(address(this)))
)
)
);
}
Expand Down Expand Up @@ -703,7 +707,7 @@ contract ChugSplashManager is Owned {
*
* @param _proxy Address of the proxy to upgrade.
* @param _proxyType The proxy's type. This is the zero-address for default proxies.
* @param _code Creation bytecode to be deployed.
* @param _code Runtime bytecode to be deployed.
*/
function _setProxyCode(
address payable _proxy,
Expand All @@ -730,7 +734,7 @@ contract ChugSplashManager is Owned {
return;
}

// Create the deploycode by prepending the magic prefix.
// Create the deploycode by prepending the magic prefix to the runtime bytecode.
bytes memory deploycode = abi.encodePacked(DEPLOY_CODE_PREFIX, _code);

// Deploy the code and set the new implementation address.
Expand All @@ -745,11 +749,11 @@ contract ChugSplashManager is Owned {
// anyway though.
require(
_getAccountCodeHash(newImplementation) == keccak256(_code),
"ProxyUpdater: code was not correctly deployed"
"ChugSplashManager: code was not correctly deployed"
);

// Delegatecall the adapter to upgrade the proxy's implementation contract.
_upgradeProxyTo(_proxy, adapter, implementation);
_upgradeProxyTo(_proxy, adapter, newImplementation);
}

/**
Expand Down
54 changes: 36 additions & 18 deletions packages/contracts/contracts/ChugSplashRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,43 @@ contract ChugSplashRegistry {
uint256 public immutable ownerBondAmount;

/**
* @param _proxyUpdater Address of the ProxyUpdater.
* @param _ownerBondAmount Amount that must be deposited in the ChugSplashManager in order to
* execute a bundle.
* @notice Amount that an executor must send to the ChugSplashManager to claim a bundle.
*/
constructor(address _proxyUpdater, uint256 _ownerBondAmount) {
uint256 public immutable executorBondAmount;

/**
* @notice Amount of time for an executor to completely execute a bundle after claiming it.
*/
uint256 public immutable executionLockTime;

/**
* @param _proxyUpdater Address of the ProxyUpdater.
* @param _ownerBondAmount Amount that must be deposited in the ChugSplashManager in order to
* execute a bundle.
* @param _executorBondAmount Amount that an executor must send to the ChugSplashManager to
* claim a bundle.
* @param _executionLockTime Amount of time for an executor to completely execute a bundle
* after claiming it.
*/
constructor(
address _proxyUpdater,
uint256 _ownerBondAmount,
uint256 _executorBondAmount,
uint256 _executionLockTime
) {
proxyUpdater = _proxyUpdater;
ownerBondAmount = _ownerBondAmount;
executorBondAmount = _executorBondAmount;
executionLockTime = _executionLockTime;
}

/**
* @notice Registers a new project.
*
* @param _name Name of the new ChugSplash project.
* @param _owner Initial owner for the new project.
* @param _executorBondAmount Executor bond amount in ETH.
* @param _executionLockTime Amount of time for an executor to completely execute a bundle
* after claiming it.
* @param _name Name of the new ChugSplash project.
* @param _owner Initial owner for the new project.
*/
function register(
string memory _name,
address _owner,
uint256 _executorBondAmount,
uint256 _executionLockTime
) public {
function register(string memory _name, address _owner) public {
require(
address(projects[_name]) == address(0),
"ChugSplashRegistry: name already registered"
Expand All @@ -109,12 +122,17 @@ contract ChugSplashRegistry {
ChugSplashManager manager = new ChugSplashManager{ salt: bytes32(0) }(
this,
_name,
_owner,
proxyUpdater,
_executorBondAmount,
_executionLockTime,
executorBondAmount,
executionLockTime,
ownerBondAmount
);

// Transfer ownership of the ChugSplashManager to the specified owner. We transfer ownership
// outside of the constructor because this makes it easier to deterministically calculate
// the ChugSplashManager's address before it's deployed.
manager.setOwner(_owner);

projects[_name] = manager;
managers[manager] = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

/**
* @title TransparentUpgradeableAdapter
* @title DefaultAdapter
* @notice Adapter for an OpenZeppelin Transparent Upgradeable proxy. This is the adapter used by
* default proxies in the ChugSplash system. To learn more about the transparent proxy
* pattern, see: https://docs.openzeppelin.com/contracts/4.x/api/proxy#transparent_proxy
*/
contract TransparentUpgradeableAdapter is IProxyAdapter {
contract DefaultAdapter is IProxyAdapter {
/**
* @inheritdoc IProxyAdapter
*/
Expand Down
22 changes: 0 additions & 22 deletions packages/contracts/deploy/000-ChugSplashRegistry.ts

This file was deleted.

6 changes: 6 additions & 0 deletions packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ dotenv.config()
const config: HardhatUserConfig = {
solidity: {
version: '0.8.15',
settings: {
optimizer: {
enabled: true,
runs: 10_000,
},
},
},
networks: {
optimism: {
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@eth-optimism/contracts": "^0.5.29",
"@eth-optimism/contracts-bedrock": "^0.5.2",
"@eth-optimism/contracts-bedrock": "^0.8.0",
"@eth-optimism/core-utils": "^0.9.0",
"@nomiclabs/hardhat-ethers": "^2.0.6",
"@openzeppelin/contracts": "^4.6.0",
Expand All @@ -40,7 +40,7 @@
"dotenv": "^16.0.1",
"ethers": "^5.6.9",
"hardhat": "^2.9.9",
"hardhat-deploy": "^0.11.10",
"hardhat-deploy": "^0.11.16",
"solhint": "^3.3.6",
"solhint-plugin-prettier": "^0.0.5"
}
Expand Down
46 changes: 46 additions & 0 deletions packages/contracts/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ethers } from 'ethers'

import {
DefaultAdapterArtifact,
ProxyUpdaterArtifact,
ChugSplashRegistryArtifact,
} from './ifaces'

const DETERMINISTIC_DEPLOYMENT_PROXY_ADDRESS =
'0x4e59b44847b379578588920ca78fbf26c0b4956c'
export const OWNER_BOND_AMOUNT = ethers.utils.parseUnits('0.1')
export const EXECUTOR_BOND_AMOUNT = ethers.utils.parseUnits('0.1')
export const EXECUTION_LOCK_TIME = 15 * 60

export const PROXY_UPDATER_ADDRESS = ethers.utils.getCreate2Address(
DETERMINISTIC_DEPLOYMENT_PROXY_ADDRESS,
ethers.utils.solidityKeccak256(['string'], ['ProxyUpdater']),
ethers.utils.solidityKeccak256(['bytes'], [ProxyUpdaterArtifact.bytecode])
)

export const CHUGSPLASH_REGISTRY_ADDRESS = ethers.utils.getCreate2Address(
DETERMINISTIC_DEPLOYMENT_PROXY_ADDRESS,
ethers.utils.solidityKeccak256(['string'], ['ChugSplashRegistry']),
ethers.utils.solidityKeccak256(
['bytes', 'bytes'],
[
ChugSplashRegistryArtifact.bytecode,
ethers.utils.defaultAbiCoder.encode(
['address', 'uint256', 'uint256', 'uint256'],
[
PROXY_UPDATER_ADDRESS,
OWNER_BOND_AMOUNT,
EXECUTOR_BOND_AMOUNT,
EXECUTION_LOCK_TIME,
]
),
]
)
)

export const DEFAULT_ADAPTER_ADDRESS =
ethers.utils.getCreate2Address(
DETERMINISTIC_DEPLOYMENT_PROXY_ADDRESS,
ethers.utils.solidityKeccak256(['string'], ['DefaultAdapter']),
ethers.utils.solidityKeccak256(['bytes'], [DefaultAdapterArtifact.bytecode])
)
4 changes: 4 additions & 0 deletions packages/contracts/src/ifaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable @typescript-eslint/no-var-requires */
export const ChugSplashRegistryArtifact = require('../artifacts/contracts/ChugSplashRegistry.sol/ChugSplashRegistry.json')
export const ChugSplashManagerArtifact = require('../artifacts/contracts/ChugSplashManager.sol/ChugSplashManager.json')
export const ProxyUpdaterArtifact = require('../artifacts/contracts/ProxyUpdater.sol/ProxyUpdater.json')
export const DefaultAdapterArtifact = require('../artifacts/contracts/adapters/DefaultAdapter.sol/DefaultAdapter.json')

export const ChugSplashRegistryABI = ChugSplashRegistryArtifact.abi
export const ChugSplashManagerABI = ChugSplashManagerArtifact.abi
export const ProxyUpdaterABI = ProxyUpdaterArtifact.abi
export const DefaultAdapterABI = DefaultAdapterArtifact.abi
2 changes: 2 additions & 0 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './ifaces'
export * from './predeploys'
export * from './constants'
Loading

0 comments on commit 19cf359

Please sign in to comment.