Skip to content

Commit

Permalink
✅ DRY up writeNew handler fn; reduce OptionHandler contract size
Browse files Browse the repository at this point in the history
  • Loading branch information
neodaoist committed Dec 2, 2023
1 parent 75bf9f4 commit 1b42311
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 96 deletions.
20 changes: 10 additions & 10 deletions test/concrete/View.Rebasing.unit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ contract RebasingTest is BaseUnitTestSuite {

uint64 internal constant SOME_WRITTEN = 10.000001e6;
uint64 internal constant MANY_WRITTEN = 6_000_000e6;
uint64 internal constant MAX_WRITTEN = 1_800_000_000_000e6; // max writable on any
// option contract
uint64 internal MAX_WRITTEN;

uint64[] internal writeGivens;

Expand All @@ -25,6 +24,7 @@ contract RebasingTest is BaseUnitTestSuite {
deal(address(FRAXLIKE), writer, type(uint256).max);

// setup write givens for use in Scenarios A-J
MAX_WRITTEN = clarity.MAXIMUM_WRITABLE();
writeGivens.push(SOME_WRITTEN);
writeGivens.push(MANY_WRITTEN);
writeGivens.push(MAX_WRITTEN);
Expand Down Expand Up @@ -212,7 +212,7 @@ contract RebasingTest is BaseUnitTestSuite {
for (uint256 i = 0; i < writeGivens.length; i++) {
// Given
uint64 amountWritten = writeGivens[i];
uint64 amountNetted = (amountWritten * 4) / 5;
uint64 amountNetted = uint64((uint256(amountWritten) * 4) / 5);

vm.startPrank(writer);
WETHLIKE.approve(address(clarity), type(uint256).max);
Expand Down Expand Up @@ -350,7 +350,7 @@ contract RebasingTest is BaseUnitTestSuite {
for (uint256 i = 0; i < writeGivens.length; i++) {
// Given
uint64 amountWritten = writeGivens[i];
uint64 amountNetted = (amountWritten * 4) / 5;
uint64 amountNetted = uint64((uint256(amountWritten) * 4) / 5);
uint64 amountExercised = amountWritten / 5;

vm.startPrank(writer);
Expand Down Expand Up @@ -390,7 +390,7 @@ contract RebasingTest is BaseUnitTestSuite {
for (uint256 i = 0; i < writeGivens.length; i++) {
// Given
uint64 amountWritten = writeGivens[i];
uint64 amountExercised = (amountWritten * 4) / 5;
uint64 amountExercised = uint64((uint256(amountWritten) * 4) / 5);

vm.startPrank(writer);
WETHLIKE.approve(address(clarity), type(uint256).max);
Expand Down Expand Up @@ -428,7 +428,7 @@ contract RebasingTest is BaseUnitTestSuite {
// Given
uint64 amountWritten = writeGivens[i];
uint64 amountNetted = amountWritten / 5;
uint64 amountExercised = (amountWritten * 4) / 5;
uint64 amountExercised = uint64((uint256(amountWritten) * 4) / 5);

vm.startPrank(writer);
WETHLIKE.approve(address(clarity), type(uint256).max);
Expand Down Expand Up @@ -701,7 +701,7 @@ contract RebasingTest is BaseUnitTestSuite {
for (uint256 i = 0; i < writeGivens.length; i++) {
// Given
uint64 amountWritten = writeGivens[i];
uint64 amountNetted = (amountWritten * 4) / 5;
uint64 amountNetted = uint64((uint256(amountWritten) * 4) / 5);

vm.startPrank(writer);
WETHLIKE.approve(address(clarity), type(uint256).max);
Expand Down Expand Up @@ -844,7 +844,7 @@ contract RebasingTest is BaseUnitTestSuite {
for (uint256 i = 0; i < writeGivens.length; i++) {
// Given
uint64 amountWritten = writeGivens[i];
uint64 amountNetted = (amountWritten * 4) / 5;
uint64 amountNetted = uint64((uint256(amountWritten) * 4) / 5);
uint64 amountExercised = amountWritten / 5;

vm.startPrank(writer);
Expand Down Expand Up @@ -885,7 +885,7 @@ contract RebasingTest is BaseUnitTestSuite {
for (uint256 i = 0; i < writeGivens.length; i++) {
// Given
uint64 amountWritten = writeGivens[i];
uint64 amountExercised = (amountWritten * 4) / 5;
uint64 amountExercised = uint64((uint256(amountWritten) * 4) / 5);

vm.startPrank(writer);
WETHLIKE.approve(address(clarity), type(uint256).max);
Expand Down Expand Up @@ -924,7 +924,7 @@ contract RebasingTest is BaseUnitTestSuite {
// Given
uint64 amountWritten = writeGivens[i];
uint64 amountNetted = amountWritten / 5;
uint64 amountExercised = (amountWritten * 4) / 5;
uint64 amountExercised = uint64((uint256(amountWritten) * 4) / 5);

vm.startPrank(writer);
WETHLIKE.approve(address(clarity), type(uint256).max);
Expand Down
17 changes: 8 additions & 9 deletions test/invariant/ClarityMarkets.invariant.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,20 @@ contract ClarityMarketsInvariantTest is BaseTestSuite {
handler = new OptionsHandler(clarity);

// target contracts
bytes4[] memory selectors = new bytes4[](8);
bytes4[] memory selectors = new bytes4[](7);
// Write
selectors[0] = OptionsHandler.writeNewCall.selector;
selectors[1] = OptionsHandler.writeNewPut.selector;
selectors[2] = OptionsHandler.writeExisting.selector;
selectors[0] = OptionsHandler.writeNew.selector;
selectors[1] = OptionsHandler.writeExisting.selector;
// TODO batchWrite
// Transfer
selectors[3] = OptionsHandler.transferLongs.selector;
selectors[4] = OptionsHandler.transferShorts.selector;
selectors[2] = OptionsHandler.transferLongs.selector;
selectors[3] = OptionsHandler.transferShorts.selector;
// Net
selectors[5] = OptionsHandler.netOffsetting.selector;
selectors[4] = OptionsHandler.netOffsetting.selector;
// Exercise
selectors[6] = OptionsHandler.exerciseOption.selector;
selectors[5] = OptionsHandler.exerciseOption.selector;
// Redeem
selectors[7] = OptionsHandler.redeemCollateral.selector;
selectors[6] = OptionsHandler.redeemCollateral.selector;
// Skim
// TODO skim

Expand Down
107 changes: 30 additions & 77 deletions test/util/OptionsHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,106 +163,60 @@ contract OptionsHandler is CommonBase, StdCheats, StdUtils {

// Write

function writeNewCall(
function writeNew(
uint256 baseAssetIndex,
uint256 quoteAssetIndex,
uint32 expiry,
uint256 strike,
bool allowEarlyExercise,
uint64 optionAmount
) external createActor countCall("writeNewCall") {
uint64 optionAmount,
bool isCall
) external createActor countCall("writeNew") {
// set assets
baseAssetIndex = baseAssetIndex % baseAssets.count();
quoteAssetIndex = quoteAssetIndex % quoteAssets.count();

// bind expiry
vm.assume(expiry > 1);

// bind strike price
// bind strike price and round to nearest million
strike = bound(strike, clarity.MINIMUM_STRIKE(), clarity.MAXIMUM_STRIKE());
strike = strike - (strike % (10 ** clarity.CONTRACT_SCALAR()));

// deal asset, approve clearinghouse, and write options
vm.startPrank(currentActor);
IERC20 baseAsset = baseAssets.at(baseAssetIndex);
IERC20 quoteAsset = quoteAssets.at(quoteAssetIndex);

uint256 writeAssetAmount = isCall
? uint256(baseAsset.decimals().oneClearingUnit()) * uint256(optionAmount)
: uint256(strike.actualScaledDownToClearingStrikeUnit()) * uint256(optionAmount);

uint256 writeAssetAmount =
uint256(baseAsset.decimals().oneClearingUnit()) * uint256(optionAmount);
deal(address(baseAsset), currentActor, writeAssetAmount);

baseAsset.approve(address(clarity), writeAssetAmount);

// begin recording storage accesses
vm.record();

uint256 optionTokenId = clarity.writeNewCall({
baseAsset: address(baseAsset),
quoteAsset: address(quoteAssets.at(quoteAssetIndex)),
expiry: expiry,
strike: strike,
allowEarlyExercise: allowEarlyExercise,
optionAmount: optionAmount
});
vm.stopPrank();
uint256 optionTokenId = isCall
? clarity.writeNewCall({
baseAsset: address(baseAsset),
quoteAsset: address(quoteAssets.at(quoteAssetIndex)),
expiry: expiry,
strike: strike,
allowEarlyExercise: allowEarlyExercise,
optionAmount: optionAmount
})
: clarity.writeNewPut({
baseAsset: address(baseAssets.at(baseAssetIndex)),
quoteAsset: address(quoteAsset),
expiry: expiry,
strike: strike,
allowEarlyExercise: allowEarlyExercise,
optionAmount: optionAmount
});

// track object sets
_options.add(optionTokenId);

// save OptionState and XYZ storage slots
(, bytes32[] memory writes) = vm.accesses(address(clarity));
ghost_optionStateSlotFor[optionTokenId] = writes[5];

// track ghost variables
if (optionAmount > 0) {
ghost_clearingLiabilityFor[address(baseAsset)] += writeAssetAmount;

ghost_longSumFor[optionTokenId] += optionAmount;
ghost_shortSumFor[optionTokenId] += optionAmount;

ghost_longOwnersOf[optionTokenId].push(currentActor);
ghost_shortOwnersOf[optionTokenId].push(currentActor);
}
}

function writeNewPut(
uint256 baseAssetIndex,
uint256 quoteAssetIndex,
uint32 expiry,
uint256 strike,
bool allowEarlyExercise,
uint64 optionAmount
) external createActor countCall("writeNewPut") {
// bind assets
baseAssetIndex = baseAssetIndex % baseAssets.count();
quoteAssetIndex = quoteAssetIndex % quoteAssets.count();

// bind expiry
vm.assume(expiry > 1);

// bind strike price and round to nearest million
strike = bound(strike, clarity.MINIMUM_STRIKE(), clarity.MAXIMUM_STRIKE());
strike = strike - (strike % (10 ** clarity.CONTRACT_SCALAR()));

// deal asset, approve clearinghouse, and write options
vm.startPrank(currentActor);
IERC20 quoteAsset = quoteAssets.at(quoteAssetIndex);

uint256 writeAssetAmount =
uint256(strike.actualScaledDownToClearingStrikeUnit()) * uint256(optionAmount);
deal(address(quoteAsset), currentActor, writeAssetAmount);

quoteAsset.approve(address(clarity), writeAssetAmount);

// begin recording storage accesses
vm.record();

uint256 optionTokenId = clarity.writeNewPut({
baseAsset: address(baseAssets.at(baseAssetIndex)),
quoteAsset: address(quoteAsset),
expiry: expiry,
strike: strike,
allowEarlyExercise: allowEarlyExercise,
optionAmount: optionAmount
});
vm.stopPrank();

// track object sets
Expand All @@ -274,7 +228,7 @@ contract OptionsHandler is CommonBase, StdCheats, StdUtils {

// track ghost variables
if (optionAmount > 0) {
ghost_clearingLiabilityFor[address(quoteAsset)] += writeAssetAmount;
ghost_clearingLiabilityFor[address(baseAsset)] += writeAssetAmount;

ghost_longSumFor[optionTokenId] += optionAmount;
ghost_shortSumFor[optionTokenId] += optionAmount;
Expand Down Expand Up @@ -666,8 +620,7 @@ contract OptionsHandler is CommonBase, StdCheats, StdUtils {
console2.log("Call summary:");
console2.log("-------------------");
// Write
console2.log("writeNewCall", calls["writeNewCall"]);
console2.log("writeNewPut", calls["writeNewPut"]);
console2.log("writeNew", calls["writeNew"]);
console2.log("writeExisting", calls["writeExisting"]);
console2.log("writeBatch", calls["writeBatch"]);
// Transfer
Expand Down

0 comments on commit 1b42311

Please sign in to comment.