Skip to content

Commit

Permalink
Update deploy script patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Jul 22, 2024
1 parent ccad4ab commit 46d61a4
Show file tree
Hide file tree
Showing 8 changed files with 1,002 additions and 981 deletions.
5 changes: 1 addition & 4 deletions packages/foundry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
"account": "node script/ListAccount.js",
"chain": "anvil --config-out localhost.json",
"compile": "forge compile",
"deploy": "yarn deploy:setup && yarn deploy:sum && yarn deploy:product",
"deploy:setup": "forge build --build-info --build-info-path out/build-info/ && forge script script/00_DeploySetup.s.sol --rpc-url ${1:-default_network} --broadcast --legacy && node scripts-js/generateTsAbis.js",
"deploy:sum": "forge script script/01_DeployConstantSumPool.s.sol --rpc-url ${1:-default_network} --broadcast",
"deploy:product": "forge script script/02_DeployConstantProductPool.s.sol --rpc-url ${1:-default_network} --broadcast",
"deploy": "forge build --build-info --build-info-path out/build-info/ && forge script script/Deploy.s.sol --rpc-url ${1:-default_network} --broadcast --legacy && node scripts-js/generateTsAbis.js",
"flatten": "forge flatten",
"fork": "anvil --fork-url ${0:-sepolia} --chain-id 31337 --config-out localhost.json",
"format": "npx prettier --write --plugin=prettier-plugin-solidity 'contracts/**/*.sol' 'test/**/*.sol' 'script/*.sol' 'utils/*.sol'",
Expand Down
62 changes: 0 additions & 62 deletions packages/foundry/script/00_DeploySetup.s.sol

This file was deleted.

