From 395c6a687fb2ab594415ff4e553b59f990dc9938 Mon Sep 17 00:00:00 2001 From: outdoteth Date: Tue, 17 May 2022 18:33:50 +0100 Subject: [PATCH 1/2] add check to make sure token cannot be cally contract --- src/Cally.sol | 1 + test/units/BuyOption.t.sol | 2 +- test/units/CreateVault.t.sol | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Cally.sol b/src/Cally.sol index 9e82f97..8cf90e7 100644 --- a/src/Cally.sol +++ b/src/Cally.sol @@ -202,6 +202,7 @@ contract Cally is CallyNft, ReentrancyGuard, Ownable, ERC721TokenReceiver { require(durationDays > 0, "durationDays too small"); require(tokenType == TokenType.ERC721 || tokenType == TokenType.ERC20, "Invalid token type"); require(token.code.length > 0, "token is not contract"); + require(token != address(this), "token cannot be Cally contract"); Vault memory vault = Vault({ tokenIdOrAmount: tokenIdOrAmount, diff --git a/test/units/BuyOption.t.sol b/test/units/BuyOption.t.sol index 214f178..f5ceeac 100644 --- a/test/units/BuyOption.t.sol +++ b/test/units/BuyOption.t.sol @@ -203,7 +203,7 @@ contract TestBuyOption is Fixture { c.buyOption{value: premium}(2); } - function testItCannotBuyOptionIfValueIsGreaterThanPremium() { + function testItCannotBuyOptionIfValueIsGreaterThanPremium() public { // act vm.expectRevert("Incorrect ETH amount sent"); c.buyOption{value: premium}(vaultId); diff --git a/test/units/CreateVault.t.sol b/test/units/CreateVault.t.sol index e455426..8330e83 100644 --- a/test/units/CreateVault.t.sol +++ b/test/units/CreateVault.t.sol @@ -146,6 +146,15 @@ contract TestCreateVault is Test, Fixture { c.createVault(1, address(0xdeadbeef), 1, 1, 1, 0, Cally.TokenType.ERC20); } + function testItCannotCreateVaultWithCallyNFTAsAsset() public { + // arrange + c.setApprovalForAll(address(c), true); + + // act + vm.expectRevert("token cannot be Cally contract"); + c.createVault(3, address(c), 1, 1, 1, 0, Cally.TokenType.ERC721); + } + function testItCreatesVault( uint8 premiumIndex, uint8 durationDays, From 11ac9a88a3e51ea2dc9d2bca98c0bf65874e2f55 Mon Sep 17 00:00:00 2001 From: outdoteth Date: Tue, 17 May 2022 18:35:49 +0100 Subject: [PATCH 2/2] update snapshot --- .gas-snapshot | 58 ++++++++++++++++++++------------------ test/units/BuyOption.t.sol | 2 +- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 4ae58c3..9d47b08 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -2,34 +2,36 @@ TestAdmin:testCannotLetNonAdminWithdrawProtocolFees() (gas: 13034) TestAdmin:testItCannotLetNonAdminSetFee() (gas: 13105) TestAdmin:testItCannotSetFeeHigherThanThreshold() (gas: 10714) TestAdmin:testItSetsFee() (gas: 30916) -TestBuyOption:testItCannotBuyIfAuctionHasNotStarted() (gas: 37109) -TestBuyOption:testItCannotBuyIfVaultHasAlreadyBeenExercised() (gas: 164233) -TestBuyOption:testItCannotBuyIfVaultIsWithdrawing() (gas: 39400) -TestBuyOption:testItCannotBuyOptionTwice() (gas: 131235) -TestBuyOption:testItCreditsPremiumToBeneficiary() (gas: 146784) -TestBuyOption:testItEmitsBoughtOptionEvent() (gas: 123777) -TestBuyOption:testItIncrementsVaultOwnersUncollectedPremiums() (gas: 121562) -TestBuyOption:testItMintsOptionERC721ToBuyer() (gas: 118087) -TestBuyOption:testItOnlyBuysValidFromValidVaultId() (gas: 26603) -TestBuyOption:testItSendsPremiumETHToContract() (gas: 117301) -TestBuyOption:testItSetsStrikeTo0AfterAuctionEnd() (gas: 104464) -TestBuyOption:testItSetsStrikeToCurrentDutchAuctionPrice() (gas: 123442) -TestBuyOption:testItSetsStrikeToCurrentDutchAuctionPriceAfterElapsedTime() (gas: 129322) -TestBuyOption:testItSetsStrikeToReserveIfDutchAuctionStrikeIsSmaller() (gas: 277726) -TestBuyOption:testItUpdatesExpiration() (gas: 123470) +TestBuyOption:testItCannotBuyIfAuctionHasNotStarted() (gas: 37128) +TestBuyOption:testItCannotBuyIfVaultHasAlreadyBeenExercised() (gas: 164230) +TestBuyOption:testItCannotBuyIfVaultIsWithdrawing() (gas: 39422) +TestBuyOption:testItCannotBuyOptionIfValueIsGreaterThanPremium() (gas: 36666) +TestBuyOption:testItCannotBuyOptionTwice() (gas: 131187) +TestBuyOption:testItCreditsPremiumToBeneficiary() (gas: 146781) +TestBuyOption:testItEmitsBoughtOptionEvent() (gas: 123774) +TestBuyOption:testItIncrementsVaultOwnersUncollectedPremiums() (gas: 121559) +TestBuyOption:testItMintsOptionERC721ToBuyer() (gas: 118084) +TestBuyOption:testItOnlyBuysValidFromValidVaultId() (gas: 26559) +TestBuyOption:testItSendsPremiumETHToContract() (gas: 117298) +TestBuyOption:testItSetsStrikeTo0AfterAuctionEnd() (gas: 104461) +TestBuyOption:testItSetsStrikeToCurrentDutchAuctionPrice() (gas: 123439) +TestBuyOption:testItSetsStrikeToCurrentDutchAuctionPriceAfterElapsedTime() (gas: 129341) +TestBuyOption:testItSetsStrikeToReserveIfDutchAuctionStrikeIsSmaller() (gas: 277785) +TestBuyOption:testItUpdatesExpiration() (gas: 123467) +TestCreateVault:testItCannotCreateVaultWithCallyNFTAsAsset() (gas: 41171) TestCreateVault:testItCannotCreateVaultWithInvalidDurationDays() (gas: 17958) TestCreateVault:testItCannotCreateVaultWithInvalidPremium() (gas: 13514) TestCreateVault:testItCannotCreateVaultWithInvalidReserveStrike() (gas: 17929) TestCreateVault:testItCannotCreateVaultWithInvalidStrike() (gas: 15621) TestCreateVault:testItCannotCreateVaultWithNonContractTokenAddress() (gas: 18577) -TestCreateVault:testItCreatesMultipleVaults() (gas: 437863) -TestCreateVault:testItCreatesVaultDetails() (gas: 168318) -TestCreateVault:testItEmitsNewVaultEvent() (gas: 148148) -TestCreateVault:testItIncrementsVaultId() (gas: 144310) -TestCreateVault:testItIncrementsVaultIdMultipleTimes() (gas: 324779) -TestCreateVault:testItMintsVaultERC721ToCreator() (gas: 144467) -TestCreateVault:testItSendsERC20ForCollateral() (gas: 169656) -TestCreateVault:testItSendsERC721ForCollateral() (gas: 145878) +TestCreateVault:testItCreatesMultipleVaults() (gas: 437983) +TestCreateVault:testItCreatesVaultDetails() (gas: 168358) +TestCreateVault:testItEmitsNewVaultEvent() (gas: 148233) +TestCreateVault:testItIncrementsVaultId() (gas: 144350) +TestCreateVault:testItIncrementsVaultIdMultipleTimes() (gas: 324899) +TestCreateVault:testItMintsVaultERC721ToCreator() (gas: 144507) +TestCreateVault:testItSendsERC20ForCollateral() (gas: 169696) +TestCreateVault:testItSendsERC721ForCollateral() (gas: 145918) TestExercise:testCannotExerciseExpiredOption() (gas: 33805) TestExercise:testCannotExerciseInvalidOptionId() (gas: 19569) TestExercise:testCannotExerciseOptionTwice() (gas: 94575) @@ -39,10 +41,10 @@ TestExercise:testItEmitsExercisedEvent() (gas: 88120) TestExercise:testItIncrementsEthBalanceOfVaultOwner() (gas: 88323) TestExercise:testItShouldBurnOptionERC721() (gas: 90200) TestExercise:testItShouldMarkVaultAsExercised() (gas: 89811) -TestExercise:testItShouldTransferERC20ToOptionOwner() (gas: 248948) +TestExercise:testItShouldTransferERC20ToOptionOwner() (gas: 248985) TestExercise:testItShouldTransferERC721ToOptionOwner() (gas: 91389) TestFees:testItDoesNotIncrementUnclaimedFeesIfFeeRateIs0() (gas: 90190) -TestFees:testItIncrementsProtocolUnclaimedFees() (gas: 332476) +TestFees:testItIncrementsProtocolUnclaimedFees() (gas: 332513) TestFees:testItWithdrawsProtocolFees() (gas: 116799) TestGetDutchAuctionStrike:testItReturnsMidStrike() (gas: 8858) TestGetDutchAuctionStrike:testItReturnsReserveStrikeIfAuctionHasEnded() (gas: 6486) @@ -65,11 +67,11 @@ TestVaultBeneficiary:testItSetsVaultBeneficiaryAsVaultOwnerIfNotSet() (gas: 1412 TestWithdraw:testCannotInitiateWithdrawalForInvalidVaultId() (gas: 15831) TestWithdraw:testItBurnsOptionERC721() (gas: 86399) TestWithdraw:testItBurnsVaultERC721() (gas: 86423) -TestWithdraw:testItCannotWithdrawIfOptionIsExercised() (gas: 164544) +TestWithdraw:testItCannotWithdrawIfOptionIsExercised() (gas: 164541) TestWithdraw:testItCannotWithdrawIfVaultIsNotInWithdrawableState() (gas: 27236) TestWithdraw:testItCannotWithdrawTwice() (gas: 87119) TestWithdraw:testItCannotWithdrawVaultYouDontOwn() (gas: 20599) -TestWithdraw:testItCannotWithdrawWhileOptionIsActive() (gas: 134404) +TestWithdraw:testItCannotWithdrawWhileOptionIsActive() (gas: 134401) TestWithdraw:testItEmitsWithdrawalEvent() (gas: 84437) -TestWithdraw:testItTransfersERC20BackToOwner() (gas: 139984) +TestWithdraw:testItTransfersERC20BackToOwner() (gas: 140016) TestWithdraw:testItTransfersERC721BackToOwner() (gas: 90031) diff --git a/test/units/BuyOption.t.sol b/test/units/BuyOption.t.sol index f5ceeac..9fde7a0 100644 --- a/test/units/BuyOption.t.sol +++ b/test/units/BuyOption.t.sol @@ -206,7 +206,7 @@ contract TestBuyOption is Fixture { function testItCannotBuyOptionIfValueIsGreaterThanPremium() public { // act vm.expectRevert("Incorrect ETH amount sent"); - c.buyOption{value: premium}(vaultId); + c.buyOption{value: premium + 1}(vaultId); } function testItBuysOption(uint256 vaultId_) public {