Skip to content

Commit

Permalink
Merge pull request #38 from neodaoist/neo/hyp-80-fix-failing-invarian…
Browse files Browse the repository at this point in the history
…t-tests-and-re-add

✅ Invariants – Replace OptionState ghost vars with actual
  • Loading branch information
neodaoist authored Dec 2, 2023
2 parents 286698f + 1b42311 commit 4b3fcca
Show file tree
Hide file tree
Showing 29 changed files with 679 additions and 348 deletions.
8 changes: 4 additions & 4 deletions .gas-report
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
| balanceOf | 1041 | 1875 | 1808 | 3900 | 1083 |
| batchWriteExisting | 875 | 351424 | 123585 | 819882 | 5 |
| decimals | 395 | 395 | 395 | 395 | 22 |
| exerciseOption | 494 | 37422 | 49682 | 67238 | 67 |
| exerciseOption | 494 | 37785 | 49682 | 67238 | 68 |
| names | 2372 | 2396 | 2421 | 2421 | 109 |
| netOffsetting | 497 | 14286 | 16921 | 16921 | 44 |
| netOffsetting | 497 | 14345 | 16921 | 16921 | 45 |
| option | 2269 | 2279 | 2269 | 2796 | 216 |
| optionTokenId | 2088 | 2487 | 2088 | 4084 | 5 |
| position | 687 | 3368 | 3274 | 5274 | 10 |
| redeemCollateral | 506 | 9281 | 11782 | 16977 | 20 |
| redeemCollateral | 506 | 9541 | 11782 | 16977 | 21 |
| skim | 1680 | 4686 | 4686 | 7693 | 2 |
| skimmable | 1607 | 1607 | 1607 | 1607 | 6 |
| tokenType | 382 | 1074 | 757 | 2717 | 5 |
Expand All @@ -26,7 +26,7 @@
| transfer | 885 | 12719 | 4517 | 26417 | 65 |
| transferFrom | 1224 | 26525 | 29084 | 29236 | 55 |
| writeExisting | 472 | 17037 | 16439 | 124739 | 3045 |
| writeNewCall | 969 | 209842 | 208402 | 314400 | 265 |
| writeNewCall | 969 | 210057 | 208402 | 314400 | 266 |
| writeNewPut | 1013 | 185183 | 210782 | 309644 | 66 |


Expand Down
171 changes: 84 additions & 87 deletions .gas-snapshot

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ jobs:

- name: Run Forge tests
run: |
forge test -vvv
forge test --nmc Invariant -vvv
id: test

- name: Run Forge coverage
run: |
forge coverage --report summary --report lcov
forge coverage --nmc Invariant --report summary --report lcov
- name: Filter coverage files
run: |
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gas:
forge snapshot
forge snapshot --nmc 'Fuzz|Invariant'
forge test --nmc 'Fuzz|Invariant' --gas-report | sed -n '/|/,$$p' | sed '/^Ran /d' > .gas-report

gass:
Expand Down
2 changes: 1 addition & 1 deletion src/ClarityMarkets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ contract ClarityMarkets is
})
});