36 changes: 36 additions & 0 deletions packages/foundry/script/Deploy.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 { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";

import { PoolHelpers } from "./PoolHelpers.sol";
import { ScaffoldHelpers } from "./ScaffoldHelpers.sol";
import { DeployMockTokens } from "./DeployMockTokens.s.sol";
import { DeployConstantSumPool } from "./DeployConstantSumPool.s.sol";
import { DeployConstantProductPool } from "./DeployConstantProductPool.s.sol";

/**
* @title Deploy Script
* @notice Deploys mock tokens, a constant sum pool, and a constant product pool
* @dev Run this script with `yarn deploy`
*/
contract DeployScript is DeployMockTokens, DeployConstantSumPool, DeployConstantProductPool {
function run() external virtual {
// Deploy the mock tokens
(IERC20 mockToken1, IERC20 mockToken2, IERC20 mockVeBAL) = deployMockTokens();

// Deploy a constant sum factory pool
deployConstantSumPool(mockToken1, mockToken2);

// Deploy a constant product pool
deployConstantProductPool(mockToken1, mockToken2, mockVeBAL);

/**
* This function generates the file containing the contracts Abi definitions that are carried from /foundry to /nextjs.
* 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 @@ -15,50 +15,47 @@ import { DevOpsTools } from "lib/foundry-devops/src/DevOpsTools.sol";

import { PoolHelpers } from "./PoolHelpers.sol";
import { ScaffoldHelpers, console } from "./ScaffoldHelpers.sol";
import { VeBALFeeDiscountHook } from "../contracts/hooks/VeBALFeeDiscountHook.sol";
import { ConstantProductFactory } from "../contracts/pools/ConstantProductFactory.sol";

/**
* @title Deploy Constant Product Pool
* @notice Deploys, registers, and initializes a Constant Product Pool
* @dev Set the registration & initialization configurations in the internal getter functions
* @dev This script runs as part of `yarn deploy`, but can also be run discretely with `yarn deploy:product`
* @notice Deploys a factory and hooks contract before deploying, registering, and initializing a Constant Product Pool
*/
contract DeployConstantProductPool is PoolHelpers, ScaffoldHelpers {
function run() external virtual {
function deployConstantProductPool(IERC20 token1, IERC20 token2, IERC20 veBAL) internal {
// Set the deployment configurations
uint32 pauseWindowDuration = 365 days;
RegistrationConfig memory regConfig = getPoolRegistrationConfig(token1, token2);
InitializationConfig memory initConfig = getPoolInitializationConfig(token1, token2);

// Start creating the transactions
uint256 deployerPrivateKey = getDeployerPrivateKey();
vm.startBroadcast(deployerPrivateKey);

// Grab the latest deployment addresses for the mock tokens, constant product factory, and hooks contract
address token1 = DevOpsTools.get_most_recent_deployment(
"MockToken1", // Must match the mock token contract name
block.chainid
);
address token2 = DevOpsTools.get_most_recent_deployment(
"MockToken2", // Must match the mock token contract name
block.chainid
);
address factory = DevOpsTools.get_most_recent_deployment(
"ConstantProductFactory", // Must match the mock token contract name
block.chainid
);
address poolHooksContract = DevOpsTools.get_most_recent_deployment(
"VeBALFeeDiscountHook", // Must match the hooks contract name
block.chainid
// Deploy a constant sum factory contract
ConstantProductFactory factory = new ConstantProductFactory(IVault(vault), pauseWindowDuration);
console.log("Constant Product Factory deployed at: %s", address(factory));

// Deploy a hooks contract that permits pools created by the Constant Product factory
VeBALFeeDiscountHook poolHooksContract = new VeBALFeeDiscountHook(
IVault(vault),
address(factory),
address(veBAL),
address(router)
);
// Grab arguments for pool deployment and initialization outside of broadcast to save gas
RegistrationConfig memory regConfig = getRegistrationConfig(IERC20(token1), IERC20(token2));
InitializationConfig memory initConfig = getInitializationConfig(IERC20(token1), IERC20(token2));
console.log("VeBALFeeDiscountHook deployed at address: %s", address(poolHooksContract));

vm.startBroadcast(deployerPrivateKey);
// Deploy a pool and register it with the vault
address pool = ConstantProductFactory(factory).create(
address pool = factory.create(
regConfig.name,
regConfig.symbol,
regConfig.salt,
regConfig.tokenConfig,
regConfig.swapFeePercentage,
regConfig.protocolFeeExempt,
regConfig.roleAccounts,
poolHooksContract,
address(poolHooksContract),
regConfig.liquidityManagement
);
console.log("Constant Product Pool deployed at: %s", pool);
Expand All @@ -83,7 +80,7 @@ contract DeployConstantProductPool is PoolHelpers, ScaffoldHelpers {
* For STANDARD tokens, the rate provider address must be 0, and paysYieldFees must be false.
* All WITH_RATE tokens need a rate provider, and may or may not be yield-bearing.
*/
function getRegistrationConfig(
function getPoolRegistrationConfig(
IERC20 token1,
IERC20 token2
) internal view returns (RegistrationConfig memory regConfig) {
Expand Down Expand Up @@ -137,7 +134,7 @@ contract DeployConstantProductPool is PoolHelpers, ScaffoldHelpers {
* @dev Set the pool initialization configurations here
* @notice This is where the amounts of tokens to seed the pool with initial liquidity are set
*/
function getInitializationConfig(
function getPoolInitializationConfig(
IERC20 token1,
IERC20 token2
) internal pure returns (InitializationConfig memory poolInitConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { IRateProvider } from "@balancer-labs/v3-interfaces/contracts/vault/IRateProvider.sol";
import { InputHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/InputHelpers.sol";
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";
import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import { DevOpsTools } from "lib/foundry-devops/src/DevOpsTools.sol";

import { PoolHelpers } from "./PoolHelpers.sol";
Expand All @@ -19,33 +20,24 @@ import { ConstantSumFactory } from "../contracts/pools/ConstantSumFactory.sol";
/**
* @title Deploy Constant Sum Pool
* @notice Deploys, registers, and initializes a Constant Sum Pool
* @dev Set the registration & initialization configurations in the internal getter functions below
* @dev This script runs as part of `yarn deploy`, but can also be run discretely with `yarn deploy:sum`
*/
contract DeployConstantSumPool is PoolHelpers, ScaffoldHelpers {
function run() external virtual {
uint256 deployerPrivateKey = getDeployerPrivateKey();

// Grab the latest deployment addresses for the mock tokens and constant sum factory
address token1 = DevOpsTools.get_most_recent_deployment(
"MockToken1", // Must match the mock token contract name
block.chainid
);
address token2 = DevOpsTools.get_most_recent_deployment(
"MockToken2", // Must match the mock token contract name
block.chainid
);
address factory = DevOpsTools.get_most_recent_deployment(
"ConstantSumFactory", // Must match the factory contract name
block.chainid
);
// Grab arguments for pool deployment and initialization outside of broadcast to save gas
function deployConstantSumPool(IERC20 token1, IERC20 token2) internal {
// Set the deployment configurations
uint32 pauseWindowDuration = 365 days;
RegistrationConfig memory regConfig = getRegistrationConfig(token1, token2);
InitializationConfig memory initConfig = getInitializationConfig(token1, token2);

// Start creating the transactions
uint256 deployerPrivateKey = getDeployerPrivateKey();
vm.startBroadcast(deployerPrivateKey);

// Deploy a constant sum factory contract
ConstantSumFactory factory = new ConstantSumFactory(IVault(vault), pauseWindowDuration);
console.log("Constant Sum Factory deployed at: %s", address(factory));

// Deploy a pool and register it with the vault
address pool = ConstantSumFactory(factory).create(
address pool = factory.create(
regConfig.name,
regConfig.symbol,
regConfig.salt,
Expand Down Expand Up @@ -78,8 +70,8 @@ contract DeployConstantSumPool is PoolHelpers, ScaffoldHelpers {
* All WITH_RATE tokens need a rate provider, and may or may not be yield-bearing.
*/
function getRegistrationConfig(
address token1,
address token2
IERC20 token1,
IERC20 token2
) internal view returns (RegistrationConfig memory regConfig) {
string memory name = "Constant Sum Pool"; // name for the pool
string memory symbol = "CSP"; // symbol for the BPT
Expand All @@ -90,13 +82,13 @@ contract DeployConstantSumPool is PoolHelpers, ScaffoldHelpers {

TokenConfig[] memory tokenConfig = new TokenConfig[](2); // An array of descriptors for the tokens the pool will manage.
tokenConfig[0] = TokenConfig({ // Make sure to have proper token order (alphanumeric)
token: IERC20(token1),
token: token1,
tokenType: TokenType.STANDARD, // STANDARD or WITH_RATE
rateProvider: IRateProvider(address(0)), // The rate provider for a token (see further documentation above)
paysYieldFees: false // Flag indicating whether yield fees should be charged on this token
});
tokenConfig[1] = TokenConfig({ // Make sure to have proper token order (alphanumeric)
token: IERC20(token2),
token: token2,
tokenType: TokenType.STANDARD, // STANDARD or WITH_RATE
rateProvider: IRateProvider(address(0)), // The rate provider for a token (see further documentation above)
paysYieldFees: false // Flag indicating whether yield fees should be charged on this token
Expand Down Expand Up @@ -132,12 +124,12 @@ contract DeployConstantSumPool is PoolHelpers, ScaffoldHelpers {
* @notice this is where the amounts of tokens to be initially added to the pool are set
*/
function getInitializationConfig(
address token1,
address token2
IERC20 token1,
IERC20 token2
) internal pure returns (InitializationConfig memory poolInitConfig) {
IERC20[] memory tokens = new IERC20[](2); // Array of tokens to be used in the pool
tokens[0] = IERC20(token1);
tokens[1] = IERC20(token2);
tokens[0] = token1;
tokens[1] = token2;
uint256[] memory exactAmountsIn = new uint256[](2); // Exact amounts of tokens to be added, sorted in token alphanumeric order
exactAmountsIn[0] = 50e18; // amount of token1 to send during pool initialization
exactAmountsIn[1] = 50e18; // amount of token2 to send during pool initialization
Expand Down
35 changes: 35 additions & 0 deletions packages/foundry/script/DeployMockTokens.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";

import { PoolHelpers } from "./PoolHelpers.sol";
import { ScaffoldHelpers, console } from "./ScaffoldHelpers.sol";
import { MockToken1 } from "../contracts/mocks/MockToken1.sol";
import { MockToken2 } from "../contracts/mocks/MockToken2.sol";
import { MockVeBAL } from "../contracts/mocks/MockVeBAL.sol";

/**
* @title Deploy Mock Tokens
* @notice Deploys mock tokens for use with pools and hooks
*/
contract DeployMockTokens is PoolHelpers, ScaffoldHelpers {
function deployMockTokens() internal returns (IERC20 mockToken1, IERC20 mockToken2, IERC20 mockVeBAL) {
uint256 deployerPrivateKey = getDeployerPrivateKey();

vm.startBroadcast(deployerPrivateKey);

// For use with pool contracts
mockToken1 = new MockToken1("Mock Token 1", "MT1", 1000e18);
mockToken2 = new MockToken2("Mock Token 2", "MT2", 1000e18);
console.log("MockToken1 deployed at: %s", address(mockToken1));
console.log("MockToken2 deployed at: %s", address(mockToken2));

// For use with VeBALFeeDiscountHook
mockVeBAL = new MockVeBAL("Vote-escrow BAL", "veBAL", 1000e18);
console.log("Mock Vote-escrow BAL deployed at: %s", address(mockVeBAL));

vm.stopBroadcast();
}
}
2 changes: 1 addition & 1 deletion packages/foundry/scripts-js/generateTsAbis.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function getInheritedFunctions(mainArtifact) {
}

function main() {
const current_path_to_broadcast = path.join(__dirname, '..', 'broadcast/00_DeploySetup.s.sol');
const current_path_to_broadcast = path.join(__dirname, '..', 'broadcast/Deploy.s.sol');
console.log('current_path_to_broadcast', current_path_to_broadcast);
const current_path_to_deployments = path.join(__dirname, '..', 'deployments');

Expand Down
Loading

0 comments on commit 46d61a4

Please sign in to comment.