From 56f0942ed9181919f49feb07c89e4a8404496a9a Mon Sep 17 00:00:00 2001 From: byshape Date: Mon, 22 Jul 2024 18:51:32 +0100 Subject: [PATCH 1/5] Separate library for tests --- .gas-snapshot | 140 ++-- test/integration/EscrowFactory.t.sol | 90 +-- .../MerkleStorageInvalidator.t.sol | 409 +++++------ test/integration/ResolverMock.t.sol | 222 +++--- test/libraries/TimelocksLib.t.sol | 15 +- test/unit/Escrow.t.sol | 650 +++++++----------- test/unit/EscrowFactory.t.sol | 188 ++--- test/unit/MerkleStorageInvalidator.t.sol | 7 +- test/utils/BaseSetup.sol | 531 +++----------- test/utils/Utils.sol | 34 + test/utils/libraries/CrossChainLib.sol | 401 +++++++++++ test/utils/libraries/TimelocksSettersLib.sol | 2 +- test/utils/mocks/TimelocksLibMock.sol | 2 +- 13 files changed, 1224 insertions(+), 1467 deletions(-) create mode 100644 test/utils/Utils.sol create mode 100644 test/utils/libraries/CrossChainLib.sol diff --git a/.gas-snapshot b/.gas-snapshot index 3666caf..7fa3d6b 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,72 +1,72 @@ -EscrowFactoryTest:test_MultipleFillsInvalidKey() (gas: 477392) -EscrowFactoryTest:test_MultipleFillsInvalidSecretsAmount() (gas: 472722) -EscrowFactoryTest:test_NoDeploymentForNotResolver() (gas: 183965) -EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 119657) -EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForTaker() (gas: 22658) -EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 112896) -EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForTaker() (gas: 27394) -EscrowFactoryTest:test_NoUnsafeDeploymentForTaker() (gas: 30108) -EscrowTest:test_CancelDst() (gas: 111454) -EscrowTest:test_CancelDstDifferentTarget() (gas: 143281) -EscrowTest:test_CancelDstWithNativeToken() (gas: 93611) -EscrowTest:test_CancelPublicSrc() (gas: 156600) -EscrowTest:test_CancelResolverSrc() (gas: 159757) -EscrowTest:test_CancelResolverSrcReceiver() (gas: 170942) -EscrowTest:test_NoAnyoneCancelDuringResolverCancelSrc() (gas: 154958) -EscrowTest:test_NoCallsWithInvalidImmutables() (gas: 277046) -EscrowTest:test_NoCancelByAnyoneDst() (gas: 117101) -EscrowTest:test_NoCancelDuringWithdrawalDst() (gas: 116861) -EscrowTest:test_NoCancelDuringWithdrawalSrc() (gas: 155092) -EscrowTest:test_NoFailedNativeTokenTransferCancelSrc() (gas: 170449) -EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDst() (gas: 149884) -EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDstNative() (gas: 83354) -EscrowTest:test_NoFailedNativeTokenTransferWithdrawalSrc() (gas: 303443) -EscrowTest:test_NoPublicCancelDuringPrivateCancellationSrc() (gas: 154273) -EscrowTest:test_NoPublicWithdrawOutsideOfAllowedPeriodDst() (gas: 122285) -EscrowTest:test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() (gas: 160156) -EscrowTest:test_NoRescueFundsByAnyoneDst() (gas: 171804) -EscrowTest:test_NoRescueFundsByAnyoneSrc() (gas: 200175) -EscrowTest:test_NoRescueFundsEarlierDst() (gas: 171247) -EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 200092) -EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 151984) -EscrowTest:test_NoWithdrawalByNonResolverDst() (gas: 116801) -EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodDst() (gas: 121701) -EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 160717) -EscrowTest:test_NoWithdrawalWithWrongSecretDst() (gas: 118166) -EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 155589) -EscrowTest:test_PublicWithdrawSrc() (gas: 172891) -EscrowTest:test_RescueFundsDst() (gas: 153614) -EscrowTest:test_RescueFundsDstNative() (gas: 182171) -EscrowTest:test_RescueFundsSrc() (gas: 186570) -EscrowTest:test_RescueFundsSrcNative() (gas: 188836) -EscrowTest:test_WithdrawByAnyoneDst() (gas: 136666) -EscrowTest:test_WithdrawByResolverDst() (gas: 137751) -EscrowTest:test_WithdrawByResolverDstNative() (gas: 97799) -EscrowTest:test_WithdrawByResolverPublicDst() (gas: 137097) -EscrowTest:test_WithdrawSrc() (gas: 177670) -EscrowTest:test_WithdrawSrcTo() (gas: 182509) -IntegrationEscrowFactoryTest:test_DeployCloneForMakerNonWhitelistedResolverInt() (gas: 470984) -IntegrationEscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMakerInt() (gas: 338739) -IntegrationResolverMockTest:test_MockCancelDst() (gas: 152609) -IntegrationResolverMockTest:test_MockCancelSrc() (gas: 351046) -IntegrationResolverMockTest:test_MockDeployDst() (gas: 146974) -IntegrationResolverMockTest:test_MockDeploySrc() (gas: 361519) -IntegrationResolverMockTest:test_MockPublicCancelSrc() (gas: 360087) -IntegrationResolverMockTest:test_MockPublicWithdrawDst() (gas: 156924) -IntegrationResolverMockTest:test_MockRescueFundsDst() (gas: 156496) -IntegrationResolverMockTest:test_MockRescueFundsSrc() (gas: 379602) -IntegrationResolverMockTest:test_MockWithdrawDst() (gas: 178381) -IntegrationResolverMockTest:test_MockWithdrawToSrc() (gas: 352052) -MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllExtra() (gas: 921492) -MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllTwoFills() (gas: 920173) -MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirst() (gas: 704406) -MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 930326) -MerkleStorageInvalidatorIntTest:test_MultipleFillsFillLast() (gas: 704142) -MerkleStorageInvalidatorIntTest:test_MultipleFillsNoDeploymentWithoutValidation() (gas: 298851) -MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 776050) -MerkleStorageInvalidatorIntTest:test_MultipleFillsOddDivision() (gas: 437381) -MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 704285) -MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 918885) -TimelocksLibTest:test_NoTimelocksOverflow() (gas: 128740) +EscrowFactoryTest:test_MultipleFillsInvalidKey() (gas: 478531) +EscrowFactoryTest:test_MultipleFillsInvalidSecretsAmount() (gas: 473846) +EscrowFactoryTest:test_NoDeploymentForNotResolver() (gas: 190767) +EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 128468) +EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForTaker() (gas: 27118) +EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 121693) +EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForTaker() (gas: 27377) +EscrowFactoryTest:test_NoUnsafeDeploymentForTaker() (gas: 34474) +EscrowTest:test_CancelDst() (gas: 116028) +EscrowTest:test_CancelDstDifferentTarget() (gas: 143286) +EscrowTest:test_CancelDstWithNativeToken() (gas: 93622) +EscrowTest:test_CancelPublicSrc() (gas: 165442) +EscrowTest:test_CancelResolverSrc() (gas: 168586) +EscrowTest:test_CancelResolverSrcReceiver() (gas: 179340) +EscrowTest:test_NoAnyoneCancelDuringResolverCancelSrc() (gas: 163829) +EscrowTest:test_NoCallsWithInvalidImmutables() (gas: 286452) +EscrowTest:test_NoCancelByAnyoneDst() (gas: 121678) +EscrowTest:test_NoCancelDuringWithdrawalDst() (gas: 121438) +EscrowTest:test_NoCancelDuringWithdrawalSrc() (gas: 163938) +EscrowTest:test_NoFailedNativeTokenTransferCancelSrc() (gas: 179280) +EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDst() (gas: 154458) +EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDstNative() (gas: 83365) +EscrowTest:test_NoFailedNativeTokenTransferWithdrawalSrc() (gas: 312353) +EscrowTest:test_NoPublicCancelDuringPrivateCancellationSrc() (gas: 163124) +EscrowTest:test_NoPublicWithdrawOutsideOfAllowedPeriodDst() (gas: 126855) +EscrowTest:test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() (gas: 169069) +EscrowTest:test_NoRescueFundsByAnyoneDst() (gas: 176275) +EscrowTest:test_NoRescueFundsByAnyoneSrc() (gas: 209062) +EscrowTest:test_NoRescueFundsEarlierDst() (gas: 175685) +EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 208959) +EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 160820) +EscrowTest:test_NoWithdrawalByNonResolverDst() (gas: 121384) +EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodDst() (gas: 126271) +EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 169632) +EscrowTest:test_NoWithdrawalWithWrongSecretDst() (gas: 122749) +EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 164438) +EscrowTest:test_PublicWithdrawSrc() (gas: 181729) +EscrowTest:test_RescueFundsDst() (gas: 158179) +EscrowTest:test_RescueFundsDstNative() (gas: 186642) +EscrowTest:test_RescueFundsSrc() (gas: 195453) +EscrowTest:test_RescueFundsSrcNative() (gas: 197721) +EscrowTest:test_WithdrawByAnyoneDst() (gas: 141240) +EscrowTest:test_WithdrawByResolverDst() (gas: 142325) +EscrowTest:test_WithdrawByResolverDstNative() (gas: 97810) +EscrowTest:test_WithdrawByResolverPublicDst() (gas: 141671) +EscrowTest:test_WithdrawSrc() (gas: 186495) +EscrowTest:test_WithdrawSrcTo() (gas: 191343) +IntegrationEscrowFactoryTest:test_DeployCloneForMakerNonWhitelistedResolverInt() (gas: 473480) +IntegrationEscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMakerInt() (gas: 341110) +IntegrationResolverMockTest:test_MockCancelDst() (gas: 157108) +IntegrationResolverMockTest:test_MockCancelSrc() (gas: 354513) +IntegrationResolverMockTest:test_MockDeployDst() (gas: 151449) +IntegrationResolverMockTest:test_MockDeploySrc() (gas: 365558) +IntegrationResolverMockTest:test_MockPublicCancelSrc() (gas: 392497) +IntegrationResolverMockTest:test_MockPublicWithdrawDst() (gas: 164765) +IntegrationResolverMockTest:test_MockRescueFundsDst() (gas: 161014) +IntegrationResolverMockTest:test_MockRescueFundsSrc() (gas: 382547) +IntegrationResolverMockTest:test_MockWithdrawDst() (gas: 182848) +IntegrationResolverMockTest:test_MockWithdrawToSrc() (gas: 355389) +MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllExtra() (gas: 923881) +MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllTwoFills() (gas: 922554) +MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirst() (gas: 706697) +MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 932899) +MerkleStorageInvalidatorIntTest:test_MultipleFillsFillLast() (gas: 706419) +MerkleStorageInvalidatorIntTest:test_MultipleFillsNoDeploymentWithoutValidation() (gas: 301339) +MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 778527) +MerkleStorageInvalidatorIntTest:test_MultipleFillsOddDivision() (gas: 439187) +MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 706707) +MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 921368) +TimelocksLibTest:test_NoTimelocksOverflow() (gas: 133743) TimelocksLibTest:test_getStartTimestamps() (gas: 16180) TimelocksLibTest:test_setDeployedAt() (gas: 5731) \ No newline at end of file diff --git a/test/integration/EscrowFactory.t.sol b/test/integration/EscrowFactory.t.sol index 4a098b8..e440e2e 100644 --- a/test/integration/EscrowFactory.t.sol +++ b/test/integration/EscrowFactory.t.sol @@ -1,14 +1,15 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.23; -import { IEscrowFactory } from "../../contracts/interfaces/IEscrowFactory.sol"; -import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; +import { TakerTraits } from "limit-order-protocol/contracts/libraries/TakerTraitsLib.sol"; +import { Address } from "solidity-utils/contracts/libraries/AddressLib.sol"; -import { Address, AddressLib, BaseSetup, IOrderMixin, TakerTraits } from "../utils/BaseSetup.sol"; +import { IEscrowFactory } from "contracts/interfaces/IEscrowFactory.sol"; -contract IntegrationEscrowFactoryTest is BaseSetup { - using AddressLib for Address; +import { BaseSetup } from "../utils/BaseSetup.sol"; +import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; +contract IntegrationEscrowFactoryTest is BaseSetup { function setUp() public virtual override { BaseSetup.setUp(); } @@ -20,47 +21,40 @@ contract IntegrationEscrowFactoryTest is BaseSetup { uint256 srcSafetyDeposit = uint256(srcAmount) * 10 / 100; uint256 dstSafetyDeposit = uint256(dstAmount) * 10 / 100; - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - /* IBaseEscrow.Immutables memory immutables */ - ) = _prepareDataSrc( + CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( keccak256(abi.encode(secret)), srcAmount, dstAmount, srcSafetyDeposit, dstSafetyDeposit, - address(0), + address(0), // receiver false, // fakeOrder false // allowMultipleFills ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, // extension + address(swapData.srcClone), // target + swapData.extension, // extension "", // interaction 0 // threshold ); { - (bool success,) = address(srcClone).call{ value: uint64(srcAmount) * 10 / 100 }(""); + (bool success,) = address(swapData.srcClone).call{ value: uint64(srcAmount) * 10 / 100 }(""); assertEq(success, true); uint256 resolverCredit = feeBank.availableCredit(bob.addr); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, srcAmount, // amount @@ -71,39 +65,32 @@ contract IntegrationEscrowFactoryTest is BaseSetup { assertEq(feeBank.availableCredit(bob.addr), resolverCredit); } - assertEq(usdc.balanceOf(address(srcClone)), srcAmount); - assertEq(address(srcClone).balance, srcSafetyDeposit); + assertEq(usdc.balanceOf(address(swapData.srcClone)), srcAmount); + assertEq(address(swapData.srcClone).balance, srcSafetyDeposit); } function test_DeployCloneForMakerNonWhitelistedResolverInt() public { - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, false); - - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - immutables.taker = Address.wrap(uint160(charlie.addr)); - srcClone = IBaseEscrow(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.taker = Address.wrap(uint160(charlie.addr)); + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, // extension + srcClone, // target + swapData.extension, // extension "", // interaction 0 // threshold ); { - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); uint256 resolverCredit = feeBank.availableCredit(bob.addr); @@ -113,7 +100,7 @@ contract IntegrationEscrowFactoryTest is BaseSetup { inch.approve(address(feeBank), 1000 ether); feeBank.deposit(10 ether); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, MAKING_AMOUNT, // amount @@ -125,41 +112,34 @@ contract IntegrationEscrowFactoryTest is BaseSetup { assertLt(feeBank.availableCredit(charlie.addr), resolverCredit); } - assertEq(usdc.balanceOf(address(srcClone)), MAKING_AMOUNT); - assertEq(address(srcClone).balance, SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(srcClone), MAKING_AMOUNT); + assertEq(srcClone.balance, SRC_SAFETY_DEPOSIT); } function test_NoInsufficientBalanceDeploymentForMakerInt() public { - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - /* IBaseEscrow.Immutables memory immutables */ - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, false); - - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 address(0), // target - extension, // extension + swapData.extension, // extension "", // interaction 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(bob.addr); vm.expectRevert(IEscrowFactory.InsufficientEscrowBalance.selector); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, MAKING_AMOUNT, // amount diff --git a/test/integration/MerkleStorageInvalidator.t.sol b/test/integration/MerkleStorageInvalidator.t.sol index 6cc492e..a3f8ef6 100644 --- a/test/integration/MerkleStorageInvalidator.t.sol +++ b/test/integration/MerkleStorageInvalidator.t.sol @@ -1,13 +1,14 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.23; +import { TakerTraits } from "limit-order-protocol/contracts/libraries/TakerTraitsLib.sol"; import { Merkle } from "murky/src/Merkle.sol"; -import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; -import { IEscrowFactory } from "../../contracts/interfaces/IEscrowFactory.sol"; -import { IMerkleStorageInvalidator } from "../../contracts/interfaces/IMerkleStorageInvalidator.sol"; +import { IEscrowFactory } from "contracts/interfaces/IEscrowFactory.sol"; +import { IMerkleStorageInvalidator } from "contracts/interfaces/IMerkleStorageInvalidator.sol"; -import { BaseSetup, EscrowSrc, IOrderMixin, TakerTraits } from "../utils/BaseSetup.sol"; +import { BaseSetup } from "../utils/BaseSetup.sol"; +import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; contract MerkleStorageInvalidatorIntTest is BaseSetup { uint256 public constant PARTS_AMOUNT = 100; @@ -38,43 +39,36 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); uint256 resolverCredit = feeBank.availableCredit(bob.addr); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -83,7 +77,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { ); assertEq(feeBank.availableCredit(bob.addr), resolverCredit); - assertEq(usdc.balanceOf(address(srcClone)), makingAmount); + assertEq(usdc.balanceOf(srcClone), makingAmount); } function testFuzz_MultipleFillsOneFillPassAndFail(uint256 makingAmount, uint256 partsAmount, uint256 idx) public { @@ -109,36 +103,29 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(partsAmount << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecretsLocal[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecretsLocal[idx]; + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecretsLocal[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(bob.addr); @@ -146,7 +133,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { vm.expectRevert(IEscrowFactory.InvalidSecretIndex.selector); } limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -155,7 +142,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { ); if (!shouldFail) { - assertEq(usdc.balanceOf(address(srcClone)), makingAmount); + assertEq(usdc.balanceOf(srcClone), makingAmount); } } @@ -167,41 +154,34 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -209,7 +189,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { args ); - assertEq(usdc.balanceOf(address(srcClone)), makingAmount); + assertEq(usdc.balanceOf(srcClone), makingAmount); // ------------ 2nd fill ------------ // uint256 makingAmount2 = MAKING_AMOUNT * 2 / 3 - makingAmount; @@ -217,22 +197,22 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { proof = merkle.getProof(hashedPairs, idx); assert(merkle.verifyProof(root, proof, hashedPairs[idx])); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount2; - address srcClone2 = address(EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables))); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount2; + address srcClone2 = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (v, r, s) = vm.sign(alice.privateKey, orderHash); + (v, r, s) = vm.sign(alice.privateKey, swapData.orderHash); vs = bytes32((uint256(v - 27) << 255)) | s; interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = _buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 srcClone2, // target - extension, + swapData.extension, interaction, 0 // threshold ); @@ -242,7 +222,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount2, // amount @@ -256,41 +236,34 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { function test_MultipleFillsNoDeploymentWithoutValidation() public { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = 0; + swapData.immutables.hashlock = 0; uint256 makingAmount = MAKING_AMOUNT / PARTS_AMOUNT; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, // extension + srcClone, // target + swapData.extension, // extension "", // interaction 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(bob.addr); vm.expectRevert(IEscrowFactory.InvalidSecretIndex.selector); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -308,41 +281,34 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -350,25 +316,25 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { args ); - assertEq(usdc.balanceOf(address(srcClone)), makingAmount); + assertEq(usdc.balanceOf(srcClone), makingAmount); // ------------ 2nd fill ------------ // uint256 makingAmount2 = fraction / 2; - immutables.amount = makingAmount2; - address srcClone2 = address(EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables))); + swapData.immutables.amount = makingAmount2; + address srcClone2 = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (v, r, s) = vm.sign(alice.privateKey, orderHash); + (v, r, s) = vm.sign(alice.privateKey, swapData.orderHash); vs = bytes32((uint256(v - 27) << 255)) | s; interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = _buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 srcClone2, // target - extension, + swapData.extension, interaction, 0 // threshold ); @@ -379,7 +345,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { vm.prank(bob.addr); vm.expectRevert(IMerkleStorageInvalidator.InvalidIndex.selector); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount2, // amount @@ -396,43 +362,36 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); uint256 resolverCredit = feeBank.availableCredit(bob.addr); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -441,7 +400,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { ); assertEq(feeBank.availableCredit(bob.addr), resolverCredit); - assertEq(usdc.balanceOf(address(srcClone)), makingAmount); + assertEq(usdc.balanceOf(srcClone), makingAmount); } function test_MultipleFillsFillFirstTwoFills() public { @@ -452,43 +411,36 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); uint256 resolverCredit = feeBank.availableCredit(bob.addr); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -497,7 +449,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { ); assertEq(feeBank.availableCredit(bob.addr), resolverCredit); - assertEq(usdc.balanceOf(address(srcClone)), makingAmount); + assertEq(usdc.balanceOf(srcClone), makingAmount); // ------------ 2nd fill ------------ // idx = 1; @@ -505,22 +457,22 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { proof = merkle.getProof(hashedPairs, idx); assert(merkle.verifyProof(root, proof, hashedPairs[idx])); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount2; - address srcClone2 = address(EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables))); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount2; + address srcClone2 = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (v, r, s) = vm.sign(alice.privateKey, orderHash); + (v, r, s) = vm.sign(alice.privateKey, swapData.orderHash); vs = bytes32((uint256(v - 27) << 255)) | s; interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = _buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 srcClone2, // target - extension, + swapData.extension, interaction, 0 // threshold ); @@ -530,7 +482,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount2, // amount @@ -540,7 +492,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { assertEq(usdc.balanceOf(address(srcClone2)), makingAmount2); (uint256 storedIndex,) = IMerkleStorageInvalidator(escrowFactory).lastValidated( - keccak256(abi.encodePacked(orderHash, uint240(uint256(root)))) + keccak256(abi.encodePacked(swapData.orderHash, uint240(uint256(root)))) ); assertEq(storedIndex, idx + 1); } @@ -552,42 +504,35 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); uint256 resolverCredit = feeBank.availableCredit(bob.addr); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, MAKING_AMOUNT, // amount @@ -596,7 +541,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { ); assertEq(feeBank.availableCredit(bob.addr), resolverCredit); - assertEq(usdc.balanceOf(address(srcClone)), MAKING_AMOUNT); + assertEq(usdc.balanceOf(srcClone), MAKING_AMOUNT); } function test_MultipleFillsFillAllTwoFills() public { @@ -607,42 +552,35 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -650,7 +588,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { args ); - assertEq(usdc.balanceOf(address(srcClone)), makingAmount); + assertEq(usdc.balanceOf(srcClone), makingAmount); // ------------ 2nd fill ------------ // idx = PARTS_AMOUNT - 1; @@ -658,22 +596,22 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { proof = merkle.getProof(hashedPairs, idx); assert(merkle.verifyProof(root, proof, hashedPairs[idx])); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount2; - address srcClone2 = address(EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables))); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount2; + address srcClone2 = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (v, r, s) = vm.sign(alice.privateKey, orderHash); + (v, r, s) = vm.sign(alice.privateKey, swapData.orderHash); vs = bytes32((uint256(v - 27) << 255)) | s; interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = _buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 srcClone2, // target - extension, + swapData.extension, interaction, 0 // threshold ); @@ -683,7 +621,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount2, // amount @@ -693,7 +631,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { assertEq(usdc.balanceOf(address(srcClone2)), makingAmount2); (uint256 storedIndex,) = IMerkleStorageInvalidator(escrowFactory).lastValidated( - keccak256(abi.encodePacked(orderHash, uint240(uint256(root)))) + keccak256(abi.encodePacked(swapData.orderHash, uint240(uint256(root)))) ); assertEq(storedIndex, PARTS_AMOUNT); } @@ -707,42 +645,35 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount, // amount @@ -750,9 +681,9 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { args ); - assertEq(usdc.balanceOf(address(srcClone)), makingAmount); + assertEq(usdc.balanceOf(srcClone), makingAmount); (uint256 storedIndex,) = IMerkleStorageInvalidator(escrowFactory).lastValidated( - keccak256(abi.encodePacked(orderHash, uint240(uint256(root)))) + keccak256(abi.encodePacked(swapData.orderHash, uint240(uint256(root)))) ); assertEq(storedIndex, PARTS_AMOUNT); @@ -761,22 +692,22 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { proof = merkle.getProof(hashedPairs, idx); assert(merkle.verifyProof(root, proof, hashedPairs[idx])); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount2; - address srcClone2 = address(EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables))); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount2; + address srcClone2 = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (v, r, s) = vm.sign(alice.privateKey, orderHash); + (v, r, s) = vm.sign(alice.privateKey, swapData.orderHash); vs = bytes32((uint256(v - 27) << 255)) | s; interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = _buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 srcClone2, // target - extension, + swapData.extension, interaction, 0 // threshold ); @@ -786,7 +717,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmount2, // amount @@ -796,7 +727,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { assertEq(usdc.balanceOf(address(srcClone2)), makingAmount2); (storedIndex,) = IMerkleStorageInvalidator(escrowFactory).lastValidated( - keccak256(abi.encodePacked(orderHash, uint240(uint256(root)))) + keccak256(abi.encodePacked(swapData.orderHash, uint240(uint256(root)))) ); assertEq(storedIndex, SECRETS_AMOUNT); } @@ -818,43 +749,45 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(secretsAmount << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, makingAmount, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( + rootPlusAmount, + makingAmount, + TAKING_AMOUNT, + SRC_SAFETY_DEPOSIT, + DST_SAFETY_DEPOSIT, + address(0), + false, + true + ); - immutables.hashlock = hashedS[idx]; - immutables.amount = makingAmountToFill; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedS[idx]; + swapData.immutables.amount = makingAmountToFill; + address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedS[idx])); - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, + srcClone, // target + swapData.extension, interaction, 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = srcClone.call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); uint256 resolverCredit = feeBank.availableCredit(bob.addr); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, makingAmountToFill, // amount @@ -863,7 +796,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { ); assertEq(feeBank.availableCredit(bob.addr), resolverCredit); - assertEq(usdc.balanceOf(address(srcClone)), makingAmountToFill); + assertEq(usdc.balanceOf(srcClone), makingAmountToFill); } /* solhint-enable func-name-mixedcase */ diff --git a/test/integration/ResolverMock.t.sol b/test/integration/ResolverMock.t.sol index 79c41dd..d965329 100644 --- a/test/integration/ResolverMock.t.sol +++ b/test/integration/ResolverMock.t.sol @@ -2,12 +2,14 @@ pragma solidity 0.8.23; -import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; -import { IEscrowDst } from "../../contracts/interfaces/IEscrowDst.sol"; -import { IEscrowSrc } from "../../contracts/interfaces/IEscrowSrc.sol"; -import { Timelocks } from "../../contracts/libraries/TimelocksLib.sol"; -import { IResolverMock, ResolverMock } from "../../contracts/mocks/ResolverMock.sol"; -import { BaseSetup, IOrderMixin, TakerTraits } from "../utils/BaseSetup.sol"; +import { TakerTraits } from "limit-order-protocol/contracts/libraries/TakerTraitsLib.sol"; + +import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; +import { IEscrowDst } from "contracts/interfaces/IEscrowDst.sol"; +import { Timelocks } from "contracts/libraries/TimelocksLib.sol"; +import { IResolverMock, ResolverMock } from "contracts/mocks/ResolverMock.sol"; +import { BaseSetup } from "../utils/BaseSetup.sol"; +import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; contract IntegrationResolverMockTest is BaseSetup { /* solhint-disable-next-line private-vars-leading-underscore */ @@ -16,6 +18,7 @@ contract IntegrationResolverMockTest is BaseSetup { function setUp() public virtual override { BaseSetup.setUp(); resolverMock = address(new ResolverMock(escrowFactory, limitOrderProtocol, address(this))); + resolvers[0] = address(resolverMock); vm.label(resolverMock, "resolverMock"); vm.deal(resolverMock, 100 ether); dai.mint(resolverMock, 1000 ether); @@ -30,44 +33,34 @@ contract IntegrationResolverMockTest is BaseSetup { function test_MockDeploySrc() public { vm.warp(1710288000); // set current timestamp - _setTimelocks(); + (timelocks, timelocksDst) = CrossChainLib.setTimelocks(srcTimelocks, dstTimelocks); - address[] memory resolvers = new address[](1); resolvers[0] = resolverMock; - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrcCustomResolver( - HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, false, resolvers - ); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 address(0), // target - extension, // extension + swapData.extension, "", // interaction 0 // threshold ); - immutables.timelocks = Timelocks.wrap(Timelocks.unwrap(immutables.timelocks) & ~(uint256(type(uint32).max))); + swapData.immutables.timelocks = Timelocks.wrap(Timelocks.unwrap(swapData.immutables.timelocks) & ~(uint256(type(uint32).max))); - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); IResolverMock(resolverMock).deploySrc( - immutables, - order, + swapData.immutables, + swapData.order, r, vs, MAKING_AMOUNT, @@ -75,42 +68,32 @@ contract IntegrationResolverMockTest is BaseSetup { args ); - assertEq(usdc.balanceOf(address(srcClone)), MAKING_AMOUNT); - assertEq(address(srcClone).balance, SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), MAKING_AMOUNT); + assertEq(address(swapData.srcClone).balance, SRC_SAFETY_DEPOSIT); } function test_MockWithdrawToSrc() public { - address[] memory resolvers = new address[](1); resolvers[0] = resolverMock; - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrcCustomResolver( - HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, false, resolvers - ); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 address(0), // target - extension, // extension + swapData.extension, // swapData.extension "", // interaction 0 // threshold ); // deploy escrow IResolverMock(resolverMock).deploySrc( - immutables, - order, + swapData.immutables, + swapData.order, r, vs, MAKING_AMOUNT, @@ -121,55 +104,45 @@ contract IntegrationResolverMockTest is BaseSetup { uint256 aliceBalance = usdc.balanceOf(alice.addr); uint256 resolverBalanceNative = resolverMock.balance; - assertEq(usdc.balanceOf(address(srcClone)), MAKING_AMOUNT); - assertEq(address(srcClone).balance, SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), MAKING_AMOUNT); + assertEq(address(swapData.srcClone).balance, SRC_SAFETY_DEPOSIT); address[] memory targets = new address[](1); bytes[] memory arguments = new bytes[](1); - targets[0] = address(srcClone); - arguments[0] = abi.encodePacked(srcClone.withdrawTo.selector, abi.encode(SECRET, alice.addr, immutables)); + targets[0] = address(swapData.srcClone); + arguments[0] = abi.encodePacked(swapData.srcClone.withdrawTo.selector, abi.encode(SECRET, alice.addr, swapData.immutables)); skip(srcTimelocks.withdrawal + 10); IResolverMock(resolverMock).arbitraryCalls(targets, arguments); assertEq(usdc.balanceOf(alice.addr), aliceBalance + MAKING_AMOUNT); assertEq(resolverMock.balance, resolverBalanceNative + SRC_SAFETY_DEPOSIT); - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); } function test_MockCancelSrc() public { - address[] memory resolvers = new address[](1); resolvers[0] = resolverMock; - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrcCustomResolver( - HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, false, resolvers - ); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 address(0), // target - extension, // extension + swapData.extension, // swapData.extension "", // interaction 0 // threshold ); // deploy escrow IResolverMock(resolverMock).deploySrc( - immutables, - order, + swapData.immutables, + swapData.order, r, vs, MAKING_AMOUNT, @@ -180,13 +153,13 @@ contract IntegrationResolverMockTest is BaseSetup { uint256 aliceBalance = usdc.balanceOf(alice.addr); uint256 resolverBalanceNative = resolverMock.balance; - assertEq(usdc.balanceOf(address(srcClone)), MAKING_AMOUNT); - assertEq(address(srcClone).balance, SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), MAKING_AMOUNT); + assertEq(address(swapData.srcClone).balance, SRC_SAFETY_DEPOSIT); address[] memory targets = new address[](1); bytes[] memory arguments = new bytes[](1); - targets[0] = address(srcClone); - arguments[0] = abi.encodePacked(srcClone.cancel.selector, abi.encode(immutables)); + targets[0] = address(swapData.srcClone); + arguments[0] = abi.encodePacked(swapData.srcClone.cancel.selector, abi.encode(swapData.immutables)); skip(srcTimelocks.cancellation + 10); // Cancel escrow @@ -194,45 +167,36 @@ contract IntegrationResolverMockTest is BaseSetup { assertEq(usdc.balanceOf(alice.addr), aliceBalance + MAKING_AMOUNT); assertEq(resolverMock.balance, resolverBalanceNative + SRC_SAFETY_DEPOSIT); - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); } function test_MockPublicCancelSrc() public { - address[] memory resolvers = new address[](2); + resolvers = new address[](2); resolvers[0] = bob.addr; resolvers[1] = resolverMock; - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrcCustomResolver( - HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, false, resolvers - ); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 - address(srcClone), // target - extension, // extension + address(swapData.srcClone), // target + swapData.extension, // swapData.extension "", // interaction 0 // threshold ); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(bob.addr); limitOrderProtocol.fillOrderArgs( - order, + swapData.order, r, vs, MAKING_AMOUNT, // amount @@ -243,13 +207,13 @@ contract IntegrationResolverMockTest is BaseSetup { uint256 aliceBalance = usdc.balanceOf(alice.addr); uint256 resolverBalanceNative = resolverMock.balance; - assertEq(usdc.balanceOf(address(srcClone)), MAKING_AMOUNT); - assertEq(address(srcClone).balance, SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), MAKING_AMOUNT); + assertEq(address(swapData.srcClone).balance, SRC_SAFETY_DEPOSIT); address[] memory targets = new address[](1); bytes[] memory arguments = new bytes[](1); - targets[0] = address(srcClone); - arguments[0] = abi.encodePacked(srcClone.cancel.selector, abi.encode(immutables)); + targets[0] = address(swapData.srcClone); + arguments[0] = abi.encodePacked(swapData.srcClone.cancel.selector, abi.encode(swapData.immutables)); vm.warp(block.timestamp + srcTimelocks.cancellation + 10); // Resolver is bob, so unable to cancel escrow @@ -257,48 +221,37 @@ contract IntegrationResolverMockTest is BaseSetup { IResolverMock(resolverMock).arbitraryCalls(targets, arguments); vm.warp(block.timestamp + srcTimelocks.publicCancellation + 10); - arguments[0] = abi.encodePacked(srcClone.publicCancel.selector, abi.encode(immutables)); + arguments[0] = abi.encodePacked(swapData.srcClone.publicCancel.selector, abi.encode(swapData.immutables)); // Now resolver mock is able to cancel escrow IResolverMock(resolverMock).arbitraryCalls(targets, arguments); assertEq(usdc.balanceOf(alice.addr), aliceBalance + MAKING_AMOUNT); assertEq(resolverMock.balance, resolverBalanceNative + SRC_SAFETY_DEPOSIT); - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); } function test_MockRescueFundsSrc() public { - address[] memory resolvers = new address[](1); - resolvers[0] = resolverMock; - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - /* bytes memory extraData */, - bytes memory extension, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrcCustomResolver( - HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, false, resolvers - ); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, orderHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = _buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit false, // usePermit2 address(0), // target - extension, // extension + swapData.extension, // swapData.extension "", // interaction 0 // threshold ); // deploy escrow IResolverMock(resolverMock).deploySrc( - immutables, - order, + swapData.immutables, + swapData.order, r, vs, MAKING_AMOUNT, @@ -309,15 +262,21 @@ contract IntegrationResolverMockTest is BaseSetup { uint256 resolverBalance = usdc.balanceOf(resolverMock); uint256 resolverBalanceNative = resolverMock.balance; - assertEq(usdc.balanceOf(address(srcClone)), MAKING_AMOUNT); - assertEq(address(srcClone).balance, SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), MAKING_AMOUNT); + assertEq(address(swapData.srcClone).balance, SRC_SAFETY_DEPOSIT); address[] memory targets = new address[](2); bytes[] memory arguments = new bytes[](2); - targets[0] = address(srcClone); - targets[1] = address(srcClone); - arguments[0] = abi.encodePacked(srcClone.rescueFunds.selector, abi.encode(address(usdc), MAKING_AMOUNT, immutables)); - arguments[1] = abi.encodePacked(srcClone.rescueFunds.selector, abi.encode(address(0), SRC_SAFETY_DEPOSIT, immutables)); + targets[0] = address(swapData.srcClone); + targets[1] = address(swapData.srcClone); + arguments[0] = abi.encodePacked( + swapData.srcClone.rescueFunds.selector, + abi.encode(address(usdc), MAKING_AMOUNT, swapData.immutables) + ); + arguments[1] = abi.encodePacked( + swapData.srcClone.rescueFunds.selector, + abi.encode(address(0), SRC_SAFETY_DEPOSIT, swapData.immutables) + ); skip(RESCUE_DELAY + 10); // Rescue USDC and native tokens @@ -325,17 +284,15 @@ contract IntegrationResolverMockTest is BaseSetup { assertEq(usdc.balanceOf(resolverMock), resolverBalance + MAKING_AMOUNT); assertEq(resolverMock.balance, resolverBalanceNative + SRC_SAFETY_DEPOSIT); - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); } function test_MockDeployDst() public { (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone - ) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, resolverMock, address(dai) - ); + ) = _prepareDataDst(); address[] memory targets = new address[](1); bytes[] memory arguments = new bytes[](1); @@ -357,9 +314,7 @@ contract IntegrationResolverMockTest is BaseSetup { (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone - ) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, resolverMock, address(dai) - ); + ) = _prepareDataDst(); address[] memory targets = new address[](1); bytes[] memory arguments = new bytes[](1); @@ -394,12 +349,11 @@ contract IntegrationResolverMockTest is BaseSetup { } function test_MockPublicWithdrawDst() public { + resolvers[0] = bob.addr; (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IEscrowDst dstClone - ) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + ) = _prepareDataDst(); vm.prank(bob.addr); escrowFactory.createDstEscrow{ value: DST_SAFETY_DEPOSIT }(immutables, srcCancellationTimestamp); @@ -435,9 +389,7 @@ contract IntegrationResolverMockTest is BaseSetup { (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone - ) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, resolverMock, address(dai) - ); + ) = _prepareDataDst(); address[] memory targets = new address[](1); bytes[] memory arguments = new bytes[](1); @@ -475,9 +427,7 @@ contract IntegrationResolverMockTest is BaseSetup { (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone - ) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, resolverMock, address(dai) - ); + ) = _prepareDataDst(); address[] memory targets = new address[](1); bytes[] memory arguments = new bytes[](1); diff --git a/test/libraries/TimelocksLib.t.sol b/test/libraries/TimelocksLib.t.sol index 1a23af5..73ec9c9 100644 --- a/test/libraries/TimelocksLib.t.sol +++ b/test/libraries/TimelocksLib.t.sol @@ -3,13 +3,14 @@ pragma solidity 0.8.23; import { stdError } from "forge-std/StdError.sol"; -import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; -import { IEscrowDst } from "../../contracts/interfaces/IEscrowDst.sol"; +import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; +import { IEscrowDst } from "contracts/interfaces/IEscrowDst.sol"; -import { Timelocks } from "../../contracts/libraries/TimelocksLib.sol"; +import { Timelocks } from "contracts/libraries/TimelocksLib.sol"; import { TimelocksSettersLib } from "../utils/libraries/TimelocksSettersLib.sol"; import { BaseSetup } from "../utils/BaseSetup.sol"; +import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; import { TimelocksLibMock } from "../utils/mocks/TimelocksLibMock.sol"; contract TimelocksLibTest is BaseSetup { @@ -52,12 +53,10 @@ contract TimelocksLibTest is BaseSetup { function test_NoTimelocksOverflow() public { vm.warp(1710159521); // make it real, it's 0 in foundry - dstTimelocks = DstTimelocks({ withdrawal: 2584807817, publicWithdrawal: 2584807817, cancellation: 1 }); - _setTimelocks(); + dstTimelocks = CrossChainLib.DstTimelocks({ withdrawal: 2584807817, publicWithdrawal: 2584807817, cancellation: 1 }); + (timelocks, timelocksDst) = CrossChainLib.setTimelocks(srcTimelocks, dstTimelocks); - (IBaseEscrow.Immutables memory immutablesDst, uint256 srcCancellationTimestamp, IEscrowDst dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutablesDst, uint256 srcCancellationTimestamp, IEscrowDst dstClone) = _prepareDataDst(); // deploy escrow vm.prank(bob.addr); diff --git a/test/unit/Escrow.t.sol b/test/unit/Escrow.t.sol index 6754028..05c8b59 100644 --- a/test/unit/Escrow.t.sol +++ b/test/unit/Escrow.t.sol @@ -3,13 +3,14 @@ pragma solidity 0.8.23; import { Address } from "solidity-utils/contracts/libraries/AddressLib.sol"; -import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; -import { IEscrowFactory } from "../../contracts/interfaces/IEscrowFactory.sol"; -import { IEscrowSrc } from "../../contracts/interfaces/IEscrowSrc.sol"; -import { IEscrowDst } from "../../contracts/interfaces/IEscrowDst.sol"; -import { NoReceiveCaller } from "../../contracts/mocks/NoReceiveCaller.sol"; +import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; +import { IEscrowFactory } from "contracts/interfaces/IEscrowFactory.sol"; +import { IEscrowSrc } from "contracts/interfaces/IEscrowSrc.sol"; +import { IEscrowDst } from "contracts/interfaces/IEscrowDst.sol"; +import { NoReceiveCaller } from "contracts/mocks/NoReceiveCaller.sol"; -import { BaseSetup, IOrderMixin } from "../utils/BaseSetup.sol"; +import { BaseSetup } from "../utils/BaseSetup.sol"; +import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; contract EscrowTest is BaseSetup { // solhint-disable-next-line private-vars-leading-underscore @@ -23,79 +24,63 @@ contract EscrowTest is BaseSetup { // Only resolver can withdraw function test_NoWithdrawalByAnyoneSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // withdraw vm.expectRevert(IBaseEscrow.InvalidCaller.selector); - srcClone.withdraw(SECRET, immutables); + swapData.srcClone.withdraw(SECRET, swapData.immutables); } function test_NoWithdrawalOutsideOfAllowedPeriodSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // withdraw during finality lock vm.prank(bob.addr); vm.expectRevert(IBaseEscrow.InvalidTime.selector); - srcClone.withdraw(SECRET, immutables); + swapData.srcClone.withdraw(SECRET, swapData.immutables); // withdraw during the cancellation period vm.warp(block.timestamp + srcTimelocks.cancellation + 100); vm.prank(bob.addr); vm.expectRevert(IBaseEscrow.InvalidTime.selector); - srcClone.withdraw(SECRET, immutables); + swapData.srcClone.withdraw(SECRET, swapData.immutables); } function test_NoWithdrawalOutsideOfAllowedPeriodDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.startPrank(bob.addr); @@ -113,72 +98,58 @@ contract EscrowTest is BaseSetup { function test_WithdrawSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceBob = usdc.balanceOf(bob.addr); uint256 balanceBobNative = bob.addr.balance; - uint256 balanceEscrow = usdc.balanceOf(address(srcClone)); + uint256 balanceEscrow = usdc.balanceOf(address(swapData.srcClone)); // withdraw vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.prank(bob.addr); vm.expectEmit(); emit IBaseEscrow.Withdrawal(SECRET); - srcClone.withdraw(SECRET, immutables); + swapData.srcClone.withdraw(SECRET, swapData.immutables); assertEq(bob.addr.balance, balanceBobNative + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), balanceEscrow - (MAKING_AMOUNT)); + assertEq(usdc.balanceOf(address(swapData.srcClone)), balanceEscrow - (MAKING_AMOUNT)); } function test_WithdrawSrcTo() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); address target = charlie.addr; @@ -186,126 +157,105 @@ contract EscrowTest is BaseSetup { uint256 balanceBob = usdc.balanceOf(bob.addr); uint256 balanceTarget = usdc.balanceOf(target); uint256 balanceBobNative = bob.addr.balance; - uint256 balanceEscrow = usdc.balanceOf(address(srcClone)); + uint256 balanceEscrow = usdc.balanceOf(address(swapData.srcClone)); // withdraw vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.prank(bob.addr); - srcClone.withdrawTo(SECRET, target, immutables); + swapData.srcClone.withdrawTo(SECRET, target, swapData.immutables); assertEq(bob.addr.balance, balanceBobNative + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(bob.addr), balanceBob); assertEq(usdc.balanceOf(target), balanceTarget + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), balanceEscrow - (MAKING_AMOUNT)); + assertEq(usdc.balanceOf(address(swapData.srcClone)), balanceEscrow - (MAKING_AMOUNT)); } function test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // withdraw during the private withdrawal vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.expectRevert(IBaseEscrow.InvalidTime.selector); - srcClone.publicWithdraw(SECRET, immutables); + swapData.srcClone.publicWithdraw(SECRET, swapData.immutables); //withdraw during the cancellation period vm.warp(block.timestamp + srcTimelocks.cancellation + 100); vm.expectRevert(IBaseEscrow.InvalidTime.selector); - srcClone.publicWithdraw(SECRET, immutables); + swapData.srcClone.publicWithdraw(SECRET, swapData.immutables); } function test_PublicWithdrawSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceBob = usdc.balanceOf(bob.addr); uint256 balanceThisNative = address(this).balance; - uint256 balanceEscrow = usdc.balanceOf(address(srcClone)); + uint256 balanceEscrow = usdc.balanceOf(address(swapData.srcClone)); // withdraw vm.warp(block.timestamp + srcTimelocks.publicWithdrawal + 100); - srcClone.publicWithdraw(SECRET, immutables); + swapData.srcClone.publicWithdraw(SECRET, swapData.immutables); assertEq(address(this).balance, balanceThisNative + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), balanceEscrow - (MAKING_AMOUNT)); + assertEq(usdc.balanceOf(address(swapData.srcClone)), balanceEscrow - (MAKING_AMOUNT)); } function test_RescueFundsSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT + SRC_SAFETY_DEPOSIT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT + SRC_SAFETY_DEPOSIT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceBob = usdc.balanceOf(bob.addr); @@ -314,49 +264,42 @@ contract EscrowTest is BaseSetup { // withdraw vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.startPrank(bob.addr); - srcClone.withdraw(SECRET, immutables); + swapData.srcClone.withdraw(SECRET, swapData.immutables); assertEq(bob.addr.balance, balanceBobNative + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), SRC_SAFETY_DEPOSIT); // rescue vm.warp(block.timestamp + RESCUE_DELAY); vm.expectEmit(); emit IBaseEscrow.FundsRescued(address(usdc), SRC_SAFETY_DEPOSIT); - srcClone.rescueFunds(address(usdc), SRC_SAFETY_DEPOSIT, immutables); + swapData.srcClone.rescueFunds(address(usdc), SRC_SAFETY_DEPOSIT, swapData.immutables); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT + SRC_SAFETY_DEPOSIT); - assertEq(usdc.balanceOf(address(srcClone)), 0); + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); } function test_RescueFundsSrcNative() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT + MAKING_AMOUNT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT + MAKING_AMOUNT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceBob = usdc.balanceOf(bob.addr); @@ -365,50 +308,43 @@ contract EscrowTest is BaseSetup { // withdraw vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.startPrank(bob.addr); - srcClone.withdraw(SECRET, immutables); + swapData.srcClone.withdraw(SECRET, swapData.immutables); assertEq(bob.addr.balance, balanceBobNative + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, MAKING_AMOUNT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, MAKING_AMOUNT); // rescue vm.warp(block.timestamp + RESCUE_DELAY); vm.expectEmit(); emit IBaseEscrow.FundsRescued(address(0), MAKING_AMOUNT); - srcClone.rescueFunds(address(0), MAKING_AMOUNT, immutables); + swapData.srcClone.rescueFunds(address(0), MAKING_AMOUNT, swapData.immutables); assertEq(bob.addr.balance, balanceBobNative + SRC_SAFETY_DEPOSIT + MAKING_AMOUNT); - assertEq(address(srcClone).balance, 0); + assertEq(address(swapData.srcClone).balance, 0); } function test_NoRescueFundsEarlierSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT + SRC_SAFETY_DEPOSIT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT + SRC_SAFETY_DEPOSIT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceBob = usdc.balanceOf(bob.addr); @@ -417,47 +353,40 @@ contract EscrowTest is BaseSetup { // withdraw vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.startPrank(bob.addr); - srcClone.withdraw(SECRET, immutables); + swapData.srcClone.withdraw(SECRET, swapData.immutables); assertEq(bob.addr.balance, balanceBobNative + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), SRC_SAFETY_DEPOSIT); // rescue vm.expectRevert(IBaseEscrow.InvalidTime.selector); - srcClone.rescueFunds(address(usdc), SRC_SAFETY_DEPOSIT, immutables); + swapData.srcClone.rescueFunds(address(usdc), SRC_SAFETY_DEPOSIT, swapData.immutables); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), SRC_SAFETY_DEPOSIT); } function test_NoRescueFundsByAnyoneSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - assertEq(usdc.balanceOf(address(srcClone)), 0); - assertEq(address(srcClone).balance, 0); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); + assertEq(address(swapData.srcClone).balance, 0); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT + SRC_SAFETY_DEPOSIT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT + SRC_SAFETY_DEPOSIT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceBob = usdc.balanceOf(bob.addr); @@ -466,24 +395,22 @@ contract EscrowTest is BaseSetup { // withdraw vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.prank(bob.addr); - srcClone.withdraw(SECRET, immutables); + swapData.srcClone.withdraw(SECRET, swapData.immutables); assertEq(bob.addr.balance, balanceBobNative + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), SRC_SAFETY_DEPOSIT); // rescue vm.warp(block.timestamp + RESCUE_DELAY); vm.expectRevert(IBaseEscrow.InvalidCaller.selector); - srcClone.rescueFunds(address(usdc), SRC_SAFETY_DEPOSIT, immutables); + swapData.srcClone.rescueFunds(address(usdc), SRC_SAFETY_DEPOSIT, swapData.immutables); assertEq(usdc.balanceOf(bob.addr), balanceBob + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), SRC_SAFETY_DEPOSIT); } function test_WithdrawByResolverDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.startPrank(bob.addr); @@ -507,8 +434,8 @@ contract EscrowTest is BaseSetup { } function test_WithdrawByResolverDstNative() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(0x00) + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDstCustom( + HASHED_SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(0x00), DST_SAFETY_DEPOSIT ); // deploy escrow @@ -531,9 +458,7 @@ contract EscrowTest is BaseSetup { } function test_RescueFundsDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); assertEq(dai.balanceOf(address(dstClone)), 0); assertEq(address(dstClone).balance, 0); @@ -567,9 +492,7 @@ contract EscrowTest is BaseSetup { } function test_RescueFundsDstNative() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); assertEq(address(dstClone).balance, 0); @@ -602,9 +525,7 @@ contract EscrowTest is BaseSetup { } function test_NoRescueFundsEarlierDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); assertEq(address(dstClone).balance, 0); @@ -635,9 +556,7 @@ contract EscrowTest is BaseSetup { } function test_NoRescueFundsByAnyoneDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); assertEq(address(dstClone).balance, 0); @@ -671,42 +590,33 @@ contract EscrowTest is BaseSetup { function test_NoWithdrawalWithWrongSecretSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // withdraw vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.prank(bob.addr); vm.expectRevert(IBaseEscrow.InvalidSecret.selector); - srcClone.withdraw(WRONG_SECRET, immutables); + swapData.srcClone.withdraw(WRONG_SECRET, swapData.immutables); } function test_NoWithdrawalWithWrongSecretDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.startPrank(bob.addr); @@ -720,9 +630,7 @@ contract EscrowTest is BaseSetup { // During non-public withdrawal period function test_NoWithdrawalByNonResolverDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.prank(bob.addr); @@ -736,9 +644,7 @@ contract EscrowTest is BaseSetup { // During public withdrawal period function test_WithdrawByAnyoneDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.prank(bob.addr); @@ -763,9 +669,7 @@ contract EscrowTest is BaseSetup { // During public withdrawal period function test_WithdrawByResolverPublicDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.startPrank(bob.addr); @@ -790,43 +694,34 @@ contract EscrowTest is BaseSetup { function test_NoFailedNativeTokenTransferWithdrawalSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // withdraw vm.warp(block.timestamp + srcTimelocks.publicWithdrawal + 100); NoReceiveCaller caller = new NoReceiveCaller(); - bytes memory data = abi.encodeWithSelector(IEscrowSrc.publicWithdraw.selector, SECRET, immutables); + bytes memory data = abi.encodeWithSelector(IEscrowSrc.publicWithdraw.selector, SECRET, swapData.immutables); vm.expectRevert(IBaseEscrow.NativeTokenSendingFailure.selector); - caller.arbitraryCall(address(srcClone), data); + caller.arbitraryCall(address(swapData.srcClone), data); } function test_NoFailedNativeTokenTransferWithdrawalDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IEscrowDst dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IEscrowDst dstClone) = _prepareDataDst(); // deploy escrow vm.prank(bob.addr); @@ -840,8 +735,8 @@ contract EscrowTest is BaseSetup { } function test_NoFailedNativeTokenTransferWithdrawalDstNative() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, address(escrowFactory), bob.addr, address(0x00) + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDstCustom( + HASHED_SECRET, TAKING_AMOUNT, address(escrowFactory), bob.addr, address(0x00), DST_SAFETY_DEPOSIT ); // deploy escrow @@ -855,9 +750,7 @@ contract EscrowTest is BaseSetup { } function test_NoPublicWithdrawOutsideOfAllowedPeriodDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.prank(bob.addr); @@ -876,62 +769,57 @@ contract EscrowTest is BaseSetup { function test_CancelResolverSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceBob = bob.addr.balance; uint256 balanceAlice = usdc.balanceOf(alice.addr); - uint256 balanceEscrow = usdc.balanceOf(address(srcClone)); + uint256 balanceEscrow = usdc.balanceOf(address(swapData.srcClone)); // cancel vm.warp(block.timestamp + srcTimelocks.cancellation + 10); vm.prank(bob.addr); vm.expectEmit(); emit IBaseEscrow.EscrowCancelled(); - srcClone.cancel(immutables); + swapData.srcClone.cancel(swapData.immutables); assertEq(bob.addr.balance, balanceBob + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(alice.addr), balanceAlice + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), balanceEscrow - (MAKING_AMOUNT)); + assertEq(usdc.balanceOf(address(swapData.srcClone)), balanceEscrow - (MAKING_AMOUNT)); } function test_CancelResolverSrcReceiver() public { address receiver = charlie.addr; // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, receiver, true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( + HASHED_SECRET, + MAKING_AMOUNT, + TAKING_AMOUNT, + SRC_SAFETY_DEPOSIT, + DST_SAFETY_DEPOSIT, + receiver, + true, + false + ); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); IEscrowFactory.DstImmutablesComplement memory immutablesComplement = IEscrowFactory.DstImmutablesComplement({ maker: Address.wrap(uint160(receiver)), @@ -943,183 +831,153 @@ contract EscrowTest is BaseSetup { vm.prank(address(limitOrderProtocol)); vm.expectEmit(); - emit IEscrowFactory.SrcEscrowCreated(immutables, immutablesComplement); + emit IEscrowFactory.SrcEscrowCreated(swapData.immutables, immutablesComplement); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceBob = bob.addr.balance; uint256 balanceAlice = usdc.balanceOf(alice.addr); uint256 balanceReceiver = usdc.balanceOf(receiver); - uint256 balanceEscrow = usdc.balanceOf(address(srcClone)); + uint256 balanceEscrow = usdc.balanceOf(address(swapData.srcClone)); // cancel vm.warp(block.timestamp + srcTimelocks.cancellation + 10); vm.prank(bob.addr); vm.expectEmit(); emit IBaseEscrow.EscrowCancelled(); - srcClone.cancel(immutables); + swapData.srcClone.cancel(swapData.immutables); assertEq(bob.addr.balance, balanceBob + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(alice.addr), balanceAlice + MAKING_AMOUNT); assertEq(usdc.balanceOf(receiver), balanceReceiver); - assertEq(usdc.balanceOf(address(srcClone)), balanceEscrow - (MAKING_AMOUNT)); + assertEq(usdc.balanceOf(address(swapData.srcClone)), balanceEscrow - (MAKING_AMOUNT)); } function test_CancelPublicSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); uint256 balanceThis = address(this).balance; uint256 balanceAlice = usdc.balanceOf(alice.addr); - uint256 balanceEscrow = usdc.balanceOf(address(srcClone)); + uint256 balanceEscrow = usdc.balanceOf(address(swapData.srcClone)); // cancel vm.warp(block.timestamp + srcTimelocks.publicCancellation + 100); vm.expectEmit(); emit IBaseEscrow.EscrowCancelled(); - srcClone.publicCancel(immutables); + swapData.srcClone.publicCancel(swapData.immutables); assertEq(address(this).balance, balanceThis + SRC_SAFETY_DEPOSIT); assertEq(usdc.balanceOf(alice.addr), balanceAlice + MAKING_AMOUNT); - assertEq(usdc.balanceOf(address(srcClone)), balanceEscrow - (MAKING_AMOUNT)); + assertEq(usdc.balanceOf(address(swapData.srcClone)), balanceEscrow - (MAKING_AMOUNT)); } function test_NoCancelDuringWithdrawalSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // cancel vm.prank(bob.addr); vm.warp(block.timestamp + srcTimelocks.withdrawal + 100); vm.expectRevert(IBaseEscrow.InvalidTime.selector); - srcClone.cancel(immutables); + swapData.srcClone.cancel(swapData.immutables); } function test_NoPublicCancelDuringPrivateCancellationSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // cancel during private cancellation period vm.warp(block.timestamp + srcTimelocks.cancellation + 100); vm.expectRevert(IBaseEscrow.InvalidTime.selector); - srcClone.publicCancel(immutables); + swapData.srcClone.publicCancel(swapData.immutables); } // During non-public cancel period function test_NoAnyoneCancelDuringResolverCancelSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // cancel vm.warp(block.timestamp + srcTimelocks.cancellation + 10); vm.expectRevert(IBaseEscrow.InvalidCaller.selector); - srcClone.cancel(immutables); + swapData.srcClone.cancel(swapData.immutables); } function test_CancelDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.startPrank(bob.addr); @@ -1144,8 +1002,8 @@ contract EscrowTest is BaseSetup { function test_CancelDstDifferentTarget() public { address target = charlie.addr; - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, target, address(dai) + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDstCustom( + HASHED_SECRET, TAKING_AMOUNT, alice.addr, target, address(dai), DST_SAFETY_DEPOSIT ); // deploy escrow @@ -1175,8 +1033,8 @@ contract EscrowTest is BaseSetup { } function test_CancelDstWithNativeToken() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(0) + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDstCustom( + HASHED_SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(0), DST_SAFETY_DEPOSIT ); // deploy escrow @@ -1198,9 +1056,7 @@ contract EscrowTest is BaseSetup { // Only resolver can cancel function test_NoCancelByAnyoneDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.prank(bob.addr); @@ -1213,9 +1069,7 @@ contract EscrowTest is BaseSetup { } function test_NoCancelDuringWithdrawalDst() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); // deploy escrow vm.startPrank(bob.addr); @@ -1229,67 +1083,51 @@ contract EscrowTest is BaseSetup { function test_NoFailedNativeTokenTransferCancelSrc() public { // deploy escrow - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IEscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // cancel vm.warp(block.timestamp + srcTimelocks.publicCancellation + 100); vm.prank(address(escrowFactory)); vm.expectRevert(IBaseEscrow.NativeTokenSendingFailure.selector); - srcClone.publicCancel(immutables); + swapData.srcClone.publicCancel(swapData.immutables); } function test_NoCallsWithInvalidImmutables() public { - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutablesSrc - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (IBaseEscrow.Immutables memory immutablesDst, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (IBaseEscrow.Immutables memory immutablesDst, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); // deploy src escrow vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); // deploy dst escrow @@ -1298,9 +1136,9 @@ contract EscrowTest is BaseSetup { // withdraw src vm.warp(block.timestamp + srcTimelocks.withdrawal + 10); - immutablesSrc.amount = TAKING_AMOUNT; + swapData.immutables.amount = TAKING_AMOUNT; vm.expectRevert(IBaseEscrow.InvalidImmutables.selector); - srcClone.withdraw(SECRET, immutablesSrc); + swapData.srcClone.withdraw(SECRET, swapData.immutables); // withdraw dst vm.warp(block.timestamp + dstTimelocks.withdrawal + 10); @@ -1311,7 +1149,7 @@ contract EscrowTest is BaseSetup { // cancel src vm.warp(block.timestamp + srcTimelocks.cancellation + 10); vm.expectRevert(IBaseEscrow.InvalidImmutables.selector); - srcClone.cancel(immutablesSrc); + swapData.srcClone.cancel(swapData.immutables); // cancel dst vm.warp(block.timestamp + dstTimelocks.cancellation + 10); @@ -1322,7 +1160,7 @@ contract EscrowTest is BaseSetup { // rescue src vm.expectRevert(IBaseEscrow.InvalidImmutables.selector); - srcClone.rescueFunds(address(usdc), SRC_SAFETY_DEPOSIT, immutablesSrc); + swapData.srcClone.rescueFunds(address(usdc), SRC_SAFETY_DEPOSIT, swapData.immutables); // rescue dst vm.expectRevert(IBaseEscrow.InvalidImmutables.selector); diff --git a/test/unit/EscrowFactory.t.sol b/test/unit/EscrowFactory.t.sol index ad195c4..6ceda4a 100644 --- a/test/unit/EscrowFactory.t.sol +++ b/test/unit/EscrowFactory.t.sol @@ -2,17 +2,18 @@ pragma solidity 0.8.23; import { ResolverValidationExtension } from "limit-order-settlement/contracts/extensions/ResolverValidationExtension.sol"; +import { Address } from "solidity-utils/contracts/libraries/AddressLib.sol"; import { Merkle } from "murky/src/Merkle.sol"; -import { EscrowDst } from "../../contracts/EscrowDst.sol"; -import { IEscrowFactory } from "../../contracts/interfaces/IEscrowFactory.sol"; -import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; -import { Timelocks, TimelocksLib } from "../../contracts/libraries/TimelocksLib.sol"; +import { EscrowDst } from "contracts/EscrowDst.sol"; +import { IEscrowFactory } from "contracts/interfaces/IEscrowFactory.sol"; +import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; +import { Timelocks, TimelocksLib } from "contracts/libraries/TimelocksLib.sol"; -import { Address, AddressLib, BaseSetup, EscrowSrc, IOrderMixin } from "../utils/BaseSetup.sol"; +import { BaseSetup } from "../utils/BaseSetup.sol"; +import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; contract EscrowFactoryTest is BaseSetup { - using AddressLib for Address; using TimelocksLib for Timelocks; uint256 public constant SECRETS_AMOUNT = 100; @@ -37,14 +38,7 @@ contract EscrowFactoryTest is BaseSetup { vm.assume(srcAmount > 0 && dstAmount > 0); uint256 srcSafetyDeposit = uint256(srcAmount) * 10 / 100; uint256 dstSafetyDeposit = uint256(dstAmount) * 10 / 100; - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - /* IBaseEscrow.Immutables memory immutables */ - ) = _prepareDataSrc( + CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( keccak256(abi.encode(secret)), srcAmount, dstAmount, @@ -55,40 +49,42 @@ contract EscrowFactoryTest is BaseSetup { false // allowMultipleFills ); - (bool success,) = address(srcClone).call{ value: srcSafetyDeposit }(""); + (bool success,) = address(swapData.srcClone).call{ value: srcSafetyDeposit }(""); assertEq(success, true); - usdc.transfer(address(srcClone), srcAmount); + usdc.transfer(address(swapData.srcClone), srcAmount); vm.prank(address(limitOrderProtocol)); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker srcAmount, // makingAmount dstAmount, // takingAmount 0, // remainingMakingAmount - extraData + swapData.extraData ); - assertEq(usdc.balanceOf(address(srcClone)), srcAmount); - assertEq(address(srcClone).balance, srcSafetyDeposit); + assertEq(usdc.balanceOf(address(swapData.srcClone)), srcAmount); + assertEq(address(swapData.srcClone).balance, srcSafetyDeposit); } function testFuzz_DeployCloneForMakerWithReceiver() public { address receiver = charlie.addr; - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, receiver, true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( + HASHED_SECRET, + MAKING_AMOUNT, + TAKING_AMOUNT, + SRC_SAFETY_DEPOSIT, + DST_SAFETY_DEPOSIT, + receiver, + true, + false + ); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); IEscrowFactory.DstImmutablesComplement memory immutablesComplement = IEscrowFactory.DstImmutablesComplement({ maker: Address.wrap(uint160(receiver)), @@ -100,32 +96,33 @@ contract EscrowFactoryTest is BaseSetup { vm.prank(address(limitOrderProtocol)); vm.expectEmit(); - emit IEscrowFactory.SrcEscrowCreated(immutables, immutablesComplement); + emit IEscrowFactory.SrcEscrowCreated(swapData.immutables, immutablesComplement); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); - assertEq(usdc.balanceOf(address(srcClone)), MAKING_AMOUNT); - assertEq(address(srcClone).balance, SRC_SAFETY_DEPOSIT); + assertEq(usdc.balanceOf(address(swapData.srcClone)), MAKING_AMOUNT); + assertEq(address(swapData.srcClone).balance, SRC_SAFETY_DEPOSIT); } function testFuzz_DeployCloneForTaker(bytes32 secret, uint56 amount) public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, EscrowDst dstClone) = _prepareDataDst( - secret, amount, alice.addr, bob.addr, address(dai) + uint256 safetyDeposit = uint64(amount) * 10 / 100; + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp, EscrowDst dstClone) = _prepareDataDstCustom( + secret, amount, alice.addr, bob.addr, address(dai), safetyDeposit ); uint256 balanceBobNative = bob.addr.balance; uint256 balanceBob = dai.balanceOf(bob.addr); uint256 balanceEscrow = dai.balanceOf(address(dstClone)); uint256 balanceEscrowNative = address(dstClone).balance; - uint256 safetyDeposit = uint64(amount) * 10 / 100; + // deploy escrow vm.prank(bob.addr); vm.expectEmit(); @@ -139,72 +136,51 @@ contract EscrowFactoryTest is BaseSetup { } function test_NoInsufficientBalanceNativeDeploymentForMaker() public { - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - /* IBaseEscrow.Immutables memory immutables */ - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); vm.prank(address(limitOrderProtocol)); vm.expectRevert(IEscrowFactory.InsufficientEscrowBalance.selector); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); } function test_NoInsufficientBalanceDeploymentForMaker() public { - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - /* IBaseEscrow.Immutables memory immutables */ - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); vm.prank(address(limitOrderProtocol)); vm.expectRevert(IEscrowFactory.InsufficientEscrowBalance.selector); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); } // Only whitelisted resolver can deploy escrow function test_NoDeploymentForNotResolver() public { - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - /* IBaseEscrow.Immutables memory immutables */ - ) = _prepareDataSrc(HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), true, false); - - (bool success,) = address(srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); + CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + + (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); - usdc.transfer(address(srcClone), MAKING_AMOUNT); + usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); inch.mint(alice.addr, 10 ether); vm.prank(alice.addr); @@ -215,21 +191,19 @@ contract EscrowFactoryTest is BaseSetup { vm.prank(address(limitOrderProtocol)); vm.expectRevert(ResolverValidationExtension.ResolverCanNotFillOrder.selector); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, alice.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); } function test_NoUnsafeDeploymentForTaker() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp,) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp,) = _prepareDataDst(); vm.warp(srcCancellationTimestamp + 1); @@ -240,9 +214,7 @@ contract EscrowFactoryTest is BaseSetup { } function test_NoInsufficientBalanceDeploymentForTaker() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp,) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(dai) - ); + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp,) = _prepareDataDst(); // deploy escrow vm.prank(bob.addr); @@ -251,8 +223,8 @@ contract EscrowFactoryTest is BaseSetup { } function test_NoInsufficientBalanceNativeDeploymentForTaker() public { - (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp,) = _prepareDataDst( - SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(0x00) + (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp,) = _prepareDataDstCustom( + HASHED_SECRET, TAKING_AMOUNT, alice.addr, bob.addr, address(0x00), DST_SAFETY_DEPOSIT ); // deploy escrow @@ -269,30 +241,22 @@ contract EscrowFactoryTest is BaseSetup { bytes32 rootPlusAmount = bytes32(uint256(0) << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; vm.prank(address(limitOrderProtocol)); vm.expectRevert(IEscrowFactory.InvalidSecretsAmount.selector); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, 0, // remainingMakingAmount - extraData + swapData.extraData ); } @@ -304,30 +268,22 @@ contract EscrowFactoryTest is BaseSetup { bytes32 rootPlusAmount = bytes32(SECRETS_AMOUNT << 240 | uint240(uint256(root))); - ( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - /* bytes memory extension */, - IBaseEscrow srcClone, - IBaseEscrow.Immutables memory immutables - ) = _prepareDataSrc(rootPlusAmount, MAKING_AMOUNT, TAKING_AMOUNT, SRC_SAFETY_DEPOSIT, DST_SAFETY_DEPOSIT, address(0), false, true); + CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); - immutables.hashlock = hashedSecrets[idx]; - immutables.amount = makingAmount; - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); + swapData.immutables.hashlock = hashedSecrets[idx]; + swapData.immutables.amount = makingAmount; vm.prank(address(limitOrderProtocol)); vm.expectRevert(IEscrowFactory.InvalidSecretIndex.selector); escrowFactory.postInteraction( - order, + swapData.order, "", // extension - orderHash, + swapData.orderHash, bob.addr, // taker MAKING_AMOUNT, TAKING_AMOUNT, MAKING_AMOUNT, // remainingMakingAmount - extraData + swapData.extraData ); } diff --git a/test/unit/MerkleStorageInvalidator.t.sol b/test/unit/MerkleStorageInvalidator.t.sol index 900b023..503764b 100644 --- a/test/unit/MerkleStorageInvalidator.t.sol +++ b/test/unit/MerkleStorageInvalidator.t.sol @@ -3,11 +3,14 @@ pragma solidity 0.8.23; import { Merkle } from "murky/src/Merkle.sol"; +import { IOrderMixin } from "limit-order-protocol/contracts/interfaces/IOrderMixin.sol"; import { ITakerInteraction } from "limit-order-protocol/contracts/interfaces/ITakerInteraction.sol"; +import { MakerTraits } from "limit-order-protocol/contracts/libraries/MakerTraitsLib.sol"; +import { Address } from "solidity-utils/contracts/libraries/AddressLib.sol"; -import { IMerkleStorageInvalidator } from "../../contracts/interfaces/IMerkleStorageInvalidator.sol"; +import { IMerkleStorageInvalidator } from "contracts/interfaces/IMerkleStorageInvalidator.sol"; -import { Address, BaseSetup, IOrderMixin, MakerTraits } from "../utils/BaseSetup.sol"; +import { BaseSetup } from "../utils/BaseSetup.sol"; contract MerkleStorageInvalidatorTest is BaseSetup { diff --git a/test/utils/BaseSetup.sol b/test/utils/BaseSetup.sol index 798bb3e..dbf8650 100644 --- a/test/utils/BaseSetup.sol +++ b/test/utils/BaseSetup.sol @@ -4,102 +4,24 @@ pragma solidity 0.8.23; import { Test } from "forge-std/Test.sol"; import { IWETH, LimitOrderProtocol } from "limit-order-protocol/contracts/LimitOrderProtocol.sol"; -import { IOrderMixin } from "limit-order-protocol/contracts/interfaces/IOrderMixin.sol"; -import { MakerTraits, MakerTraitsLib } from "limit-order-protocol/contracts/libraries/MakerTraitsLib.sol"; -import { TakerTraits } from "limit-order-protocol/contracts/libraries/TakerTraitsLib.sol"; import { WrappedTokenMock } from "limit-order-protocol/contracts/mocks/WrappedTokenMock.sol"; import { IFeeBank } from "limit-order-settlement/contracts/interfaces/IFeeBank.sol"; -import { Address, AddressLib } from "solidity-utils/contracts/libraries/AddressLib.sol"; import { TokenCustomDecimalsMock } from "solidity-utils/contracts/mocks/TokenCustomDecimalsMock.sol"; import { TokenMock } from "solidity-utils/contracts/mocks/TokenMock.sol"; -import { EscrowDst } from "../../contracts/EscrowDst.sol"; -import { EscrowSrc } from "../../contracts/EscrowSrc.sol"; -import { BaseEscrowFactory } from "../../contracts/BaseEscrowFactory.sol"; -import { EscrowFactory } from "../../contracts/EscrowFactory.sol"; -import { ERC20True } from "../../contracts/mocks/ERC20True.sol"; -import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; -import { EscrowFactoryZkSync } from "../../contracts/zkSync/EscrowFactoryZkSync.sol"; -import { Timelocks, TimelocksSettersLib } from "./libraries/TimelocksSettersLib.sol"; +import { EscrowDst } from "contracts/EscrowDst.sol"; +import { EscrowSrc } from "contracts/EscrowSrc.sol"; +import { BaseEscrowFactory } from "contracts/BaseEscrowFactory.sol"; +import { EscrowFactory } from "contracts/EscrowFactory.sol"; +import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; +import { EscrowFactoryZkSync } from "contracts/zkSync/EscrowFactoryZkSync.sol"; +import { Utils } from "./Utils.sol"; +import { CrossChainLib } from "./libraries/CrossChainLib.sol"; +import { Timelocks } from "./libraries/TimelocksSettersLib.sol"; /* solhint-disable max-states-count */ -contract BaseSetup is Test { - using AddressLib for Address; - using MakerTraitsLib for MakerTraits; - using TimelocksSettersLib for Timelocks; - - /** - * Timelocks for the source chain. - * withdrawal: Seconds between `deployedAt` and the start of the withdrawal period. - * cancellation: Seconds between `deployedAt` and the start of the cancellation period. - * publicCancellation: Seconds between `deployedAt` and the start of the public cancellation period. - */ - struct SrcTimelocks { - uint32 withdrawal; - uint32 publicWithdrawal; - uint32 cancellation; - uint32 publicCancellation; - } - - /** - * Timelocks for the destination chain. - * withdrawal: Seconds between `deployedAt` and the start of the withdrawal period. - * publicWithdrawal: Seconds between `deployedAt` and the start of the public withdrawal period. - * cancellation: Seconds between `deployedAt` and the start of the cancellation period. - */ - struct DstTimelocks { - uint32 withdrawal; - uint32 publicWithdrawal; - uint32 cancellation; - } - - struct InteractionParams { - bytes makerAssetSuffix; - bytes takerAssetSuffix; - bytes makingAmountData; - bytes takingAmountData; - bytes predicate; - bytes permit; - bytes preInteraction; - bytes postInteraction; - } - - struct MakerTraitsParams { - address allowedSender; - bool shouldCheckEpoch; - bool allowPartialFill; - bool allowMultipleFills; - bool usePermit2; - bool unwrapWeth; - uint40 expiry; - uint40 nonce; - uint40 series; - } - - struct Wallet { - address addr; - uint256 privateKey; - } - - // Limit order protocol flags - uint256 internal constant _NO_PARTIAL_FILLS_FLAG = 1 << 255; - uint256 internal constant _ALLOW_MULTIPLE_FILLS_FLAG = 1 << 254; - uint256 internal constant _PRE_INTERACTION_CALL_FLAG = 1 << 252; - uint256 internal constant _POST_INTERACTION_CALL_FLAG = 1 << 251; - uint256 internal constant _NEED_CHECK_EPOCH_MANAGER_FLAG = 1 << 250; - uint256 internal constant _HAS_EXTENSION_FLAG = 1 << 249; - uint256 internal constant _USE_PERMIT2_FLAG = 1 << 248; - uint256 internal constant _UNWRAP_WETH_FLAG = 1 << 247; - // Taker traits flags - uint256 private constant _MAKER_AMOUNT_FLAG_TT = 1 << 255; - uint256 private constant _UNWRAP_WETH_FLAG_TT = 1 << 254; - uint256 private constant _SKIP_ORDER_PERMIT_FLAG = 1 << 253; - uint256 private constant _USE_PERMIT2_FLAG_TT = 1 << 252; - uint256 private constant _ARGS_HAS_TARGET = 1 << 251; - uint256 private constant _ARGS_EXTENSION_LENGTH_OFFSET = 224; - uint256 private constant _ARGS_INTERACTION_LENGTH_OFFSET = 200; - +contract BaseSetup is Test, Utils { /* solhint-disable private-vars-leading-underscore */ bytes32 internal constant SECRET = keccak256(abi.encodePacked("secret")); bytes32 internal constant HASHED_SECRET = keccak256(abi.encodePacked(SECRET)); @@ -110,10 +32,6 @@ contract BaseSetup is Test { uint32 internal constant RESOLVER_FEE = 100; uint32 internal constant RESCUE_DELAY = 604800; // 7 days - bytes32 internal constant ZKSYNC_PROFILE_HASH = keccak256(abi.encodePacked("zksync")); - - Wallet[] internal users; - uint256 internal nextUser = uint256(keccak256(abi.encodePacked("user address"))); Wallet internal alice; Wallet internal bob; Wallet internal charlie; @@ -129,16 +47,22 @@ contract BaseSetup is Test { EscrowDst internal escrowDst; IFeeBank internal feeBank; + address[] internal resolvers; + Timelocks internal timelocks; Timelocks internal timelocksDst; - SrcTimelocks internal srcTimelocks = SrcTimelocks({ + CrossChainLib.SrcTimelocks internal srcTimelocks = CrossChainLib.SrcTimelocks({ withdrawal: 120, publicWithdrawal: 500, cancellation: 1020, publicCancellation: 1530 }); - DstTimelocks internal dstTimelocks = DstTimelocks({ withdrawal: 300, publicWithdrawal: 540, cancellation: 900 }); + CrossChainLib.DstTimelocks internal dstTimelocks = CrossChainLib.DstTimelocks({ + withdrawal: 300, + publicWithdrawal: 540, + cancellation: 900 + }); bytes internal auctionPoints = abi.encodePacked( uint24(800000), uint16(100), uint24(700000), uint16(100), @@ -153,7 +77,7 @@ contract BaseSetup is Test { function setUp() public virtual { bytes32 profileHash = keccak256(abi.encodePacked(vm.envString("FOUNDRY_PROFILE"))); - if (profileHash == ZKSYNC_PROFILE_HASH) isZkSync = true; + if (profileHash == CrossChainLib.ZKSYNC_PROFILE_HASH) isZkSync = true; _createUsers(3); alice = users[0]; @@ -163,12 +87,15 @@ contract BaseSetup is Test { charlie = users[2]; vm.label(charlie.addr, "Charlie"); + resolvers = new address[](1); + resolvers[0] = bob.addr; + _deployTokens(); dai.mint(bob.addr, 1000 ether); usdc.mint(alice.addr, 1000 ether); inch.mint(bob.addr, 1000 ether); - _setTimelocks(); + (timelocks, timelocksDst) = CrossChainLib.setTimelocks(srcTimelocks, dstTimelocks); _deployContracts(); @@ -181,23 +108,6 @@ contract BaseSetup is Test { usdc.approve(address(limitOrderProtocol), 1000 ether); } - function _getNextUserAddress() internal returns (Wallet memory) { - address addr = vm.addr(nextUser); - Wallet memory user = Wallet(addr, nextUser); - nextUser = uint256(keccak256(abi.encodePacked(nextUser))); - return user; - } - - // create users with 100 ETH balance each - function _createUsers(uint256 userNum) internal { - users = new Wallet[](userNum); - for (uint256 i = 0; i < userNum; i++) { - Wallet memory user = _getNextUserAddress(); - vm.deal(user.addr, 100 ether); - users[i] = user; - } - } - function _deployTokens() internal { dai = new TokenMock("DAI", "DAI"); vm.label(address(dai), "DAI"); @@ -209,29 +119,6 @@ contract BaseSetup is Test { vm.label(address(inch), "1INCH"); } - function _setTimelocks() internal { - timelocks = TimelocksSettersLib.init( - srcTimelocks.withdrawal, - srcTimelocks.publicWithdrawal, - srcTimelocks.cancellation, - srcTimelocks.publicCancellation, - dstTimelocks.withdrawal, - dstTimelocks.publicWithdrawal, - dstTimelocks.cancellation, - uint32(block.timestamp) - ); - timelocksDst = TimelocksSettersLib.init( - 0, - 0, - 0, - 0, - dstTimelocks.withdrawal, - dstTimelocks.publicWithdrawal, - dstTimelocks.cancellation, - uint32(block.timestamp) - ); - } - function _deployContracts() internal { limitOrderProtocol = new LimitOrderProtocol(IWETH(weth)); @@ -250,74 +137,37 @@ contract BaseSetup is Test { vm.label(address(feeBank), "FeeBank"); } - function _buidDynamicData( - bytes32 hashlock, - uint256 chainId, - address token, - uint256 srcSafetyDeposit, - uint256 dstSafetyDeposit - ) internal view returns (bytes memory) { - return ( - abi.encode( - hashlock, - chainId, - token, - (srcSafetyDeposit << 128) | dstSafetyDeposit, - timelocks - ) + function _prepareDataSrc(bool fakeOrder, bool allowMultipleFills) internal returns(CrossChainLib.SwapData memory) { + return _prepareDataSrcCustom( + HASHED_SECRET, + MAKING_AMOUNT, + TAKING_AMOUNT, + SRC_SAFETY_DEPOSIT, + DST_SAFETY_DEPOSIT, + address(0), + fakeOrder, + allowMultipleFills ); } - function _buildAuctionDetails( - uint24 gasBumpEstimate, - uint32 gasPriceEstimate, - uint32 startTime, - uint24 duration, - uint32 delay, - uint24 initialRateBump - ) internal view returns (bytes memory auctionDetails) { - auctionDetails = abi.encodePacked( - gasBumpEstimate, - gasPriceEstimate, - startTime + delay, - duration, - initialRateBump, - auctionPoints - ); - } - function _prepareDataSrc( - bytes32 secret, - uint256 srcAmount, - uint256 dstAmount, - uint256 srcSafetyDeposit, - uint256 dstSafetyDeposit, - address receiver, - bool fakeOrder, + function _prepareDataSrcHashlock( + bytes32 hashlock, + bool fakeOrder, bool allowMultipleFills - ) internal returns( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - bytes memory extension, - EscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) { - address[] memory resolvers = new address[](1); - resolvers[0] = bob.addr; - (order, orderHash, extraData, extension, srcClone, immutables) = _prepareDataSrcCustomResolver( - secret, - srcAmount, - dstAmount, - srcSafetyDeposit, - dstSafetyDeposit, - receiver, + ) internal returns(CrossChainLib.SwapData memory) { + return _prepareDataSrcCustom( + hashlock, + MAKING_AMOUNT, + TAKING_AMOUNT, + SRC_SAFETY_DEPOSIT, + DST_SAFETY_DEPOSIT, + address(0), fakeOrder, - allowMultipleFills, - resolvers + allowMultipleFills ); } - function _prepareDataSrcCustomResolver( + function _prepareDataSrcCustom( bytes32 hashlock, uint256 srcAmount, uint256 dstAmount, @@ -325,254 +175,67 @@ contract BaseSetup is Test { uint256 dstSafetyDeposit, address receiver, bool fakeOrder, - bool allowMultipleFills, - address[] memory resolvers - ) internal returns( - IOrderMixin.Order memory order, - bytes32 orderHash, - bytes memory extraData, - bytes memory extension, - EscrowSrc srcClone, - IBaseEscrow.Immutables memory immutables - ) { - extraData = _buidDynamicData( - hashlock, - block.chainid, - address(dai), - srcSafetyDeposit, - dstSafetyDeposit + bool allowMultipleFills + ) internal returns(CrossChainLib.SwapData memory swapData) { + swapData = CrossChainLib.prepareDataSrc( + CrossChainLib.OrderDetails({ + maker: alice.addr, + receiver: receiver, + srcToken: address(usdc), + dstToken: address(dai), + srcAmount: srcAmount, + dstAmount: dstAmount, + srcSafetyDeposit: srcSafetyDeposit, + dstSafetyDeposit: dstSafetyDeposit, + resolvers: resolvers, + resolverFee: RESOLVER_FEE, + auctionDetails: CrossChainLib.buildAuctionDetails( + 0, // gasBumpEstimate + 0, // gasPriceEstimate + uint32(block.timestamp), // startTime + 1800, // duration: 30 minutes + 0, // delay + 900000, // initialRateBump + auctionPoints + ) + }), + CrossChainLib.EscrowDetails({ + hashlock: hashlock, + timelocks: timelocks, + fakeOrder: fakeOrder, + allowMultipleFills: allowMultipleFills + }), + address(escrowFactory), + limitOrderProtocol ); - - bytes memory whitelist = abi.encodePacked(uint32(block.timestamp)); // auction start time - for (uint256 i = 0; i < resolvers.length; i++) { - whitelist = abi.encodePacked(whitelist, uint80(uint160(resolvers[i])), uint16(0)); // resolver address, time delta - } - - if (fakeOrder) { - order = IOrderMixin.Order({ - salt: 0, - maker: Address.wrap(uint160(alice.addr)), - receiver: Address.wrap(uint160(receiver)), - makerAsset: Address.wrap(uint160(address(usdc))), - takerAsset: Address.wrap(uint160(address(dai))), - makingAmount: srcAmount, - takingAmount: dstAmount, - makerTraits: MakerTraits.wrap(0) - }); - } else { - bytes memory postInteractionData = abi.encodePacked( - address(escrowFactory), - RESOLVER_FEE, - whitelist, - bytes1(0x08) | bytes1(0x01), // 0x08 - whitelist length = 1, 0x01 - turn on resolver fee - extraData - ); - - bytes memory auctionDetails = _buildAuctionDetails( - 0, // gasBumpEstimate - 0, // gasPriceEstimate - uint32(block.timestamp), // startTime - 1800, // duration: 30 minutes - 0, // delay - 900000 // initialRateBump - ); - bytes memory gettersAmountData = abi.encodePacked(address(escrowFactory), auctionDetails); - - (order, extension) = _buildOrder( - alice.addr, - receiver, - address(usdc), - address(new ERC20True()), - srcAmount, - dstAmount, - MakerTraits.wrap(0), - allowMultipleFills, - InteractionParams("", "", gettersAmountData, gettersAmountData, "", "", "", postInteractionData), - "" - ); - - dstAmount = escrowFactory.getTakingAmount(order, extension, orderHash, resolvers[0], srcAmount, srcAmount, auctionDetails); - } - - orderHash = limitOrderProtocol.hashOrder(order); - - immutables = IBaseEscrow.Immutables({ - orderHash: orderHash, - amount: srcAmount, - maker: Address.wrap(uint160(alice.addr)), - taker: Address.wrap(uint160(resolvers[0])), - token: Address.wrap(uint160(address(usdc))), - hashlock: hashlock, - safetyDeposit: srcSafetyDeposit, - timelocks: timelocks - }); - - srcClone = EscrowSrc(escrowFactory.addressOfEscrowSrc(immutables)); - // 0x08 - whitelist length = 1, 0x01 - turn on resolver fee - extraData = abi.encodePacked(RESOLVER_FEE, whitelist, bytes1(0x08) | bytes1(0x01), extraData); } function _prepareDataDst( - bytes32 secret, - uint256 amount, - address maker, - address taker, - address token - ) internal view returns (IBaseEscrow.Immutables memory, uint256, EscrowDst) { - (IBaseEscrow.Immutables memory escrowImmutables, uint256 srcCancellationTimestamp) = _buildDstEscrowImmutables( - secret, amount, maker, taker, token - ); - return (escrowImmutables, srcCancellationTimestamp, EscrowDst(escrowFactory.addressOfEscrowDst(escrowImmutables))); + ) internal view returns (IBaseEscrow.Immutables memory escrowImmutables, uint256 srcCancellationTimestamp, EscrowDst escrow) { + return _prepareDataDstCustom(HASHED_SECRET, TAKING_AMOUNT, alice.addr, resolvers[0], address(dai), DST_SAFETY_DEPOSIT); } - function _buildDstEscrowImmutables( - bytes32 secret, + function _prepareDataDstCustom( + bytes32 hashlock, uint256 amount, address maker, address taker, - address token - ) internal view returns (IBaseEscrow.Immutables memory immutables, uint256 srcCancellationTimestamp) { - bytes32 hashlock = keccak256(abi.encodePacked(secret)); - uint256 safetyDeposit = amount * 10 / 100; - srcCancellationTimestamp = block.timestamp + srcTimelocks.cancellation; - - immutables = IBaseEscrow.Immutables({ - orderHash: bytes32(block.timestamp), // fake order hash - hashlock: hashlock, - maker: Address.wrap(uint160(maker)), - taker: Address.wrap(uint160(taker)), - token: Address.wrap(uint160(token)), - amount: amount, - safetyDeposit: safetyDeposit, - timelocks: timelocksDst - }); - } - - function _buildMakerTraits(MakerTraitsParams memory params) internal pure returns (MakerTraits) { - uint256 data = 0 - | params.series << 160 - | params.nonce << 120 - | params.expiry << 80 - | uint160(params.allowedSender) & ((1 << 80) - 1) - | (params.unwrapWeth == true ? _UNWRAP_WETH_FLAG : 0) - | (params.allowMultipleFills == true ? _ALLOW_MULTIPLE_FILLS_FLAG : 0) - | (params.allowPartialFill == false ? _NO_PARTIAL_FILLS_FLAG : 0) - | (params.shouldCheckEpoch == true ? _NEED_CHECK_EPOCH_MANAGER_FLAG : 0) - | (params.usePermit2 == true ? _USE_PERMIT2_FLAG : 0); - return MakerTraits.wrap(data); - } - - function _buildOrder( - address maker, - address receiver, - address makerAsset, - address takerAsset, - uint256 makingAmount, - uint256 takingAmount, - MakerTraits makerTraits, - bool allowMultipleFills, - InteractionParams memory interactions, - bytes memory customData - ) internal pure returns (IOrderMixin.Order memory, bytes memory) { - MakerTraitsParams memory makerTraitsParams = MakerTraitsParams({ - allowedSender: address(0), - shouldCheckEpoch: false, - allowPartialFill: true, - allowMultipleFills: allowMultipleFills, - usePermit2: false, - unwrapWeth: false, - expiry: 0, - nonce: 0, - series: 0 - }); - bytes[8] memory allInteractions = [ - interactions.makerAssetSuffix, - interactions.takerAssetSuffix, - interactions.makingAmountData, - interactions.takingAmountData, - interactions.predicate, - interactions.permit, - interactions.preInteraction, - interactions.postInteraction - ]; - bytes memory allInteractionsConcat = bytes.concat( - interactions.makerAssetSuffix, - interactions.takerAssetSuffix, - interactions.makingAmountData, - interactions.takingAmountData, - interactions.predicate, - interactions.permit, - interactions.preInteraction, - interactions.postInteraction, - customData + address token, + uint256 safetyDeposit + ) internal view returns (IBaseEscrow.Immutables memory, uint256, EscrowDst) { + bytes32 orderHash = bytes32(block.timestamp); // fake order hash + uint256 srcCancellationTimestamp = block.timestamp + srcTimelocks.cancellation; + IBaseEscrow.Immutables memory escrowImmutables = CrossChainLib.buildDstEscrowImmutables( + orderHash, + hashlock, + amount, + maker, + taker, + token, + safetyDeposit, + timelocksDst ); - - bytes32 offsets = 0; - uint256 sum = 0; - for (uint256 i = 0; i < allInteractions.length; i++) { - if (allInteractions[i].length > 0) { - sum += allInteractions[i].length; - } - offsets |= bytes32(sum << (i * 32)); - } - - bytes memory extension = ""; - if (allInteractionsConcat.length > 0) { - extension = abi.encodePacked(offsets, allInteractionsConcat); - } - if (MakerTraits.unwrap(makerTraits) == 0) { - makerTraits = _buildMakerTraits(makerTraitsParams); - } - - uint256 salt = 1; - if (extension.length > 0) { - salt = uint256(keccak256(extension)) & ((1 << 160) - 1); - makerTraits = MakerTraits.wrap(MakerTraits.unwrap(makerTraits) | _HAS_EXTENSION_FLAG); - } - - if (interactions.preInteraction.length > 0) { - makerTraits = MakerTraits.wrap(MakerTraits.unwrap(makerTraits) | _PRE_INTERACTION_CALL_FLAG); - } - - if (interactions.postInteraction.length > 0) { - makerTraits = MakerTraits.wrap(MakerTraits.unwrap(makerTraits) | _POST_INTERACTION_CALL_FLAG); - } - - IOrderMixin.Order memory order = IOrderMixin.Order({ - salt: salt, - maker: Address.wrap(uint160(maker)), - receiver: Address.wrap(uint160(receiver)), - makerAsset: Address.wrap(uint160(makerAsset)), - takerAsset: Address.wrap(uint160(takerAsset)), - makingAmount: makingAmount, - takingAmount: takingAmount, - makerTraits: makerTraits - }); - return (order, extension); - } - - function _buildTakerTraits( - bool makingAmount, - bool unwrapWeth, - bool skipMakerPermit, - bool usePermit2, - address target, - bytes memory extension, - bytes memory interaction, - uint256 threshold - ) internal pure returns (TakerTraits, bytes memory) { - uint256 data = threshold - | (makingAmount ? _MAKER_AMOUNT_FLAG_TT : 0) - | (unwrapWeth ? _UNWRAP_WETH_FLAG_TT : 0) - | (skipMakerPermit ? _SKIP_ORDER_PERMIT_FLAG : 0) - | (usePermit2 ? _USE_PERMIT2_FLAG_TT : 0) - | (target != address(0) ? _ARGS_HAS_TARGET : 0) - | (extension.length << _ARGS_EXTENSION_LENGTH_OFFSET) - | (interaction.length << _ARGS_INTERACTION_LENGTH_OFFSET); - TakerTraits traits = TakerTraits.wrap(data); - bytes memory targetBytes = target != address(0) ? abi.encodePacked(target) : abi.encodePacked(""); - bytes memory args = abi.encodePacked(targetBytes, extension, interaction); - return (traits, args); + return (escrowImmutables, srcCancellationTimestamp, EscrowDst(escrowFactory.addressOfEscrowDst(escrowImmutables))); } } diff --git a/test/utils/Utils.sol b/test/utils/Utils.sol new file mode 100644 index 0000000..c7101e6 --- /dev/null +++ b/test/utils/Utils.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT + +pragma solidity 0.8.23; + +import { CommonBase } from "forge-std/Base.sol"; + +contract Utils is CommonBase { + struct Wallet { + address addr; + uint256 privateKey; + } + + /* solhint-disable private-vars-leading-underscore */ + Wallet[] internal users; + uint256 internal nextUser = uint256(keccak256(abi.encodePacked("user address"))); + /* solhint-enable private-vars-leading-underscore */ + + function _getNextUserAddress() internal returns (Wallet memory) { + address addr = vm.addr(nextUser); + Wallet memory user = Wallet(addr, nextUser); + nextUser = uint256(keccak256(abi.encodePacked(nextUser))); + return user; + } + + // create users with 100 ETH balance each + function _createUsers(uint256 userNum) internal { + users = new Wallet[](userNum); + for (uint256 i = 0; i < userNum; i++) { + Wallet memory user = _getNextUserAddress(); + vm.deal(user.addr, 100 ether); + users[i] = user; + } + } +} diff --git a/test/utils/libraries/CrossChainLib.sol b/test/utils/libraries/CrossChainLib.sol new file mode 100644 index 0000000..5ba05f4 --- /dev/null +++ b/test/utils/libraries/CrossChainLib.sol @@ -0,0 +1,401 @@ +// SPDX-License-Identifier: MIT + +pragma solidity 0.8.23; + +import { BaseEscrowFactory } from "contracts/BaseEscrowFactory.sol"; +import { EscrowSrc } from "contracts/EscrowSrc.sol"; +import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; +import { ERC20True } from "contracts/mocks/ERC20True.sol"; +import { IOrderMixin } from "limit-order-protocol/contracts/interfaces/IOrderMixin.sol"; +import { MakerTraits } from "limit-order-protocol/contracts/libraries/MakerTraitsLib.sol"; +import { TakerTraits } from "limit-order-protocol/contracts/libraries/TakerTraitsLib.sol"; +import { Address } from "solidity-utils/contracts/libraries/AddressLib.sol"; +import { Timelocks, TimelocksSettersLib } from "./TimelocksSettersLib.sol"; + +library CrossChainLib { + + /** + * Timelocks for the source chain. + * withdrawal: Seconds between `deployedAt` and the start of the withdrawal period. + * cancellation: Seconds between `deployedAt` and the start of the cancellation period. + * publicCancellation: Seconds between `deployedAt` and the start of the public cancellation period. + */ + struct SrcTimelocks { + uint32 withdrawal; + uint32 publicWithdrawal; + uint32 cancellation; + uint32 publicCancellation; + } + + /** + * Timelocks for the destination chain. + * withdrawal: Seconds between `deployedAt` and the start of the withdrawal period. + * publicWithdrawal: Seconds between `deployedAt` and the start of the public withdrawal period. + * cancellation: Seconds between `deployedAt` and the start of the cancellation period. + */ + struct DstTimelocks { + uint32 withdrawal; + uint32 publicWithdrawal; + uint32 cancellation; + } + + struct InteractionParams { + bytes makerAssetSuffix; + bytes takerAssetSuffix; + bytes makingAmountData; + bytes takingAmountData; + bytes predicate; + bytes permit; + bytes preInteraction; + bytes postInteraction; + } + + struct MakerTraitsParams { + address allowedSender; + bool shouldCheckEpoch; + bool allowPartialFill; + bool allowMultipleFills; + bool usePermit2; + bool unwrapWeth; + uint40 expiry; + uint40 nonce; + uint40 series; + } + + struct OrderDetails { + address maker; + address receiver; + address srcToken; + address dstToken; + uint256 srcAmount; + uint256 dstAmount; + uint256 srcSafetyDeposit; + uint256 dstSafetyDeposit; + address[] resolvers; + uint32 resolverFee; + bytes auctionDetails; + } + + struct EscrowDetails { + bytes32 hashlock; + Timelocks timelocks; + bool fakeOrder; + bool allowMultipleFills; + } + + struct SwapData { + IOrderMixin.Order order; + bytes32 orderHash; + bytes extraData; + bytes extension; + EscrowSrc srcClone; + IBaseEscrow.Immutables immutables; + } + + // Limit order protocol flags + uint256 internal constant _NO_PARTIAL_FILLS_FLAG = 1 << 255; + uint256 internal constant _ALLOW_MULTIPLE_FILLS_FLAG = 1 << 254; + uint256 internal constant _PRE_INTERACTION_CALL_FLAG = 1 << 252; + uint256 internal constant _POST_INTERACTION_CALL_FLAG = 1 << 251; + uint256 internal constant _NEED_CHECK_EPOCH_MANAGER_FLAG = 1 << 250; + uint256 internal constant _HAS_EXTENSION_FLAG = 1 << 249; + uint256 internal constant _USE_PERMIT2_FLAG = 1 << 248; + uint256 internal constant _UNWRAP_WETH_FLAG = 1 << 247; + // Taker traits flags + uint256 private constant _MAKER_AMOUNT_FLAG_TT = 1 << 255; + uint256 private constant _UNWRAP_WETH_FLAG_TT = 1 << 254; + uint256 private constant _SKIP_ORDER_PERMIT_FLAG = 1 << 253; + uint256 private constant _USE_PERMIT2_FLAG_TT = 1 << 252; + uint256 private constant _ARGS_HAS_TARGET = 1 << 251; + uint256 private constant _ARGS_EXTENSION_LENGTH_OFFSET = 224; + uint256 private constant _ARGS_INTERACTION_LENGTH_OFFSET = 200; + + bytes32 internal constant ZKSYNC_PROFILE_HASH = keccak256(abi.encodePacked("zksync")); + + function setTimelocks( + SrcTimelocks memory srcTimelocks, + DstTimelocks memory dstTimelocks + ) internal view returns (Timelocks timelocksSrc, Timelocks timelocksDst) { + timelocksSrc = TimelocksSettersLib.init( + srcTimelocks.withdrawal, + srcTimelocks.publicWithdrawal, + srcTimelocks.cancellation, + srcTimelocks.publicCancellation, + dstTimelocks.withdrawal, + dstTimelocks.publicWithdrawal, + dstTimelocks.cancellation, + uint32(block.timestamp) + ); + timelocksDst = TimelocksSettersLib.init( + 0, + 0, + 0, + 0, + dstTimelocks.withdrawal, + dstTimelocks.publicWithdrawal, + dstTimelocks.cancellation, + uint32(block.timestamp) + ); + } + + function buildAuctionDetails( + uint24 gasBumpEstimate, + uint32 gasPriceEstimate, + uint32 startTime, + uint24 duration, + uint32 delay, + uint24 initialRateBump, + bytes memory auctionPoints + ) internal pure returns (bytes memory auctionDetails) { + auctionDetails = abi.encodePacked( + gasBumpEstimate, + gasPriceEstimate, + startTime + delay, + duration, + initialRateBump, + auctionPoints + ); + } + + function buildMakerTraits(MakerTraitsParams memory params) internal pure returns (MakerTraits) { + uint256 data = 0 + | params.series << 160 + | params.nonce << 120 + | params.expiry << 80 + | uint160(params.allowedSender) & ((1 << 80) - 1) + | (params.unwrapWeth == true ? _UNWRAP_WETH_FLAG : 0) + | (params.allowMultipleFills == true ? _ALLOW_MULTIPLE_FILLS_FLAG : 0) + | (params.allowPartialFill == false ? _NO_PARTIAL_FILLS_FLAG : 0) + | (params.shouldCheckEpoch == true ? _NEED_CHECK_EPOCH_MANAGER_FLAG : 0) + | (params.usePermit2 == true ? _USE_PERMIT2_FLAG : 0); + return MakerTraits.wrap(data); + } + + function buildTakerTraits( + bool makingAmount, + bool unwrapWeth, + bool skipMakerPermit, + bool usePermit2, + address target, + bytes memory extension, + bytes memory interaction, + uint256 threshold + ) internal pure returns (TakerTraits, bytes memory) { + uint256 data = threshold + | (makingAmount ? _MAKER_AMOUNT_FLAG_TT : 0) + | (unwrapWeth ? _UNWRAP_WETH_FLAG_TT : 0) + | (skipMakerPermit ? _SKIP_ORDER_PERMIT_FLAG : 0) + | (usePermit2 ? _USE_PERMIT2_FLAG_TT : 0) + | (target != address(0) ? _ARGS_HAS_TARGET : 0) + | (extension.length << _ARGS_EXTENSION_LENGTH_OFFSET) + | (interaction.length << _ARGS_INTERACTION_LENGTH_OFFSET); + TakerTraits traits = TakerTraits.wrap(data); + bytes memory targetBytes = target != address(0) ? abi.encodePacked(target) : abi.encodePacked(""); + bytes memory args = abi.encodePacked(targetBytes, extension, interaction); + return (traits, args); + } + + function buildOrder( + address maker, + address receiver, + address makerAsset, + address takerAsset, + uint256 makingAmount, + uint256 takingAmount, + MakerTraits makerTraits, + bool allowMultipleFills, + InteractionParams memory interactions, + bytes memory customData + ) internal pure returns (IOrderMixin.Order memory, bytes memory) { + MakerTraitsParams memory makerTraitsParams = MakerTraitsParams({ + allowedSender: address(0), + shouldCheckEpoch: false, + allowPartialFill: true, + allowMultipleFills: allowMultipleFills, + usePermit2: false, + unwrapWeth: false, + expiry: 0, + nonce: 0, + series: 0 + }); + bytes[8] memory allInteractions = [ + interactions.makerAssetSuffix, + interactions.takerAssetSuffix, + interactions.makingAmountData, + interactions.takingAmountData, + interactions.predicate, + interactions.permit, + interactions.preInteraction, + interactions.postInteraction + ]; + bytes memory allInteractionsConcat = bytes.concat( + interactions.makerAssetSuffix, + interactions.takerAssetSuffix, + interactions.makingAmountData, + interactions.takingAmountData, + interactions.predicate, + interactions.permit, + interactions.preInteraction, + interactions.postInteraction, + customData + ); + + bytes32 offsets = 0; + uint256 sum = 0; + for (uint256 i = 0; i < allInteractions.length; i++) { + if (allInteractions[i].length > 0) { + sum += allInteractions[i].length; + } + offsets |= bytes32(sum << (i * 32)); + } + + bytes memory extension = ""; + if (allInteractionsConcat.length > 0) { + extension = abi.encodePacked(offsets, allInteractionsConcat); + } + if (MakerTraits.unwrap(makerTraits) == 0) { + makerTraits = buildMakerTraits(makerTraitsParams); + } + + uint256 salt = 1; + if (extension.length > 0) { + salt = uint256(keccak256(extension)) & ((1 << 160) - 1); + makerTraits = MakerTraits.wrap(MakerTraits.unwrap(makerTraits) | _HAS_EXTENSION_FLAG); + } + + if (interactions.preInteraction.length > 0) { + makerTraits = MakerTraits.wrap(MakerTraits.unwrap(makerTraits) | _PRE_INTERACTION_CALL_FLAG); + } + + if (interactions.postInteraction.length > 0) { + makerTraits = MakerTraits.wrap(MakerTraits.unwrap(makerTraits) | _POST_INTERACTION_CALL_FLAG); + } + + IOrderMixin.Order memory order = IOrderMixin.Order({ + salt: salt, + maker: Address.wrap(uint160(maker)), + receiver: Address.wrap(uint160(receiver)), + makerAsset: Address.wrap(uint160(makerAsset)), + takerAsset: Address.wrap(uint160(takerAsset)), + makingAmount: makingAmount, + takingAmount: takingAmount, + makerTraits: makerTraits + }); + return (order, extension); + } + + function buidDynamicData( + bytes32 hashlock, + uint256 chainId, + address token, + uint256 srcSafetyDeposit, + uint256 dstSafetyDeposit, + Timelocks timelocks + ) internal pure returns (bytes memory) { + return ( + abi.encode( + hashlock, + chainId, + token, + (srcSafetyDeposit << 128) | dstSafetyDeposit, + timelocks + ) + ); + } + + function prepareDataSrc( + OrderDetails memory orderDetails, + EscrowDetails memory escrowDetails, + address factory, + IOrderMixin limitOrderProtocol + ) internal returns(SwapData memory swapData) { + swapData.extraData = buidDynamicData( + escrowDetails.hashlock, + block.chainid, + orderDetails.dstToken, + orderDetails.srcSafetyDeposit, + orderDetails.dstSafetyDeposit, + escrowDetails.timelocks + ); + + bytes memory whitelist = abi.encodePacked(uint32(block.timestamp)); // auction start time + for (uint256 i = 0; i < orderDetails.resolvers.length; i++) { + whitelist = abi.encodePacked(whitelist, uint80(uint160(orderDetails.resolvers[i])), uint16(0)); // resolver address, time delta + } + + if (escrowDetails.fakeOrder) { + swapData.order = IOrderMixin.Order({ + salt: 0, + maker: Address.wrap(uint160(orderDetails.maker)), + receiver: Address.wrap(uint160(orderDetails.receiver)), + makerAsset: Address.wrap(uint160(address(orderDetails.srcToken))), + takerAsset: Address.wrap(uint160(address(orderDetails.dstToken))), + makingAmount: orderDetails.srcAmount, + takingAmount: orderDetails.dstAmount, + makerTraits: MakerTraits.wrap(0) + }); + } else { + bytes memory postInteractionData = abi.encodePacked( + factory, + orderDetails.resolverFee, + whitelist, + bytes1(0x08) | bytes1(0x01), // 0x08 - whitelist length = 1, 0x01 - turn on resolver fee + swapData.extraData + ); + + bytes memory gettersAmountData = abi.encodePacked(factory, orderDetails.auctionDetails); + + (swapData.order, swapData.extension) = buildOrder( + orderDetails.maker, + orderDetails.receiver, + orderDetails.srcToken, + address(new ERC20True()), + orderDetails.srcAmount, + orderDetails.dstAmount, + MakerTraits.wrap(0), + escrowDetails.allowMultipleFills, + InteractionParams("", "", gettersAmountData, gettersAmountData, "", "", "", postInteractionData), + "" + ); + } + + swapData.orderHash = limitOrderProtocol.hashOrder(swapData.order); + + swapData.immutables = IBaseEscrow.Immutables({ + orderHash: swapData.orderHash, + amount: orderDetails.srcAmount, + maker: Address.wrap(uint160(orderDetails.maker)), + taker: Address.wrap(uint160(orderDetails.resolvers[0])), + token: Address.wrap(uint160(orderDetails.srcToken)), + hashlock: escrowDetails.hashlock, + safetyDeposit: orderDetails.srcSafetyDeposit, + timelocks: escrowDetails.timelocks + }); + + swapData.srcClone = EscrowSrc(BaseEscrowFactory(factory).addressOfEscrowSrc(swapData.immutables)); + // 0x08 - whitelist length = 1, 0x01 - turn on resolver fee + swapData.extraData = abi.encodePacked(orderDetails.resolverFee, whitelist, bytes1(0x08) | bytes1(0x01), swapData.extraData); + } + + function buildDstEscrowImmutables( + bytes32 orderHash, + bytes32 hashlock, + uint256 amount, + address maker, + address taker, + address token, + uint256 safetyDeposit, + Timelocks timelocks + ) internal pure returns (IBaseEscrow.Immutables memory immutables) { + immutables = IBaseEscrow.Immutables({ + orderHash: orderHash, + hashlock: hashlock, + maker: Address.wrap(uint160(maker)), + taker: Address.wrap(uint160(taker)), + token: Address.wrap(uint160(token)), + amount: amount, + safetyDeposit: safetyDeposit, + timelocks: timelocks + }); + } +} diff --git a/test/utils/libraries/TimelocksSettersLib.sol b/test/utils/libraries/TimelocksSettersLib.sol index 0271c6f..1a3e204 100644 --- a/test/utils/libraries/TimelocksSettersLib.sol +++ b/test/utils/libraries/TimelocksSettersLib.sol @@ -2,7 +2,7 @@ pragma solidity 0.8.23; -import { Timelocks, TimelocksLib } from "../../../contracts/libraries/TimelocksLib.sol"; +import { Timelocks, TimelocksLib } from "contracts/libraries/TimelocksLib.sol"; /** * @title Library with setters for Timelocks. diff --git a/test/utils/mocks/TimelocksLibMock.sol b/test/utils/mocks/TimelocksLibMock.sol index 7a3b4de..e80b2f7 100644 --- a/test/utils/mocks/TimelocksLibMock.sol +++ b/test/utils/mocks/TimelocksLibMock.sol @@ -2,7 +2,7 @@ pragma solidity 0.8.23; -import { Timelocks, TimelocksLib } from "../../../contracts/libraries/TimelocksLib.sol"; +import { Timelocks, TimelocksLib } from "contracts/libraries/TimelocksLib.sol"; contract TimelocksLibMock { using TimelocksLib for Timelocks; From e16339000144402113b47d79ccf4bdde04057a14 Mon Sep 17 00:00:00 2001 From: byshape Date: Mon, 22 Jul 2024 18:59:49 +0100 Subject: [PATCH 2/5] Update .gas-snapshot --- .gas-snapshot | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 7fa3d6b..1363440 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -17,34 +17,34 @@ EscrowTest:test_NoCallsWithInvalidImmutables() (gas: 286452) EscrowTest:test_NoCancelByAnyoneDst() (gas: 121678) EscrowTest:test_NoCancelDuringWithdrawalDst() (gas: 121438) EscrowTest:test_NoCancelDuringWithdrawalSrc() (gas: 163938) -EscrowTest:test_NoFailedNativeTokenTransferCancelSrc() (gas: 179280) +EscrowTest:test_NoFailedNativeTokenTransferCancelSrc() (gas: 179317) EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDst() (gas: 154458) EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDstNative() (gas: 83365) -EscrowTest:test_NoFailedNativeTokenTransferWithdrawalSrc() (gas: 312353) +EscrowTest:test_NoFailedNativeTokenTransferWithdrawalSrc() (gas: 312313) EscrowTest:test_NoPublicCancelDuringPrivateCancellationSrc() (gas: 163124) EscrowTest:test_NoPublicWithdrawOutsideOfAllowedPeriodDst() (gas: 126855) EscrowTest:test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() (gas: 169069) EscrowTest:test_NoRescueFundsByAnyoneDst() (gas: 176275) EscrowTest:test_NoRescueFundsByAnyoneSrc() (gas: 209062) EscrowTest:test_NoRescueFundsEarlierDst() (gas: 175685) -EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 208959) -EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 160820) +EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 209019) +EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 160840) EscrowTest:test_NoWithdrawalByNonResolverDst() (gas: 121384) EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodDst() (gas: 126271) EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 169632) EscrowTest:test_NoWithdrawalWithWrongSecretDst() (gas: 122749) -EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 164438) -EscrowTest:test_PublicWithdrawSrc() (gas: 181729) +EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 164468) +EscrowTest:test_PublicWithdrawSrc() (gas: 181745) EscrowTest:test_RescueFundsDst() (gas: 158179) EscrowTest:test_RescueFundsDstNative() (gas: 186642) -EscrowTest:test_RescueFundsSrc() (gas: 195453) +EscrowTest:test_RescueFundsSrc() (gas: 195445) EscrowTest:test_RescueFundsSrcNative() (gas: 197721) EscrowTest:test_WithdrawByAnyoneDst() (gas: 141240) EscrowTest:test_WithdrawByResolverDst() (gas: 142325) EscrowTest:test_WithdrawByResolverDstNative() (gas: 97810) EscrowTest:test_WithdrawByResolverPublicDst() (gas: 141671) -EscrowTest:test_WithdrawSrc() (gas: 186495) -EscrowTest:test_WithdrawSrcTo() (gas: 191343) +EscrowTest:test_WithdrawSrc() (gas: 186512) +EscrowTest:test_WithdrawSrcTo() (gas: 191326) IntegrationEscrowFactoryTest:test_DeployCloneForMakerNonWhitelistedResolverInt() (gas: 473480) IntegrationEscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMakerInt() (gas: 341110) IntegrationResolverMockTest:test_MockCancelDst() (gas: 157108) From c61fac5d6d1100adbbaf5732bcc122fe519531df Mon Sep 17 00:00:00 2001 From: byshape Date: Fri, 26 Jul 2024 12:04:15 +0100 Subject: [PATCH 3/5] Fix imports --- .gas-snapshot | 18 +++++++++--------- test/utils/BaseSetup.sol | 12 ++++++------ test/utils/libraries/CrossChainLib.sol | 8 ++++---- test/utils/libraries/TimelocksSettersLib.sol | 2 +- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 1363440..522d6eb 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,9 +1,9 @@ EscrowFactoryTest:test_MultipleFillsInvalidKey() (gas: 478531) EscrowFactoryTest:test_MultipleFillsInvalidSecretsAmount() (gas: 473846) EscrowFactoryTest:test_NoDeploymentForNotResolver() (gas: 190767) -EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 128468) +EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 128460) EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForTaker() (gas: 27118) -EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 121693) +EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 121696) EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForTaker() (gas: 27377) EscrowFactoryTest:test_NoUnsafeDeploymentForTaker() (gas: 34474) EscrowTest:test_CancelDst() (gas: 116028) @@ -31,14 +31,14 @@ EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 209019) EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 160840) EscrowTest:test_NoWithdrawalByNonResolverDst() (gas: 121384) EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodDst() (gas: 126271) -EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 169632) +EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 169623) EscrowTest:test_NoWithdrawalWithWrongSecretDst() (gas: 122749) EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 164468) -EscrowTest:test_PublicWithdrawSrc() (gas: 181745) +EscrowTest:test_PublicWithdrawSrc() (gas: 181729) EscrowTest:test_RescueFundsDst() (gas: 158179) EscrowTest:test_RescueFundsDstNative() (gas: 186642) EscrowTest:test_RescueFundsSrc() (gas: 195445) -EscrowTest:test_RescueFundsSrcNative() (gas: 197721) +EscrowTest:test_RescueFundsSrcNative() (gas: 197720) EscrowTest:test_WithdrawByAnyoneDst() (gas: 141240) EscrowTest:test_WithdrawByResolverDst() (gas: 142325) EscrowTest:test_WithdrawByResolverDstNative() (gas: 97810) @@ -60,13 +60,13 @@ IntegrationResolverMockTest:test_MockWithdrawToSrc() (gas: 355389) MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllExtra() (gas: 923881) MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllTwoFills() (gas: 922554) MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirst() (gas: 706697) -MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 932899) +MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 932927) MerkleStorageInvalidatorIntTest:test_MultipleFillsFillLast() (gas: 706419) MerkleStorageInvalidatorIntTest:test_MultipleFillsNoDeploymentWithoutValidation() (gas: 301339) -MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 778527) +MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 778503) MerkleStorageInvalidatorIntTest:test_MultipleFillsOddDivision() (gas: 439187) -MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 706707) -MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 921368) +MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 706683) +MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 921346) TimelocksLibTest:test_NoTimelocksOverflow() (gas: 133743) TimelocksLibTest:test_getStartTimestamps() (gas: 16180) TimelocksLibTest:test_setDeployedAt() (gas: 5731) \ No newline at end of file diff --git a/test/utils/BaseSetup.sol b/test/utils/BaseSetup.sol index dbf8650..3fe0648 100644 --- a/test/utils/BaseSetup.sol +++ b/test/utils/BaseSetup.sol @@ -9,12 +9,12 @@ import { IFeeBank } from "limit-order-settlement/contracts/interfaces/IFeeBank.s import { TokenCustomDecimalsMock } from "solidity-utils/contracts/mocks/TokenCustomDecimalsMock.sol"; import { TokenMock } from "solidity-utils/contracts/mocks/TokenMock.sol"; -import { EscrowDst } from "contracts/EscrowDst.sol"; -import { EscrowSrc } from "contracts/EscrowSrc.sol"; -import { BaseEscrowFactory } from "contracts/BaseEscrowFactory.sol"; -import { EscrowFactory } from "contracts/EscrowFactory.sol"; -import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; -import { EscrowFactoryZkSync } from "contracts/zkSync/EscrowFactoryZkSync.sol"; +import { EscrowDst } from "../../contracts/EscrowDst.sol"; +import { EscrowSrc } from "../../contracts/EscrowSrc.sol"; +import { BaseEscrowFactory } from "../../contracts/BaseEscrowFactory.sol"; +import { EscrowFactory } from "../../contracts/EscrowFactory.sol"; +import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; +import { EscrowFactoryZkSync } from "../../contracts/zkSync/EscrowFactoryZkSync.sol"; import { Utils } from "./Utils.sol"; import { CrossChainLib } from "./libraries/CrossChainLib.sol"; import { Timelocks } from "./libraries/TimelocksSettersLib.sol"; diff --git a/test/utils/libraries/CrossChainLib.sol b/test/utils/libraries/CrossChainLib.sol index 5ba05f4..5e2f968 100644 --- a/test/utils/libraries/CrossChainLib.sol +++ b/test/utils/libraries/CrossChainLib.sol @@ -2,10 +2,10 @@ pragma solidity 0.8.23; -import { BaseEscrowFactory } from "contracts/BaseEscrowFactory.sol"; -import { EscrowSrc } from "contracts/EscrowSrc.sol"; -import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; -import { ERC20True } from "contracts/mocks/ERC20True.sol"; +import { BaseEscrowFactory } from "../../../contracts/BaseEscrowFactory.sol"; +import { EscrowSrc } from "../../../contracts/EscrowSrc.sol"; +import { IBaseEscrow } from "../../../contracts/interfaces/IBaseEscrow.sol"; +import { ERC20True } from "../../../contracts/mocks/ERC20True.sol"; import { IOrderMixin } from "limit-order-protocol/contracts/interfaces/IOrderMixin.sol"; import { MakerTraits } from "limit-order-protocol/contracts/libraries/MakerTraitsLib.sol"; import { TakerTraits } from "limit-order-protocol/contracts/libraries/TakerTraitsLib.sol"; diff --git a/test/utils/libraries/TimelocksSettersLib.sol b/test/utils/libraries/TimelocksSettersLib.sol index 1a3e204..0271c6f 100644 --- a/test/utils/libraries/TimelocksSettersLib.sol +++ b/test/utils/libraries/TimelocksSettersLib.sol @@ -2,7 +2,7 @@ pragma solidity 0.8.23; -import { Timelocks, TimelocksLib } from "contracts/libraries/TimelocksLib.sol"; +import { Timelocks, TimelocksLib } from "../../../contracts/libraries/TimelocksLib.sol"; /** * @title Library with setters for Timelocks. From 6672b2f015f5a65ac2d1faff264e6a0aab3600f9 Mon Sep 17 00:00:00 2001 From: byshape Date: Fri, 26 Jul 2024 12:55:48 +0100 Subject: [PATCH 4/5] Update .gas-snapshot --- .gas-snapshot | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 522d6eb..1363440 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,9 +1,9 @@ EscrowFactoryTest:test_MultipleFillsInvalidKey() (gas: 478531) EscrowFactoryTest:test_MultipleFillsInvalidSecretsAmount() (gas: 473846) EscrowFactoryTest:test_NoDeploymentForNotResolver() (gas: 190767) -EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 128460) +EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 128468) EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForTaker() (gas: 27118) -EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 121696) +EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 121693) EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForTaker() (gas: 27377) EscrowFactoryTest:test_NoUnsafeDeploymentForTaker() (gas: 34474) EscrowTest:test_CancelDst() (gas: 116028) @@ -31,14 +31,14 @@ EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 209019) EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 160840) EscrowTest:test_NoWithdrawalByNonResolverDst() (gas: 121384) EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodDst() (gas: 126271) -EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 169623) +EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 169632) EscrowTest:test_NoWithdrawalWithWrongSecretDst() (gas: 122749) EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 164468) -EscrowTest:test_PublicWithdrawSrc() (gas: 181729) +EscrowTest:test_PublicWithdrawSrc() (gas: 181745) EscrowTest:test_RescueFundsDst() (gas: 158179) EscrowTest:test_RescueFundsDstNative() (gas: 186642) EscrowTest:test_RescueFundsSrc() (gas: 195445) -EscrowTest:test_RescueFundsSrcNative() (gas: 197720) +EscrowTest:test_RescueFundsSrcNative() (gas: 197721) EscrowTest:test_WithdrawByAnyoneDst() (gas: 141240) EscrowTest:test_WithdrawByResolverDst() (gas: 142325) EscrowTest:test_WithdrawByResolverDstNative() (gas: 97810) @@ -60,13 +60,13 @@ IntegrationResolverMockTest:test_MockWithdrawToSrc() (gas: 355389) MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllExtra() (gas: 923881) MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllTwoFills() (gas: 922554) MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirst() (gas: 706697) -MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 932927) +MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 932899) MerkleStorageInvalidatorIntTest:test_MultipleFillsFillLast() (gas: 706419) MerkleStorageInvalidatorIntTest:test_MultipleFillsNoDeploymentWithoutValidation() (gas: 301339) -MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 778503) +MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 778527) MerkleStorageInvalidatorIntTest:test_MultipleFillsOddDivision() (gas: 439187) -MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 706683) -MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 921346) +MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 706707) +MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 921368) TimelocksLibTest:test_NoTimelocksOverflow() (gas: 133743) TimelocksLibTest:test_getStartTimestamps() (gas: 16180) TimelocksLibTest:test_setDeployedAt() (gas: 5731) \ No newline at end of file From 90c744378ef63c7e58706ab0b488cc575841e7ff Mon Sep 17 00:00:00 2001 From: byshape Date: Tue, 30 Jul 2024 13:03:51 +0100 Subject: [PATCH 5/5] Rename CrossChainLib -> CrossChainTestLib --- test/integration/EscrowFactory.t.sol | 14 ++--- .../MerkleStorageInvalidator.t.sol | 56 +++++++++---------- test/integration/ResolverMock.t.sol | 24 ++++---- test/libraries/TimelocksLib.t.sol | 6 +- test/unit/Escrow.t.sol | 42 +++++++------- test/unit/EscrowFactory.t.sol | 16 +++--- test/utils/BaseSetup.sol | 26 ++++----- ...rossChainLib.sol => CrossChainTestLib.sol} | 2 +- 8 files changed, 93 insertions(+), 93 deletions(-) rename test/utils/libraries/{CrossChainLib.sol => CrossChainTestLib.sol} (99%) diff --git a/test/integration/EscrowFactory.t.sol b/test/integration/EscrowFactory.t.sol index e440e2e..fd0f03f 100644 --- a/test/integration/EscrowFactory.t.sol +++ b/test/integration/EscrowFactory.t.sol @@ -7,7 +7,7 @@ import { Address } from "solidity-utils/contracts/libraries/AddressLib.sol"; import { IEscrowFactory } from "contracts/interfaces/IEscrowFactory.sol"; import { BaseSetup } from "../utils/BaseSetup.sol"; -import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; +import { CrossChainTestLib } from "../utils/libraries/CrossChainTestLib.sol"; contract IntegrationEscrowFactoryTest is BaseSetup { function setUp() public virtual override { @@ -21,7 +21,7 @@ contract IntegrationEscrowFactoryTest is BaseSetup { uint256 srcSafetyDeposit = uint256(srcAmount) * 10 / 100; uint256 dstSafetyDeposit = uint256(dstAmount) * 10 / 100; - CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcCustom( keccak256(abi.encode(secret)), srcAmount, dstAmount, @@ -35,7 +35,7 @@ contract IntegrationEscrowFactoryTest is BaseSetup { (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -70,7 +70,7 @@ contract IntegrationEscrowFactoryTest is BaseSetup { } function test_DeployCloneForMakerNonWhitelistedResolverInt() public { - CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(false, false); (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; @@ -78,7 +78,7 @@ contract IntegrationEscrowFactoryTest is BaseSetup { swapData.immutables.taker = Address.wrap(uint160(charlie.addr)); address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -117,12 +117,12 @@ contract IntegrationEscrowFactoryTest is BaseSetup { } function test_NoInsufficientBalanceDeploymentForMakerInt() public { - CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(false, false); (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit diff --git a/test/integration/MerkleStorageInvalidator.t.sol b/test/integration/MerkleStorageInvalidator.t.sol index a3f8ef6..213a7e1 100644 --- a/test/integration/MerkleStorageInvalidator.t.sol +++ b/test/integration/MerkleStorageInvalidator.t.sol @@ -8,7 +8,7 @@ import { IEscrowFactory } from "contracts/interfaces/IEscrowFactory.sol"; import { IMerkleStorageInvalidator } from "contracts/interfaces/IMerkleStorageInvalidator.sol"; import { BaseSetup } from "../utils/BaseSetup.sol"; -import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; +import { CrossChainTestLib } from "../utils/libraries/CrossChainTestLib.sol"; contract MerkleStorageInvalidatorIntTest is BaseSetup { uint256 public constant PARTS_AMOUNT = 100; @@ -39,7 +39,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; @@ -50,7 +50,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -103,7 +103,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(partsAmount << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecretsLocal[idx]; swapData.immutables.amount = makingAmount; @@ -114,7 +114,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecretsLocal[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -154,7 +154,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; @@ -165,7 +165,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -206,7 +206,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -236,7 +236,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { function test_MultipleFillsNoDeploymentWithoutValidation() public { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = 0; uint256 makingAmount = MAKING_AMOUNT / PARTS_AMOUNT; @@ -246,7 +246,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -281,7 +281,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; @@ -292,7 +292,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -328,7 +328,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -362,7 +362,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; @@ -373,7 +373,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -411,7 +411,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; @@ -422,7 +422,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -466,7 +466,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -504,7 +504,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; address srcClone = escrowFactory.addressOfEscrowSrc(swapData.immutables); @@ -514,7 +514,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -552,7 +552,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; @@ -563,7 +563,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -605,7 +605,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -645,7 +645,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(PARTS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; @@ -656,7 +656,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -701,7 +701,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedSecrets[idx])); - (TakerTraits takerTraits2, bytes memory args2) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits2, bytes memory args2) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -749,7 +749,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes32 rootPlusAmount = bytes32(secretsAmount << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcCustom( rootPlusAmount, makingAmount, TAKING_AMOUNT, @@ -769,7 +769,7 @@ contract MerkleStorageInvalidatorIntTest is BaseSetup { bytes memory interaction = abi.encodePacked(escrowFactory, abi.encode(root, proof, idx, hashedS[idx])); - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit diff --git a/test/integration/ResolverMock.t.sol b/test/integration/ResolverMock.t.sol index d965329..b74b3b1 100644 --- a/test/integration/ResolverMock.t.sol +++ b/test/integration/ResolverMock.t.sol @@ -9,7 +9,7 @@ import { IEscrowDst } from "contracts/interfaces/IEscrowDst.sol"; import { Timelocks } from "contracts/libraries/TimelocksLib.sol"; import { IResolverMock, ResolverMock } from "contracts/mocks/ResolverMock.sol"; import { BaseSetup } from "../utils/BaseSetup.sol"; -import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; +import { CrossChainTestLib } from "../utils/libraries/CrossChainTestLib.sol"; contract IntegrationResolverMockTest is BaseSetup { /* solhint-disable-next-line private-vars-leading-underscore */ @@ -33,16 +33,16 @@ contract IntegrationResolverMockTest is BaseSetup { function test_MockDeploySrc() public { vm.warp(1710288000); // set current timestamp - (timelocks, timelocksDst) = CrossChainLib.setTimelocks(srcTimelocks, dstTimelocks); + (timelocks, timelocksDst) = CrossChainTestLib.setTimelocks(srcTimelocks, dstTimelocks); resolvers[0] = resolverMock; - CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(false, false); (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -74,12 +74,12 @@ contract IntegrationResolverMockTest is BaseSetup { function test_MockWithdrawToSrc() public { resolvers[0] = resolverMock; - CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(false, false); (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -123,12 +123,12 @@ contract IntegrationResolverMockTest is BaseSetup { function test_MockCancelSrc() public { resolvers[0] = resolverMock; - CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(false, false); (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -175,12 +175,12 @@ contract IntegrationResolverMockTest is BaseSetup { resolvers = new address[](2); resolvers[0] = bob.addr; resolvers[1] = resolverMock; - CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(false, false); (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit @@ -232,12 +232,12 @@ contract IntegrationResolverMockTest is BaseSetup { } function test_MockRescueFundsSrc() public { - CrossChainLib.SwapData memory swapData = _prepareDataSrc(false, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(false, false); (uint8 v, bytes32 r, bytes32 s) = vm.sign(alice.privateKey, swapData.orderHash); bytes32 vs = bytes32((uint256(v - 27) << 255)) | s; - (TakerTraits takerTraits, bytes memory args) = CrossChainLib.buildTakerTraits( + (TakerTraits takerTraits, bytes memory args) = CrossChainTestLib.buildTakerTraits( true, // makingAmount false, // unwrapWeth false, // skipMakerPermit diff --git a/test/libraries/TimelocksLib.t.sol b/test/libraries/TimelocksLib.t.sol index 73ec9c9..a8666ad 100644 --- a/test/libraries/TimelocksLib.t.sol +++ b/test/libraries/TimelocksLib.t.sol @@ -10,7 +10,7 @@ import { Timelocks } from "contracts/libraries/TimelocksLib.sol"; import { TimelocksSettersLib } from "../utils/libraries/TimelocksSettersLib.sol"; import { BaseSetup } from "../utils/BaseSetup.sol"; -import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; +import { CrossChainTestLib } from "../utils/libraries/CrossChainTestLib.sol"; import { TimelocksLibMock } from "../utils/mocks/TimelocksLibMock.sol"; contract TimelocksLibTest is BaseSetup { @@ -53,8 +53,8 @@ contract TimelocksLibTest is BaseSetup { function test_NoTimelocksOverflow() public { vm.warp(1710159521); // make it real, it's 0 in foundry - dstTimelocks = CrossChainLib.DstTimelocks({ withdrawal: 2584807817, publicWithdrawal: 2584807817, cancellation: 1 }); - (timelocks, timelocksDst) = CrossChainLib.setTimelocks(srcTimelocks, dstTimelocks); + dstTimelocks = CrossChainTestLib.DstTimelocks({ withdrawal: 2584807817, publicWithdrawal: 2584807817, cancellation: 1 }); + (timelocks, timelocksDst) = CrossChainTestLib.setTimelocks(srcTimelocks, dstTimelocks); (IBaseEscrow.Immutables memory immutablesDst, uint256 srcCancellationTimestamp, IEscrowDst dstClone) = _prepareDataDst(); diff --git a/test/unit/Escrow.t.sol b/test/unit/Escrow.t.sol index 05c8b59..526a3c8 100644 --- a/test/unit/Escrow.t.sol +++ b/test/unit/Escrow.t.sol @@ -10,7 +10,7 @@ import { IEscrowDst } from "contracts/interfaces/IEscrowDst.sol"; import { NoReceiveCaller } from "contracts/mocks/NoReceiveCaller.sol"; import { BaseSetup } from "../utils/BaseSetup.sol"; -import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; +import { CrossChainTestLib } from "../utils/libraries/CrossChainTestLib.sol"; contract EscrowTest is BaseSetup { // solhint-disable-next-line private-vars-leading-underscore @@ -24,7 +24,7 @@ contract EscrowTest is BaseSetup { // Only resolver can withdraw function test_NoWithdrawalByAnyoneSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -49,7 +49,7 @@ contract EscrowTest is BaseSetup { function test_NoWithdrawalOutsideOfAllowedPeriodSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -98,7 +98,7 @@ contract EscrowTest is BaseSetup { function test_WithdrawSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -134,7 +134,7 @@ contract EscrowTest is BaseSetup { function test_WithdrawSrcTo() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -172,7 +172,7 @@ contract EscrowTest is BaseSetup { function test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -203,7 +203,7 @@ contract EscrowTest is BaseSetup { function test_PublicWithdrawSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -237,7 +237,7 @@ contract EscrowTest is BaseSetup { function test_RescueFundsSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); assertEq(address(swapData.srcClone).balance, 0); @@ -281,7 +281,7 @@ contract EscrowTest is BaseSetup { function test_RescueFundsSrcNative() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); assertEq(address(swapData.srcClone).balance, 0); @@ -326,7 +326,7 @@ contract EscrowTest is BaseSetup { function test_NoRescueFundsEarlierSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); assertEq(address(swapData.srcClone).balance, 0); @@ -368,7 +368,7 @@ contract EscrowTest is BaseSetup { function test_NoRescueFundsByAnyoneSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); assertEq(usdc.balanceOf(address(swapData.srcClone)), 0); assertEq(address(swapData.srcClone).balance, 0); @@ -590,7 +590,7 @@ contract EscrowTest is BaseSetup { function test_NoWithdrawalWithWrongSecretSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -694,7 +694,7 @@ contract EscrowTest is BaseSetup { function test_NoFailedNativeTokenTransferWithdrawalSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -769,7 +769,7 @@ contract EscrowTest is BaseSetup { function test_CancelResolverSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -806,7 +806,7 @@ contract EscrowTest is BaseSetup { function test_CancelResolverSrcReceiver() public { address receiver = charlie.addr; // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcCustom( HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, @@ -863,7 +863,7 @@ contract EscrowTest is BaseSetup { function test_CancelPublicSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -898,7 +898,7 @@ contract EscrowTest is BaseSetup { function test_NoCancelDuringWithdrawalSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -925,7 +925,7 @@ contract EscrowTest is BaseSetup { function test_NoPublicCancelDuringPrivateCancellationSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -952,7 +952,7 @@ contract EscrowTest is BaseSetup { // During non-public cancel period function test_NoAnyoneCancelDuringResolverCancelSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -1083,7 +1083,7 @@ contract EscrowTest is BaseSetup { function test_NoFailedNativeTokenTransferCancelSrc() public { // deploy escrow - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -1109,7 +1109,7 @@ contract EscrowTest is BaseSetup { } function test_NoCallsWithInvalidImmutables() public { - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (IBaseEscrow.Immutables memory immutablesDst, uint256 srcCancellationTimestamp, IBaseEscrow dstClone) = _prepareDataDst(); diff --git a/test/unit/EscrowFactory.t.sol b/test/unit/EscrowFactory.t.sol index 6ceda4a..94886d4 100644 --- a/test/unit/EscrowFactory.t.sol +++ b/test/unit/EscrowFactory.t.sol @@ -11,7 +11,7 @@ import { IBaseEscrow } from "contracts/interfaces/IBaseEscrow.sol"; import { Timelocks, TimelocksLib } from "contracts/libraries/TimelocksLib.sol"; import { BaseSetup } from "../utils/BaseSetup.sol"; -import { CrossChainLib } from "../utils/libraries/CrossChainLib.sol"; +import { CrossChainTestLib } from "../utils/libraries/CrossChainTestLib.sol"; contract EscrowFactoryTest is BaseSetup { using TimelocksLib for Timelocks; @@ -38,7 +38,7 @@ contract EscrowFactoryTest is BaseSetup { vm.assume(srcAmount > 0 && dstAmount > 0); uint256 srcSafetyDeposit = uint256(srcAmount) * 10 / 100; uint256 dstSafetyDeposit = uint256(dstAmount) * 10 / 100; - CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcCustom( keccak256(abi.encode(secret)), srcAmount, dstAmount, @@ -71,7 +71,7 @@ contract EscrowFactoryTest is BaseSetup { function testFuzz_DeployCloneForMakerWithReceiver() public { address receiver = charlie.addr; - CrossChainLib.SwapData memory swapData = _prepareDataSrcCustom( + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcCustom( HASHED_SECRET, MAKING_AMOUNT, TAKING_AMOUNT, @@ -136,7 +136,7 @@ contract EscrowFactoryTest is BaseSetup { } function test_NoInsufficientBalanceNativeDeploymentForMaker() public { - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); usdc.transfer(address(swapData.srcClone), MAKING_AMOUNT); @@ -155,7 +155,7 @@ contract EscrowFactoryTest is BaseSetup { } function test_NoInsufficientBalanceDeploymentForMaker() public { - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -176,7 +176,7 @@ contract EscrowFactoryTest is BaseSetup { // Only whitelisted resolver can deploy escrow function test_NoDeploymentForNotResolver() public { - CrossChainLib.SwapData memory swapData = _prepareDataSrc(true, false); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrc(true, false); (bool success,) = address(swapData.srcClone).call{ value: SRC_SAFETY_DEPOSIT }(""); assertEq(success, true); @@ -241,7 +241,7 @@ contract EscrowFactoryTest is BaseSetup { bytes32 rootPlusAmount = bytes32(uint256(0) << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; @@ -268,7 +268,7 @@ contract EscrowFactoryTest is BaseSetup { bytes32 rootPlusAmount = bytes32(SECRETS_AMOUNT << 240 | uint240(uint256(root))); - CrossChainLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); + CrossChainTestLib.SwapData memory swapData = _prepareDataSrcHashlock(rootPlusAmount, false, true); swapData.immutables.hashlock = hashedSecrets[idx]; swapData.immutables.amount = makingAmount; diff --git a/test/utils/BaseSetup.sol b/test/utils/BaseSetup.sol index 3fe0648..434e844 100644 --- a/test/utils/BaseSetup.sol +++ b/test/utils/BaseSetup.sol @@ -16,7 +16,7 @@ import { EscrowFactory } from "../../contracts/EscrowFactory.sol"; import { IBaseEscrow } from "../../contracts/interfaces/IBaseEscrow.sol"; import { EscrowFactoryZkSync } from "../../contracts/zkSync/EscrowFactoryZkSync.sol"; import { Utils } from "./Utils.sol"; -import { CrossChainLib } from "./libraries/CrossChainLib.sol"; +import { CrossChainTestLib } from "./libraries/CrossChainTestLib.sol"; import { Timelocks } from "./libraries/TimelocksSettersLib.sol"; @@ -52,13 +52,13 @@ contract BaseSetup is Test, Utils { Timelocks internal timelocks; Timelocks internal timelocksDst; - CrossChainLib.SrcTimelocks internal srcTimelocks = CrossChainLib.SrcTimelocks({ + CrossChainTestLib.SrcTimelocks internal srcTimelocks = CrossChainTestLib.SrcTimelocks({ withdrawal: 120, publicWithdrawal: 500, cancellation: 1020, publicCancellation: 1530 }); - CrossChainLib.DstTimelocks internal dstTimelocks = CrossChainLib.DstTimelocks({ + CrossChainTestLib.DstTimelocks internal dstTimelocks = CrossChainTestLib.DstTimelocks({ withdrawal: 300, publicWithdrawal: 540, cancellation: 900 @@ -77,7 +77,7 @@ contract BaseSetup is Test, Utils { function setUp() public virtual { bytes32 profileHash = keccak256(abi.encodePacked(vm.envString("FOUNDRY_PROFILE"))); - if (profileHash == CrossChainLib.ZKSYNC_PROFILE_HASH) isZkSync = true; + if (profileHash == CrossChainTestLib.ZKSYNC_PROFILE_HASH) isZkSync = true; _createUsers(3); alice = users[0]; @@ -95,7 +95,7 @@ contract BaseSetup is Test, Utils { usdc.mint(alice.addr, 1000 ether); inch.mint(bob.addr, 1000 ether); - (timelocks, timelocksDst) = CrossChainLib.setTimelocks(srcTimelocks, dstTimelocks); + (timelocks, timelocksDst) = CrossChainTestLib.setTimelocks(srcTimelocks, dstTimelocks); _deployContracts(); @@ -137,7 +137,7 @@ contract BaseSetup is Test, Utils { vm.label(address(feeBank), "FeeBank"); } - function _prepareDataSrc(bool fakeOrder, bool allowMultipleFills) internal returns(CrossChainLib.SwapData memory) { + function _prepareDataSrc(bool fakeOrder, bool allowMultipleFills) internal returns(CrossChainTestLib.SwapData memory) { return _prepareDataSrcCustom( HASHED_SECRET, MAKING_AMOUNT, @@ -154,7 +154,7 @@ contract BaseSetup is Test, Utils { bytes32 hashlock, bool fakeOrder, bool allowMultipleFills - ) internal returns(CrossChainLib.SwapData memory) { + ) internal returns(CrossChainTestLib.SwapData memory) { return _prepareDataSrcCustom( hashlock, MAKING_AMOUNT, @@ -176,9 +176,9 @@ contract BaseSetup is Test, Utils { address receiver, bool fakeOrder, bool allowMultipleFills - ) internal returns(CrossChainLib.SwapData memory swapData) { - swapData = CrossChainLib.prepareDataSrc( - CrossChainLib.OrderDetails({ + ) internal returns(CrossChainTestLib.SwapData memory swapData) { + swapData = CrossChainTestLib.prepareDataSrc( + CrossChainTestLib.OrderDetails({ maker: alice.addr, receiver: receiver, srcToken: address(usdc), @@ -189,7 +189,7 @@ contract BaseSetup is Test, Utils { dstSafetyDeposit: dstSafetyDeposit, resolvers: resolvers, resolverFee: RESOLVER_FEE, - auctionDetails: CrossChainLib.buildAuctionDetails( + auctionDetails: CrossChainTestLib.buildAuctionDetails( 0, // gasBumpEstimate 0, // gasPriceEstimate uint32(block.timestamp), // startTime @@ -199,7 +199,7 @@ contract BaseSetup is Test, Utils { auctionPoints ) }), - CrossChainLib.EscrowDetails({ + CrossChainTestLib.EscrowDetails({ hashlock: hashlock, timelocks: timelocks, fakeOrder: fakeOrder, @@ -225,7 +225,7 @@ contract BaseSetup is Test, Utils { ) internal view returns (IBaseEscrow.Immutables memory, uint256, EscrowDst) { bytes32 orderHash = bytes32(block.timestamp); // fake order hash uint256 srcCancellationTimestamp = block.timestamp + srcTimelocks.cancellation; - IBaseEscrow.Immutables memory escrowImmutables = CrossChainLib.buildDstEscrowImmutables( + IBaseEscrow.Immutables memory escrowImmutables = CrossChainTestLib.buildDstEscrowImmutables( orderHash, hashlock, amount, diff --git a/test/utils/libraries/CrossChainLib.sol b/test/utils/libraries/CrossChainTestLib.sol similarity index 99% rename from test/utils/libraries/CrossChainLib.sol rename to test/utils/libraries/CrossChainTestLib.sol index 5e2f968..6da903c 100644 --- a/test/utils/libraries/CrossChainLib.sol +++ b/test/utils/libraries/CrossChainTestLib.sol @@ -12,7 +12,7 @@ import { TakerTraits } from "limit-order-protocol/contracts/libraries/TakerTrait import { Address } from "solidity-utils/contracts/libraries/AddressLib.sol"; import { Timelocks, TimelocksSettersLib } from "./TimelocksSettersLib.sol"; -library CrossChainLib { +library CrossChainTestLib { /** * Timelocks for the source chain.