Skip to content

Commit af6962c

Browse files
authored
cover all lines, some branches show as uncovered even though they are hit (#310)
1 parent e5fe1b3 commit af6962c

File tree

4 files changed

+86
-16
lines changed

4 files changed

+86
-16
lines changed

.gas-snapshot

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ UNIT_CashAssetTotalSupplyBorrow:testBorrowWillChangeSupplyAndTotalBorrow() (gas:
167167
UNIT_CashAssetTotalSupplyBorrow:testFuzzDepositDecreasesTotalBorrow(uint256,uint256) (runs: 256, μ: 323159, ~: 327096)
168168
UNIT_CashAssetTotalSupplyBorrow:testFuzzDepositIncreasesTotalSupply(uint256) (runs: 256, μ: 99926, ~: 100889)
169169
UNIT_CashAssetTotalSupplyBorrow:testFuzzDepositNegativeBalanceToPositiveBalance(uint256,uint256) (runs: 256, μ: 320203, ~: 325156)
170-
UNIT_CashAssetTotalSupplyBorrow:testFuzzNormalTransferToNewAccountDoesnotChangeBorrowOrSupply(uint256,uint256) (runs: 256, μ: 511251, ~: 519391)
171-
UNIT_CashAssetTotalSupplyBorrow:testFuzzTransferDoesnotChangeInvariant(uint256,int256) (runs: 256, μ: 351387, ~: 360110)
170+
UNIT_CashAssetTotalSupplyBorrow:testFuzzNormalTransferToNewAccountDoesnotChangeBorrowOrSupply(uint256,uint256) (runs: 256, μ: 511604, ~: 519391)
171+
UNIT_CashAssetTotalSupplyBorrow:testFuzzTransferDoesnotChangeInvariant(uint256,int256) (runs: 256, μ: 351139, ~: 360110)
172172
UNIT_CashAssetTotalSupplyBorrow:testFuzzWithdrawDecreasesTotalSupply(uint256) (runs: 256, μ: 116523, ~: 118734)
173173
UNIT_CashAssetTotalSupplyBorrow:testFuzzWithdrawIncreasesTotalBorrow(uint256) (runs: 256, μ: 285790, ~: 296433)
174174
UNIT_CashAssetTotalSupplyBorrow:testFuzzWithdrawPositiveBalanceToNegativeBalance(uint256,uint256) (runs: 256, μ: 335100, ~: 339444)
@@ -400,16 +400,17 @@ UNIT_TestOptionBasics:testWhitelistedManagerCheck() (gas: 438294)
400400
UNIT_TestPMRM_ArrangePortfolio:testPMRMArrangePortfolio_ExpiredOption() (gas: 145879)
401401
UNIT_TestPMRM_ArrangePortfolio:testPMRMArrangePortfolio_MaxExpiries() (gas: 61612)
402402
UNIT_TestPMRM_Contingencies:testPMRMScenario_OptionContingency() (gas: 178729)
403-
UNIT_TestPMRM_EdgeCases:testPMRMCanOnlyDepositCashIfLiveAuction() (gas: 1981807)
404-
UNIT_TestPMRM_EdgeCases:testPMRMCanSettlePerpsIfBeyondMaxAssetSize() (gas: 7113265)
405-
UNIT_TestPMRM_EdgeCases:testPMRMCanTradeMoreThanMaxAccountSizeIfAlreadyHadIt() (gas: 7156711)
406-
UNIT_TestPMRM_EdgeCases:testPMRM_CanTradeIfMaxAccountSizeDecreased() (gas: 4251262)
407-
UNIT_TestPMRM_EdgeCases:testPMRM_CannotTradeIfExceed_MaxAssets() (gas: 2159512)
408-
UNIT_TestPMRM_EdgeCases:testPMRM_invalidGetMarginState() (gas: 27232)
409-
UNIT_TestPMRM_EdgeCases:testPMRM_invalidSpotShock() (gas: 174502)
410-
UNIT_TestPMRM_EdgeCases:testPMRM_noScenarios() (gas: 11262)
403+
UNIT_TestPMRM_EdgeCases:testPMRMCanGetScenarioMTM() (gas: 5070200)
404+
UNIT_TestPMRM_EdgeCases:testPMRMCanOnlyDepositCashIfLiveAuction() (gas: 1981881)
405+
UNIT_TestPMRM_EdgeCases:testPMRMCanSettlePerpsIfBeyondMaxAssetSize() (gas: 7113393)
406+
UNIT_TestPMRM_EdgeCases:testPMRMCanTradeMoreThanMaxAccountSizeIfAlreadyHadIt() (gas: 7156781)
407+
UNIT_TestPMRM_EdgeCases:testPMRM_CanTradeIfMaxAccountSizeDecreased() (gas: 4251266)
408+
UNIT_TestPMRM_EdgeCases:testPMRM_CannotTradeIfExceed_MaxAssets() (gas: 2159446)
409+
UNIT_TestPMRM_EdgeCases:testPMRM_invalidGetMarginState() (gas: 27164)
410+
UNIT_TestPMRM_EdgeCases:testPMRM_invalidSpotShock() (gas: 174561)
411+
UNIT_TestPMRM_EdgeCases:testPMRM_noScenarios() (gas: 11280)
411412
UNIT_TestPMRM_EdgeCases:testPMRM_notFoundError() (gas: 9501)
412-
UNIT_TestPMRM_EdgeCases:testPMRM_perpTransfer() (gas: 3375197)
413+
UNIT_TestPMRM_EdgeCases:testPMRM_perpTransfer() (gas: 3375267)
413414
UNIT_TestPMRM_EdgeCases:testPMRM_unsupportedAsset() (gas: 788035)
414415
UNIT_TestPMRM_EdgeCases:testRevertsIfUsingBadScenarioId() (gas: 57350)
415416
UNIT_TestPMRM_PortfolioCases:testCase1() (gas: 1055995)
@@ -532,12 +533,13 @@ UNIT_TestStandardManager:testSettlePerpWithManagerData() (gas: 670397)
532533
UNIT_TestStandardManager:testWontSettlePerpIfNoBalance() (gas: 77505)
533534
UNIT_TestStandardManager_Misc:testCanEnableBorrowing() (gas: 31902)
534535
UNIT_TestStandardManager_Misc:testCanHaveNegativeCashIfBorrowingEnabled() (gas: 710204)
535-
UNIT_TestStandardManager_Misc:testCanHaveNegativeCashWhenBorrowingBecomesDisabled() (gas: 1217880)
536-
UNIT_TestStandardManager_Misc:testCanSettlePerpsIfBeyondMaxAssetSize() (gas: 4571431)
536+
UNIT_TestStandardManager_Misc:testCanHaveNegativeCashWhenBorrowingBecomesDisabled() (gas: 1218013)
537+
UNIT_TestStandardManager_Misc:testCanSettlePerpsIfBeyondMaxAssetSize() (gas: 4571409)
537538
UNIT_TestStandardManager_Misc:testCanTradeMoreThanMaxAccountSizeIfAlreadyHadIt() (gas: 4544104)
538539
UNIT_TestStandardManager_Misc:testCanTransferCash() (gas: 323912)
539-
UNIT_TestStandardManager_Misc:testCannotSetInvalidMarginParams() (gas: 15812)
540-
UNIT_TestStandardManager_Misc:testCannotTradeMoreThanMaxAccountSize() (gas: 2245054)
540+
UNIT_TestStandardManager_Misc:testCannotSetInvalidMarginParams() (gas: 15790)
541+
UNIT_TestStandardManager_Misc:testCannotTradeMoreThanMaxAccountSize() (gas: 2245032)
542+
UNIT_TestStandardManager_Misc:testTrustedRiskAssessorCanBypassIMButNotMM() (gas: 1403691)
541543
UNIT_TestStandardManager_MultiAsset:testBaseAssetCanAddMargin() (gas: 409369)
542544
UNIT_TestStandardManager_MultiAsset:testBaseAssetMarginDefaultToZero() (gas: 349399)
543545
UNIT_TestStandardManager_MultiAsset:testBaseAssetMarginWithContingency() (gas: 524436)

src/risk-managers/PMRMLib.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,5 @@ contract PMRMLib is IPMRMLib, Ownable2Step {
379379
scenarios[0] = IPMRM.Scenario({spotShock: basisContParams.scenarioSpotUp, volShock: IPMRM.VolShockDirection.None});
380380
scenarios[1] = IPMRM.Scenario({spotShock: basisContParams.scenarioSpotDown, volShock: IPMRM.VolShockDirection.None});
381381
scenarios[2] = IPMRM.Scenario({spotShock: DecimalMath.UNIT, volShock: IPMRM.VolShockDirection.None});
382-
return scenarios;
383382
}
384383
}

test/risk-managers/unit-tests/PMRM/TestPMRM_EdgeCases.t.sol

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,34 @@ contract UNIT_TestPMRM_EdgeCases is PMRMSimTest {
285285

286286
assertEq(subAccounts.getAccountBalances(bobAcc).length, 13);
287287
}
288+
289+
function testPMRMCanGetScenarioMTM() public {
290+
uint64 expiry = uint64(block.timestamp + 1 weeks);
291+
292+
_depositCash(aliceAcc, 10_000e18);
293+
_depositCash(bobAcc, 10_000e18);
294+
295+
feed.setForwardPrice(expiry, 1300e18, 1e18);
296+
297+
ISubAccounts.AssetTransfer[] memory transfers = new ISubAccounts.AssetTransfer[](10);
298+
for (uint i; i < 10; i++) {
299+
transfers[i] = ISubAccounts.AssetTransfer({
300+
fromAcc: aliceAcc,
301+
toAcc: bobAcc,
302+
asset: option,
303+
subId: OptionEncoding.toSubId(expiry, 1500e18 + i * 1e18, true),
304+
amount: 0.1e18,
305+
assetData: ""
306+
});
307+
feed.setVol(expiry, uint128(1500e18 + i * 1e18), 0.5e18, 1e18);
308+
}
309+
subAccounts.submitTransfers(transfers, "");
310+
311+
IPMRM.Portfolio memory portfolio = pmrm.arrangePortfolio(aliceAcc);
312+
313+
int mtm = lib.getScenarioMtM(portfolio, lib.getBasisContingencyScenarios()[0]);
314+
assertLt(mtm, 0);
315+
mtm = lib.getScenarioMtM(portfolio, lib.getBasisContingencyScenarios()[1]);
316+
assertGt(mtm, 0);
317+
}
288318
}

