Skip to content

Commit faf629a

Browse files
authored
Merge pull request #138 from 1inch/fix/additional-amount-checks
[PT1-97] Fixes after OpenZeppelin audit review
2 parents 366f745 + 911bc65 commit faf629a

File tree

8 files changed

+117
-54
lines changed

8 files changed

+117
-54
lines changed

.gas-snapshot

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
EscrowCancelTest:test_CancelDst() (gas: 123967)
22
EscrowCancelTest:test_CancelDstDifferentTarget() (gas: 151802)
33
EscrowCancelTest:test_CancelDstWithNativeToken() (gas: 101063)
4-
EscrowCancelTest:test_CancelPublicSrc() (gas: 182872)
5-
EscrowCancelTest:test_CancelResolverSrc() (gas: 180725)
6-
EscrowCancelTest:test_CancelResolverSrcReceiver() (gas: 202247)
7-
EscrowCancelTest:test_NoAnyoneCancelDuringResolverCancelSrc() (gas: 174648)
8-
EscrowCancelTest:test_NoCallsWithInvalidImmutables() (gas: 303324)
4+
EscrowCancelTest:test_CancelPublicSrc() (gas: 182952)
5+
EscrowCancelTest:test_CancelResolverSrc() (gas: 180805)
6+
EscrowCancelTest:test_CancelResolverSrcReceiver() (gas: 202327)
7+
EscrowCancelTest:test_NoAnyoneCancelDuringResolverCancelSrc() (gas: 174728)
8+
EscrowCancelTest:test_NoCallsWithInvalidImmutables() (gas: 303404)
99
EscrowCancelTest:test_NoCancelByAnyoneDst() (gas: 128060)
1010
EscrowCancelTest:test_NoCancelDuringWithdrawalDst() (gas: 128407)
11-
EscrowCancelTest:test_NoCancelDuringWithdrawalSrc() (gas: 174945)
12-
EscrowCancelTest:test_NoFailedNativeTokenTransferCancelSrc() (gas: 230224)
13-
EscrowCancelTest:test_NoPublicCallsByAnyone() (gas: 301744)
14-
EscrowCancelTest:test_NoPublicCancelDuringPrivateCancellationSrc() (gas: 179665)
15-
EscrowFactoryTest:test_MultipleFillsInvalidKey() (gas: 482346)
16-
EscrowFactoryTest:test_MultipleFillsInvalidSecretsAmount() (gas: 482090)
11+
EscrowCancelTest:test_NoCancelDuringWithdrawalSrc() (gas: 175025)
12+
EscrowCancelTest:test_NoFailedNativeTokenTransferCancelSrc() (gas: 230304)
13+
EscrowCancelTest:test_NoPublicCallsByAnyone() (gas: 301824)
14+
EscrowCancelTest:test_NoPublicCancelDuringPrivateCancellationSrc() (gas: 179745)
15+
EscrowFactoryTest:test_MultipleFillsInvalidKey() (gas: 482426)
16+
EscrowFactoryTest:test_MultipleFillsInvalidSecretsAmount() (gas: 482170)
1717
EscrowFactoryTest:test_NoDeploymentForNotResolver() (gas: 156992)
18-
EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 139700)
18+
EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMaker() (gas: 139780)
1919
EscrowFactoryTest:test_NoInsufficientBalanceDeploymentForTaker() (gas: 32999)
20-
EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 132574)
20+
EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForMaker() (gas: 132654)
2121
EscrowFactoryTest:test_NoInsufficientBalanceNativeDeploymentForTaker() (gas: 33287)
2222
EscrowFactoryTest:test_NoRescueFundsERC20NotOwner() (gas: 60457)
2323
EscrowFactoryTest:test_NoRescueFundsNativeNotOwner() (gas: 24875)
@@ -26,56 +26,56 @@ EscrowFactoryTest:test_RescueFundsERC20() (gas: 67574)
2626
EscrowFactoryTest:test_RescueFundsNative() (gas: 31067)
2727
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDst() (gas: 263622)
2828
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalDstNative() (gas: 127238)
29-
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalSrc() (gas: 361667)
29+
EscrowTest:test_NoFailedNativeTokenTransferWithdrawalSrc() (gas: 361747)
3030
EscrowTest:test_NoPublicWithdrawOutsideOfAllowedPeriodDst() (gas: 145135)
31-
EscrowTest:test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() (gas: 191532)
31+
EscrowTest:test_NoPublicWithdrawalOutsideOfAllowedPeriodSrc() (gas: 191612)
3232
EscrowTest:test_NoRescueFundsByAnyoneDst() (gas: 251799)
33-
EscrowTest:test_NoRescueFundsByAnyoneSrc() (gas: 223354)
33+
EscrowTest:test_NoRescueFundsByAnyoneSrc() (gas: 223434)
3434
EscrowTest:test_NoRescueFundsEarlierDst() (gas: 251565)
35-
EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 223482)
36-
EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 172308)
35+
EscrowTest:test_NoRescueFundsEarlierSrc() (gas: 223562)
36+
EscrowTest:test_NoWithdrawalByAnyoneSrc() (gas: 172388)
3737
EscrowTest:test_NoWithdrawalByNonResolverDst() (gas: 128322)
3838
EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodDst() (gas: 133787)
39-
EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 181329)
39+
EscrowTest:test_NoWithdrawalOutsideOfAllowedPeriodSrc() (gas: 181409)
4040
EscrowTest:test_NoWithdrawalWithWrongSecretDst() (gas: 129738)
41-
EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 176159)
42-
EscrowTest:test_PublicWithdrawSrc() (gas: 199676)
41+
EscrowTest:test_NoWithdrawalWithWrongSecretSrc() (gas: 176239)
42+
EscrowTest:test_PublicWithdrawSrc() (gas: 199756)
4343
EscrowTest:test_RescueFundsDst() (gas: 231195)
4444
EscrowTest:test_RescueFundsDstNative() (gas: 262234)
45-
EscrowTest:test_RescueFundsSrc() (gas: 209951)
46-
EscrowTest:test_RescueFundsSrcNative() (gas: 212590)
45+
EscrowTest:test_RescueFundsSrc() (gas: 210031)
46+
EscrowTest:test_RescueFundsSrcNative() (gas: 212670)
4747
EscrowTest:test_WithdrawByAnyoneDst() (gas: 220204)
4848
EscrowTest:test_WithdrawByResolverDst() (gas: 213095)
4949
EscrowTest:test_WithdrawByResolverDstNative() (gas: 136994)
5050
EscrowTest:test_WithdrawByResolverPublicDst() (gas: 215234)
51-
EscrowTest:test_WithdrawSrc() (gas: 198985)
52-
EscrowTest:test_WithdrawSrcTo() (gas: 203858)
51+
EscrowTest:test_WithdrawSrc() (gas: 199065)
52+
EscrowTest:test_WithdrawSrcTo() (gas: 203938)
5353
FeeCalcLibTest:test_getFeeAmounts() (gas: 29011)
54-
IntegrationEscrowFactoryTest:test_DeployCloneForMakerNonWhitelistedResolverInt() (gas: 433651)
55-
IntegrationEscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMakerInt() (gas: 355292)
56-
IntegrationEscrowFactoryTest:test_NoResolverReentrancy() (gas: 2354724)
54+
IntegrationEscrowFactoryTest:test_DeployCloneForMakerNonWhitelistedResolverInt() (gas: 433731)
55+
IntegrationEscrowFactoryTest:test_NoInsufficientBalanceDeploymentForMakerInt() (gas: 355372)
56+
IntegrationEscrowFactoryTest:test_NoResolverReentrancy() (gas: 2354804)
5757
IntegrationResolverMockTest:test_MockCancelDst() (gas: 168508)
58-
IntegrationResolverMockTest:test_MockCancelSrc() (gas: 372104)
58+
IntegrationResolverMockTest:test_MockCancelSrc() (gas: 372184)
5959
IntegrationResolverMockTest:test_MockDeployDst() (gas: 162086)
60-
IntegrationResolverMockTest:test_MockDeploySrc() (gas: 381125)
61-
IntegrationResolverMockTest:test_MockPublicCancelSrc() (gas: 416126)
60+
IntegrationResolverMockTest:test_MockDeploySrc() (gas: 381205)
61+
IntegrationResolverMockTest:test_MockPublicCancelSrc() (gas: 416206)
6262
IntegrationResolverMockTest:test_MockPublicWithdrawDst() (gas: 247094)
6363
IntegrationResolverMockTest:test_MockRescueFundsDst() (gas: 174407)
64-
IntegrationResolverMockTest:test_MockRescueFundsSrc() (gas: 401592)
64+
IntegrationResolverMockTest:test_MockRescueFundsSrc() (gas: 401672)
6565
IntegrationResolverMockTest:test_MockWithdrawDst() (gas: 259529)
66-
IntegrationResolverMockTest:test_MockWithdrawToSrc() (gas: 372889)
67-
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllExtra() (gas: 946191)
68-
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllFromLast() (gas: 944854)
69-
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllTwoFills() (gas: 944662)
70-
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirst() (gas: 712231)
71-
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 945481)
72-
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillLast() (gas: 711825)
73-
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoDeploymentWithoutValidation() (gas: 312112)
74-
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoReuseOfSecrets() (gas: 1088778)
75-
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 804290)
76-
MerkleStorageInvalidatorIntTest:test_MultipleFillsOddDivision() (gas: 449479)
77-
MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 712271)
78-
MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 943677)
66+
IntegrationResolverMockTest:test_MockWithdrawToSrc() (gas: 372969)
67+
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllExtra() (gas: 946351)
68+
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllFromLast() (gas: 945014)
69+
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillAllTwoFills() (gas: 944822)
70+
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirst() (gas: 712311)
71+
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillFirstTwoFills() (gas: 945641)
72+
MerkleStorageInvalidatorIntTest:test_MultipleFillsFillLast() (gas: 711905)
73+
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoDeploymentWithoutValidation() (gas: 312192)
74+
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoReuseOfSecrets() (gas: 1088938)
75+
MerkleStorageInvalidatorIntTest:test_MultipleFillsNoSecondDeploymentWithTheSameIndex() (gas: 804450)
76+
MerkleStorageInvalidatorIntTest:test_MultipleFillsOddDivision() (gas: 449559)
77+
MerkleStorageInvalidatorIntTest:test_MultipleFillsOneFill() (gas: 712351)
78+
MerkleStorageInvalidatorIntTest:test_MultipleFillsTwoFills() (gas: 943837)
7979
MerkleStorageInvalidatorTest:test_ShortenedProofIsNotValid() (gas: 237658)
8080
TimelocksLibTest:test_NoTimelocksOverflow() (gas: 264056)
8181
TimelocksLibTest:test_getStartTimestamps() (gas: 19881)

