Skip to content

Commit

Permalink
Merge pull request #39 from Dev-Rel-as-a-Service/feat/sort-tokens-in-…
Browse files Browse the repository at this point in the history
…scripts

Fix sorting issue for TokenConfig and tokens, and small debugs for 2nd pool with DeployPool.s.sol
  • Loading branch information
steve0xp authored May 16, 2024
2 parents 1a4ebcd + c2131fb commit 5697ca0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
10 changes: 7 additions & 3 deletions packages/foundry/script/DeployFactoryAndPool.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "./ScaffoldETHDeploy.s.sol";
import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol";
import {TokenConfig} from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import {HelperConfig} from "../utils/HelperConfig.sol";
import {ArrayHelpers} from "@balancer-labs/v3-solidity-utils/contracts/helpers/ArrayHelpers.sol";
import {InputHelpers} from "@balancer-labs/v3-solidity-utils/contracts/helpers/InputHelpers.sol";

/**
* @title DeployFactoryAndPool
Expand All @@ -28,7 +30,7 @@ contract DeployFactoryAndPool is ScaffoldETHDeploy, DeployPool {

// Deploy mock tokens. Remove this if using already deployed tokens and instead set the tokens above
vm.startBroadcast(deployerPrivateKey);
(IERC20 token1, IERC20 token2) = deployMockTokens();
(address mockToken1, address mockToken2) = deployMockTokens();
vm.stopBroadcast();

// Look up configuration options from `HelperConfig.sol`
Expand All @@ -38,7 +40,7 @@ contract DeployFactoryAndPool is ScaffoldETHDeploy, DeployPool {
string memory name,
string memory symbol,
TokenConfig[] memory tokenConfig
) = helperConfig.getPoolConfig(token1, token2);
) = helperConfig.getPoolConfig(mockToken1, mockToken2);
(
IERC20[] memory tokens,
uint256[] memory exactAmountsIn,
Expand All @@ -58,8 +60,10 @@ contract DeployFactoryAndPool is ScaffoldETHDeploy, DeployPool {
address(customPoolFactory),
name,
symbol,
tokenConfig
helperConfig.sortTokenConfig(tokenConfig)
);

tokens = InputHelpers.sortTokens(tokens);
initializePool(
pool,
tokens,
Expand Down
28 changes: 18 additions & 10 deletions packages/foundry/script/DeployPool.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol";
import {TokenConfig} from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import {DevOpsTools} from "lib/foundry-devops/src/DevOpsTools.sol";
import {Script, console} from "forge-std/Script.sol";
import {InputHelpers} from "@balancer-labs/v3-solidity-utils/contracts/helpers/InputHelpers.sol";
import {HelperConfig} from "../utils/HelperConfig.sol";

/**
* @title DeployPool Script
Expand All @@ -18,6 +20,8 @@ import {Script, console} from "forge-std/Script.sol";
*/
contract DeployPool is HelperFunctions, HelperConfig, Script {
error InvalidPrivateKey(string);
string name = "Scaffold Balancer Constant Price Pool #2"; // name for the pool
string symbol = "POOL2-SB-50scUSD-50scDAI"; // symbol for the BPT

/**
* @dev Set your pool deployment and initialization configurations in `HelperConfig.sol`
Expand All @@ -31,18 +35,21 @@ contract DeployPool is HelperFunctions, HelperConfig, Script {
);
}

// Deploy mock tokens to use in the pool
vm.startBroadcast(deployerPrivateKey);
(IERC20 token1, IERC20 token2) = deployMockTokens();
vm.stopBroadcast();
address mockToken1 = DevOpsTools.get_most_recent_deployment(
"MockToken1", // Must match the mock token contract name
block.chainid
);
address mockToken2 = DevOpsTools.get_most_recent_deployment(
"MockToken2", // Must match the mock token contract name
block.chainid
);

// Look up configurations from `HelperConfig.sol`
HelperConfig helperConfig = new HelperConfig();
(
string memory name,
string memory symbol,
TokenConfig[] memory tokenConfig
) = helperConfig.getPoolConfig(token1, token2);
(, , TokenConfig[] memory tokenConfig) = helperConfig.getPoolConfig(
mockToken1,
mockToken2
);
(
IERC20[] memory tokens,
uint256[] memory exactAmountsIn,
Expand All @@ -61,8 +68,9 @@ contract DeployPool is HelperFunctions, HelperConfig, Script {
poolFactoryAddress,
name,
symbol,
tokenConfig
helperConfig.sortTokenConfig(tokenConfig)
);
tokens = InputHelpers.sortTokens(tokens);
initializePool(
pool,
tokens,
Expand Down
38 changes: 24 additions & 14 deletions packages/foundry/utils/HelperConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,14 @@ contract HelperConfig {
IVault public vault = IVault(0x1FC7F1F84CFE61a04224AC8D3F87f56214FeC08c);
IRouter public router = IRouter(0xA0De078cd5cFa7088821B83e0bD7545ccfb7c883);

/**
* @dev Tokens for pool (also requires configuration of `TokenConfig` in the `getPoolConfig` function)
* @dev If using already deployed tokens, set the addresses here and remove the args for `getPoolConfig` function
* @notice the args for `getPoolConfig` enable the use of mock tokens that are not yet deployed
*/
IERC20 mockToken1; // Make sure to have proper token order (alphanumeric)
IERC20 mockToken2; // Make sure to have proper token order (alphanumeric)

/**
* @notice Creates mock tokens for the pool and mints 1000 of each to the deployer wallet
*/
function deployMockTokens() internal returns (IERC20, IERC20) {
function deployMockTokens() internal returns (address, address) {
MockToken1 scUSD = new MockToken1("Scaffold USD", "scUSD");
MockToken2 scDAI = new MockToken2("Scaffold DAI", "scDAI");

return (scDAI, scUSD);
return (address(scDAI), address(scUSD));
}

/**
Expand All @@ -53,8 +45,8 @@ contract HelperConfig {
* @dev Set the name, symbol, and token configuration for the pool here
*/
function getPoolConfig(
IERC20 token1,
IERC20 token2
address token1,
address token2
)
public
pure
Expand All @@ -69,13 +61,13 @@ contract HelperConfig {

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: token1,
token: IERC20(token1),
tokenType: TokenType.STANDARD, // STANDARD, WITH_RATE, or ERC4626
rateProvider: IRateProvider(address(0)), // The rate provider for a token
yieldFeeExempt: false // Flag indicating whether yield fees should be charged on this token
});
tokenConfig[1] = TokenConfig({ // Make sure to have proper token order (alphanumeric)
token: token2,
token: IERC20(token2),
tokenType: TokenType.STANDARD, // STANDARD, WITH_RATE, or ERC4626
rateProvider: IRateProvider(address(0)), // The rate provider for a token
yieldFeeExempt: false // Flag indicating whether yield fees should be charged on this token
Expand Down Expand Up @@ -108,4 +100,22 @@ contract HelperConfig {
wethIsEth = false; // If true, incoming ETH will be wrapped to WETH; otherwise the Vault will pull WETH tokens
userData = bytes(""); // Additional (optional) data required for adding initial liquidity
}

function sortTokenConfig(
TokenConfig[] memory tokenConfig
) public pure returns (TokenConfig[] memory) {
for (uint256 i = 0; i < tokenConfig.length - 1; i++) {
for (uint256 j = 0; j < tokenConfig.length - i - 1; j++) {
if (tokenConfig[j].token > tokenConfig[j + 1].token) {
// Swap if they're out of order.
(tokenConfig[j], tokenConfig[j + 1]) = (
tokenConfig[j + 1],
tokenConfig[j]
);
}
}
}

return tokenConfig;
}
}

0 comments on commit 5697ca0

Please sign in to comment.