Skip to content

Commit

Permalink
Merge pull request #64 from 1inch/feature/SC-1130-Support-zkSync-create2
Browse files Browse the repository at this point in the history
Support ZkSync deployment
  • Loading branch information
byshape authored Jun 25, 2024
2 parents a9a7fe9 + 9779e66 commit 76c4956
Show file tree
Hide file tree
Showing 35 changed files with 1,130 additions and 825 deletions.
143 changes: 71 additions & 72 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,72 +1,71 @@
EscrowFactoryTest:test_MultipleFillsInvalidKey() (gas: 482641)
EscrowFactoryTest:test_MultipleFillsInvalidSecretsAmount() (gas: 477964)
EscrowFactoryTest:test_NoDeploymentForNotResolver() (gas: 183323)
EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 124894)
EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForTaker() (gas: 22749)
EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 118134)
EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForTaker() (gas: 27489)
EscrowFactoryTest:test_NoUnsafeDeploymentForTaker() (gas: 30208)
EscrowTest:test_CancelDst() (gas: 111159)
EscrowTest:test_CancelDstDifferentTarget() (gas: 145153)
EscrowTest:test_CancelDstWithNativeToken() (gas: 93329)
EscrowTest:test_CancelPublicSrc() (gas: 161908)
EscrowTest:test_CancelResolverSrc() (gas: 165065)
EscrowTest:test_CancelResolverSrcReceiver() (gas: 178454)
EscrowTest:test_NoAnyoneCancelDuringResolverCancelSrc() (gas: 160156)
EscrowTest:test_NoCallsWithInvalidImmutables() (gas: 281927)
EscrowTest:test_NoCancelByAnyoneDst() (gas: 116806)
EscrowTest:test_NoCancelDuringWithdrawalDst() (gas: 116544)
EscrowTest:test_NoCancelDuringWithdrawalSrc() (gas: 160290)
EscrowTest:test_NoFailedNativeTokenTransferCancelDst() (gas: 127614)
EscrowTest:test_NoFailedNativeTokenTransferCancelSrc() (gas: 175675)
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDst() (gas: 149579)
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDstNative() (gas: 83078)
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalSrc() (gas: 196435)
EscrowTest:test_NoPublicCancelDuringPrivateCancellationSrc() (gas: 159449)
EscrowTest:test_NoPublicWithdrawOutsideOfAllowedPeriodDst() (gas: 121984)
EscrowTest:test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() (gas: 165354)
EscrowTest:test_NoRescueFundsByAnyoneDst() (gas: 171487)
EscrowTest:test_NoRescueFundsByAnyoneSrc() (gas: 205531)
EscrowTest:test_NoRescueFundsEarlierDst() (gas: 170904)
EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 205423)
EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 157182)
EscrowTest:test_NoWithdrawalByNonResolverDst() (gas: 116487)
EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodDst() (gas: 121400)
EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 165900)
EscrowTest:test_NoWithdrawalWithWrongSecretDst() (gas: 117874)
EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 160765)
EscrowTest:test_PublicWithdrawSrc() (gas: 178199)
EscrowTest:test_RescueFundsDst() (gas: 153319)
EscrowTest:test_RescueFundsDstNative() (gas: 181867)
EscrowTest:test_RescueFundsSrc() (gas: 191928)
EscrowTest:test_RescueFundsSrcNative() (gas: 194144)
EscrowTest:test_WithdrawByAnyoneDst() (gas: 136371)
EscrowTest:test_WithdrawByResolverDst() (gas: 137456)
EscrowTest:test_WithdrawByResolverDstNative() (gas: 97517)
EscrowTest:test_WithdrawByResolverPublicDst() (gas: 136802)
EscrowTest:test_WithdrawSrc() (gas: 182978)
EscrowTest:test_WithdrawSrcTo() (gas: 190028)
IntegrationEscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMakerInt() (gas: 348391)
IntegrationResolverMockTest:test_MockCancelDst() (gas: 152322)
IntegrationResolverMockTest:test_MockCancelSrc() (gas: 360903)
IntegrationResolverMockTest:test_MockDeployDst() (gas: 146726)
IntegrationResolverMockTest:test_MockDeploySrc() (gas: 371276)
IntegrationResolverMockTest:test_MockPublicCancelSrc() (gas: 369916)
IntegrationResolverMockTest:test_MockPublicWithdrawDst() (gas: 156761)
IntegrationResolverMockTest:test_MockRescueFundsDst() (gas: 156195)
IntegrationResolverMockTest:test_MockRescueFundsSrc() (gas: 389324)
IntegrationResolverMockTest:test_MockWithdrawDst() (gas: 178109)
IntegrationResolverMockTest:test_MockWithdrawToSrc() (gas: 361662)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllExtra() (gas: 931948)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllTwoFills() (gas: 930695)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirst() (gas: 711892)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 939035)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillLast() (gas: 711758)
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoDeploymentWithoutValidation() (gas: 308538)
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 786186)
MerkleStorageInvalidatorIntTest:test_MultipleFillsOddDivision() (gas: 445171)
MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 712010)
MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 929572)
TimelocksLibTest:test_NoTimelocksOverflow() (gas: 128599)
TimelocksLibTest:test_getStartTimestamps() (gas: 16176)
TimelocksLibTest:test_setDeployedAt() (gas: 5743)
EscrowFactoryTest:test_MultipleFillsInvalidKey() (gas: 482528)
EscrowFactoryTest:test_MultipleFillsInvalidSecretsAmount() (gas: 477860)
EscrowFactoryTest:test_NoDeploymentForNotResolver() (gas: 183405)
EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 124810)
EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForTaker() (gas: 22669)
EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 118051)
EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForTaker() (gas: 27405)
EscrowFactoryTest:test_NoUnsafeDeploymentForTaker() (gas: 30092)
EscrowTest:test_CancelDst() (gas: 111139)
EscrowTest:test_CancelDstDifferentTarget() (gas: 142966)
EscrowTest:test_CancelDstWithNativeToken() (gas: 93309)
EscrowTest:test_CancelPublicSrc() (gas: 161885)
EscrowTest:test_CancelResolverSrc() (gas: 165042)
EscrowTest:test_CancelResolverSrcReceiver() (gas: 176271)
EscrowTest:test_NoAnyoneCancelDuringResolverCancelSrc() (gas: 160133)
EscrowTest:test_NoCallsWithInvalidImmutables() (gas: 281906)
EscrowTest:test_NoCancelByAnyoneDst() (gas: 116786)
EscrowTest:test_NoCancelDuringWithdrawalDst() (gas: 116546)
EscrowTest:test_NoCancelDuringWithdrawalSrc() (gas: 160267)
EscrowTest:test_NoFailedNativeTokenTransferCancelSrc() (gas: 175652)
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDst() (gas: 149575)
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDstNative() (gas: 83058)
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalSrc() (gas: 308640)
EscrowTest:test_NoPublicCancelDuringPrivateCancellationSrc() (gas: 159448)
EscrowTest:test_NoPublicWithdrawOutsideOfAllowedPeriodDst() (gas: 121970)
EscrowTest:test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() (gas: 165331)
EscrowTest:test_NoRescueFundsByAnyoneDst() (gas: 171489)
EscrowTest:test_NoRescueFundsByAnyoneSrc() (gas: 205504)
EscrowTest:test_NoRescueFundsEarlierDst() (gas: 170932)
EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 205421)
EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 157159)
EscrowTest:test_NoWithdrawalByNonResolverDst() (gas: 116486)
EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodDst() (gas: 121386)
EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 165892)
EscrowTest:test_NoWithdrawalWithWrongSecretDst() (gas: 117851)
EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 160764)
EscrowTest:test_PublicWithdrawSrc() (gas: 178176)
EscrowTest:test_RescueFundsDst() (gas: 153299)
EscrowTest:test_RescueFundsDstNative() (gas: 181856)
EscrowTest:test_RescueFundsSrc() (gas: 191921)
EscrowTest:test_RescueFundsSrcNative() (gas: 194121)
EscrowTest:test_WithdrawByAnyoneDst() (gas: 136351)
EscrowTest:test_WithdrawByResolverDst() (gas: 137436)
EscrowTest:test_WithdrawByResolverDstNative() (gas: 97497)
EscrowTest:test_WithdrawByResolverPublicDst() (gas: 136782)
EscrowTest:test_WithdrawSrc() (gas: 182955)
EscrowTest:test_WithdrawSrcTo() (gas: 187838)
IntegrationEscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMakerInt() (gas: 343841)
IntegrationResolverMockTest:test_MockCancelDst() (gas: 152294)
IntegrationResolverMockTest:test_MockCancelSrc() (gas: 356315)
IntegrationResolverMockTest:test_MockDeployDst() (gas: 146659)
IntegrationResolverMockTest:test_MockDeploySrc() (gas: 366925)
IntegrationResolverMockTest:test_MockPublicCancelSrc() (gas: 365352)
IntegrationResolverMockTest:test_MockPublicWithdrawDst() (gas: 156609)
IntegrationResolverMockTest:test_MockRescueFundsDst() (gas: 156181)
IntegrationResolverMockTest:test_MockRescueFundsSrc() (gas: 385029)
IntegrationResolverMockTest:test_MockWithdrawDst() (gas: 178066)
IntegrationResolverMockTest:test_MockWithdrawToSrc() (gas: 357321)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllExtra() (gas: 927003)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllTwoFills() (gas: 925492)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirst() (gas: 707474)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 933836)
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillLast() (gas: 707200)
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoDeploymentWithoutValidation() (gas: 304237)
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 781397)
MerkleStorageInvalidatorIntTest:test_MultipleFillsOddDivision() (gas: 440557)
MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 707452)
MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 924404)
TimelocksLibTest:test_NoTimelocksOverflow() (gas: 128425)
TimelocksLibTest:test_getStartTimestamps() (gas: 16180)
TimelocksLibTest:test_setDeployedAt() (gas: 5731)
42 changes: 40 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
- name: Check snapshot
run: forge snapshot --check --no-match-test "testFuzz_*"
run: FOUNDRY_PROFILE=default forge snapshot --check --no-match-test "testFuzz_*"

