Skip to content

Commit

Permalink
refactor deploy scripts using foundry best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Jun 22, 2024
1 parent f2fad7f commit 79f955c
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 895 deletions.
5 changes: 3 additions & 2 deletions packages/foundry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"account": "node script/ListAccount.js",
"chain": "anvil --config-out localhost.json",
"compile": "forge compile",
"deploy:all": "forge build --build-info --build-info-path out/build-info/ && forge script script/DeployFactoryAndPool.s.sol --rpc-url ${1:-default_network} --broadcast --legacy && node script/generateTsAbis.js",
"deploy:pool": "forge build --build-info --build-info-path out/build-info/ && forge script script/DeployPool.s.sol --rpc-url ${1:-default_network} --broadcast",
"deploy:tokens": "forge build --build-info --build-info-path out/build-info/ && forge script script/00_DeployMockTokens.s.sol --rpc-url ${1:-default_network} --broadcast --legacy",
"deploy:factory": "forge build --build-info --build-info-path out/build-info/ && forge script script/01_DeployFactory.s.sol --rpc-url ${1:-default_network} --broadcast --legacy && node script/generateTsAbis.js",
"deploy:pool": "forge build --build-info --build-info-path out/build-info/ && forge script script/02_DeployPool.s.sol --rpc-url ${1:-default_network} --broadcast",
"deploy:verify": "forge build --build-info --build-info-path out/build-info/ && forge script script/DeployFactoryAndPool.s.sol --rpc-url ${1:-default_network} --broadcast --legacy --verify ; node script/generateTsAbis.js",
"flatten": "forge flatten",
"fork": "anvil --fork-url ${0:-sepolia} --chain-id 31337 --config-out localhost.json",
Expand Down
36 changes: 36 additions & 0 deletions packages/foundry/script/00_DeployMockTokens.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { ScaffoldETHDeploy, console } from "./ScaffoldETHDeploy.s.sol";
import { MockToken1 } from "../contracts/MockToken1.sol";
import { MockToken2 } from "../contracts/MockToken2.sol";

/**
* @title Deploy Mock Tokens
* @dev run this script with `yarn deploy:tokens`
*/
contract DeployMockTokens is ScaffoldETHDeploy {
function run() external virtual {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
if (deployerPrivateKey == 0) {
revert InvalidPrivateKey(
"You don't have a deployer account. Make sure you have set DEPLOYER_PRIVATE_KEY in .env or use `yarn generate` to generate a new random account"
);
}

vm.startBroadcast(deployerPrivateKey);
MockToken1 scUSD = new MockToken1("Scaffold USD", "scUSD");
MockToken2 scDAI = new MockToken2("Scaffold DAI", "scDAI");
console.log("Deployed MockToken1 Address: %s", address(scUSD));
console.log("Deployed MockToken2 Address: %s", address(scDAI));
vm.stopBroadcast();

// TODO: figure out how to carry contract info from foundry to nextjs for more than a single deploy script
// /**
// * This function generates the file containing the contracts Abi definitions.
// * These definitions are used to derive the types needed in the custom scaffold-eth hooks, for example.
// * This function should be called last.
// */
// exportDeployments();
}
}
36 changes: 36 additions & 0 deletions packages/foundry/script/01_DeployFactory.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { ScaffoldETHDeploy, console } from "./ScaffoldETHDeploy.s.sol";
import { CustomPoolFactory } from "../contracts/CustomPoolFactory.sol";
import { HelperConfig } from "../utils/HelperConfig.sol";

