diff --git a/contracts/gas-snapshots/ccip.gas-snapshot b/contracts/gas-snapshots/ccip.gas-snapshot index 7da740eb014..6f8fc3466cf 100644 --- a/contracts/gas-snapshots/ccip.gas-snapshot +++ b/contracts/gas-snapshots/ccip.gas-snapshot @@ -316,6 +316,33 @@ EtherSenderReceiverTest_validatedMessage:test_validatedMessage_emptyDataOverwrit EtherSenderReceiverTest_validatedMessage:test_validatedMessage_invalidTokenAmounts() (gas: 17925) EtherSenderReceiverTest_validatedMessage:test_validatedMessage_tokenOverwrittenToWeth() (gas: 25329) EtherSenderReceiverTest_validatedMessage:test_validatedMessage_validMessage_extraArgs() (gas: 26370) +FactoryBurnMintERC20approve:testApproveSuccess() (gas: 55767) +FactoryBurnMintERC20approve:testInvalidAddressReverts() (gas: 10709) +FactoryBurnMintERC20burn:testBasicBurnSuccess() (gas: 172380) +FactoryBurnMintERC20burn:testBurnFromZeroAddressReverts() (gas: 47384) +FactoryBurnMintERC20burn:testExceedsBalanceReverts() (gas: 21962) +FactoryBurnMintERC20burn:testSenderNotBurnerReverts() (gas: 13491) +FactoryBurnMintERC20burnFrom:testBurnFromSuccess() (gas: 58212) +FactoryBurnMintERC20burnFrom:testExceedsBalanceReverts() (gas: 36130) +FactoryBurnMintERC20burnFrom:testInsufficientAllowanceReverts() (gas: 22054) +FactoryBurnMintERC20burnFrom:testSenderNotBurnerReverts() (gas: 13491) +FactoryBurnMintERC20burnFromAlias:testBurnFromSuccess() (gas: 58187) +FactoryBurnMintERC20burnFromAlias:testExceedsBalanceReverts() (gas: 36094) +FactoryBurnMintERC20burnFromAlias:testInsufficientAllowanceReverts() (gas: 22009) +FactoryBurnMintERC20burnFromAlias:testSenderNotBurnerReverts() (gas: 13446) +FactoryBurnMintERC20constructor:testConstructorSuccess() (gas: 1495459) +FactoryBurnMintERC20decreaseApproval:testDecreaseApprovalSuccess() (gas: 31323) +FactoryBurnMintERC20grantMintAndBurnRoles:testGrantMintAndBurnRolesSuccess() (gas: 121439) +FactoryBurnMintERC20grantRole:testGrantBurnAccessSuccess() (gas: 53612) +FactoryBurnMintERC20grantRole:testGrantManySuccess() (gas: 963184) +FactoryBurnMintERC20grantRole:testGrantMintAccessSuccess() (gas: 94434) +FactoryBurnMintERC20increaseApproval:testIncreaseApprovalSuccess() (gas: 44368) +FactoryBurnMintERC20mint:testBasicMintSuccess() (gas: 149987) +FactoryBurnMintERC20mint:testMaxSupplyExceededReverts() (gas: 50703) +FactoryBurnMintERC20mint:testSenderNotMinterReverts() (gas: 11328) +FactoryBurnMintERC20supportsInterface:testConstructorSuccess() (gas: 11345) +FactoryBurnMintERC20transfer:testInvalidAddressReverts() (gas: 10707) +FactoryBurnMintERC20transfer:testTransferSuccess() (gas: 42427) FeeQuoter_applyDestChainConfigUpdates:test_InvalidChainFamilySelector_Revert() (gas: 16686) FeeQuoter_applyDestChainConfigUpdates:test_InvalidDestChainConfigDestChainSelectorEqZero_Revert() (gas: 16588) FeeQuoter_applyDestChainConfigUpdates:test_applyDestChainConfigUpdatesDefaultTxGasLimitEqZero_Revert() (gas: 16630) @@ -368,20 +395,20 @@ FeeQuoter_getValidatedFee:test_NotAFeeToken_Revert() (gas: 21172) FeeQuoter_getValidatedFee:test_SingleTokenMessage_Success() (gas: 113309) FeeQuoter_getValidatedFee:test_TooManyTokens_Revert() (gas: 22691) FeeQuoter_getValidatedFee:test_ZeroDataAvailabilityMultiplier_Success() (gas: 62714) -FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedErc20Above18Decimals_Success() (gas: 1973907) -FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedErc20Below18Decimals_Success() (gas: 1973865) -FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedFeedAt0Decimals_Success() (gas: 1953984) -FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedFeedAt18Decimals_Success() (gas: 1973639) -FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedFlippedDecimals_Success() (gas: 1973843) -FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedMaxInt224Value_Success() (gas: 1973655) +FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedErc20Above18Decimals_Success() (gas: 1973707) +FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedErc20Below18Decimals_Success() (gas: 1973665) +FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedFeedAt0Decimals_Success() (gas: 1953784) +FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedFeedAt18Decimals_Success() (gas: 1973439) +FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedFlippedDecimals_Success() (gas: 1973643) +FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedMaxInt224Value_Success() (gas: 1973455) FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeedOverStalenessPeriod_Success() (gas: 64610) FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPriceFromFeed_Success() (gas: 64490) FeeQuoter_getValidatedTokenPrice:test_GetValidatedTokenPrice_Success() (gas: 58894) -FeeQuoter_getValidatedTokenPrice:test_OverflowFeedPrice_Revert() (gas: 1973352) +FeeQuoter_getValidatedTokenPrice:test_OverflowFeedPrice_Revert() (gas: 1973152) FeeQuoter_getValidatedTokenPrice:test_StaleFeeToken_Success() (gas: 61764) FeeQuoter_getValidatedTokenPrice:test_TokenNotSupportedFeed_Revert() (gas: 116495) FeeQuoter_getValidatedTokenPrice:test_TokenNotSupported_Revert() (gas: 14037) -FeeQuoter_getValidatedTokenPrice:test_UnderflowFeedPrice_Revert() (gas: 1972029) +FeeQuoter_getValidatedTokenPrice:test_UnderflowFeedPrice_Revert() (gas: 1971829) FeeQuoter_onReport:test_OnReport_StaleUpdate_Revert() (gas: 43631) FeeQuoter_onReport:test_onReport_InvalidForwarder_Reverts() (gas: 23492) FeeQuoter_onReport:test_onReport_Success() (gas: 80094) @@ -983,6 +1010,12 @@ TokenPoolAndProxy:test_lockOrBurn_lockRelease_Success() (gas: 5793246) TokenPoolAndProxy:test_setPreviousPool_Success() (gas: 3070731) TokenPoolAndProxyMigration:test_tokenPoolMigration_Success_1_2() (gas: 6434801) TokenPoolAndProxyMigration:test_tokenPoolMigration_Success_1_4() (gas: 6634934) +TokenPoolFactoryTests:test_TokenPoolFactory_Constructor_Revert() (gas: 1206695) +TokenPoolFactoryTests:test_createTokenPoolLockRelease_NoExistingToken_predict_Success() (gas: 12624842) +TokenPoolFactoryTests:test_createTokenPool_ExistingRemoteToken_AndPredictPool_Success() (gas: 12546380) +TokenPoolFactoryTests:test_createTokenPool_WithNoExistingRemoteContracts_predict_Success() (gas: 12887535) +TokenPoolFactoryTests:test_createTokenPool_WithNoExistingTokenOnRemoteChain_Success() (gas: 5833663) +TokenPoolFactoryTests:test_createTokenPool_WithRemoteTokenAndRemotePool_Success() (gas: 5974010) TokenPoolWithAllowList_applyAllowListUpdates:test_AllowListNotEnabled_Revert() (gas: 1979943) TokenPoolWithAllowList_applyAllowListUpdates:test_OnlyOwner_Revert() (gas: 12113) TokenPoolWithAllowList_applyAllowListUpdates:test_SetAllowListSkipsZero_Success() (gas: 23476) diff --git a/contracts/gas-snapshots/operatorforwarder.gas-snapshot b/contracts/gas-snapshots/operatorforwarder.gas-snapshot index 66bb19f1f6f..551fde38f35 100644 --- a/contracts/gas-snapshots/operatorforwarder.gas-snapshot +++ b/contracts/gas-snapshots/operatorforwarder.gas-snapshot @@ -2,8 +2,8 @@ FactoryTest:test_DeployNewForwarderAndTransferOwnership_Success() (gas: 1059722) FactoryTest:test_DeployNewForwarder_Success() (gas: 1048209) FactoryTest:test_DeployNewOperatorAndForwarder_Success() (gas: 4069305) FactoryTest:test_DeployNewOperator_Success() (gas: 3020464) -ForwarderTest:test_Forward_Success(uint256) (runs: 257, μ: 226979, ~: 227289) -ForwarderTest:test_MultiForward_Success(uint256,uint256) (runs: 257, μ: 258577, ~: 259120) +ForwarderTest:test_Forward_Success(uint256) (runs: 256, μ: 226978, ~: 227289) +ForwarderTest:test_MultiForward_Success(uint256,uint256) (runs: 256, μ: 258575, ~: 259120) ForwarderTest:test_OwnerForward_Success() (gas: 30118) ForwarderTest:test_SetAuthorizedSenders_Success() (gas: 160524) ForwarderTest:test_TransferOwnershipWithMessage_Success() (gas: 35123) @@ -11,5 +11,5 @@ OperatorTest:test_CancelOracleRequest_Success() (gas: 274436) OperatorTest:test_FulfillOracleRequest_Success() (gas: 330603) OperatorTest:test_NotAuthorizedSender_Revert() (gas: 246716) OperatorTest:test_OracleRequest_Success() (gas: 250019) -OperatorTest:test_SendRequestAndCancelRequest_Success(uint96) (runs: 257, μ: 387121, ~: 387124) -OperatorTest:test_SendRequest_Success(uint96) (runs: 257, μ: 303612, ~: 303615) \ No newline at end of file +OperatorTest:test_SendRequestAndCancelRequest_Success(uint96) (runs: 256, μ: 387121, ~: 387124) +OperatorTest:test_SendRequest_Success(uint96) (runs: 256, μ: 303612, ~: 303615) \ No newline at end of file diff --git a/contracts/src/v0.8/ccip/interfaces/IRegistryModuleOwnerCustom.sol b/contracts/src/v0.8/ccip/interfaces/IRegistryModuleOwnerCustom.sol new file mode 100644 index 00000000000..f0412015d37 --- /dev/null +++ b/contracts/src/v0.8/ccip/interfaces/IRegistryModuleOwnerCustom.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity ^0.8.19; + +interface IRegistryModuleOwnerCustom { + /// @notice Registers the admin of the token using the `getCCIPAdmin` method. + /// @param token The token to register the admin for. + /// @dev The caller must be the admin returned by the `getCCIPAdmin` method. + function registerAdminViaGetCCIPAdmin(address token) external; + + /// @notice Registers the admin of the token using the `owner` method. + /// @param token The token to register the admin for. + /// @dev The caller must be the admin returned by the `owner` method. + function registerAdminViaOwner(address token) external; +} diff --git a/contracts/src/v0.8/ccip/interfaces/ITokenAdminRegistry.sol b/contracts/src/v0.8/ccip/interfaces/ITokenAdminRegistry.sol index 0e441229011..3cd620580ee 100644 --- a/contracts/src/v0.8/ccip/interfaces/ITokenAdminRegistry.sol +++ b/contracts/src/v0.8/ccip/interfaces/ITokenAdminRegistry.sol @@ -9,4 +9,22 @@ interface ITokenAdminRegistry { /// @param localToken The token to register the administrator for. /// @param administrator The administrator to register. function proposeAdministrator(address localToken, address administrator) external; + + /// @notice Accepts the administrator role for a token. + /// @param localToken The token to accept the administrator role for. + /// @dev This function can only be called by the pending administrator. + function acceptAdminRole(address localToken) external; + + /// @notice Sets the pool for a token. Setting the pool to address(0) effectively delists the token + /// from CCIP. Setting the pool to any other address enables the token on CCIP. + /// @param localToken The token to set the pool for. + /// @param pool The pool to set for the token. + function setPool(address localToken, address pool) external; + + /// @notice Transfers the administrator role for a token to a new address with a 2-step process. + /// @param localToken The token to transfer the administrator role for. + /// @param newAdmin The address to transfer the administrator role to. Can be address(0) to cancel + /// a pending transfer. + /// @dev The new admin must call `acceptAdminRole` to accept the role. + function transferAdminRole(address localToken, address newAdmin) external; } diff --git a/contracts/src/v0.8/ccip/test/tokenAdminRegistry/FactoryBurnMintERC20.t.sol b/contracts/src/v0.8/ccip/test/tokenAdminRegistry/FactoryBurnMintERC20.t.sol new file mode 100644 index 00000000000..49e309c0a40 --- /dev/null +++ b/contracts/src/v0.8/ccip/test/tokenAdminRegistry/FactoryBurnMintERC20.t.sol @@ -0,0 +1,361 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import {IBurnMintERC20} from "../../../shared/token/ERC20/IBurnMintERC20.sol"; + +import {FactoryBurnMintERC20} from "../../tokenAdminRegistry/TokenPoolFactory/FactoryBurnMintERC20.sol"; +import {BaseTest} from "../BaseTest.t.sol"; + +import {IERC20} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol"; +import {IERC165} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/utils/introspection/IERC165.sol"; + +contract BurnMintERC20Setup is BaseTest { + event Transfer(address indexed from, address indexed to, uint256 value); + event MintAccessGranted(address indexed minter); + event BurnAccessGranted(address indexed burner); + event MintAccessRevoked(address indexed minter); + event BurnAccessRevoked(address indexed burner); + + FactoryBurnMintERC20 internal s_burnMintERC20; + + address internal s_mockPool = address(6243783892); + uint256 internal s_amount = 1e18; + + address internal s_alice; + + function setUp() public virtual override { + BaseTest.setUp(); + + s_alice = makeAddr("alice"); + + s_burnMintERC20 = new FactoryBurnMintERC20("Chainlink Token", "LINK", 18, 1e27, 0, s_alice); + + // Set s_mockPool to be a burner and minter + s_burnMintERC20.grantMintAndBurnRoles(s_mockPool); + deal(address(s_burnMintERC20), OWNER, s_amount); + } +} + +contract FactoryBurnMintERC20constructor is BurnMintERC20Setup { + function testConstructorSuccess() public { + string memory name = "Chainlink token v2"; + string memory symbol = "LINK2"; + uint8 decimals = 19; + uint256 maxSupply = 1e33; + + s_burnMintERC20 = new FactoryBurnMintERC20(name, symbol, decimals, maxSupply, 1e18, s_alice); + + assertEq(name, s_burnMintERC20.name()); + assertEq(symbol, s_burnMintERC20.symbol()); + assertEq(decimals, s_burnMintERC20.decimals()); + assertEq(maxSupply, s_burnMintERC20.maxSupply()); + + assertTrue(s_burnMintERC20.isMinter(s_alice)); + assertTrue(s_burnMintERC20.isBurner(s_alice)); + assertEq(s_burnMintERC20.balanceOf(s_alice), 1e18); + assertEq(s_burnMintERC20.totalSupply(), 1e18); + } +} + +contract FactoryBurnMintERC20approve is BurnMintERC20Setup { + function testApproveSuccess() public { + uint256 balancePre = s_burnMintERC20.balanceOf(STRANGER); + uint256 sendingAmount = s_amount / 2; + + s_burnMintERC20.approve(STRANGER, sendingAmount); + + changePrank(STRANGER); + + s_burnMintERC20.transferFrom(OWNER, STRANGER, sendingAmount); + + assertEq(sendingAmount + balancePre, s_burnMintERC20.balanceOf(STRANGER)); + } + + // Reverts + + function testInvalidAddressReverts() public { + vm.expectRevert(); + + s_burnMintERC20.approve(address(s_burnMintERC20), s_amount); + } +} + +contract FactoryBurnMintERC20transfer is BurnMintERC20Setup { + function testTransferSuccess() public { + uint256 balancePre = s_burnMintERC20.balanceOf(STRANGER); + uint256 sendingAmount = s_amount / 2; + + s_burnMintERC20.transfer(STRANGER, sendingAmount); + + assertEq(sendingAmount + balancePre, s_burnMintERC20.balanceOf(STRANGER)); + } + + // Reverts + + function testInvalidAddressReverts() public { + vm.expectRevert(); + + s_burnMintERC20.transfer(address(s_burnMintERC20), s_amount); + } +} + +contract FactoryBurnMintERC20mint is BurnMintERC20Setup { + function testBasicMintSuccess() public { + uint256 balancePre = s_burnMintERC20.balanceOf(OWNER); + + s_burnMintERC20.grantMintAndBurnRoles(OWNER); + + vm.expectEmit(); + emit Transfer(address(0), OWNER, s_amount); + + s_burnMintERC20.mint(OWNER, s_amount); + + assertEq(balancePre + s_amount, s_burnMintERC20.balanceOf(OWNER)); + } + + // Revert + + function testSenderNotMinterReverts() public { + vm.expectRevert(abi.encodeWithSelector(FactoryBurnMintERC20.SenderNotMinter.selector, OWNER)); + s_burnMintERC20.mint(STRANGER, 1e18); + } + + function testMaxSupplyExceededReverts() public { + changePrank(s_mockPool); + + // Mint max supply + s_burnMintERC20.mint(OWNER, s_burnMintERC20.maxSupply()); + + vm.expectRevert( + abi.encodeWithSelector(FactoryBurnMintERC20.MaxSupplyExceeded.selector, s_burnMintERC20.maxSupply() + 1) + ); + + // Attempt to mint 1 more than max supply + s_burnMintERC20.mint(OWNER, 1); + } +} + +contract FactoryBurnMintERC20burn is BurnMintERC20Setup { + function testBasicBurnSuccess() public { + s_burnMintERC20.grantBurnRole(OWNER); + deal(address(s_burnMintERC20), OWNER, s_amount); + + vm.expectEmit(); + emit Transfer(OWNER, address(0), s_amount); + + s_burnMintERC20.burn(s_amount); + + assertEq(0, s_burnMintERC20.balanceOf(OWNER)); + } + + // Revert + + function testSenderNotBurnerReverts() public { + vm.expectRevert(abi.encodeWithSelector(FactoryBurnMintERC20.SenderNotBurner.selector, OWNER)); + + s_burnMintERC20.burnFrom(STRANGER, s_amount); + } + + function testExceedsBalanceReverts() public { + changePrank(s_mockPool); + + vm.expectRevert("ERC20: burn amount exceeds balance"); + + s_burnMintERC20.burn(s_amount * 2); + } + + function testBurnFromZeroAddressReverts() public { + s_burnMintERC20.grantBurnRole(address(0)); + changePrank(address(0)); + + vm.expectRevert("ERC20: burn from the zero address"); + + s_burnMintERC20.burn(0); + } +} + +contract FactoryBurnMintERC20burnFromAlias is BurnMintERC20Setup { + function setUp() public virtual override { + BurnMintERC20Setup.setUp(); + } + + function testBurnFromSuccess() public { + s_burnMintERC20.approve(s_mockPool, s_amount); + + changePrank(s_mockPool); + + s_burnMintERC20.burn(OWNER, s_amount); + + assertEq(0, s_burnMintERC20.balanceOf(OWNER)); + } + + // Reverts + + function testSenderNotBurnerReverts() public { + vm.expectRevert(abi.encodeWithSelector(FactoryBurnMintERC20.SenderNotBurner.selector, OWNER)); + + s_burnMintERC20.burn(OWNER, s_amount); + } + + function testInsufficientAllowanceReverts() public { + changePrank(s_mockPool); + + vm.expectRevert("ERC20: insufficient allowance"); + + s_burnMintERC20.burn(OWNER, s_amount); + } + + function testExceedsBalanceReverts() public { + s_burnMintERC20.approve(s_mockPool, s_amount * 2); + + changePrank(s_mockPool); + + vm.expectRevert("ERC20: burn amount exceeds balance"); + + s_burnMintERC20.burn(OWNER, s_amount * 2); + } +} + +contract FactoryBurnMintERC20burnFrom is BurnMintERC20Setup { + function setUp() public virtual override { + BurnMintERC20Setup.setUp(); + } + + function testBurnFromSuccess() public { + s_burnMintERC20.approve(s_mockPool, s_amount); + + changePrank(s_mockPool); + + s_burnMintERC20.burnFrom(OWNER, s_amount); + + assertEq(0, s_burnMintERC20.balanceOf(OWNER)); + } + + // Reverts + + function testSenderNotBurnerReverts() public { + vm.expectRevert(abi.encodeWithSelector(FactoryBurnMintERC20.SenderNotBurner.selector, OWNER)); + + s_burnMintERC20.burnFrom(OWNER, s_amount); + } + + function testInsufficientAllowanceReverts() public { + changePrank(s_mockPool); + + vm.expectRevert("ERC20: insufficient allowance"); + + s_burnMintERC20.burnFrom(OWNER, s_amount); + } + + function testExceedsBalanceReverts() public { + s_burnMintERC20.approve(s_mockPool, s_amount * 2); + + changePrank(s_mockPool); + + vm.expectRevert("ERC20: burn amount exceeds balance"); + + s_burnMintERC20.burnFrom(OWNER, s_amount * 2); + } +} + +contract FactoryBurnMintERC20grantRole is BurnMintERC20Setup { + function testGrantMintAccessSuccess() public { + assertFalse(s_burnMintERC20.isMinter(STRANGER)); + + vm.expectEmit(); + emit MintAccessGranted(STRANGER); + + s_burnMintERC20.grantMintAndBurnRoles(STRANGER); + + assertTrue(s_burnMintERC20.isMinter(STRANGER)); + + vm.expectEmit(); + emit MintAccessRevoked(STRANGER); + + s_burnMintERC20.revokeMintRole(STRANGER); + + assertFalse(s_burnMintERC20.isMinter(STRANGER)); + } + + function testGrantBurnAccessSuccess() public { + assertFalse(s_burnMintERC20.isBurner(STRANGER)); + + vm.expectEmit(); + emit BurnAccessGranted(STRANGER); + + s_burnMintERC20.grantBurnRole(STRANGER); + + assertTrue(s_burnMintERC20.isBurner(STRANGER)); + + vm.expectEmit(); + emit BurnAccessRevoked(STRANGER); + + s_burnMintERC20.revokeBurnRole(STRANGER); + + assertFalse(s_burnMintERC20.isBurner(STRANGER)); + } + + function testGrantManySuccess() public { + // Since alice was already granted mint and burn roles in the setup, we will revoke them + // and then grant them again for the purposes of the test + s_burnMintERC20.revokeMintRole(s_alice); + s_burnMintERC20.revokeBurnRole(s_alice); + + uint256 numberOfPools = 10; + address[] memory permissionedAddresses = new address[](numberOfPools + 1); + permissionedAddresses[0] = s_mockPool; + + for (uint160 i = 0; i < numberOfPools; ++i) { + permissionedAddresses[i + 1] = address(i); + s_burnMintERC20.grantMintAndBurnRoles(address(i)); + } + + assertEq(permissionedAddresses, s_burnMintERC20.getBurners()); + assertEq(permissionedAddresses, s_burnMintERC20.getMinters()); + } +} + +contract FactoryBurnMintERC20grantMintAndBurnRoles is BurnMintERC20Setup { + function testGrantMintAndBurnRolesSuccess() public { + assertFalse(s_burnMintERC20.isMinter(STRANGER)); + assertFalse(s_burnMintERC20.isBurner(STRANGER)); + + vm.expectEmit(); + emit MintAccessGranted(STRANGER); + vm.expectEmit(); + emit BurnAccessGranted(STRANGER); + + s_burnMintERC20.grantMintAndBurnRoles(STRANGER); + + assertTrue(s_burnMintERC20.isMinter(STRANGER)); + assertTrue(s_burnMintERC20.isBurner(STRANGER)); + } +} + +contract FactoryBurnMintERC20decreaseApproval is BurnMintERC20Setup { + function testDecreaseApprovalSuccess() public { + s_burnMintERC20.approve(s_mockPool, s_amount); + uint256 allowance = s_burnMintERC20.allowance(OWNER, s_mockPool); + assertEq(allowance, s_amount); + s_burnMintERC20.decreaseApproval(s_mockPool, s_amount); + assertEq(s_burnMintERC20.allowance(OWNER, s_mockPool), allowance - s_amount); + } +} + +contract FactoryBurnMintERC20increaseApproval is BurnMintERC20Setup { + function testIncreaseApprovalSuccess() public { + s_burnMintERC20.approve(s_mockPool, s_amount); + uint256 allowance = s_burnMintERC20.allowance(OWNER, s_mockPool); + assertEq(allowance, s_amount); + s_burnMintERC20.increaseApproval(s_mockPool, s_amount); + assertEq(s_burnMintERC20.allowance(OWNER, s_mockPool), allowance + s_amount); + } +} + +contract FactoryBurnMintERC20supportsInterface is BurnMintERC20Setup { + function testConstructorSuccess() public view { + assertTrue(s_burnMintERC20.supportsInterface(type(IERC20).interfaceId)); + assertTrue(s_burnMintERC20.supportsInterface(type(IBurnMintERC20).interfaceId)); + assertTrue(s_burnMintERC20.supportsInterface(type(IERC165).interfaceId)); + } +} diff --git a/contracts/src/v0.8/ccip/test/tokenAdminRegistry/TokenPoolFactory.t.sol b/contracts/src/v0.8/ccip/test/tokenAdminRegistry/TokenPoolFactory.t.sol new file mode 100644 index 00000000000..b7e9adf5991 --- /dev/null +++ b/contracts/src/v0.8/ccip/test/tokenAdminRegistry/TokenPoolFactory.t.sol @@ -0,0 +1,496 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity ^0.8.24; + +import {IBurnMintERC20} from "../../../shared/token/ERC20/IBurnMintERC20.sol"; +import {IOwner} from "../../interfaces/IOwner.sol"; +import {ITokenAdminRegistry} from "../../interfaces/ITokenAdminRegistry.sol"; + +import {OwnerIsCreator} from "../../../shared/access/OwnerIsCreator.sol"; + +import {RateLimiter} from "../../libraries/RateLimiter.sol"; +import {BurnMintTokenPool} from "../../pools/BurnMintTokenPool.sol"; +import {LockReleaseTokenPool} from "../../pools/LockReleaseTokenPool.sol"; +import {TokenPool} from "../../pools/TokenPool.sol"; + +import {RegistryModuleOwnerCustom} from "../../tokenAdminRegistry/RegistryModuleOwnerCustom.sol"; +import {TokenAdminRegistry} from "../../tokenAdminRegistry/TokenAdminRegistry.sol"; +import {FactoryBurnMintERC20} from "../../tokenAdminRegistry/TokenPoolFactory/FactoryBurnMintERC20.sol"; +import {TokenPoolFactory} from "../../tokenAdminRegistry/TokenPoolFactory/TokenPoolFactory.sol"; +import {TokenAdminRegistrySetup} from "./TokenAdminRegistry.t.sol"; + +import {Create2} from "../../../vendor/openzeppelin-solidity/v5.0.2/contracts/utils/Create2.sol"; + +contract TokenPoolFactorySetup is TokenAdminRegistrySetup { + using Create2 for bytes32; + + TokenPoolFactory internal s_tokenPoolFactory; + RegistryModuleOwnerCustom internal s_registryModuleOwnerCustom; + + bytes internal s_poolInitCode; + bytes internal s_poolInitArgs; + + bytes32 internal constant FAKE_SALT = keccak256(abi.encode("FAKE_SALT")); + + address internal s_rmnProxy = address(0x1234); + + bytes internal s_tokenCreationParams; + bytes internal s_tokenInitCode; + + uint256 public constant PREMINT_AMOUNT = 1e20; // 100 tokens in 18 decimals + + function setUp() public virtual override { + TokenAdminRegistrySetup.setUp(); + + 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)); + + // 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); + + s_tokenInitCode = abi.encodePacked(type(FactoryBurnMintERC20).creationCode, s_tokenCreationParams); + + s_poolInitCode = type(BurnMintTokenPool).creationCode; + + // Create Init Args for BurnMintTokenPool with no allowlist minus the token address + address[] memory allowlist = new address[](1); + allowlist[0] = OWNER; + s_poolInitArgs = abi.encode(allowlist, address(0x1234), s_sourceRouter); + } +} + +contract TokenPoolFactoryTests is TokenPoolFactorySetup { + using Create2 for bytes32; + + 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(0xdeadbeef)), + RegistryModuleOwnerCustom(address(0xdeadbeef)), + address(0xdeadbeef), + address(0xdeadbeef) + ); + } + + function test_createTokenPool_WithNoExistingTokenOnRemoteChain_Success() public { + vm.startPrank(OWNER); + + bytes32 dynamicSalt = keccak256(abi.encodePacked(FAKE_SALT, OWNER)); + + 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 tokenAddress, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool( + new TokenPoolFactory.RemoteTokenPoolInfo[](0), + s_tokenInitCode, + s_poolInitCode, + FAKE_SALT, + TokenPoolFactory.PoolType.BURN_MINT + ); + + assertNotEq(address(0), tokenAddress, "Token Address should not be 0"); + assertNotEq(address(0), poolAddress, "Pool Address should not be 0"); + + assertEq(predictedTokenAddress, tokenAddress, "Token Address should have been predicted"); + assertEq(predictedPoolAddress, poolAddress, "Pool Address should have been predicted"); + + s_tokenAdminRegistry.acceptAdminRole(tokenAddress); + OwnerIsCreator(tokenAddress).acceptOwnership(); + OwnerIsCreator(poolAddress).acceptOwnership(); + + assertEq(poolAddress, s_tokenAdminRegistry.getPool(tokenAddress), "Token Pool should be set"); + assertEq(IOwner(tokenAddress).owner(), OWNER, "Token should be owned by the owner"); + assertEq(IOwner(poolAddress).owner(), OWNER, "Token should be owned by the owner"); + } + + function test_createTokenPool_WithNoExistingRemoteContracts_predict_Success() public { + vm.startPrank(OWNER); + bytes32 dynamicSalt = keccak256(abi.encodePacked(FAKE_SALT, 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)); + + newTokenAdminRegistry.addRegistryModule(address(newRegistryModule)); + + TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = + TokenPoolFactory.RemoteChainConfig(address(newTokenPoolFactory), address(s_destRouter), address(s_rmnProxy)); + + // Create an array of remote pools where nothing exists yet, but we want to predict the address for + // the new pool and token on DEST_CHAIN_SELECTOR + TokenPoolFactory.RemoteTokenPoolInfo[] memory remoteTokenPools = new TokenPoolFactory.RemoteTokenPoolInfo[](1); + + // 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, // remoteChainSelector + "", // remotePoolAddress + type(BurnMintTokenPool).creationCode, // remotePoolInitCode + remoteChainConfig, // remoteChainConfig + TokenPoolFactory.PoolType.BURN_MINT, // poolType + "", // remoteTokenAddress + s_tokenInitCode, // remoteTokenInitCode + 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)); + + // 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, // No existing remote pools + s_tokenInitCode, // Token Init Code + s_poolInitCode, // Pool Init Code + FAKE_SALT, // Salt + TokenPoolFactory.PoolType.BURN_MINT // Pool Type + ); + + // Ensure that the remote Token was set to the one we predicted + assertEq( + abi.encode(predictedTokenAddress), + TokenPool(poolAddress).getRemoteToken(DEST_CHAIN_SELECTOR), + "Token Address should have been predicted" + ); + + { + // 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)); + + // 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)); + + // Assert that the address set for the remote pool is the same as the predicted address + assertEq( + abi.encode(predictedPoolAddress), + TokenPool(poolAddress).getRemotePool(DEST_CHAIN_SELECTOR), + "Pool Address should have been predicted" + ); + } + + // 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, + TokenPoolFactory.PoolType.BURN_MINT + ); + + assertEq( + TokenPool(poolAddress).getRemotePool(DEST_CHAIN_SELECTOR), + abi.encode(newPoolAddress), + "New Pool Address should have been deployed correctly" + ); + + assertEq( + TokenPool(poolAddress).getRemoteToken(DEST_CHAIN_SELECTOR), + abi.encode(newTokenAddress), + "New Token Address should have been deployed correctly" + ); + + // Check that the token pool has the correct permissions + vm.startPrank(poolAddress); + IBurnMintERC20(tokenAddress).mint(poolAddress, 1e18); + + assertEq(1e18, IBurnMintERC20(tokenAddress).balanceOf(poolAddress), "Balance should be 1e18"); + + IBurnMintERC20(tokenAddress).burn(1e18); + assertEq(0, IBurnMintERC20(tokenAddress).balanceOf(poolAddress), "Balance should be 0"); + + vm.stopPrank(); + + assertEq(s_tokenAdminRegistry.getPool(tokenAddress), poolAddress, "Token Pool should be set"); + + // Check the token admin registry for config + TokenAdminRegistry.TokenConfig memory tokenConfig = s_tokenAdminRegistry.getTokenConfig(tokenAddress); + assertEq(tokenConfig.administrator, address(s_tokenPoolFactory), "Administrator should be set"); + assertEq(tokenConfig.pendingAdministrator, OWNER, "Pending Administrator should be 0"); + assertEq(tokenConfig.tokenPool, poolAddress, "Pool Address should be set"); + + // Accept Ownership of the token, pool, and adminRegistry + vm.startPrank(OWNER); + s_tokenAdminRegistry.acceptAdminRole(tokenAddress); + assertEq(s_tokenAdminRegistry.getTokenConfig(tokenAddress).administrator, OWNER, "Administrator should be set"); + assertEq( + s_tokenAdminRegistry.getTokenConfig(tokenAddress).pendingAdministrator, address(0), "Administrator should be set" + ); + + OwnerIsCreator(tokenAddress).acceptOwnership(); + OwnerIsCreator(poolAddress).acceptOwnership(); + + assertEq(IOwner(tokenAddress).owner(), OWNER, "Token should be controlled by the OWNER"); + assertEq(IOwner(poolAddress).owner(), OWNER, "Pool should be controlled by the OWNER"); + } + + function test_createTokenPool_ExistingRemoteToken_AndPredictPool_Success() public { + vm.startPrank(OWNER); + bytes32 dynamicSalt = keccak256(abi.encodePacked(FAKE_SALT, 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)); + + newTokenAdminRegistry.addRegistryModule(address(newRegistryModule)); + + TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = + TokenPoolFactory.RemoteChainConfig(address(newTokenPoolFactory), address(s_destRouter), address(s_rmnProxy)); + + // Create an array of remote pools where nothing exists yet, but we want to predict the address for + // the new pool and token on DEST_CHAIN_SELECTOR + TokenPoolFactory.RemoteTokenPoolInfo[] memory remoteTokenPools = new TokenPoolFactory.RemoteTokenPoolInfo[](1); + + // 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, // remoteChainSelector + "", // remotePoolAddress + type(BurnMintTokenPool).creationCode, // remotePoolInitCode + remoteChainConfig, // remoteChainConfig + TokenPoolFactory.PoolType.BURN_MINT, // poolType + abi.encode(address(newRemoteToken)), // remoteTokenAddress + s_tokenInitCode, // remoteTokenInitCode + RateLimiter.Config(false, 0, 0) // rateLimiterConfig + ); + + // 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, TokenPoolFactory.PoolType.BURN_MINT + ); + + assertEq(address(TokenPool(poolAddress).getToken()), tokenAddress, "Token Address should have been set locally"); + + // Ensure that the remote Token was set to the one we predicted + assertEq( + abi.encode(address(newRemoteToken)), + TokenPool(poolAddress).getRemoteToken(DEST_CHAIN_SELECTOR), + "Token Address should have been predicted" + ); + + // 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)); + + // 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)); + + // Assert that the address set for the remote pool is the same as the predicted address + assertEq( + abi.encode(predictedPoolAddress), + TokenPool(poolAddress).getRemotePool(DEST_CHAIN_SELECTOR), + "Pool Address should have been predicted" + ); + + // 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, + TokenPoolFactory.PoolType.BURN_MINT + ); + + assertEq( + abi.encode(newRemoteToken), + TokenPool(poolAddress).getRemoteToken(DEST_CHAIN_SELECTOR), + "Remote Token Address should have been set correctly" + ); + + assertEq( + TokenPool(poolAddress).getRemotePool(DEST_CHAIN_SELECTOR), + abi.encode(newPoolAddress), + "New Pool Address should have been deployed correctly" + ); + } + + function test_createTokenPool_WithRemoteTokenAndRemotePool_Success() public { + vm.startPrank(OWNER); + + bytes memory RANDOM_TOKEN_ADDRESS = abi.encode(makeAddr("RANDOM_TOKEN")); + bytes memory RANDOM_POOL_ADDRESS = abi.encode(makeAddr("RANDOM_POOL")); + + // Create an array of remote pools with some fake addresses + TokenPoolFactory.RemoteTokenPoolInfo[] memory remoteTokenPools = new TokenPoolFactory.RemoteTokenPoolInfo[](1); + + remoteTokenPools[0] = TokenPoolFactory.RemoteTokenPoolInfo( + DEST_CHAIN_SELECTOR, // remoteChainSelector + RANDOM_POOL_ADDRESS, // remotePoolAddress + type(BurnMintTokenPool).creationCode, // remotePoolInitCode + TokenPoolFactory.RemoteChainConfig(address(0), address(0), address(0)), // remoteChainConfig + TokenPoolFactory.PoolType.BURN_MINT, // poolType + RANDOM_TOKEN_ADDRESS, // remoteTokenAddress + "", // remoteTokenInitCode + RateLimiter.Config(false, 0, 0) // rateLimiterConfig + ); + + (address tokenAddress, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool( + remoteTokenPools, s_tokenInitCode, s_poolInitCode, FAKE_SALT, TokenPoolFactory.PoolType.BURN_MINT + ); + + assertNotEq(address(0), tokenAddress, "Token Address should not be 0"); + assertNotEq(address(0), poolAddress, "Pool Address should not be 0"); + + s_tokenAdminRegistry.acceptAdminRole(tokenAddress); + OwnerIsCreator(tokenAddress).acceptOwnership(); + OwnerIsCreator(poolAddress).acceptOwnership(); + + assertEq( + TokenPool(poolAddress).getRemoteToken(DEST_CHAIN_SELECTOR), + RANDOM_TOKEN_ADDRESS, + "Remote Token Address should have been set" + ); + + assertEq( + TokenPool(poolAddress).getRemotePool(DEST_CHAIN_SELECTOR), + RANDOM_POOL_ADDRESS, + "Remote Pool Address should have been set" + ); + + assertEq(poolAddress, s_tokenAdminRegistry.getPool(tokenAddress), "Token Pool should be set"); + + assertEq(IOwner(tokenAddress).owner(), OWNER, "Token should be owned by the owner"); + + assertEq(IOwner(poolAddress).owner(), OWNER, "Token should be owned by the owner"); + } + + function test_createTokenPoolLockRelease_NoExistingToken_predict_Success() public { + vm.startPrank(OWNER); + bytes32 dynamicSalt = keccak256(abi.encodePacked(FAKE_SALT, 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)); + + newTokenAdminRegistry.addRegistryModule(address(newRegistryModule)); + + TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = + TokenPoolFactory.RemoteChainConfig(address(newTokenPoolFactory), address(s_destRouter), address(s_rmnProxy)); + + // Create an array of remote pools where nothing exists yet, but we want to predict the address for + // the new pool and token on DEST_CHAIN_SELECTOR + TokenPoolFactory.RemoteTokenPoolInfo[] memory remoteTokenPools = new TokenPoolFactory.RemoteTokenPoolInfo[](1); + + // 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, // remoteChainSelector + "", // remotePoolAddress + type(LockReleaseTokenPool).creationCode, // remotePoolInitCode + remoteChainConfig, // remoteChainConfig + TokenPoolFactory.PoolType.LOCK_RELEASE, // poolType + "", // remoteTokenAddress + s_tokenInitCode, // remoteTokenInitCode + 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)); + + // 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, + type(LockReleaseTokenPool).creationCode, + FAKE_SALT, + TokenPoolFactory.PoolType.LOCK_RELEASE + ); + + // Ensure that the remote Token was set to the one we predicted + assertEq( + abi.encode(predictedTokenAddress), + TokenPool(poolAddress).getRemoteToken(DEST_CHAIN_SELECTOR), + "Token Address should have been predicted" + ); + + { + // 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, true, address(s_destRouter)); + + // Take the init code and concat the destination params to it, the initCode shouldn't change + bytes memory predictedPoolInitCode = + abi.encodePacked(type(LockReleaseTokenPool).creationCode, predictedPoolCreationParams); + + // Predict the address of the pool on the DESTINATION chain + 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( + abi.encode(predictedPoolAddress), + TokenPool(poolAddress).getRemotePool(DEST_CHAIN_SELECTOR), + "Pool Address should have been predicted" + ); + } + + // 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), // No existing remote pools + s_tokenInitCode, // Token Init Code + type(LockReleaseTokenPool).creationCode, // Pool Init Code + FAKE_SALT, // Salt + TokenPoolFactory.PoolType.LOCK_RELEASE // Pool Type + ); + + assertEq( + TokenPool(poolAddress).getRemotePool(DEST_CHAIN_SELECTOR), + abi.encode(newPoolAddress), + "New Pool Address should have been deployed correctly" + ); + + assertEq( + TokenPool(poolAddress).getRemoteToken(DEST_CHAIN_SELECTOR), + abi.encode(newTokenAddress), + "New Token Address should have been deployed correctly" + ); + } +} diff --git a/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory/FactoryBurnMintERC20.sol b/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory/FactoryBurnMintERC20.sol new file mode 100644 index 00000000000..c59be5775d4 --- /dev/null +++ b/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory/FactoryBurnMintERC20.sol @@ -0,0 +1,268 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import {IGetCCIPAdmin} from "../../../ccip/interfaces/IGetCCIPAdmin.sol"; +import {IOwnable} from "../../../shared/interfaces/IOwnable.sol"; +import {IBurnMintERC20} from "../../../shared/token/ERC20/IBurnMintERC20.sol"; + +import {OwnerIsCreator} from "../../../shared/access/OwnerIsCreator.sol"; + +import {ERC20} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/ERC20.sol"; + +import {IERC20} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol"; +import {ERC20Burnable} from + "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/extensions/ERC20Burnable.sol"; +import {IERC165} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/utils/introspection/IERC165.sol"; +import {EnumerableSet} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/utils/structs/EnumerableSet.sol"; + +/// @notice A basic ERC20 compatible token contract with burn and minting roles. +/// @dev The constructor has been modified to support the deployment pattern used by a factory contract. +/// @dev The total supply can be limited during deployment. +contract FactoryBurnMintERC20 is IBurnMintERC20, IGetCCIPAdmin, IERC165, ERC20Burnable, OwnerIsCreator { + using EnumerableSet for EnumerableSet.AddressSet; + + error SenderNotMinter(address sender); + error SenderNotBurner(address sender); + error MaxSupplyExceeded(uint256 supplyAfterMint); + + event MintAccessGranted(address indexed minter); + event BurnAccessGranted(address indexed burner); + event MintAccessRevoked(address indexed minter); + event BurnAccessRevoked(address indexed burner); + + event CCIPAdminTransferred(address indexed previousAdmin, address indexed newAdmin); + + /// @dev The number of decimals for the token + uint8 internal immutable i_decimals; + + /// @dev The maximum supply of the token, 0 if unlimited + uint256 internal immutable i_maxSupply; + + /// @dev the CCIPAdmin can be used to register with the CCIP token admin registry, but has no other special powers, + /// and can only be transferred by the owner. + address internal s_ccipAdmin; + + /// @dev the allowed minter addresses + EnumerableSet.AddressSet internal s_minters; + /// @dev the allowed burner addresses + EnumerableSet.AddressSet internal s_burners; + + constructor( + string memory name, + string memory symbol, + uint8 decimals_, + uint256 maxSupply_, + uint256 preMint_, + address newOwner_ + ) ERC20(name, symbol) { + i_decimals = decimals_; + i_maxSupply = maxSupply_; + + s_ccipAdmin = newOwner_; + + // Mint the initial supply to the new Owner, saving gas by not calling if the mint amount is zero + if (preMint_ != 0) _mint(newOwner_, preMint_); + + // Grant the deployer the minter and burner roles. This contract is expected to be deployed by a factory + // contract that will transfer ownership to the correct address after deployment, so granting minting and burning + // privileges here saves gas by not requiring two transactions. + grantMintRole(newOwner_); + grantBurnRole(newOwner_); + } + + function supportsInterface(bytes4 interfaceId) public pure virtual override returns (bool) { + return interfaceId == type(IERC20).interfaceId || interfaceId == type(IBurnMintERC20).interfaceId + || interfaceId == type(IERC165).interfaceId || interfaceId == type(IOwnable).interfaceId; + } + + // ================================================================ + // | ERC20 | + // ================================================================ + + /// @dev Returns the number of decimals used in its user representation. + function decimals() public view virtual override returns (uint8) { + return i_decimals; + } + + /// @dev Returns the max supply of the token, 0 if unlimited. + function maxSupply() public view virtual returns (uint256) { + return i_maxSupply; + } + + /// @dev Uses OZ ERC20 _transfer to disallow sending to address(0). + /// @dev Disallows sending to address(this) + function _transfer(address from, address to, uint256 amount) internal virtual override validAddress(to) { + super._transfer(from, to, amount); + } + + /// @dev Uses OZ ERC20 _approve to disallow approving for address(0). + /// @dev Disallows approving for address(this) + function _approve(address owner, address spender, uint256 amount) internal virtual override validAddress(spender) { + super._approve(owner, spender, amount); + } + + /// @dev Exists to be backwards compatible with the older naming convention. + /// @param spender the account being approved to spend on the users' behalf. + /// @param subtractedValue the amount being removed from the approval. + /// @return success Bool to return if the approval was successfully decreased. + function decreaseApproval(address spender, uint256 subtractedValue) external returns (bool success) { + return decreaseAllowance(spender, subtractedValue); + } + + /// @dev Exists to be backwards compatible with the older naming convention. + /// @param spender the account being approved to spend on the users' behalf. + /// @param addedValue the amount being added to the approval. + function increaseApproval(address spender, uint256 addedValue) external { + increaseAllowance(spender, addedValue); + } + + /// @notice Check if recipient is valid (not this contract address). + /// @param recipient the account we transfer/approve to. + /// @dev Reverts with an empty revert to be compatible with the existing link token when + /// the recipient is this contract address. + modifier validAddress(address recipient) virtual { + // solhint-disable-next-line reason-string, gas-custom-errors + if (recipient == address(this)) revert(); + _; + } + + // ================================================================ + // | Burning & minting | + // ================================================================ + + /// @inheritdoc ERC20Burnable + /// @dev Uses OZ ERC20 _burn to disallow burning from address(0). + /// @dev Decreases the total supply. + function burn(uint256 amount) public override(IBurnMintERC20, ERC20Burnable) onlyBurner { + super.burn(amount); + } + + /// @inheritdoc IBurnMintERC20 + /// @dev Alias for BurnFrom for compatibility with the older naming convention. + /// @dev Uses burnFrom for all validation & logic. + + function burn(address account, uint256 amount) public virtual override { + burnFrom(account, amount); + } + + /// @inheritdoc ERC20Burnable + /// @dev Uses OZ ERC20 _burn to disallow burning from address(0). + /// @dev Decreases the total supply. + function burnFrom(address account, uint256 amount) public override(IBurnMintERC20, ERC20Burnable) onlyBurner { + super.burnFrom(account, amount); + } + + /// @inheritdoc IBurnMintERC20 + /// @dev Uses OZ ERC20 _mint to disallow minting to address(0). + /// @dev Disallows minting to address(this) + /// @dev Increases the total supply. + function mint(address account, uint256 amount) external override onlyMinter validAddress(account) { + if (i_maxSupply != 0 && totalSupply() + amount > i_maxSupply) revert MaxSupplyExceeded(totalSupply() + amount); + + _mint(account, amount); + } + + // ================================================================ + // | Roles | + // ================================================================ + + /// @notice grants both mint and burn roles to `burnAndMinter`. + /// @dev calls public functions so this function does not require + /// access controls. This is handled in the inner functions. + function grantMintAndBurnRoles(address burnAndMinter) external { + grantMintRole(burnAndMinter); + grantBurnRole(burnAndMinter); + } + + /// @notice Grants mint role to the given address. + /// @dev only the owner can call this function. + function grantMintRole(address minter) public onlyOwner { + if (s_minters.add(minter)) { + emit MintAccessGranted(minter); + } + } + + /// @notice Grants burn role to the given address. + /// @dev only the owner can call this function. + /// @param burner the address to grant the burner role to + function grantBurnRole(address burner) public onlyOwner { + if (s_burners.add(burner)) { + emit BurnAccessGranted(burner); + } + } + + /// @notice Revokes mint role for the given address. + /// @dev only the owner can call this function. + /// @param minter the address to revoke the mint role from. + function revokeMintRole(address minter) public onlyOwner { + if (s_minters.remove(minter)) { + emit MintAccessRevoked(minter); + } + } + + /// @notice Revokes burn role from the given address. + /// @dev only the owner can call this function + /// @param burner the address to revoke the burner role from + function revokeBurnRole(address burner) public onlyOwner { + if (s_burners.remove(burner)) { + emit BurnAccessRevoked(burner); + } + } + + /// @notice Returns all permissioned minters + function getMinters() public view returns (address[] memory) { + return s_minters.values(); + } + + /// @notice Returns all permissioned burners + function getBurners() public view returns (address[] memory) { + return s_burners.values(); + } + + /// @notice Returns the current CCIPAdmin + function getCCIPAdmin() public view returns (address) { + return s_ccipAdmin; + } + + /// @notice Transfers the CCIPAdmin role to a new address + /// @dev only the owner can call this function, NOT the current ccipAdmin, and 1-step ownership transfer is used. + /// @param newAdmin The address to transfer the CCIPAdmin role to. Setting to address(0) is a valid way to revoke + /// the role + function setCCIPAdmin(address newAdmin) public onlyOwner { + address currentAdmin = s_ccipAdmin; + + s_ccipAdmin = newAdmin; + + emit CCIPAdminTransferred(currentAdmin, newAdmin); + } + + // ================================================================ + // | Access | + // ================================================================ + + /// @notice Checks whether a given address is a minter for this token. + /// @return true if the address is allowed to mint. + function isMinter(address minter) public view returns (bool) { + return s_minters.contains(minter); + } + + /// @notice Checks whether a given address is a burner for this token. + /// @return true if the address is allowed to burn. + function isBurner(address burner) public view returns (bool) { + return s_burners.contains(burner); + } + + /// @notice Checks whether the msg.sender is a permissioned minter for this token + /// @dev Reverts with a SenderNotMinter if the check fails + modifier onlyMinter() { + if (!isMinter(msg.sender)) revert SenderNotMinter(msg.sender); + _; + } + + /// @notice Checks whether the msg.sender is a permissioned burner for this token + /// @dev Reverts with a SenderNotBurner if the check fails + modifier onlyBurner() { + if (!isBurner(msg.sender)) revert SenderNotBurner(msg.sender); + _; + } +} diff --git a/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory/TokenPoolFactory.sol b/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory/TokenPoolFactory.sol new file mode 100644 index 00000000000..b545a601e2a --- /dev/null +++ b/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory/TokenPoolFactory.sol @@ -0,0 +1,292 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.24; + +import {IOwnable} from "../../../shared/interfaces/IOwnable.sol"; +import {ITypeAndVersion} from "../../../shared/interfaces/ITypeAndVersion.sol"; +import {IBurnMintERC20} from "../../../shared/token/ERC20/IBurnMintERC20.sol"; +import {ITokenAdminRegistry} from "../../interfaces/ITokenAdminRegistry.sol"; + +import {OwnerIsCreator} from "../../../shared/access/OwnerIsCreator.sol"; +import {RateLimiter} from "../../libraries/RateLimiter.sol"; +import {TokenPool} from "../../pools/TokenPool.sol"; +import {RegistryModuleOwnerCustom} from "../RegistryModuleOwnerCustom.sol"; + +import {Create2} from "../../../vendor/openzeppelin-solidity/v5.0.2/contracts/utils/Create2.sol"; + +/// @notice A contract for deploying new tokens and token pools, and configuring them with the token admin registry +/// @dev At the end of the transaction, the ownership transfer process will begin, but the user must accept the +/// ownership transfer in a separate transaction. +/// @dev The address prediction mechanism is only capable of deploying and predicting addresses for EVM based chains. +/// adding compatibility for other chains will require additional offchain computation. +contract TokenPoolFactory is OwnerIsCreator, ITypeAndVersion { + using Create2 for bytes32; + + event RemoteChainConfigUpdated(uint64 indexed remoteChainSelector, RemoteChainConfig remoteChainConfig); + + error InvalidZeroAddress(); + + /// @notice The type of pool to deploy. Types may be expanded in future versions + enum PoolType { + BURN_MINT, + LOCK_RELEASE + } + + struct RemoteTokenPoolInfo { + uint64 remoteChainSelector; // The CCIP specific selector for the remote chain + bytes remotePoolAddress; // The address of the remote pool to either deploy or use as is. If empty, address + // will be predicted + bytes remotePoolInitCode; // Remote pool creation code if it needs to be deployed, without constructor params + // appended to the end. + RemoteChainConfig remoteChainConfig; // The addresses of the remote RMNProxy, Router, and factory for determining + // the remote address + PoolType poolType; // The type of pool to deploy, either Burn/Mint or Lock/Release + bytes remoteTokenAddress; // EVM address for remote token. If empty, the address will be predicted + bytes remoteTokenInitCode; // The init code to be deployed on the remote chain and includes constructor params + RateLimiter.Config rateLimiterConfig; // Token Pool rate limit. Values will be applied on incoming an outgoing messages + } + + // solhint-disable-next-line gas-struct-packing + struct RemoteChainConfig { + address remotePoolFactory; // The factory contract on the remote chain which will make the deployment + address remoteRouter; // The router on the remote chain + address remoteRMNProxy; // The RMNProxy contract on the remote chain + } + + string public constant typeAndVersion = "TokenPoolFactory 1.7.0-dev"; + + ITokenAdminRegistry private immutable i_tokenAdminRegistry; + RegistryModuleOwnerCustom private immutable i_registryModuleOwnerCustom; + + address private immutable i_rmnProxy; + address private immutable i_ccipRouter; + + /// @notice Construct the TokenPoolFactory + /// @param tokenAdminRegistry The address of the token admin registry + /// @param tokenAdminModule The address of the token admin module which can register the token via ownership module + /// @param rmnProxy The address of the RMNProxy contract token pools will be deployed with + /// @param ccipRouter The address of the CCIPRouter contract token pools will be deployed with + constructor( + ITokenAdminRegistry tokenAdminRegistry, + RegistryModuleOwnerCustom tokenAdminModule, + address rmnProxy, + address ccipRouter + ) { + if ( + address(tokenAdminRegistry) == address(0) || address(tokenAdminModule) == address(0) || rmnProxy == address(0) + || ccipRouter == address(0) + ) revert InvalidZeroAddress(); + + i_tokenAdminRegistry = ITokenAdminRegistry(tokenAdminRegistry); + i_registryModuleOwnerCustom = RegistryModuleOwnerCustom(tokenAdminModule); + i_rmnProxy = rmnProxy; + i_ccipRouter = ccipRouter; + } + + // ================================================================ + // | Top-Level Deployment | + // ================================================================ + + /// @notice Deploys a token and token pool with the given token information and configures it with remote token pools + /// @dev The token and token pool are deployed in the same transaction, and the token pool is configured with the + /// remote token pools. The token pool is then set in the token admin registry. Ownership of the everything is transferred + /// to the msg.sender, but must be accepted in a separate transaction due to 2-step ownership transfer. + /// @param remoteTokenPools An array of remote token pools info to be used in the pool's applyChainUpdates function + /// or to be predicted if the pool has not been deployed yet on the remote chain + /// @param tokenInitCode The creation code for the token, which includes the constructor parameters already appended + /// @param tokenPoolInitCode The creation code for the token pool, without the constructor parameters appended + /// @param salt The salt to be used in the create2 deployment of the token and token pool to ensure a unique address + /// @param poolType The type of pool to deploy, either Burn/Mint or Lock/Release + /// @return token The address of the token that was deployed + /// @return pool The address of the token pool that was deployed + function deployTokenAndTokenPool( + RemoteTokenPoolInfo[] calldata remoteTokenPools, + bytes memory tokenInitCode, + bytes calldata tokenPoolInitCode, + bytes32 salt, + PoolType poolType + ) external returns (address, address) { + // 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 + address token = Create2.deploy(0, salt, tokenInitCode); + + // Deploy the token pool + address pool = _createTokenPool(token, remoteTokenPools, tokenPoolInitCode, salt, poolType); + + // Grant the mint and burn roles to the pool for the token + IBurnMintERC20(token).grantMintAndBurnRoles(pool); + + // Set the token pool for token in the token admin registry since this contract is the token and pool owner + _setTokenPoolInTokenAdminRegistry(token, pool); + + // Begin the 2 step ownership transfer of the newly deployed token to the msg.sender + IOwnable(token).transferOwnership(msg.sender); + + return (token, pool); + } + + /// @notice Deploys a token pool with an existing ERC20 token + /// @dev Since the token already exists, this contract is not the owner and therefore cannot configure the + /// token pool in the token admin registry in the same transaction. The user must invoke the calls to the + /// tokenAdminRegistry manually + /// @dev since the token already exists, the owner must grant the mint and burn roles to the pool manually + /// @param token The address of the existing token to be used in the token pool + /// @param remoteTokenPools An array of remote token pools info to be used in the pool's applyChainUpdates function + /// @param tokenPoolInitCode The creation code for the token pool + /// @param salt The salt to be used in the create2 deployment of the token pool + /// @return poolAddress The address of the token pool that was deployed + function deployTokenPoolWithExistingToken( + address token, + RemoteTokenPoolInfo[] calldata remoteTokenPools, + bytes calldata tokenPoolInitCode, + bytes32 salt, + PoolType poolType + ) external returns (address poolAddress) { + // 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 + return _createTokenPool(token, remoteTokenPools, tokenPoolInitCode, salt, poolType); + } + + // ================================================================ + // | Pool Deployment/Configuration | + // ================================================================ + + /// @notice Deploys a token pool with the given token information and remote token pools + /// @param token The token to be used in the token pool + /// @param remoteTokenPools An array of remote token pools info to be used in the pool's applyChainUpdates function + /// @param tokenPoolInitCode The creation code for the token pool + /// @param salt The salt to be used in the create2 deployment of the token pool + /// @return poolAddress The address of the token pool that was deployed + function _createTokenPool( + address token, + RemoteTokenPoolInfo[] calldata remoteTokenPools, + bytes calldata tokenPoolInitCode, + bytes32 salt, + PoolType poolType + ) private returns (address) { + // Create an array of chain updates to apply to the token pool + TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](remoteTokenPools.length); + + RemoteTokenPoolInfo memory remoteTokenPool; + for (uint256 i = 0; i < remoteTokenPools.length; ++i) { + remoteTokenPool = remoteTokenPools[i]; + + // If the user provides an empty byte string, indicated no token has already been deployed, + // then the address of the token needs to be predicted. Otherwise the address provided will be used. + if (remoteTokenPool.remoteTokenAddress.length == 0) { + // The user must provide the initCode for the remote token, so its address can be predicted correctly. It's + // provided in the remoteTokenInitCode field for the remoteTokenPool + remoteTokenPool.remoteTokenAddress = abi.encode( + salt.computeAddress( + keccak256(remoteTokenPool.remoteTokenInitCode), remoteTokenPool.remoteChainConfig.remotePoolFactory + ) + ); + } + + // If the user provides an empty byte string parameter, indicating the pool has not been deployed yet, + // the address of the pool should be predicted. Otherwise use the provided address. + if (remoteTokenPool.remotePoolAddress.length == 0) { + // Address is predicted based on the init code hash and the deployer, so the hash must first be computed + // using the initCode and a concatenated set of constructor parameters. + bytes32 remotePoolInitcodeHash = _generatePoolInitcodeHash( + remoteTokenPool.remotePoolInitCode, + remoteTokenPool.remoteChainConfig, + abi.decode(remoteTokenPool.remoteTokenAddress, (address)), + remoteTokenPool.poolType + ); + + // Abi encode the computed remote address so it can be used as bytes in the chain update + remoteTokenPool.remotePoolAddress = + abi.encode(salt.computeAddress(remotePoolInitcodeHash, remoteTokenPool.remoteChainConfig.remotePoolFactory)); + } + + chainUpdates[i] = TokenPool.ChainUpdate({ + remoteChainSelector: remoteTokenPool.remoteChainSelector, + allowed: true, + remotePoolAddress: remoteTokenPool.remotePoolAddress, + remoteTokenAddress: remoteTokenPool.remoteTokenAddress, + outboundRateLimiterConfig: remoteTokenPool.rateLimiterConfig, + inboundRateLimiterConfig: remoteTokenPool.rateLimiterConfig + }); + } + + // Construct the initArgs for the token pool using the immutable contracts for CCIP on the local chain + bytes memory tokenPoolInitArgs; + if (poolType == PoolType.BURN_MINT) { + tokenPoolInitArgs = abi.encode(token, new address[](0), i_rmnProxy, i_ccipRouter); + } else if (poolType == PoolType.LOCK_RELEASE) { + // Lock/Release pools have an additional boolean constructor parameter that must be accounted for, acceptLiquidity, + // which is set to true by default in this case. Users wishing to set it to false must deploy the pool manually. + tokenPoolInitArgs = abi.encode(token, new address[](0), i_rmnProxy, true, i_ccipRouter); + } + + // Construct the deployment code from the initCode and the initArgs and then deploy + address poolAddress = Create2.deploy(0, salt, abi.encodePacked(tokenPoolInitCode, tokenPoolInitArgs)); + + // Apply the chain updates to the token pool + TokenPool(poolAddress).applyChainUpdates(chainUpdates); + + // Begin the 2 step ownership transfer of the token pool to the msg.sender. + IOwnable(poolAddress).transferOwnership(address(msg.sender)); // 2 step ownership transfer + + return poolAddress; + } + + /// @notice Generates the hash of the init code the pool will be deployed with + /// @dev The init code hash is used with Create2 to predict the address of the pool on the remote chain + /// @dev ABI-encoding limitations prevent arbitrary constructor parameters from being used, so pool type must be + /// restricted to those with known types in the constructor. This function should be updated if new pool types are needed. + /// @param initCode The init code of the pool + /// @param remoteChainConfig The remote chain config for the pool + /// @param remoteTokenAddress The address of the remote token + /// @param poolType The type of pool to deploy + /// @return bytes32 hash of the init code to be used in the deterministic address calculation + function _generatePoolInitcodeHash( + bytes memory initCode, + RemoteChainConfig memory remoteChainConfig, + address remoteTokenAddress, + PoolType poolType + ) internal pure virtual returns (bytes32) { + if (poolType == PoolType.BURN_MINT) { + return keccak256( + abi.encodePacked( + initCode, + // constructor(address, address[], address, address) + abi.encode( + remoteTokenAddress, new address[](0), remoteChainConfig.remoteRMNProxy, remoteChainConfig.remoteRouter + ) + ) + ); + } else { + // if poolType is PoolType.LOCK_RELEASE, but may be expanded in future versions + return keccak256( + abi.encodePacked( + initCode, + // constructor(address, address[], address, bool, address) + abi.encode( + remoteTokenAddress, new address[](0), remoteChainConfig.remoteRMNProxy, true, remoteChainConfig.remoteRouter + ) + ) + ); + } + } + + /// @notice Sets the token pool address in the token admin registry for a newly deployed token pool. + /// @dev this function should only be called when the token is deployed by this contract as well, otherwise + /// the token pool will not be able to be set in the token admin registry, and this function will revert. + /// @param token The address of the token to set the pool for + /// @param pool The address of the pool to set in the token admin registry + function _setTokenPoolInTokenAdminRegistry(address token, address pool) private { + i_registryModuleOwnerCustom.registerAdminViaOwner(token); + i_tokenAdminRegistry.acceptAdminRole(token); + i_tokenAdminRegistry.setPool(token, pool); + + // Begin the 2 admin transfer process which must be accepted in a separate tx. + i_tokenAdminRegistry.transferAdminRole(token, msg.sender); + } +} diff --git a/contracts/src/v0.8/shared/test/token/ERC677/OpStackBurnMintERC677.t.sol b/contracts/src/v0.8/shared/test/token/ERC677/OpStackBurnMintERC677.t.sol index 614b3bea15b..b084ebd38a2 100644 --- a/contracts/src/v0.8/shared/test/token/ERC677/OpStackBurnMintERC677.t.sol +++ b/contracts/src/v0.8/shared/test/token/ERC677/OpStackBurnMintERC677.t.sol @@ -6,6 +6,7 @@ import {IOptimismMintableERC20Minimal, IOptimismMintableERC20} from "../../../to import {IERC677} from "../../../token/ERC677/IERC677.sol"; import {BurnMintERC677} from "../../../token/ERC677/BurnMintERC677.sol"; + import {BaseTest} from "../../BaseTest.t.sol"; import {OpStackBurnMintERC677} from "../../../token/ERC677/OpStackBurnMintERC677.sol"; diff --git a/contracts/src/v0.8/shared/token/ERC20/IBurnMintERC20.sol b/contracts/src/v0.8/shared/token/ERC20/IBurnMintERC20.sol index b9b3b54bf73..0ee844d34fe 100644 --- a/contracts/src/v0.8/shared/token/ERC20/IBurnMintERC20.sol +++ b/contracts/src/v0.8/shared/token/ERC20/IBurnMintERC20.sol @@ -26,4 +26,6 @@ interface IBurnMintERC20 is IERC20 { /// @param amount The number of tokens to be burned. /// @dev this function decreases the total supply. function burnFrom(address account, uint256 amount) external; + + function grantMintAndBurnRoles(address account) external; } diff --git a/contracts/src/v0.8/vendor/openzeppelin-solidity/v5.0.2/contracts/utils/Create2.sol b/contracts/src/v0.8/vendor/openzeppelin-solidity/v5.0.2/contracts/utils/Create2.sol new file mode 100644 index 00000000000..fe0a6edd564 --- /dev/null +++ b/contracts/src/v0.8/vendor/openzeppelin-solidity/v5.0.2/contracts/utils/Create2.sol @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: MIT +// OpenZeppelin Contracts (last updated v5.0.0) (utils/Create2.sol) + +pragma solidity ^0.8.20; + +import {Errors} from "./Errors.sol"; + +/** + * @dev Helper to make usage of the `CREATE2` EVM opcode easier and safer. + * `CREATE2` can be used to compute in advance the address where a smart + * contract will be deployed, which allows for interesting new mechanisms known + * as 'counterfactual interactions'. + * + * See the https://eips.ethereum.org/EIPS/eip-1014#motivation[EIP] for more + * information. + */ +library Create2 { + /** + * @dev There's no code to deploy. + */ + error Create2EmptyBytecode(); + + /** + * @dev Deploys a contract using `CREATE2`. The address where the contract + * will be deployed can be known in advance via {computeAddress}. + * + * The bytecode for a contract can be obtained from Solidity with + * `type(contractName).creationCode`. + * + * Requirements: + * + * - `bytecode` must not be empty. + * - `salt` must have not been used for `bytecode` already. + * - the factory must have a balance of at least `amount`. + * - if `amount` is non-zero, `bytecode` must have a `payable` constructor. + */ + function deploy(uint256 amount, bytes32 salt, bytes memory bytecode) internal returns (address addr) { + if (address(this).balance < amount) { + revert Errors.InsufficientBalance(address(this).balance, amount); + } + if (bytecode.length == 0) { + revert Create2EmptyBytecode(); + } + assembly ("memory-safe") { + addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt) + // if no address was created, and returndata is not empty, bubble revert + if and(iszero(addr), not(iszero(returndatasize()))) { + let p := mload(0x40) + returndatacopy(p, 0, returndatasize()) + revert(p, returndatasize()) + } + } + if (addr == address(0)) { + revert Errors.FailedDeployment(); + } + } + + /** + * @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the + * `bytecodeHash` or `salt` will result in a new destination address. + */ + function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) { + return computeAddress(salt, bytecodeHash, address(this)); + } + + /** + * @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at + * `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}. + */ + function computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) internal pure returns (address addr) { + assembly ("memory-safe") { + let ptr := mload(0x40) // Get free memory pointer + + // | | ↓ ptr ... ↓ ptr + 0x0B (start) ... ↓ ptr + 0x20 ... ↓ ptr + 0x40 ... | + // |-------------------|---------------------------------------------------------------------------| + // | bytecodeHash | CCCCCCCCCCCCC...CC | + // | salt | BBBBBBBBBBBBB...BB | + // | deployer | 000000...0000AAAAAAAAAAAAAAAAAAA...AA | + // | 0xFF | FF | + // |-------------------|---------------------------------------------------------------------------| + // | memory | 000000...00FFAAAAAAAAAAAAAAAAAAA...AABBBBBBBBBBBBB...BBCCCCCCCCCCCCC...CC | + // | keccak(start, 85) | ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ | + + mstore(add(ptr, 0x40), bytecodeHash) + mstore(add(ptr, 0x20), salt) + mstore(ptr, deployer) // Right-aligned with 12 preceding garbage bytes + let start := add(ptr, 0x0b) // The hashed data starts at the final garbage byte which we will set to 0xff + mstore8(start, 0xff) + addr := and(keccak256(start, 85), 0xffffffffffffffffffffffffffffffffffffffff) + } + } +} \ No newline at end of file diff --git a/contracts/src/v0.8/vendor/openzeppelin-solidity/v5.0.2/contracts/utils/Errors.sol b/contracts/src/v0.8/vendor/openzeppelin-solidity/v5.0.2/contracts/utils/Errors.sol new file mode 100644 index 00000000000..cb8833b46e1 --- /dev/null +++ b/contracts/src/v0.8/vendor/openzeppelin-solidity/v5.0.2/contracts/utils/Errors.sol @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.20; + +/** + * @dev Collection of common custom errors used in multiple contracts + * + * IMPORTANT: Backwards compatibility is not guaranteed in future versions of the library. + * It is recommended to avoid relying on the error API for critical functionality. + */ +library Errors { + /** + * @dev The ETH balance of the account is not enough to perform the operation. + */ + error InsufficientBalance(uint256 balance, uint256 needed); + + /** + * @dev A call to an address target failed. The target may have reverted. + */ + error FailedCall(); + + /** + * @dev The deployment failed. + */ + error FailedDeployment(); + + /** + * @dev A necessary precompile is missing. + */ + error MissingPrecompile(address); +} \ No newline at end of file