From 3b8dc73cbe55d11b33911abd9761350f9f47d99c Mon Sep 17 00:00:00 2001 From: cranium7811 Date: Wed, 23 Mar 2022 17:35:23 +1100 Subject: [PATCH 1/4] testFuzzExercise() --- src/test/OptionSettlement.t.sol | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/OptionSettlement.t.sol b/src/test/OptionSettlement.t.sol index 0590dff..d6e6a15 100644 --- a/src/test/OptionSettlement.t.sol +++ b/src/test/OptionSettlement.t.sol @@ -328,6 +328,28 @@ contract OptionSettlementTest is DSTest, NFTreceiver { assertEq(engine.balanceOf(address(this), 0), 0); } + function testFuzzExercise(uint16 amountWrite, uint16 amountExercise) + public + { + VM.assume(amountWrite >= amountExercise); + VM.assume(amountWrite > 0); + VM.assume(amountExercise > 0); + VM.assume(amountWrite <= type(uint16).max); + VM.assume(amountExercise <= type(uint16).max); + + engine.write(0, uint256(amountWrite)); + + engine.exercise(0, uint256(amountExercise)); + + assertEq( + engine.balanceOf(address(this), 0), + amountWrite - amountExercise + ); + assertEq(engine.balanceOf(address(this), 1), 1); + + // TODO(Wallet balance checks) + } + // TODO(a function to create random new chains for fuzzing) // TODO(testFuzzExercise) // TODO(testFuzzRedeem) From de83d916b6c5847b9a0523608b2db5b70cbaeeae Mon Sep 17 00:00:00 2001 From: cranium7811 Date: Wed, 23 Mar 2022 17:53:24 +1100 Subject: [PATCH 2/4] some more checks --- src/test/OptionSettlement.t.sol | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/OptionSettlement.t.sol b/src/test/OptionSettlement.t.sol index d6e6a15..4e61873 100644 --- a/src/test/OptionSettlement.t.sol +++ b/src/test/OptionSettlement.t.sol @@ -218,10 +218,11 @@ contract OptionSettlementTest is DSTest, NFTreceiver { function testWrite() public { uint256 nextTokenId = engine.nextTokenId(); - uint256 wethBalance = IERC20(weth).balanceOf(address(engine)); + uint256 wethBalanceEngine = IERC20(weth).balanceOf(address(engine)); uint256 testWallet = IERC20(weth).balanceOf( address(0x36273803306a3C22bc848f8Db761e974697ece0d) ); + uint256 wethBalance = IERC20(weth).balanceOf(address(this)); uint256 rxAmount = 10000 * 1 ether; uint256 fee = ((rxAmount / 10000) * engine.feeBps()); @@ -237,7 +238,7 @@ contract OptionSettlementTest is DSTest, NFTreceiver { assertEq( IERC20(weth).balanceOf(address(engine)), - wethBalance + rxAmount + wethBalanceEngine + rxAmount ); assertEq( IERC20(weth).balanceOf( @@ -245,6 +246,10 @@ contract OptionSettlementTest is DSTest, NFTreceiver { ), testWallet + fee ); + assertEq( + IERC20(weth).balanceOf(address(this)), + wethBalance - rxAmount - fee + ); assertEq(engine.balanceOf(address(this), 0), 10000); assertEq(engine.balanceOf(address(this), 1), 1); @@ -326,6 +331,7 @@ contract OptionSettlementTest is DSTest, NFTreceiver { (daiBalance - rxAmount - fee) ); assertEq(engine.balanceOf(address(this), 0), 0); + assertEq(engine.balanceOf(address(this), 1), 1); } function testFuzzExercise(uint16 amountWrite, uint16 amountExercise) From 30d4ea2a7287cae72a10bfe61108e36a40239aab Mon Sep 17 00:00:00 2001 From: cranium7811 Date: Wed, 23 Mar 2022 22:50:38 +1100 Subject: [PATCH 3/4] balance checks in testFuzzExercise --- src/test/OptionSettlement.t.sol | 74 +++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/src/test/OptionSettlement.t.sol b/src/test/OptionSettlement.t.sol index 4e61873..f5065fd 100644 --- a/src/test/OptionSettlement.t.sol +++ b/src/test/OptionSettlement.t.sol @@ -92,6 +92,9 @@ contract OptionSettlementTest is DSTest, NFTreceiver { // And 10 M WETH writeTokenBalance(address(this), address(weth), 10000000 * 1e18); + writeTokenBalance(address(engine), address(dai), 1000000000 * 1e18); + writeTokenBalance(address(engine), address(weth), 10000000 * 1e18); + // Issue approvals IERC20(weth).approve(address(engine), type(uint256).max); IERC20(dai).approve(address(engine), type(uint256).max); @@ -219,9 +222,7 @@ contract OptionSettlementTest is DSTest, NFTreceiver { function testWrite() public { uint256 nextTokenId = engine.nextTokenId(); uint256 wethBalanceEngine = IERC20(weth).balanceOf(address(engine)); - uint256 testWallet = IERC20(weth).balanceOf( - address(0x36273803306a3C22bc848f8Db761e974697ece0d) - ); + uint256 wethFeeTo = IERC20(weth).balanceOf(address(engine.feeTo())); uint256 wethBalance = IERC20(weth).balanceOf(address(this)); uint256 rxAmount = 10000 * 1 ether; @@ -241,10 +242,8 @@ contract OptionSettlementTest is DSTest, NFTreceiver { wethBalanceEngine + rxAmount ); assertEq( - IERC20(weth).balanceOf( - address(0x36273803306a3C22bc848f8Db761e974697ece0d) - ), - testWallet + fee + IERC20(weth).balanceOf(address(engine.feeTo())), + wethFeeTo + fee ); assertEq( IERC20(weth).balanceOf(address(this)), @@ -296,9 +295,7 @@ contract OptionSettlementTest is DSTest, NFTreceiver { function testExercise() public { uint256 wethBalanceEngine = IERC20(weth).balanceOf(address(engine)); uint256 daiBalanceEngine = IERC20(dai).balanceOf(address(engine)); - uint256 daiTestWallet = IERC20(dai).balanceOf( - address(0x36273803306a3C22bc848f8Db761e974697ece0d) - ); + uint256 daiFeeTo = IERC20(dai).balanceOf(address(engine.feeTo())); uint256 rxAmount = 10 * 3000 ether; uint256 txAmount = 10 * 1 ether; @@ -312,10 +309,8 @@ contract OptionSettlementTest is DSTest, NFTreceiver { engine.exercise(0, 10); assertEq( - IERC20(dai).balanceOf( - address(0x36273803306a3C22bc848f8Db761e974697ece0d) - ), - daiTestWallet + fee + IERC20(dai).balanceOf(address(engine.feeTo())), + daiFeeTo + fee ); assertEq(IERC20(weth).balanceOf(address(engine)), wethBalanceEngine); assertEq( @@ -334,26 +329,63 @@ contract OptionSettlementTest is DSTest, NFTreceiver { assertEq(engine.balanceOf(address(this), 1), 1); } - function testFuzzExercise(uint16 amountWrite, uint16 amountExercise) + function testFuzzExercise(uint256 amountWrite, uint256 amountExercise) public { + uint256 wethBalanceEngine = IERC20(weth).balanceOf(address(engine)); + uint256 daiBalanceEngine = IERC20(dai).balanceOf(address(engine)); + uint256 wethFeeTo = IERC20(weth).balanceOf(address(engine.feeTo())); + uint256 daiFeeTo = IERC20(dai).balanceOf(address(engine.feeTo())); + + VM.assume(amountWrite <= wethBalanceEngine / 1 ether); + VM.assume(amountExercise <= daiBalanceEngine / 3000 ether); VM.assume(amountWrite >= amountExercise); VM.assume(amountWrite > 0); VM.assume(amountExercise > 0); - VM.assume(amountWrite <= type(uint16).max); - VM.assume(amountExercise <= type(uint16).max); + VM.assume(amountWrite <= type(uint256).max); + VM.assume(amountExercise <= type(uint256).max); - engine.write(0, uint256(amountWrite)); + uint256 rxAmount = amountExercise * 3000 ether; + uint256 txAmount = amountExercise * 1 ether; + uint256 exerciseFee = (rxAmount / 10000) * engine.feeBps(); + uint256 writeFee = ((amountWrite * 1 ether) / 10000) * engine.feeBps(); - engine.exercise(0, uint256(amountExercise)); + engine.write(0, amountWrite); + uint256 wethBalance = IERC20(weth).balanceOf(address(this)); + uint256 daiBalance = IERC20(dai).balanceOf(address(this)); + + engine.exercise(0, amountExercise); + + assertEq( + IERC20(weth).balanceOf(address(engine.feeTo())), + wethFeeTo + writeFee + ); + assertEq( + IERC20(dai).balanceOf(address(engine.feeTo())), + daiFeeTo + exerciseFee + ); + assertEq( + IERC20(weth).balanceOf(address(engine)), + wethBalanceEngine + ((amountWrite - amountExercise) * 1 ether) + ); + assertEq( + IERC20(dai).balanceOf(address(engine)), + daiBalanceEngine + rxAmount + ); + assertEq( + IERC20(weth).balanceOf(address(this)), + (wethBalance + txAmount) + ); + assertEq( + IERC20(dai).balanceOf(address(this)), + (daiBalance - rxAmount - exerciseFee) + ); assertEq( engine.balanceOf(address(this), 0), amountWrite - amountExercise ); assertEq(engine.balanceOf(address(this), 1), 1); - - // TODO(Wallet balance checks) } // TODO(a function to create random new chains for fuzzing) From 4b687b98562b637a2767f2ca53c5208ab3c352f0 Mon Sep 17 00:00:00 2001 From: cranium7811 Date: Wed, 23 Mar 2022 23:14:23 +1100 Subject: [PATCH 4/4] additional checks in testFuzzWrite --- src/test/OptionSettlement.t.sol | 35 ++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/test/OptionSettlement.t.sol b/src/test/OptionSettlement.t.sol index f5065fd..728ba31 100644 --- a/src/test/OptionSettlement.t.sol +++ b/src/test/OptionSettlement.t.sol @@ -262,13 +262,20 @@ contract OptionSettlementTest is DSTest, NFTreceiver { assertTrue(true); } - function testFuzzWrite(uint16 amount) public { - VM.assume(amount > 0); - VM.assume(amount <= type(uint16).max); - + function testFuzzWrite(uint256 amountWrite) public { uint256 nextTokenId = engine.nextTokenId(); + uint256 wethBalanceEngine = IERC20(weth).balanceOf(address(engine)); + uint256 wethFeeTo = IERC20(weth).balanceOf(address(engine.feeTo())); + uint256 wethBalance = IERC20(weth).balanceOf(address(this)); + + VM.assume(amountWrite <= wethBalanceEngine / 1 ether); + VM.assume(amountWrite <= type(uint256).max); + VM.assume(amountWrite > 0); + + uint256 rxAmount = amountWrite * 1 ether; + uint256 fee = ((rxAmount / 10000) * engine.feeBps()); - engine.write(0, uint256(amount)); + engine.write(0, amountWrite); ( uint256 option, @@ -277,14 +284,25 @@ contract OptionSettlementTest is DSTest, NFTreceiver { bool claimed ) = engine.claim(nextTokenId); - // TODO(Wallet balance checks) + assertEq( + IERC20(weth).balanceOf(address(engine)), + wethBalanceEngine + rxAmount + ); + assertEq( + IERC20(weth).balanceOf(address(engine.feeTo())), + wethFeeTo + fee + ); + assertEq( + IERC20(weth).balanceOf(address(this)), + wethBalance - rxAmount - fee + ); - assertEq(engine.balanceOf(address(this), 0), amount); + assertEq(engine.balanceOf(address(this), 0), amountWrite); assertEq(engine.balanceOf(address(this), 1), 1); assertTrue(!claimed); assertEq(option, 0); - assertEq(amountWritten, amount); + assertEq(amountWritten, amountWrite); assertEq(amountExercised, 0); assertEq(engine.nextTokenId(), nextTokenId + 1); @@ -389,6 +407,5 @@ contract OptionSettlementTest is DSTest, NFTreceiver { } // TODO(a function to create random new chains for fuzzing) - // TODO(testFuzzExercise) // TODO(testFuzzRedeem) }