Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] ✨ Clearinghouse v1.1 #217

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 55 additions & 52 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,82 +1,85 @@
ValoremOptionsClearinghouseFuzzTest:test_fuzzExercise(uint112,uint112) (runs: 256, μ: 363079, ~: 366353)
ValoremOptionsClearinghouseFuzzTest:test_fuzzExercise(uint112,uint112) (runs: 256, μ: 363675, ~: 366353)
ValoremOptionsClearinghouseFuzzTest:test_fuzzNewOptionType(uint96,uint96,uint40,uint40) (runs: 256, μ: 130922, ~: 130922)
ValoremOptionsClearinghouseFuzzTest:test_fuzzWrite(uint112) (runs: 256, μ: 284216, ~: 284216)
ValoremOptionsClearinghouseFuzzTest:test_fuzzWriteExerciseRedeem(uint32) (runs: 256, μ: 9435521, ~: 9506149)
ValoremOptionsClearinghouseFuzzTest:test_fuzzWriteExerciseRedeem(uint32) (runs: 256, μ: 9524189, ~: 9614090)
ValoremOptionsClearinghouseIntegrationTest:test_integrationAddOptionsToExistingClaim() (gas: 400435)
ValoremOptionsClearinghouseIntegrationTest:test_integrationDustHandling() (gas: 905028)
ValoremOptionsClearinghouseIntegrationTest:test_integrationDustHandling() (gas: 920361)
ValoremOptionsClearinghouseIntegrationTest:test_integrationFairAssignment() (gas: 603589)
ValoremOptionsClearinghouseIntegrationTest:test_integrationRandomAssignment() (gas: 906223)
ValoremOptionsClearinghouseIntegrationTest:test_integrationSweepFeesWhenFeesAccruedForExercise() (gas: 1418985)
ValoremOptionsClearinghouseIntegrationTest:test_integrationSweepFeesWhenFeesAccruedForWrite() (gas: 1343568)
ValoremOptionsClearinghouseIntegrationTest:test_integrationWriteExerciseAddBuckets() (gas: 1062312)
ValoremOptionsClearinghouseInvariantTest:invariant_erc20_balances() (runs: 10, calls: 10000, reverts: 5042)
ValoremOptionsClearinghouseInvariantTest:invariant_options_written_match_claims() (runs: 10, calls: 10000, reverts: 4917)
ValoremOptionsClearinghouseInvariantTest:invariant_positions_accounting() (runs: 10, calls: 10000, reverts: 5034)
ValoremOptionsClearinghouseUnitTest:testRevert_acceptFeeTo_whenNotPendingFeeTo() (gas: 35323)
ValoremOptionsClearinghouseUnitTest:testRevert_claim_whenClaimDoesNotExist() (gas: 13769)
ValoremOptionsClearinghouseUnitTest:testRevert_construction_whenFeeToIsZeroAddress() (gas: 2055299)
ValoremOptionsClearinghouseUnitTest:testRevert_construction_whenTokenURIGeneratorIsZeroAddress() (gas: 39981)
ValoremOptionsClearinghouseUnitTest:testRevert_exercise_whenCallerHasNotGrantedSufficientApprovalToengine() (gas: 584793)
ValoremOptionsClearinghouseUnitTest:testRevert_exercise_whenCallerHoldsInsufficientExerciseAsset() (gas: 582992)
ValoremOptionsClearinghouseInvariantTest:invariant_erc20_balances() (runs: 10, calls: 10000, reverts: 5019)
ValoremOptionsClearinghouseInvariantTest:invariant_options_written_match_claims() (runs: 10, calls: 10000, reverts: 4965)
ValoremOptionsClearinghouseInvariantTest:invariant_positions_accounting() (runs: 10, calls: 10000, reverts: 4922)
ValoremOptionsClearinghouseUnitTest:testRevert_acceptFeeTo_whenNotPendingFeeTo() (gas: 35301)
ValoremOptionsClearinghouseUnitTest:testRevert_claim_whenClaimDoesNotExist() (gas: 14648)
ValoremOptionsClearinghouseUnitTest:testRevert_construction_whenFeeToIsZeroAddress() (gas: 2055329)
ValoremOptionsClearinghouseUnitTest:testRevert_construction_whenTokenURIGeneratorIsZeroAddress() (gas: 39989)
ValoremOptionsClearinghouseUnitTest:testRevert_exercise_whenCallerHasNotGrantedSufficientApprovalToEngine() (gas: 584824)
ValoremOptionsClearinghouseUnitTest:testRevert_exercise_whenCallerHoldsInsufficientExerciseAsset() (gas: 582959)
ValoremOptionsClearinghouseUnitTest:testRevert_exercise_whenCallerHoldsInsufficientOptions() (gas: 264635)
ValoremOptionsClearinghouseUnitTest:testRevert_exercise_whenExerciseTooEarly() (gas: 268095)
ValoremOptionsClearinghouseUnitTest:testRevert_exercise_whenExpiredOption() (gas: 267890)
ValoremOptionsClearinghouseUnitTest:testRevert_exercise_whenInvalidOption() (gas: 254368)
ValoremOptionsClearinghouseUnitTest:testRevert_newOptionType_whenExerciseWindowTooShort() (gas: 23334)
ValoremOptionsClearinghouseUnitTest:testRevert_newOptionType_whenExerciseWindowTooShort() (gas: 23356)
ValoremOptionsClearinghouseUnitTest:testRevert_newOptionType_whenExpiryWindowTooShort() (gas: 24821)
ValoremOptionsClearinghouseUnitTest:testRevert_newOptionType_whenInvalidAssets() (gas: 21521)
ValoremOptionsClearinghouseUnitTest:testRevert_newOptionType_whenInvalidAssets() (gas: 21454)
ValoremOptionsClearinghouseUnitTest:testRevert_newOptionType_whenOptionsTypeExists() (gas: 26485)
ValoremOptionsClearinghouseUnitTest:testRevert_newOptionType_whenTotalSuppliesAreTooLowToExercise() (gas: 44348)
ValoremOptionsClearinghouseUnitTest:testRevert_option_whenOptionDoesNotExist() (gas: 12493)
ValoremOptionsClearinghouseUnitTest:testRevert_position_whenExpiredOption() (gas: 19238)
ValoremOptionsClearinghouseUnitTest:testRevert_position_whenTokenNotFound() (gas: 11847)
ValoremOptionsClearinghouseUnitTest:testRevert_redeem_whenCallerDoesNotOwnClaimId() (gas: 247148)
ValoremOptionsClearinghouseUnitTest:testRevert_redeem_whenClaimTooSoon() (gas: 260127)
ValoremOptionsClearinghouseUnitTest:testRevert_redeem_whenInvalidClaim() (gas: 9432)
ValoremOptionsClearinghouseUnitTest:testRevert_newOptionType_whenTotalSuppliesAreTooLowToExercise() (gas: 45213)
ValoremOptionsClearinghouseUnitTest:testRevert_option_whenOptionDoesNotExist() (gas: 13355)
ValoremOptionsClearinghouseUnitTest:testRevert_position_whenExpiredOption() (gas: 20053)
ValoremOptionsClearinghouseUnitTest:testRevert_position_whenTokenNotFound() (gas: 12705)
ValoremOptionsClearinghouseUnitTest:testRevert_redeem_whenCallerDoesNotOwnClaimId() (gas: 249313)
ValoremOptionsClearinghouseUnitTest:testRevert_redeem_whenInvalidClaim() (gas: 9520)
ValoremOptionsClearinghouseUnitTest:testRevert_setFeeTo_whenNotCurrentFeeTo() (gas: 11919)
ValoremOptionsClearinghouseUnitTest:testRevert_setFeeTo_whenZeroAddress() (gas: 11641)
ValoremOptionsClearinghouseUnitTest:testRevert_setFeesEnabled_whenNotFeeTo() (gas: 11843)
ValoremOptionsClearinghouseUnitTest:testRevert_setTokenURIGenerator_whenNotCurrentFeeTo() (gas: 11911)
ValoremOptionsClearinghouseUnitTest:testRevert_setTokenURIGenerator_whenZeroAddress() (gas: 11653)
ValoremOptionsClearinghouseUnitTest:testRevert_sweepFees_whenNotFeeTo() (gas: 16949)
ValoremOptionsClearinghouseUnitTest:testRevert_uri_whenTokenNotFound() (gas: 18686)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenAmountWrittenCannotBeZero() (gas: 10763)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenCallerDoesNotOwnClaimId() (gas: 261825)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenCallerHasNotGrantedSufficientApprovalToengine() (gas: 685646)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenCallerHoldsInsufficientExerciseAsset() (gas: 685705)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenExpiredOption() (gas: 16935)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenInvalidOption() (gas: 13604)
ValoremOptionsClearinghouseUnitTest:test_claim_whenWrittenMultiple() (gas: 383202)
ValoremOptionsClearinghouseUnitTest:test_claim_whenWrittenMultipleTimesOnMultipleClaims() (gas: 458203)
ValoremOptionsClearinghouseUnitTest:test_claim_whenWrittenOnce() (gas: 303666)
ValoremOptionsClearinghouseUnitTest:test_exercise() (gas: 384012)
ValoremOptionsClearinghouseUnitTest:testRevert_setFeesEnabled_whenNotFeeTo() (gas: 11821)
ValoremOptionsClearinghouseUnitTest:testRevert_setTokenURIGenerator_whenNotCurrentFeeTo() (gas: 11889)
ValoremOptionsClearinghouseUnitTest:testRevert_setTokenURIGenerator_whenZeroAddress() (gas: 11609)
ValoremOptionsClearinghouseUnitTest:testRevert_sweepFees_whenNotFeeTo() (gas: 16927)
ValoremOptionsClearinghouseUnitTest:testRevert_uri_whenTokenNotFound() (gas: 20262)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenAmountWrittenCannotBeZero() (gas: 10676)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenCallerDoesNotOwnClaimId() (gas: 261803)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenCallerHasNotGrantedSufficientApprovalToengine() (gas: 687624)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenCallerHoldsInsufficientExerciseAsset() (gas: 687659)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenExpiredOption() (gas: 16957)
ValoremOptionsClearinghouseUnitTest:testRevert_write_whenInvalidOption() (gas: 13626)
ValoremOptionsClearinghouseUnitTest:test_claim_whenWrittenMultiple() (gas: 387402)
ValoremOptionsClearinghouseUnitTest:test_claim_whenWrittenMultipleTimesOnMultipleClaims() (gas: 458225)
ValoremOptionsClearinghouseUnitTest:test_claim_whenWrittenOnce() (gas: 306556)
ValoremOptionsClearinghouseUnitTest:test_exercise() (gas: 383968)
ValoremOptionsClearinghouseUnitTest:test_exercise_whenExercisingMultipleTimes() (gas: 425505)
ValoremOptionsClearinghouseUnitTest:test_feeBalance_whenFeeOff() (gas: 267101)
ValoremOptionsClearinghouseUnitTest:test_feeBalance_whenFeeOn() (gas: 323819)
ValoremOptionsClearinghouseUnitTest:test_feeBalance_whenMinimum() (gas: 401587)
ValoremOptionsClearinghouseUnitTest:test_feeBalance_whenMinimum() (gas: 401565)
ValoremOptionsClearinghouseUnitTest:test_feeBps() (gas: 5644)
ValoremOptionsClearinghouseUnitTest:test_feeTo() (gas: 7781)
ValoremOptionsClearinghouseUnitTest:test_feeTo() (gas: 7737)
ValoremOptionsClearinghouseUnitTest:test_feesEnabled() (gas: 7684)
ValoremOptionsClearinghouseUnitTest:test_newOptionType() (gas: 130017)
ValoremOptionsClearinghouseUnitTest:test_option_returnsOptionInfo() (gas: 25650)
ValoremOptionsClearinghouseUnitTest:test_newOptionType() (gas: 130083)
ValoremOptionsClearinghouseUnitTest:test_option_returnsOptionInfo() (gas: 25672)
ValoremOptionsClearinghouseUnitTest:test_position_whenFullyExercisedClaim() (gas: 302781)
ValoremOptionsClearinghouseUnitTest:test_position_whenOption() (gas: 30855)
ValoremOptionsClearinghouseUnitTest:test_position_whenPartiallyExercisedClaim() (gas: 427836)
ValoremOptionsClearinghouseUnitTest:test_position_whenOption() (gas: 31743)
ValoremOptionsClearinghouseUnitTest:test_position_whenPartiallyExercisedClaim() (gas: 427858)
ValoremOptionsClearinghouseUnitTest:test_position_whenUnexercisedClaim() (gas: 263939)
ValoremOptionsClearinghouseUnitTest:test_redeem_whenFullyExercised() (gas: 333102)
ValoremOptionsClearinghouseUnitTest:test_redeem_whenPartiallyExercised() (gas: 343924)
ValoremOptionsClearinghouseUnitTest:test_redeem_whenUnexercised() (gas: 235840)
ValoremOptionsClearinghouseUnitTest:test_setFeeToAndAcceptFeeTo() (gas: 29510)
ValoremOptionsClearinghouseUnitTest:test_redeem_whenFullyAssigned() (gas: 335329)
ValoremOptionsClearinghouseUnitTest:test_redeem_whenPartiallyAssigned() (gas: 346683)
ValoremOptionsClearinghouseUnitTest:test_redeem_whenUnassigned() (gas: 238600)
ValoremOptionsClearinghouseUnitTest:test_setFeeToAndAcceptFeeTo() (gas: 29546)
ValoremOptionsClearinghouseUnitTest:test_setFeeToAndAcceptFeeTo_multipleTimes() (gas: 50411)
ValoremOptionsClearinghouseUnitTest:test_setFeesEnabled() (gas: 20514)
ValoremOptionsClearinghouseUnitTest:test_setFeesEnabled() (gas: 20581)
ValoremOptionsClearinghouseUnitTest:test_setTokenURIGenerator() (gas: 2035989)
ValoremOptionsClearinghouseUnitTest:test_sweepFees() (gas: 376903)
ValoremOptionsClearinghouseUnitTest:test_sweepFees_whenNoFees() (gas: 53303)
ValoremOptionsClearinghouseUnitTest:test_sweepFees() (gas: 376859)
ValoremOptionsClearinghouseUnitTest:test_sweepFees_whenNoFees() (gas: 53259)
ValoremOptionsClearinghouseUnitTest:test_tokenType_returnsClaim() (gas: 253539)
ValoremOptionsClearinghouseUnitTest:test_tokenType_returnsNone() (gas: 7980)
ValoremOptionsClearinghouseUnitTest:test_tokenType_returnsOption() (gas: 10216)
ValoremOptionsClearinghouseUnitTest:test_tokenURIGenerator() (gas: 9963)
ValoremOptionsClearinghouseUnitTest:test_tokenType_returnsOption() (gas: 10128)
ValoremOptionsClearinghouseUnitTest:test_tokenURIGenerator() (gas: 9941)
ValoremOptionsClearinghouseUnitTest:test_uri() (gas: 479483)
ValoremOptionsClearinghouseUnitTest:test_write_whenExistingClaim() (gas: 301062)
ValoremOptionsClearinghouseUnitTest:test_write_whenFeeOff() (gas: 259288)
ValoremOptionsClearinghouseUnitTest:test_write_whenNewClaim() (gas: 280712)
ValoremOptionsClearinghouseUnitTest:test_write_whenNewClaim() (gas: 280534)
ValoremOptionsClearinghousev11UnitTest:testRevert_redeem_whenBeforeExpiryAndClaimIsPartiallyAssigned() (gas: 352537)
ValoremOptionsClearinghousev11UnitTest:testRevert_redeem_whenBeforeExpiryAndClaimIsUnassigned() (gas: 262772)
ValoremOptionsClearinghousev11UnitTest:test_claimAssignmentStatus() (gas: 342942)
ValoremOptionsClearinghousev11UnitTest:test_redeem_whenBeforeExpiryAndClaimIsFullyAssigned() (gas: 312642)
28 changes: 28 additions & 0 deletions .gas-snapshot-custom
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
| src/ValoremOptionsClearinghouse.sol:ValoremOptionsClearinghouse contract | | | | | |
|--------------------------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 3281087 | 16499 | | | | |
| Function Name | min | avg | median | max | # calls |
| acceptFeeTo | 585 | 2455 | 2705 | 3825 | 4 |
| balanceOf | 619 | 2583 | 2619 | 2619 | 9722 |
| claim | 831 | 2832 | 2831 | 4660 | 4495 |
| exercise | 513 | 31318 | 20848 | 80748 | 94 |
| feeBalance | 641 | 2633 | 2641 | 2641 | 6036 |
| feeBps | 338 | 338 | 338 | 338 | 37 |
| feeTo | 383 | 2278 | 2383 | 2383 | 344 |
| feesEnabled | 350 | 1016 | 350 | 2350 | 3 |
| net | x | x | x | x | x |
| nettable | x | x | x | x | x |
| newOptionType | 3527 | 47960 | 9148 | 106071 | 294 |
| option | 1978 | 3231 | 1978 | 9978 | 7 |
| position | 1272 | 6147 | 6354 | 14038 | 38 |
| redeem | 465 | 14783 | 15492 | 29378 | 44 |
| safeTransferFrom | 3522 | 16945 | 19442 | 28802 | 79 |
| setFeeTo | 720 | 1837 | 720 | 24712 | 84 |
| setFeesEnabled | 1690 | 2581 | 2687 | 6912 | 351 |
| setTokenURIGenerator | 2638 | 4715 | 2733 | 8775 | 3 |
| sweepFees | 2885 | 16380 | 15798 | 83958 | 253 |
| tokenType | 724 | 1647 | 1062 | 2724 | 5 |
| tokenURIGenerator | 426 | 1426 | 1426 | 2426 | 2 |
| uri | 9574 | 236923 | 236923 | 464273 | 2 |
| write | 450 | 125068 | 115360 | 242841 | 432 |
34 changes: 26 additions & 8 deletions .solhint.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error",">=0.8.0"],
"no-inline-assembly": ["off",">=0.8.0"],
"avoid-low-level-calls": ["off",">=0.8.0"],
"not-rely-on-time": ["off",">=0.8.0"],
"func-visibility": ["warn",{"ignoreConstructors":true}]
}
"extends": "solhint:recommended",
"rules": {
"compiler-version": [
"error",
">=0.8.0"
],
"no-inline-assembly": [
"off",
">=0.8.0"
],
"avoid-low-level-calls": [
"off",
">=0.8.0"
],
"not-rely-on-time": [
"off",
">=0.8.0"
],
"no-global-import": "off",
"func-visibility": [
"warn",
{
"ignoreConstructors": true
}
]
}
}
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
src = 'src'
out = 'out'
libs = ['lib']
remappings = ['forge-std/=lib/forge-std/src/', 'solmate/=lib/solmate/src/']
remappings = ['forge-std/=lib/forge-std/src/', 'solmate/=lib/solmate/src/', 'SolPretty/=lib/SolPretty/src/']