// Store the base ticker
// Store the shortened ticker
tickers[optionHash] = LibMetadata.paramsToTicker(
assetInfo.baseSymbol,
assetInfo.quoteSymbol,
Expand Down
2 changes: 1 addition & 1 deletion test/BaseExerciseUnitTestSuite.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "./BaseUnitTestSuite.t.sol";

abstract contract BaseExerciseUnitTestSuite is BaseUnitTestSuite {
Expand Down
32 changes: 32 additions & 0 deletions test/BaseTestSuite.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// External Test Helpers
import {Test, console2, stdError} from "forge-std/Test.sol";

// Contracts
import "../src/ClarityMarkets.sol";

abstract contract BaseTestSuite is Test {
/////////

// Contract Under Test
ClarityMarkets internal clarity;

///////// Internal State Helpers

function getInternalOptionState(bytes32 slot)
public
view
returns (uint64, uint64, uint64, uint64)
{
bytes32 state = vm.load(address(clarity), slot);

return (
uint64(uint256(state)),
uint64(uint256(state >> 64)),
uint64(uint256(state >> 128)),
uint64(uint256(state >> 192))
);
}
}
11 changes: 4 additions & 7 deletions test/BaseUnitTestSuite.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ import {Test, console2, stdError} from "forge-std/Test.sol";
// Test Contracts
import {MockERC20} from "./util/MockERC20.sol";

// Test Fixture
import "./BaseTestSuite.t.sol";

// Interfaces
import {IOption} from "../src/interface/option/IOption.sol";
import {IOptionEvents} from "../src/interface/option/IOptionEvents.sol";
import {IOptionErrors} from "../src/interface/option/IOptionErrors.sol";

// Contract Under Test
import "../src/ClarityMarkets.sol";

abstract contract BaseUnitTestSuite is Test {
abstract contract BaseUnitTestSuite is BaseTestSuite {
/////////

using LibPosition for uint256;

/////////

// DCP
ClarityMarkets internal clarity;

// Actors
address internal writer;
address internal writer1;
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Action.Exercise.Amer.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseExerciseUnitTestSuite.t.sol";

contract AmericanExerciseTest is BaseExerciseUnitTestSuite {
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Action.Exercise.Euro.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseExerciseUnitTestSuite.t.sol";

contract EuropeanExerciseTest is BaseExerciseUnitTestSuite {
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Action.NetOff.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

contract NetOffsettingTest is BaseUnitTestSuite {
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Action.Redeem.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

contract RedeemTest is BaseUnitTestSuite {
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Action.Transfer.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

contract TransferTest is BaseUnitTestSuite {
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Action.Write.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

contract WriteTest is BaseUnitTestSuite {
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Adapter.ERC20Factory.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

// Interfaces
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Adapter.WrappedLong.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

// Interfaces
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Adapter.WrappedShort.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

// Interfaces
Expand Down
127 changes: 127 additions & 0 deletions test/concrete/Internal.StateChanges.unit.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Fixture
import "../BaseUnitTestSuite.t.sol";

contract StateChangesTest is BaseUnitTestSuite {
/////////

using LibPosition for uint256;

function test_stateChanges() public {
vm.startPrank(writer);
WETHLIKE.approve(address(clarity), type(uint256).max);
FRAXLIKE.approve(address(clarity), type(uint256).max);
vm.record();
uint256 optionTokenId = clarity.writeNewCall({
baseAsset: address(WETHLIKE),
quoteAsset: address(FRAXLIKE),
expiry: FRI1,
strike: 2050e18,
allowEarlyExercise: true,
optionAmount: 1e6
});
clarity.netOffsetting(optionTokenId, 0.5e6);
clarity.exerciseOption(optionTokenId, 0.45e6);
vm.warp(FRI1 + 1 seconds);
clarity.redeemCollateral(optionTokenId.longToShort());
vm.stopPrank();

(, bytes32[] memory writes) = vm.accesses(address(clarity));

(uint64 w, uint64 n, uint64 x, uint64 r) = getInternalOptionState(writes[5]);

assertEq(w, 1e6);
assertEq(n, 0.5e6);
assertEq(x, 0.45e6);
assertEq(r, 0.5e6);

// console2.log("Amount written", w);
// console2.log("Amount netted", n);
// console2.log("Amount exercised", x);
// console2.log("Amount redeemed", r);

// console2.log(reads.length);
// console2.log(writes.length);

// for (uint256 i = 0; i < writes.length; i++) {
// console2.logBytes32(writes[i]);
// }

// bytes32 state = vm.load(
// address(clarity),
// 0x35e50ad5316a07f423998518dec1bf9bff6be39bc580c77622ff4b474a489eb2
// );
// console2.log("State packed");
// console2.logBytes32(state);

// writeNewCall()
// 1. Option Info and State
// 0x35e50ad5316a07f423998518dec1bf9bff6be39bc580c77622ff4b474a489eb0
// address writeAsset;
// uint64 writeAmount;
// OptionType optionType;
// ExerciseStyle exerciseStyle;
//
// 0x35e50ad5316a07f423998518dec1bf9bff6be39bc580c77622ff4b474a489eb1
// address exerciseAsset;
// uint64 exerciseAmount;
// uint32 expiry;
//
// 0x35e50ad5316a07f423998518dec1bf9bff6be39bc580c77622ff4b474a489eb2
// OptionState optionState;
//
// 2. Shortened Ticker
// 0xe8f58977c73b01eb27ffff969fe1227c62f817fe0a27a00b68d79a092e4aee73
// 3. Base Asset Info
// 0xa36e1c24ba79d81bb60bdfe6ae9bc204457dacfbf6329fc5a823be2c8b70bb40
// 4. Quote Asset Info
// 0xa3c8b8a4608eb61dca040724839500d762e10d5edf37f5e141dbd8bd59f6db2b
// 5. Mint Longs
// 0xb8b95d50487616eb7f989adf66efb8356a820a13599986065c18da7581105764
// 6. Mint Shorts
// 0x64e5819c9c7683c69044f7be8b67f199235a6f5963f834314e626da70c22eecb
// 7. Increment Write Asset Clearing Liability
// 0x4743af99d74bc9527f04d4a794f0718d207b154212be76625538e9a1aa8b8908
//
// (1. External -- Write Asset ERC20 Transfer)

// netOff()
// 1. Update Option State
// 0x35e50ad5316a07f423998518dec1bf9bff6be39bc580c77622ff4b474a489eb2
// 2. Burn Longs
// 0xb8b95d50487616eb7f989adf66efb8356a820a13599986065c18da7581105764
// 3. Burn Shorts
// 0x64e5819c9c7683c69044f7be8b67f199235a6f5963f834314e626da70c22eecb
// 4. Decrement Write Asset Clearing Liability
//
// (1. External -- Write Asset ERC20 Transfer)

// exerciseOption()
// 1. Update Option State
// 0x35e50ad5316a07f423998518dec1bf9bff6be39bc580c77622ff4b474a489eb2
// 2. Burn Longs
// 0xb8b95d50487616eb7f989adf66efb8356a820a13599986065c18da7581105764
// 3. Decrement Write Asset Clearing Liability
// 0x4743af99d74bc9527f04d4a794f0718d207b154212be76625538e9a1aa8b8908
// 4. Increment Exercise Asset Clearing Liability
// 0x6bce9b592bcd9632b463383792a5ec3de1f8ec3cf8b2cd967d5e59ac275b6b4b
//
// (1. External -- Exercise Asset ERC20 Transfer)
// (2. External -- Write Asset ERC20 Transfer)

// redeemCollateral()
// 1. Burn Shorts
// 0x64e5819c9c7683c69044f7be8b67f199235a6f5963f834314e626da70c22eecb
// 2. Update Option State
// 0x35e50ad5316a07f423998518dec1bf9bff6be39bc580c77622ff4b474a489eb2
// 3. Decrement Write Asset Clearing Liability
// 0x4743af99d74bc9527f04d4a794f0718d207b154212be76625538e9a1aa8b8908
// 4. Increment Exercise Asset Clearing Liability
// 0x6bce9b592bcd9632b463383792a5ec3de1f8ec3cf8b2cd967d5e59ac275b6b4b
//
// (1. External -- Exercise Asset ERC20 Transfer)
// (2. External -- Write Asset ERC20 Transfer)
}
}
2 changes: 1 addition & 1 deletion test/concrete/Library.LibMetadata.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

// Libraries
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Library.LibOption.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

// Library Under test
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Library.LibPosition.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

// Library Under test
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/Pool.Skim.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

contract SkimTest is BaseUnitTestSuite {
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/View.Option.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

// Libraries
Expand Down
2 changes: 1 addition & 1 deletion test/concrete/View.Position.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

// Test Harness
// Test Fixture
import "../BaseUnitTestSuite.t.sol";

// Views Under Test
Expand Down
Loading

0 comments on commit 4b3fcca

Please sign in to comment.