test/risk-managers/unit-tests/StandardManager/TestStandardManager_Misc.t.sol

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,43 @@ contract UNIT_TestStandardManager_Misc is TestStandardManagerBase {
237237
manager.setBorrowingEnabled(true);
238238
cash.withdraw(bobAcc, 1e18, bob);
239239
}
240+
241+
function testTrustedRiskAssessorCanBypassIMButNotMM() public {
242+
ISubAccounts.AssetTransfer memory transfer = ISubAccounts.AssetTransfer({
243+
fromAcc: aliceAcc,
244+
toAcc: bobAcc,
245+
asset: ethOption,
246+
subId: OptionEncoding.toSubId(expiry2, 1500e18, true),
247+
amount: 1e18,
248+
assetData: ""
249+
});
250+
251+
// for single option: im ~-220, mm ~-120
252+
// so start with 100
253+
cash.deposit(aliceAcc, 100e18);
254+
255+
// set TRA to true
256+
manager.setTrustedRiskAssessor(address(this), true);
257+
258+
// Will still fail as below MM requirements
259+
vm.expectRevert(IStandardManager.SRM_PortfolioBelowMargin.selector);
260+
subAccounts.submitTransfer(transfer, "");
261+
262+
cash.deposit(aliceAcc, 100e18);
263+
264+
manager.setTrustedRiskAssessor(address(this), false);
265+
266+
// Now above MM requirements, but below IM requirements, so will fail as not TRA
267+
vm.expectRevert(IStandardManager.SRM_PortfolioBelowMargin.selector);
268+
subAccounts.submitTransfer(transfer, "");
269+
270+
// But succeeds when TRA set to true
271+
manager.setTrustedRiskAssessor(address(this), true);
272+
subAccounts.submitTransfer(transfer, "");
273+
274+
(int im,) = manager.getMarginAndMarkToMarket(aliceAcc, true, 0);
275+
assertLt(im, 0);
276+
(int mm,) = manager.getMarginAndMarkToMarket(aliceAcc, false, 0);
277+
assertGt(mm, 0);
278+
}
240279
}

0 commit comments

Comments
 (0)