contracts/BaseEscrow.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ abstract contract BaseEscrow is IBaseEscrow {
3535
_ACCESS_TOKEN = accessToken;
3636
}
3737

38-
modifier onlyTaker(address taker) {
39-
if (msg.sender != taker) revert InvalidCaller();
38+
modifier onlyCaller(address expected) {
39+
if (msg.sender != expected) revert InvalidCaller();
4040
_;
4141
}
4242

@@ -70,7 +70,7 @@ abstract contract BaseEscrow is IBaseEscrow {
7070
*/
7171
function rescueFunds(address token, uint256 amount, Immutables calldata immutables)
7272
external
73-
onlyTaker(immutables.taker.get())
73+
onlyCaller(immutables.taker.get())
7474
onlyValidImmutables(immutables.hash())
7575
onlyAfter(immutables.timelocks.rescueStart(RESCUE_DELAY))
7676
{

contracts/BaseEscrowFactory.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ abstract contract BaseEscrowFactory is IEscrowFactory, SimpleSettlement, MerkleS
3535
using SafeERC20 for IERC20;
3636
using TimelocksLib for Timelocks;
3737

38+
error InvalidFeeAmounts();
39+
3840
/// @notice See {IEscrowFactory-ESCROW_SRC_IMPLEMENTATION}.
3941
address public immutable ESCROW_SRC_IMPLEMENTATION;
4042
/// @notice See {IEscrowFactory-ESCROW_DST_IMPLEMENTATION}.
@@ -87,6 +89,8 @@ abstract contract BaseEscrowFactory is IEscrowFactory, SimpleSettlement, MerkleS
8789
extraData[:superArgsLength]
8890
);
8991

92+
if (integratorFeeAmount + protocolFeeAmount >= takingAmount) revert InvalidFeeAmounts();
93+
9094
if (tail.length > 19) {
9195
IPostInteraction(address(bytes20(tail))).postInteraction(
9296
order,

contracts/EscrowDst.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ contract EscrowDst is Escrow, IEscrowDst {
3535
*/
3636
function withdraw(bytes32 secret, Immutables calldata immutables)
3737
external
38-
onlyTaker(immutables.taker.get())
38+
onlyCaller(immutables.taker.get())
3939
onlyAfter(immutables.timelocks.get(TimelocksLib.Stage.DstWithdrawal))
4040
onlyBefore(immutables.timelocks.get(TimelocksLib.Stage.DstCancellation))
4141
{
@@ -63,7 +63,7 @@ contract EscrowDst is Escrow, IEscrowDst {
6363
*/
6464
function cancel(Immutables calldata immutables)
6565
external
66-
onlyTaker(immutables.taker.get())
66+
onlyCaller(immutables.taker.get())
6767
onlyValidImmutables(immutables.hash())
6868
onlyAfter(immutables.timelocks.get(TimelocksLib.Stage.DstCancellation))
6969
{

contracts/EscrowSrc.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ contract EscrowSrc is Escrow, IEscrowSrc {
3737
*/
3838
function withdraw(bytes32 secret, Immutables calldata immutables)
3939
external
40-
onlyTaker(immutables.taker.get())
40+
onlyCaller(immutables.taker.get())
4141
onlyAfter(immutables.timelocks.get(TimelocksLib.Stage.SrcWithdrawal))
4242
onlyBefore(immutables.timelocks.get(TimelocksLib.Stage.SrcCancellation))
4343
{
@@ -52,7 +52,7 @@ contract EscrowSrc is Escrow, IEscrowSrc {
5252
*/
5353
function withdrawTo(bytes32 secret, address target, Immutables calldata immutables)
5454
external
55-
onlyTaker(immutables.taker.get())
55+
onlyCaller(immutables.taker.get())
5656
onlyAfter(immutables.timelocks.get(TimelocksLib.Stage.SrcWithdrawal))
5757
onlyBefore(immutables.timelocks.get(TimelocksLib.Stage.SrcCancellation))
5858
{
@@ -82,7 +82,7 @@ contract EscrowSrc is Escrow, IEscrowSrc {
8282
*/
8383
function cancel(Immutables calldata immutables)
8484
external
85-
onlyTaker(immutables.taker.get())
85+
onlyCaller(immutables.taker.get())
8686
onlyAfter(immutables.timelocks.get(TimelocksLib.Stage.SrcCancellation))
8787
{
8888
_cancel(immutables);

contracts/libraries/ImmutablesLib.sol

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ library ImmutablesLib {
1616
uint256 internal constant IMMUTABLES_SIZE = 0x120;
1717
uint256 internal constant IMMUTABLES_LAST_WORD = 0x100;
1818

19+
/**
20+
* @notice Returns the protocol fee amount from the immutables.
21+
* @param immutables The immutables to extract the fee from.
22+
* @return ret The protocol fee amount.
23+
*/
1924
function protocolFeeAmount(IBaseEscrow.Immutables memory immutables) internal pure returns (uint256 ret) {
2025
bytes memory parameters = immutables.parameters;
2126
if (parameters.length < 0x20) revert IndexOutOfRange();
@@ -24,6 +29,11 @@ library ImmutablesLib {
2429
}
2530
}
2631

32+
/**
33+
* @notice Returns the integrator fee amount from the immutables.
34+
* @param immutables The immutables to extract the fee from.
35+
* @return ret The integrator fee amount.
36+
*/
2737
function integratorFeeAmount(IBaseEscrow.Immutables memory immutables) internal pure returns (uint256 ret) {
2838
bytes memory parameters = immutables.parameters;
2939
if (parameters.length < 0x40) revert IndexOutOfRange();
@@ -32,6 +42,11 @@ library ImmutablesLib {
3242
}
3343
}
3444

45+
/**
46+
* @notice Returns the protocol fee recipient from the immutables.
47+
* @param immutables The immutables to extract the recipient from.
48+
* @return ret The protocol fee recipient.
49+
*/
3550
function protocolFeeRecipient(IBaseEscrow.Immutables memory immutables) internal pure returns (Address ret) {
3651
bytes memory parameters = immutables.parameters;
3752
if (parameters.length < 0x60) revert IndexOutOfRange();
@@ -40,6 +55,11 @@ library ImmutablesLib {
4055
}
4156
}
4257

58+
/**
59+
* @notice Returns the integrator fee recipient from the immutables.
60+
* @param immutables The immutables to extract the recipient from.
61+
* @return ret The integrator fee recipient.
62+
*/
4363
function integratorFeeRecipient(IBaseEscrow.Immutables memory immutables) internal pure returns (Address ret) {
4464
bytes memory parameters = immutables.parameters;
4565
if (parameters.length < 0x80) revert IndexOutOfRange();
@@ -48,6 +68,11 @@ library ImmutablesLib {
4868
}
4969
}
5070

71+
/**
72+
* @notice Returns the protocol fee amount from the immutables (calldata version).
73+
* @param immutables The immutables to extract the fee from.
74+
* @return ret The protocol fee amount.
75+
*/
5176
function protocolFeeAmountCd(IBaseEscrow.Immutables calldata immutables) external pure returns (uint256 ret) {
5277
bytes calldata parameters = immutables.parameters;
5378
if (parameters.length < 0x20) revert IndexOutOfRange();
@@ -56,6 +81,11 @@ library ImmutablesLib {
5681
}
5782
}
5883

84+
/**
85+
* @notice Returns the integrator fee amount from the immutables (calldata version).
86+
* @param immutables The immutables to extract the fee from.
87+
* @return ret The integrator fee amount.
88+
*/
5989
function integratorFeeAmountCd(IBaseEscrow.Immutables calldata immutables) external pure returns (uint256 ret) {
6090
bytes calldata parameters = immutables.parameters;
6191
if (parameters.length < 0x40) revert IndexOutOfRange();
@@ -64,6 +94,11 @@ library ImmutablesLib {
6494
}
6595
}
6696

97+
/**
98+
* @notice Returns the protocol fee recipient from the immutables (calldata version).
99+
* @param immutables The immutables to extract the recipient from.
100+
* @return ret The protocol fee recipient.
101+
*/
67102
function protocolFeeRecipientCd(IBaseEscrow.Immutables calldata immutables) external pure returns (Address ret) {
68103
bytes calldata parameters = immutables.parameters;
69104
if (parameters.length < 0x60) revert IndexOutOfRange();
@@ -72,6 +107,11 @@ library ImmutablesLib {
72107
}
73108
}
74109

110+
/**
111+
* @notice Returns the integrator fee recipient from the immutables (calldata version).
112+
* @param immutables The immutables to extract the recipient from.
113+
* @return ret The integrator fee recipient.
114+
*/
75115
function integratorFeeRecipientCd(IBaseEscrow.Immutables calldata immutables) external pure returns (Address ret) {
76116
bytes calldata parameters = immutables.parameters;
77117
if (parameters.length < 0x80) revert IndexOutOfRange();

foundry.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"lib/murky": {
3+
"rev": "991e371eb1dfa9f86701869eb08ec4e98c3cc0b0"
4+
},
5+
"lib/limit-order-settlement": {
6+
"rev": "78a5a68f4814361332e347071513b3d191a630e1"
7+
},
8+
"lib/openzeppelin-contracts": {
9+
"rev": "6ef73e33866527291e81cf98ddd1ec8b60d7aac8"
10+
},
11+
"lib/solidity-utils": {
12+
"rev": "94611cc4ddb6c7c55f9d20f3093074ecca5440ba"
13+
},
14+
"lib/limit-order-protocol": {
15+
"rev": "b655e74665559450ebf3500c2149b805db8ee229"
16+
},
17+
"lib/forge-std": {
18+
"rev": "60acb7aaadcce2d68e52986a0a66fe79f07d138f"
19+
}
20+
}

test/integration/EscrowFactory.t.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ contract IntegrationEscrowFactoryTest is BaseSetup {
3030
/* solhint-disable func-name-mixedcase */
3131

3232
function testFuzz_DeployCloneForMakerInt(bytes32 secret, uint56 srcAmount, uint56 dstAmount) public {
33-
vm.skip(true);
3433
vm.assume(srcAmount > 0 && dstAmount > 0);
3534
uint256 srcSafetyDeposit = uint256(srcAmount) * 10 / 100;
3635
uint256 dstSafetyDeposit = uint256(dstAmount) * 10 / 100;

0 commit comments

Comments
 (0)