Skip to content

Commit

Permalink
✅ assertWithTolerance helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cmontecoding committed Oct 9, 2024
1 parent 9f5e5c1 commit 78bb359
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
30 changes: 10 additions & 20 deletions test/Collateral.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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),
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion test/utils/Bootstrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,7 +35,7 @@ import {ArbitrumSepoliaParameters} from
/// and effectively tests the deploy script as well
///
/// @author JaredBorders ([email protected])
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 *;
Expand Down
9 changes: 9 additions & 0 deletions test/utils/TestHelpers.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 78bb359

Please sign in to comment.