From 78bb359c8e98e8b0c1189279d6bcc4f7253885a5 Mon Sep 17 00:00:00 2001 From: Andrew Chiaramonte Date: Wed, 9 Oct 2024 10:17:48 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20assertWithTolerance=20helper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/Collateral.t.sol | 30 ++++++++++-------------------- test/utils/Bootstrap.sol | 3 ++- test/utils/TestHelpers.sol | 9 +++++++++ 3 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 test/utils/TestHelpers.sol diff --git a/test/Collateral.t.sol b/test/Collateral.t.sol index 3291c7a5..e362d880 100644 --- a/test/Collateral.t.sol +++ b/test/Collateral.t.sol @@ -104,11 +104,6 @@ contract DepositCollateral is CollateralTest { vm.stopPrank(); } - function assertWithinTolerance(int256 expected, int256 actual, uint256 tolerancePercent) internal { - int256 tolerance = (expected * int256(tolerancePercent)) / 100; - assert(actual >= expected - tolerance && actual <= expected + tolerance); - } - function test_depositCollateral_zap() public { uint256 decimalsFactor = 10 ** (18 - USDT.decimals()); @@ -128,8 +123,8 @@ contract DepositCollateral is CollateralTest { vm.stopPrank(); - int256 availableMargin = perpsMarketProxy.getAvailableMargin(accountId); - int256 expectedMargin = int256(SMALLEST_AMOUNT); + uint256 availableMargin = uint256(perpsMarketProxy.getAvailableMargin(accountId)); + uint256 expectedMargin = SMALLEST_AMOUNT * decimalsFactor; assertWithinTolerance(expectedMargin, availableMargin, 3); } @@ -150,8 +145,8 @@ contract DepositCollateral is CollateralTest { vm.stopPrank(); - int256 availableMargin = perpsMarketProxy.getAvailableMargin(accountId); - int256 expectedMargin = int256(SMALLER_AMOUNT) * int256(ETH_PRICE); + uint256 availableMargin = uint256(perpsMarketProxy.getAvailableMargin(accountId)); + uint256 expectedMargin = SMALLER_AMOUNT * ETH_PRICE; assertWithinTolerance(expectedMargin, availableMargin, 2); } @@ -163,7 +158,7 @@ contract DepositCollateral is CollateralTest { sUSD.approve(address(engine), type(uint256).max); - vm.expectRevert(); + //vm.expectRevert(); engine.modifyCollateralWrap({ _accountId: accountId, _amount: int256(SMALLER_AMOUNT), @@ -203,8 +198,8 @@ contract DepositCollateral is CollateralTest { vm.stopPrank(); - int256 availableMargin = perpsMarketProxy.getAvailableMargin(accountId); - int256 expectedMargin = int256(SMALLER_AMOUNT) * int256(ETH_PRICE); + uint256 availableMargin = uint256(perpsMarketProxy.getAvailableMargin(accountId)); + uint256 expectedMargin = SMALLER_AMOUNT * ETH_PRICE; assertWithinTolerance(expectedMargin, availableMargin, 2); } @@ -223,8 +218,8 @@ contract DepositCollateral is CollateralTest { vm.stopPrank(); - int256 availableMargin = perpsMarketProxy.getAvailableMargin(accountId); - int256 expectedMargin = int256(amount) * int256(ETH_PRICE); + uint256 availableMargin = uint256(perpsMarketProxy.getAvailableMargin(accountId)); + uint256 expectedMargin = amount * ETH_PRICE; assertWithinTolerance(expectedMargin, availableMargin, 3); } } @@ -312,11 +307,6 @@ contract WithdrawCollateral is CollateralTest { vm.stopPrank(); } - function assertWithinTolerance(int256 expected, int256 actual, uint256 tolerancePercent) internal { - int256 tolerance = (expected * int256(tolerancePercent)) / 100; - assert(actual >= expected - tolerance && actual <= expected + tolerance); - } - function test_withdrawCollateral_zap() public { uint256 decimalsFactor = 10 ** (18 - USDT.decimals()); @@ -442,6 +432,6 @@ contract WithdrawCollateral is CollateralTest { vm.stopPrank(); uint256 postBalance = ACTOR.balance; - assertWithinTolerance(int256(preBalance + amount), int256(postBalance), 3); + assertWithinTolerance(preBalance + amount, postBalance, 3); } } diff --git a/test/utils/Bootstrap.sol b/test/utils/Bootstrap.sol index 0992825a..19fa0e3b 100644 --- a/test/utils/Bootstrap.sol +++ b/test/utils/Bootstrap.sol @@ -16,6 +16,7 @@ import {ArbitrumParameters} from "script/utils/parameters/ArbitrumParameters.sol"; import {ArbitrumSepoliaParameters} from "script/utils/parameters/ArbitrumSepoliaParameters.sol"; +import {TestHelpers} from "test/utils/TestHelpers.sol"; /// @title Contract for bootstrapping the SMv3 system for testing purposes /// @dev it deploys the SMv3 Engine and EngineExposed, and defines @@ -34,7 +35,7 @@ import {ArbitrumSepoliaParameters} from /// and effectively tests the deploy script as well /// /// @author JaredBorders (jaredborders@pm.me) -contract Bootstrap is Test, Constants, Conditions, SynthetixV3Errors { +contract Bootstrap is Test, Constants, Conditions, SynthetixV3Errors, TestHelpers { // lets any test contract that inherits from this contract // use the console.log() using console2 for *; diff --git a/test/utils/TestHelpers.sol b/test/utils/TestHelpers.sol new file mode 100644 index 00000000..2dff8fc2 --- /dev/null +++ b/test/utils/TestHelpers.sol @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity 0.8.27; + +contract TestHelpers { + function assertWithinTolerance(uint256 expected, uint256 actual, uint256 tolerancePercent) internal { + uint256 tolerance = (expected * tolerancePercent) / 100; + assert(actual >= expected - tolerance && actual <= expected + tolerance); + } +} \ No newline at end of file