Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jhweintraub committed Sep 25, 2024
1 parent 729dbb9 commit 0688f98
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 136 deletions.
179 changes: 45 additions & 134 deletions contracts/src/v0.8/ccip/test/tokenAdminRegistry/TokenPoolFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ contract TokenPoolFactorySetup is TokenAdminRegistrySetup {
s_registryModuleOwnerCustom = new RegistryModuleOwnerCustom(address(s_tokenAdminRegistry));
s_tokenAdminRegistry.addRegistryModule(address(s_registryModuleOwnerCustom));

s_tokenPoolFactory = new TokenPoolFactory(
s_tokenAdminRegistry,
s_registryModuleOwnerCustom,
s_rmnProxy,
address(s_sourceRouter)
);
s_tokenPoolFactory =
new TokenPoolFactory(s_tokenAdminRegistry, s_registryModuleOwnerCustom, s_rmnProxy, address(s_sourceRouter));

// Create Init Code for BurnMintERC20 TestToken with 18 decimals and supply cap of max uint256 value
s_tokenCreationParams = abi.encode("TestToken", "TT", 18, type(uint256).max, PREMINT_AMOUNT, OWNER);
Expand All @@ -67,12 +63,7 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
function test_TokenPoolFactory_Constructor_Revert() public {
// Revert cause the tokenAdminRegistry is address(0)
vm.expectRevert(TokenPoolFactory.InvalidZeroAddress.selector);
new TokenPoolFactory(
ITokenAdminRegistry(address(0)),
RegistryModuleOwnerCustom(address(0)),
address(0),
address(0)
);
new TokenPoolFactory(ITokenAdminRegistry(address(0)), RegistryModuleOwnerCustom(address(0)), address(0), address(0));

new TokenPoolFactory(
ITokenAdminRegistry(address(0xdeadbeef)),
Expand All @@ -87,29 +78,20 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {

bytes32 dynamicSalt = keccak256(abi.encodePacked(FAKE_SALT, OWNER));

address predictedTokenAddress = Create2.computeAddress(
dynamicSalt,
keccak256(s_tokenInitCode),
address(s_tokenPoolFactory)
);
address predictedTokenAddress =
Create2.computeAddress(dynamicSalt, keccak256(s_tokenInitCode), address(s_tokenPoolFactory));

// Create the constructor params for the predicted pool
bytes memory poolCreationParams = abi.encode(predictedTokenAddress, new address[](0), s_rmnProxy, s_sourceRouter);

// Predict the address of the pool before we make the tx by using the init code and the params
bytes memory predictedPoolInitCode = abi.encodePacked(s_poolInitCode, poolCreationParams);

address predictedPoolAddress = dynamicSalt.computeAddress(
keccak256(predictedPoolInitCode),
address(s_tokenPoolFactory)
);
address predictedPoolAddress =
dynamicSalt.computeAddress(keccak256(predictedPoolInitCode), address(s_tokenPoolFactory));

(address tokenAddress, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool(
new TokenPoolFactory.RemoteTokenPoolInfo[](0),
s_tokenInitCode,
s_poolInitCode,
poolCreationParams,
FAKE_SALT
new TokenPoolFactory.RemoteTokenPoolInfo[](0), s_tokenInitCode, s_poolInitCode, poolCreationParams, FAKE_SALT
);

assertNotEq(address(0), tokenAddress, "Token Address should not be 0");
Expand All @@ -136,27 +118,20 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
RegistryModuleOwnerCustom newRegistryModule = new RegistryModuleOwnerCustom(address(newTokenAdminRegistry));

// We want to deploy a new factory and Owner Module.
TokenPoolFactory newTokenPoolFactory = new TokenPoolFactory(
newTokenAdminRegistry,
newRegistryModule,
s_rmnProxy,
address(s_destRouter)
);
TokenPoolFactory newTokenPoolFactory =
new TokenPoolFactory(newTokenAdminRegistry, newRegistryModule, s_rmnProxy, address(s_destRouter));

newTokenAdminRegistry.addRegistryModule(address(newRegistryModule));

TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = TokenPoolFactory.RemoteChainConfig(
address(newTokenPoolFactory),
address(s_destRouter),
address(s_rmnProxy)
);
TokenPoolFactory.RemoteChainConfig memory remoteChainConfig =
TokenPoolFactory.RemoteChainConfig(address(newTokenPoolFactory), address(s_destRouter), address(s_rmnProxy));

{
TokenPoolFactory.RemoteChainConfig[] memory remoteChainConfigs = new TokenPoolFactory.RemoteChainConfig[](1);
remoteChainConfigs[0] = remoteChainConfig;

TokenPoolFactory.RemoteChainConfigUpdate[]
memory remoteChainConfigUpdates = new TokenPoolFactory.RemoteChainConfigUpdate[](1);
TokenPoolFactory.RemoteChainConfigUpdate[] memory remoteChainConfigUpdates =
new TokenPoolFactory.RemoteChainConfigUpdate[](1);
remoteChainConfigUpdates[0] = TokenPoolFactory.RemoteChainConfigUpdate(DEST_CHAIN_SELECTOR, remoteChainConfig);

// Add the new token Factory to the remote chain config and set it for the simulated destination chain
Expand All @@ -170,28 +145,16 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
// The only field that matters is DEST_CHAIN_SELECTOR because we dont want any existing token pool or token
// on the remote chain
remoteTokenPools[0] = TokenPoolFactory.RemoteTokenPoolInfo(
DEST_CHAIN_SELECTOR,
"",
"",
s_tokenInitCode,
RateLimiter.Config(false, 0, 0)
DEST_CHAIN_SELECTOR, "", "", s_tokenInitCode, RateLimiter.Config(false, 0, 0)
);

// Predict the address of the token and pool on the DESTINATION chain
address predictedTokenAddress = dynamicSalt.computeAddress(
keccak256(s_tokenInitCode),
address(newTokenPoolFactory)
);
address predictedTokenAddress = dynamicSalt.computeAddress(keccak256(s_tokenInitCode), address(newTokenPoolFactory));

// Since the remote chain information was provided, we should be able to get the information from the newly
// deployed token pool using the available getter functions
(, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool(
remoteTokenPools,
s_tokenInitCode,
s_poolInitCode,
"",
FAKE_SALT
);
(, address poolAddress) =
s_tokenPoolFactory.deployTokenAndTokenPool(remoteTokenPools, s_tokenInitCode, s_poolInitCode, "", FAKE_SALT);

// Ensure that the remote Token was set to the one we predicted
assertEq(
Expand All @@ -204,21 +167,15 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
// Create the constructor params for the predicted pool
// The predictedTokenAddress is NOT abi-encoded since the raw evm-address
// is used in the constructor params
bytes memory predictedPoolCreationParams = abi.encode(
predictedTokenAddress,
new address[](0),
s_rmnProxy,
address(s_destRouter)
);
bytes memory predictedPoolCreationParams =
abi.encode(predictedTokenAddress, new address[](0), s_rmnProxy, address(s_destRouter));

// Take the init code and concat the destination params to it, the initCode shouldn't change
bytes memory predictedPoolInitCode = abi.encodePacked(s_poolInitCode, predictedPoolCreationParams);

// Predict the address of the pool on the DESTINATION chain
address predictedPoolAddress = dynamicSalt.computeAddress(
keccak256(predictedPoolInitCode),
address(newTokenPoolFactory)
);
address predictedPoolAddress =
dynamicSalt.computeAddress(keccak256(predictedPoolInitCode), address(newTokenPoolFactory));

// Assert that the address set for the remote pool is the same as the predicted address
assertEq(
Expand All @@ -231,11 +188,7 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
// On the new token pool factory, representing a destination chain,
// deploy a new token and a new pool
(address newTokenAddress, address newPoolAddress) = newTokenPoolFactory.deployTokenAndTokenPool(
new TokenPoolFactory.RemoteTokenPoolInfo[](0),
s_tokenInitCode,
s_poolInitCode,
"",
FAKE_SALT
new TokenPoolFactory.RemoteTokenPoolInfo[](0), s_tokenInitCode, s_poolInitCode, "", FAKE_SALT
);

assertEq(
Expand All @@ -255,42 +208,28 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
vm.startPrank(OWNER);
bytes32 dynamicSalt = keccak256(abi.encodePacked(FAKE_SALT, OWNER));

FactoryBurnMintERC20 newRemoteToken = new FactoryBurnMintERC20(
"TestToken",
"TT",
18,
type(uint256).max,
PREMINT_AMOUNT,
OWNER
);
FactoryBurnMintERC20 newRemoteToken =
new FactoryBurnMintERC20("TestToken", "TT", 18, type(uint256).max, PREMINT_AMOUNT, OWNER);

// We have to create a new factory, registry module, and token admin registry to simulate the other chain

TokenAdminRegistry newTokenAdminRegistry = new TokenAdminRegistry();
RegistryModuleOwnerCustom newRegistryModule = new RegistryModuleOwnerCustom(address(newTokenAdminRegistry));

// We want to deploy a new factory and Owner Module.
TokenPoolFactory newTokenPoolFactory = new TokenPoolFactory(
newTokenAdminRegistry,
newRegistryModule,
s_rmnProxy,
address(s_destRouter)
);
TokenPoolFactory newTokenPoolFactory =
new TokenPoolFactory(newTokenAdminRegistry, newRegistryModule, s_rmnProxy, address(s_destRouter));

newTokenAdminRegistry.addRegistryModule(address(newRegistryModule));

{
TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = TokenPoolFactory.RemoteChainConfig(
address(newTokenPoolFactory),
address(s_destRouter),
address(s_rmnProxy)
);
TokenPoolFactory.RemoteChainConfig memory remoteChainConfig =
TokenPoolFactory.RemoteChainConfig(address(newTokenPoolFactory), address(s_destRouter), address(s_rmnProxy));

TokenPoolFactory.RemoteChainConfig[] memory remoteChainConfigs = new TokenPoolFactory.RemoteChainConfig[](1);
remoteChainConfigs[0] = remoteChainConfig;

TokenPoolFactory.RemoteChainConfigUpdate[]
memory remoteChainConfigUpdates = new TokenPoolFactory.RemoteChainConfigUpdate[](1);
TokenPoolFactory.RemoteChainConfigUpdate[] memory remoteChainConfigUpdates =
new TokenPoolFactory.RemoteChainConfigUpdate[](1);
remoteChainConfigUpdates[0] = TokenPoolFactory.RemoteChainConfigUpdate(DEST_CHAIN_SELECTOR, remoteChainConfig);

// Add the new token Factory to the remote chain config and set it for the simulated destination chain
Expand All @@ -304,22 +243,13 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
// The only field that matters is DEST_CHAIN_SELECTOR because we dont want any existing token pool or token
// on the remote chain
remoteTokenPools[0] = TokenPoolFactory.RemoteTokenPoolInfo(
DEST_CHAIN_SELECTOR,
"",
abi.encode(address(newRemoteToken)),
s_tokenInitCode,
RateLimiter.Config(false, 0, 0)
DEST_CHAIN_SELECTOR, "", abi.encode(address(newRemoteToken)), s_tokenInitCode, RateLimiter.Config(false, 0, 0)
);

// Since the remote chain information was provided, we should be able to get the information from the newly
// deployed token pool using the available getter functions
(address tokenAddress, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool(
remoteTokenPools,
s_tokenInitCode,
s_poolInitCode,
"",
FAKE_SALT
);
(address tokenAddress, address poolAddress) =
s_tokenPoolFactory.deployTokenAndTokenPool(remoteTokenPools, s_tokenInitCode, s_poolInitCode, "", FAKE_SALT);

assertEq(address(TokenPool(poolAddress).getToken()), tokenAddress, "Token Address should have been set locally");

Expand All @@ -333,21 +263,15 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
// Create the constructor params for the predicted pool
// The predictedTokenAddress is NOT abi-encoded since the raw evm-address
// is used in the constructor params
bytes memory predictedPoolCreationParams = abi.encode(
address(newRemoteToken),
new address[](0),
s_rmnProxy,
address(s_destRouter)
);
bytes memory predictedPoolCreationParams =
abi.encode(address(newRemoteToken), new address[](0), s_rmnProxy, address(s_destRouter));

// Take the init code and concat the destination params to it, the initCode shouldn't change
bytes memory predictedPoolInitCode = abi.encodePacked(s_poolInitCode, predictedPoolCreationParams);

// Predict the address of the pool on the DESTINATION chain
address predictedPoolAddress = dynamicSalt.computeAddress(
keccak256(predictedPoolInitCode),
address(newTokenPoolFactory)
);
address predictedPoolAddress =
dynamicSalt.computeAddress(keccak256(predictedPoolInitCode), address(newTokenPoolFactory));

// Assert that the address set for the remote pool is the same as the predicted address
assertEq(
Expand All @@ -359,11 +283,7 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
// On the new token pool factory, representing a destination chain,
// deploy a new token and a new pool
address newPoolAddress = newTokenPoolFactory.deployTokenPoolWithExistingToken(
address(newRemoteToken),
new TokenPoolFactory.RemoteTokenPoolInfo[](0),
s_poolInitCode,
"",
FAKE_SALT
address(newRemoteToken), new TokenPoolFactory.RemoteTokenPoolInfo[](0), s_poolInitCode, "", FAKE_SALT
);

assertEq(
Expand Down Expand Up @@ -396,19 +316,11 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {
TokenPoolFactory.RemoteTokenPoolInfo[] memory remoteTokenPools = new TokenPoolFactory.RemoteTokenPoolInfo[](1);

remoteTokenPools[0] = TokenPoolFactory.RemoteTokenPoolInfo(
DEST_CHAIN_SELECTOR,
RANDOM_POOL_ADDRESS,
RANDOM_TOKEN_ADDRESS,
"",
RateLimiter.Config(false, 0, 0)
DEST_CHAIN_SELECTOR, RANDOM_POOL_ADDRESS, RANDOM_TOKEN_ADDRESS, "", RateLimiter.Config(false, 0, 0)
);

(address tokenAddress, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool(
remoteTokenPools,
s_tokenInitCode,
s_poolInitCode,
poolCreationParams,
FAKE_SALT
remoteTokenPools, s_tokenInitCode, s_poolInitCode, poolCreationParams, FAKE_SALT
);

assertNotEq(address(0), tokenAddress, "Token Address should not be 0");
Expand Down Expand Up @@ -448,15 +360,14 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup {

remoteChainConfigs[0] = remoteChainConfig;

TokenPoolFactory.RemoteChainConfigUpdate[]
memory remoteChainConfigUpdates = new TokenPoolFactory.RemoteChainConfigUpdate[](1);
TokenPoolFactory.RemoteChainConfigUpdate[] memory remoteChainConfigUpdates =
new TokenPoolFactory.RemoteChainConfigUpdate[](1);
remoteChainConfigUpdates[0] = TokenPoolFactory.RemoteChainConfigUpdate(DEST_CHAIN_SELECTOR, remoteChainConfig);

s_tokenPoolFactory.updateRemoteChainConfig(remoteChainConfigUpdates);

TokenPoolFactory.RemoteChainConfig memory updatedRemoteChainConfig = s_tokenPoolFactory.getRemoteChainConfig(
DEST_CHAIN_SELECTOR
);
TokenPoolFactory.RemoteChainConfig memory updatedRemoteChainConfig =
s_tokenPoolFactory.getRemoteChainConfig(DEST_CHAIN_SELECTOR);

assertEq(
remoteChainConfig.remotePoolFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ contract TokenPoolFactory is OwnerIsCreator, ITypeAndVersion {
bytes memory tokenPoolInitArgs,
bytes32 salt
) external returns (address, address) {
// Ensure a unique deployment between senders even if the same input parameter is used
// Ensure a unique deployment between senders even if the same input parameter is used to prevent
// DOS/Frontrunning attacks
salt = keccak256(abi.encodePacked(salt, msg.sender));

// Deploy the token. The constructor parameters are already provided in the tokenInitCode
Expand Down Expand Up @@ -124,7 +125,8 @@ contract TokenPoolFactory is OwnerIsCreator, ITypeAndVersion {
bytes memory tokenPoolInitArgs,
bytes32 salt
) external returns (address poolAddress) {
// Ensure a unique deployment between senders even if the same input parameter is used
// Ensure a unique deployment between senders even if the same input parameter is used to prevent
// DOS/Frontrunning attacks
salt = keccak256(abi.encodePacked(salt, msg.sender));

// create the token pool and return the address
Expand Down

0 comments on commit 0688f98

Please sign in to comment.