Skip to content

Commit a296792

Browse files
authoredOct 12, 2023
feature: show debt and payout (#16)
* removing revert on not expired token to allow querying before exercise window * showing debt and payout even if not yet expired * snapshot * fixing test * removing commented out code * reventing option from being exercised prior to expiry * snapshot
1 parent 59402e1 commit a296792

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed
 

‎.gas-snapshot

+13-11
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ChainlinkOracleInternalTests:testDecimalConversion0Decimals() (gas: 628)
4141
ChainlinkOracleInternalTests:testDecimalConversionDiffDecimals() (gas: 2138)
4242
ChainlinkOracleInternalTests:testDecimalConversionDiffDecimals2() (gas: 2046)
4343
ChainlinkOracleInternalTests:testDecimalConversionDiffDecimals3() (gas: 2046)
44-
ChainlinkOracleInternalTests:testDecimalConversionDiffDecimalsFuzz(uint8,uint8) (runs: 256, μ: 5264, ~: 5271)
44+
ChainlinkOracleInternalTests:testDecimalConversionDiffDecimalsFuzz(uint8,uint8) (runs: 256, μ: 5260, ~: 5271)
4545
ChainlinkOracleInternalTests:testDecimalConversionNormalDecimals() (gas: 808)
4646
ChainlinkOracleTest:testCannotGetSpotWhenAggregatorIsNotSet() (gas: 23300)
4747
ChainlinkOracleTest:testCannotGetSpotWhenAggregatorIsStale() (gas: 41702)
@@ -68,6 +68,7 @@ DecimalMathTest:testConversionScaleUp() (gas: 7854)
6868
DecimalMathTest:testMul(uint256,uint256) (runs: 256, μ: 8980, ~: 8980)
6969
DecimalMathTest:testMulOverflowInUncheck() (gas: 8345)
7070
Deploy:testChill() (gas: 120)
71+
Deploy:testChill() (gas: 98)
7172
Deploy:testUtils() (gas: 143)
7273
MockEngineV2:test() (gas: 162)
7374
MockOracle:test() (gas: 186)
@@ -96,10 +97,10 @@ PomaceAccessTest:testCannotUpdateRandomAccount() (gas: 23686)
9697
PomaceAccessTest:testForgeCoverageIgnoreThis() (gas: 208)
9798
PomaceAccessTest:testGranteeCanRevokeAccess() (gas: 35761)
9899
PomaceAccessTest:testUtils() (gas: 167)
99-
PomacePayoutTest:testCanGetBatchPayout() (gas: 107755)
100-
PomacePayoutTest:testCannotGetPayoutBeforeExpiry() (gas: 59845)
101-
PomacePayoutTest:testPayoutETHCollatCall() (gas: 64682)
102-
PomacePayoutTest:testPayoutUSDCollatPut() (gas: 64732)
100+
PomacePayoutTest:testCanGetBatchPayout() (gas: 107693)
101+
PomacePayoutTest:testCanGetPayoutBeforeExpiry() (gas: 61682)
102+
PomacePayoutTest:testPayoutETHCollatCall() (gas: 64651)
103+
PomacePayoutTest:testPayoutUSDCollatPut() (gas: 64701)
103104
PomacePayoutTest:testUtils() (gas: 144)
104105
PomaceProxyTest:testCanUpgradeToAnotherUUPSContract() (gas: 753600)
105106
PomaceProxyTest:testCannotUpgradeFromNonOwner() (gas: 16122)
@@ -114,16 +115,17 @@ PomaceRegistry:testRegisterAssetFromId1() (gas: 88175)
114115
PomaceRegistry:testRegisterAssetRecordDecimals() (gas: 88287)
115116
PomaceRegistry:testReturnAssetsFromProductId() (gas: 100852)
116117
PomaceRegistry:testReturnOptionDetailsFromTokenId() (gas: 97228)
117-
PomaceSettlementPriceDeductionTest:testSettleCallWithNonUnderlyingNorStrike() (gas: 1081068)
118-
PomaceSettlementPriceDeductionTest:testSettlePutWithNonUnderlyingNorStrike() (gas: 1033721)
118+
PomaceSettlementPriceDeductionTest:testSettleCallWithNonUnderlyingNorStrike() (gas: 1081113)
119+
PomaceSettlementPriceDeductionTest:testSettlePutWithNonUnderlyingNorStrike() (gas: 1033766)
119120
PomaceSettlementPriceDeductionTest:testUtils() (gas: 144)
120-
PomaceSettlementTest:testBatchSettleSameCollat() (gas: 186951)
121+
PomaceSettlementTest:testBatchSettleSameCollat() (gas: 186979)
121122
PomaceSettlementTest:testCannotMintExpiredOption() (gas: 25295)
122123
PomaceSettlementTest:testCannotMintOptionZeroExerciseWindow() (gas: 22640)
123124
PomaceSettlementTest:testCannotPassInInconsistentArray() (gas: 20608)
124-
PomaceSettlementTest:testSettleDiffCollat() (gas: 179373)
125-
PomaceSettlementTest:testSettleETHCollatCall() (gas: 127028)
126-
PomaceSettlementTest:testSettleUSDCollatPut() (gas: 138304)
125+
PomaceSettlementTest:testCannotSettleOptionBeforeExpiry() (gas: 67620)
126+
PomaceSettlementTest:testSettleDiffCollat() (gas: 179396)
127+
PomaceSettlementTest:testSettleETHCollatCall() (gas: 127042)
128+
PomaceSettlementTest:testSettleUSDCollatPut() (gas: 138316)
127129
PomaceSettlementTest:testUtils() (gas: 189)
128130
PomaceSettlementTestBase:testUtils() (gas: 144)
129131
ProductIdUtilTest:testFormatAndParseAreMirrored(uint8,uint8,uint8,uint8) (runs: 256, μ: 1025, ~: 1025)

‎src/core/Pomace.sol

+10-5
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ contract Pomace is OwnableUpgradeable, ReentrancyGuardUpgradeable, UUPSUpgradeab
401401
internal
402402
returns (Balance memory, Balance memory)
403403
{
404+
(,, uint64 expiry,,) = TokenIdUtil.parseTokenId(_tokenId);
405+
if (block.timestamp < expiry) revert PM_NotExpired();
406+
404407
(address engine_, uint8 debtId, uint256 debt, uint8 payoutId, uint256 payout) =
405408
getDebtAndPayout(_tokenId, _amount.toUint64());
406409

@@ -461,8 +464,6 @@ contract Pomace is OwnableUpgradeable, ReentrancyGuardUpgradeable, UUPSUpgradeab
461464
(TokenType tokenType, uint32 productId, uint64 expiry, uint64 strikePrice, uint64 exerciseWindow) =
462465
TokenIdUtil.parseTokenId(_tokenId);
463466

464-
if (block.timestamp < expiry) revert PM_NotExpired();
465-
466467
if (block.timestamp > expiry + exerciseWindow) return (address(0), 0, 0, 0, 0);
467468

468469
(uint8 engineId, uint8 underlyingId, uint8 strikeId, uint8 collateralId) = productId.parseProductId();
@@ -551,8 +552,12 @@ contract Pomace is OwnableUpgradeable, ReentrancyGuardUpgradeable, UUPSUpgradeab
551552
* @param _expiry expiry timestamp
552553
*/
553554
function _getSettlementPrice(address _base, address _quote, uint256 _expiry) internal view returns (uint256) {
554-
(uint256 price, bool isFinalized) = oracle.getPriceAtExpiry(_base, _quote, _expiry);
555-
if (!isFinalized) revert PM_PriceNotFinalized();
556-
return price;
555+
if (_expiry > block.timestamp) {
556+
return oracle.getSpotPrice(_base, _quote);
557+
} else {
558+
(uint256 price, bool isFinalized) = oracle.getPriceAtExpiry(_base, _quote, _expiry);
559+
if (!isFinalized) revert PM_PriceNotFinalized();
560+
return price;
561+
}
557562
}
558563
}

‎test/integration-tests/PomacePayoutTest.t.sol

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ import "../../src/config/constants.sol";
1111
* @dev test getDebtAndPayout function on different token types
1212
*/
1313
contract PomacePayoutTest is EngineIntegrationFixture {
14-
function testCannotGetPayoutBeforeExpiry() public {
15-
uint256 tokenId = _mintCallOption(2000 * 1e6, usdcCollatProductId, 1 * UNIT);
14+
function testCanGetPayoutBeforeExpiry() public {
15+
uint256 tokenId = _mintCallOption(2000 * 1e6, wethCollatProductId, 1 * UNIT);
1616

17-
vm.expectRevert(PM_NotExpired.selector);
18-
pomace.getDebtAndPayout(tokenId, uint64(1 * UNIT));
17+
(address engine, uint8 debtId, uint256 debt, uint8 payoutId, uint256 payout) =
18+
pomace.getDebtAndPayout(tokenId, uint64(1 * UNIT));
19+
20+
assertEq(engine, address(engine));
21+
assertEq(debtId, usdcId);
22+
assertEq(debt, 2000 * 1e6);
23+
assertEq(payoutId, wethId);
24+
assertEq(payout, 1 * 1e18);
1925
}
2026

2127
function testPayoutETHCollatCall() public {

‎test/integration-tests/PomaceSettlement.t.sol

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,18 @@ contract PomaceSettlementTestBase is EngineIntegrationFixture {
3535
}
3636

3737
/**
38-
* @dev test getPayout function on different token types
38+
* @dev test settle option function on different token types
3939
*/
4040
contract PomaceSettlementTest is PomaceSettlementTestBase {
41+
function testCannotSettleOptionBeforeExpiry() public {
42+
uint256 tokenId = _mintCallOption(2000 * 1e6, wethCollatProductId, 1 * UNIT);
43+
44+
vm.warp(expiry - 1);
45+
46+
vm.expectRevert(PM_NotExpired.selector);
47+
pomace.settleOption(address(this), tokenId, 1 * UNIT);
48+
}
49+
4150
function testSettleETHCollatCall() public {
4251
uint256 tokenId = _mintCallOption(2000 * 1e6, wethCollatProductId, 1 * UNIT);
4352

0 commit comments

Comments
 (0)
Please sign in to comment.