/**
* @title Deploy Factory
* @dev Set the factory pauseWindowDuration in `HelperConfig.sol`
* @dev Run this script with `yarn deploy:factory`
*/
contract DeployFactory is HelperConfig, ScaffoldETHDeploy {
function run() external virtual {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
if (deployerPrivateKey == 0) {
revert InvalidPrivateKey(
"You don't have a deployer account. Make sure you have set DEPLOYER_PRIVATE_KEY in .env or use `yarn generate` to generate a new random account"
);
}

HelperConfig helperConfig = new HelperConfig();
uint32 pauseWindowDuration = helperConfig.getFactoryConfig();
vm.startBroadcast(deployerPrivateKey);
CustomPoolFactory factory = new CustomPoolFactory(vault, pauseWindowDuration);
console.log("Deployed Factory Address: %s", address(factory));
vm.stopBroadcast();

/**
* This function generates the file containing the contracts Abi definitions.
* These definitions are used to derive the types needed in the custom scaffold-eth hooks, for example.
* This function should be called last.
*/
exportDeployments();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@ import { DevOpsTools } from "lib/foundry-devops/src/DevOpsTools.sol";
import { Script, console } from "forge-std/Script.sol";
import { RegistrationConfig, InitializationConfig } from "../utils/PoolTypes.sol";

import {
TokenConfig,
LiquidityManagement,
PoolRoleAccounts
} from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import { InputHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/InputHelpers.sol";

/**
* @title Deploy Pool Script
* @notice This script creates a new pool using the most recently deployed pool factory and then initializes it
* @notice This script can be run directly with `yarn deploy:pool`, but is also inherited by the `DeployFactoryAndPool.s.sol` script
* @notice This script deploys a new pool using the most recently deployed pool factory and mock tokens
* @dev Set the pool registration and initialization configurations in `HelperConfig.sol`
* @dev Run this script with `yarn deploy:pool`
*/
contract DeployPool is HelperConfig, Script {
error InvalidPrivateKey(string);

/**
* @dev Set your pool deployment and initialization configurations in `HelperConfig.sol`
* @dev Deploy only the pool with the CLI command `yarn deploy:pool`
*/
function run() external virtual {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
if (deployerPrivateKey == 0) {
Expand All @@ -35,6 +27,7 @@ contract DeployPool is HelperConfig, Script {
);
}

// Grab the most recently deployed addresses of mock tokens and factory
address mockToken1 = DevOpsTools.get_most_recent_deployment(
"MockToken1", // Must match the mock token contract name
block.chainid
Expand All @@ -43,22 +36,18 @@ contract DeployPool is HelperConfig, Script {
"MockToken2", // Must match the mock token contract name
block.chainid
);

// Generate all configurations from `HelperConfig.sol`
HelperConfig helperConfig = new HelperConfig();
RegistrationConfig memory regConfig = helperConfig.getPoolConfig(
"Scaffold Balancer Constant Price Pool #2", // name for the pool
"POOL2-SB-50scUSD-50scDAI", // symbol for the BPT
mockToken1,
mockToken2
);
InitializationConfig memory poolInitConfig = helperConfig.getInitializationConfig(regConfig.tokenConfig);
// Get the most recently deployed address of the pool factory
address poolFactoryAddress = DevOpsTools.get_most_recent_deployment(
"CustomPoolFactory", // Must match the pool factory contract name
block.chainid
);
CustomPoolFactory factory = CustomPoolFactory(poolFactoryAddress);

// Set all pool configurations from `HelperConfig.sol`
HelperConfig helperConfig = new HelperConfig();
RegistrationConfig memory regConfig = helperConfig.getPoolConfig(mockToken1, mockToken2);
InitializationConfig memory initConfig = helperConfig.getInitializationConfig(regConfig.tokenConfig);

vm.startBroadcast(deployerPrivateKey);
// Deploy the pool (and register it with the vault)
address newPool = factory.create(
regConfig.name,
Expand All @@ -72,14 +61,14 @@ contract DeployPool is HelperConfig, Script {
regConfig.liquidityManagement
);
console.log("Deployed pool at address: %s", newPool);
IERC20[] memory tokens = InputHelpers.sortTokens(poolInitConfig.tokens);
// Initialize the pool
initializePool(
newPool,
tokens,
poolInitConfig.exactAmountsIn,
poolInitConfig.minBptAmountOut,
poolInitConfig.wethIsEth,
poolInitConfig.userData
InputHelpers.sortTokens(initConfig.tokens),
initConfig.exactAmountsIn,
initConfig.minBptAmountOut,
initConfig.wethIsEth,
initConfig.userData
);
vm.stopBroadcast();
}
Expand Down
87 changes: 0 additions & 87 deletions packages/foundry/script/DeployFactoryAndPool.s.sol

This file was deleted.

1 change: 1 addition & 0 deletions packages/foundry/script/ScaffoldETHDeploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "forge-std/Vm.sol";

contract ScaffoldETHDeploy is Script {
error InvalidChain();
error InvalidPrivateKey(string);

struct Deployment {
string name;
Expand Down
Loading

0 comments on commit 79f955c

Please sign in to comment.