test:
runs-on: ubuntu-latest
Expand All @@ -36,7 +36,25 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
- name: Run tests
run: forge test -vvv --gas-report
run: FOUNDRY_PROFILE=default forge test -vvv --gas-report

test-zksync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/checkout@v4
with:
repository: matter-labs/foundry-zksync
ref: 'dev'
path: 'foundry-zksync'

- name: Install Foundry for zksync
run: cd foundry-zksync && ./install-foundry-zksync

- name: Run tests
run: FOUNDRY_PROFILE=zksync forge test -vvv --zksync

coverage:
runs-on: ubuntu-latest
Expand All @@ -51,3 +69,23 @@ jobs:
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

coverage-zksync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/checkout@v4
with:
repository: matter-labs/foundry-zksync
ref: 'dev'
path: 'foundry-zksync'

- name: Install Foundry for zksync
run: cd foundry-zksync && ./install-foundry-zksync
- name: Run coverage
run: yarn coverage:zksync
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ build
# foundry
cache_forge
out

# zkSync
zkout
106 changes: 106 additions & 0 deletions contracts/BaseEscrow.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.23;

import { IERC20 } from "openzeppelin-contracts/token/ERC20/IERC20.sol";
import { AddressLib, Address } from "solidity-utils/libraries/AddressLib.sol";
import { SafeERC20 } from "solidity-utils/libraries/SafeERC20.sol";