[fuzz]
seed = 7
Expand Down
2 changes: 2 additions & 0 deletions src/TokenURIGenerator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ contract TokenURIGenerator is ITokenURIGenerator {
buffer[i] = ALPHABET[value & 0xf];
value >>= 4;
}

// solhint-disable custom-errors
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
Expand Down
9 changes: 5 additions & 4 deletions src/ValoremOptionsClearinghouse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ contract ValoremOptionsClearinghouse is ERC1155, IValoremOptionsClearinghouse {
}

/// @inheritdoc IValoremOptionsClearinghouse
function claim(uint256 claimId) external view returns (Claim memory claimInfo) {
function claim(uint256 claimId) public view returns (Claim memory claimInfo) {
(uint160 optionKey, uint96 claimKey) = _decodeTokenId(claimId);

if (!_isClaimInitialized(optionKey, claimKey)) {
Expand Down Expand Up @@ -516,12 +516,13 @@ contract ValoremOptionsClearinghouse is ERC1155, IValoremOptionsClearinghouse {
revert CallerDoesNotOwnClaimId(claimId);
}

// Setup pointers to the option and info.
// Setup pointers to the option and claim info.
OptionTypeState storage optionTypeState = optionTypeStates[optionKey];
Option memory optionRecord = optionTypeState.option;
Claim memory claimInfo = claim(claimId); // TODO can we combine this with Claim accounting below?

// Can't redeem until expiry.
if (optionRecord.expiryTimestamp > block.timestamp) {
// Can't redeem before expiry, unless Claim is fully assigned.
if (optionRecord.expiryTimestamp > block.timestamp && claimInfo.amountWritten > claimInfo.amountExercised) {
revert ClaimTooSoon(claimId, optionRecord.expiryTimestamp);
}

Expand Down
11 changes: 11 additions & 0 deletions src/interfaces/IValoremOptionsClearinghouse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ interface IValoremOptionsClearinghouse {
uint96 nextClaimKey;
}

// TODO add early redeem
/**
* @notice Data about a claim to a short position written on an option type.
* When writing an amount of options of a particular type, the writer will be issued an ERC 1155 NFT
Expand Down Expand Up @@ -477,10 +478,20 @@ interface IValoremOptionsClearinghouse {
*/
function write(uint256 tokenId, uint112 amount) external returns (uint256 claimId);

/*//////////////////////////////////////////////////////////////
// Net Offsetting Positions
//////////////////////////////////////////////////////////////*/

/**
* TODO
*/
// function net(uint256 optionid) external;

/*//////////////////////////////////////////////////////////////
// Redeem Claims
//////////////////////////////////////////////////////////////*/

// TODO add early redeem
/**
* @notice Redeems a claim NFT, transfers the underlying/exercise tokens to the caller.
* Can be called after option expiry timestamp (inclusive).
Expand Down
Loading
Loading