import { ImmutablesLib } from "./libraries/ImmutablesLib.sol";
import { Timelocks, TimelocksLib } from "./libraries/TimelocksLib.sol";

import { IBaseEscrow } from "./interfaces/IBaseEscrow.sol";

/**
* @title Base abstract Escrow contract for cross-chain atomic swap.
* @dev {IBaseEscrow-withdraw}, {IBaseEscrow-cancel} and _validateImmutables functions must be implemented in the derived contracts.
*/
abstract contract BaseEscrow is IBaseEscrow {
using AddressLib for Address;
using SafeERC20 for IERC20;
using TimelocksLib for Timelocks;
using ImmutablesLib for Immutables;

/// @notice See {IBaseEscrow-RESCUE_DELAY}.
uint256 public immutable RESCUE_DELAY;
/// @notice See {IBaseEscrow-FACTORY}.
address public immutable FACTORY = msg.sender;

constructor(uint32 rescueDelay) {
RESCUE_DELAY = rescueDelay;
}

modifier onlyTaker(Immutables calldata immutables) {
if (msg.sender != immutables.taker.get()) revert InvalidCaller();
_;
}

modifier onlyValidImmutables(Immutables calldata immutables) virtual {
_validateImmutables(immutables);
_;
}

modifier onlyValidSecret(bytes32 secret, Immutables calldata immutables) {
if (_keccakBytes32(secret) != immutables.hashlock) revert InvalidSecret();
_;
}

modifier onlyAfter(uint256 start) {
if (block.timestamp < start) revert InvalidTime();
_;
}

modifier onlyBefore(uint256 stop) {
if (block.timestamp >= stop) revert InvalidTime();
_;
}

/**
* @notice See {IBaseEscrow-rescueFunds}.
*/
function rescueFunds(address token, uint256 amount, Immutables calldata immutables)
external
onlyTaker(immutables)
onlyValidImmutables(immutables)
onlyAfter(immutables.timelocks.rescueStart(RESCUE_DELAY))
{
_uniTransfer(token, msg.sender, amount);
emit FundsRescued(token, amount);
}

/**
* @dev Transfers ERC20 or native tokens to the recipient.
*/
function _uniTransfer(address token, address to, uint256 amount) internal {
if (token == address(0)) {
_ethTransfer(to, amount);
} else {
IERC20(token).safeTransfer(to, amount);
}
}

/**
* @dev Transfers native tokens to the recipient.
*/
function _ethTransfer(address to, uint256 amount) internal {
(bool success,) = to.call{ value: amount }("");
if (!success) revert NativeTokenSendingFailure();
}

/**
* @dev Should verify that the computed escrow address matches the address of this contract.
*/
function _validateImmutables(Immutables calldata immutables) internal view virtual;

/**
* @dev Computes the Keccak-256 hash of the secret.
* @param secret The secret that unlocks the escrow.
* @return ret The computed hash.
*/
function _keccakBytes32(bytes32 secret) private pure returns (bytes32 ret) {
assembly ("memory-safe") {
mstore(0, secret)
ret := keccak256(0, 0x20)
}
}
}
Loading

0 comments on commit 76c4956

Please sign in to comment.