From dfadbcf618979fb58eb8c44223c6f123eeb14b5e Mon Sep 17 00:00:00 2001 From: probematicp Date: Tue, 5 Nov 2024 15:34:24 +1100 Subject: [PATCH] prettierfix squash this --- .../src/DebtRepayer.sol | 9 +- .../test/BaseTest.t.sol | 87 +++++++++-------- .../test/DebtRepayerTest.t.sol | 94 +++++++------------ 3 files changed, 82 insertions(+), 108 deletions(-) diff --git a/contracts/debt-repayer-base-andromeda/src/DebtRepayer.sol b/contracts/debt-repayer-base-andromeda/src/DebtRepayer.sol index 2b9a440b..f910eb03 100644 --- a/contracts/debt-repayer-base-andromeda/src/DebtRepayer.sol +++ b/contracts/debt-repayer-base-andromeda/src/DebtRepayer.sol @@ -11,7 +11,6 @@ import {IAccountProxy} from "./lib/IAccountProxy.sol"; * Front end helper contract which repays debt through collateral in your account or wallet */ contract DebtRepayer { - function depositDebtToRepay( address coreProxyAddress, address spotMarketAddress, @@ -30,7 +29,7 @@ contract DebtRepayer { if (debt > 0) { uint256 depositedAmount = coreProxy.getAccountAvailableCollateral(accountId, coreProxy.getUsdToken()); - + if (uint256(debt) > depositedAmount) { uint256 remainingDebt = uint256(debt) - depositedAmount; @@ -38,7 +37,7 @@ contract DebtRepayer { (uint256 neededSynth,) = spotMarket.quoteSellExactOut(spotMarketId, remainingDebt, 0); (address toWrapToken,) = spotMarket.getWrapper(spotMarketId); uint256 toWrapTokenAmount = neededSynth * (10 ** IUSDToken(toWrapToken).decimals()) / (10 ** 18) + 1; // Assumption: All synths are 18 decimals - + IUSDToken(toWrapToken).transferFrom(msgSender, address(this), uint256(toWrapTokenAmount)); IUSDToken(toWrapToken).approve(address(spotMarket), toWrapTokenAmount); spotMarket.wrap(spotMarketId, toWrapTokenAmount, neededSynth); @@ -47,12 +46,12 @@ contract DebtRepayer { IUSDToken(coreProxy.getUsdToken()).approve(address(coreProxy), remainingDebt); coreProxy.deposit(accountId, coreProxy.getUsdToken(), remainingDebt); } - + coreProxy.burnUsd(accountId, poolId, collateralType, uint256(debt)); } else if (debt < 0) { coreProxy.mintUsd(accountId, poolId, collateralType, uint256(-debt)); } - + accountProxy.transferFrom(address(this), msgSender, uint256(accountId)); } } diff --git a/contracts/debt-repayer-base-andromeda/test/BaseTest.t.sol b/contracts/debt-repayer-base-andromeda/test/BaseTest.t.sol index aa6bc992..9b7d4b7a 100644 --- a/contracts/debt-repayer-base-andromeda/test/BaseTest.t.sol +++ b/contracts/debt-repayer-base-andromeda/test/BaseTest.t.sol @@ -4,53 +4,50 @@ pragma solidity >=0.8.0; import "forge-std/src/Test.sol"; contract BaseTest is Test { - uint256 private seed; - - function generateAddress(string memory _name, bool _isContract) - internal - returns (address) - { - return generateAddress(_name, _isContract, 0); - } - - function generateAddress(string memory _name, bool _isContract, uint256 _ethBalance) - internal - returns (address newAddress_) - { - seed++; - newAddress_ = vm.addr(seed); - - vm.label(newAddress_, _name); - - if (_isContract) { - vm.etch(newAddress_, "Generated Contract Address"); + uint256 private seed; + + function generateAddress(string memory _name, bool _isContract) internal returns (address) { + return generateAddress(_name, _isContract, 0); + } + + function generateAddress(string memory _name, bool _isContract, uint256 _ethBalance) + internal + returns (address newAddress_) + { + seed++; + newAddress_ = vm.addr(seed); + + vm.label(newAddress_, _name); + + if (_isContract) { + vm.etch(newAddress_, "Generated Contract Address"); + } + + vm.deal(newAddress_, _ethBalance); + + return newAddress_; } - vm.deal(newAddress_, _ethBalance); - - return newAddress_; - } - - function assertEqTolerance( - uint256 a, - uint256 b, - uint256 tolerancePercentage //4 decimals - ) internal { - uint256 diff = b > a ? b - a : a - b; - uint256 maxForgivness = (b * tolerancePercentage) / 100_000; - - if (maxForgivness < diff) { - emit log("Error: a == b not satisfied [with tolerance]"); - emit log_named_uint(" A", a); - emit log_named_uint(" B", b); - emit log_named_uint(" Max tolerance", maxForgivness); - emit log_named_uint(" Actual Difference", diff); - fail(); + function assertEqTolerance( + uint256 a, + uint256 b, + uint256 tolerancePercentage //4 decimals + ) internal { + uint256 diff = b > a ? b - a : a - b; + uint256 maxForgivness = (b * tolerancePercentage) / 100_000; + + if (maxForgivness < diff) { + emit log("Error: a == b not satisfied [with tolerance]"); + emit log_named_uint(" A", a); + emit log_named_uint(" B", b); + emit log_named_uint(" Max tolerance", maxForgivness); + emit log_named_uint(" Actual Difference", diff); + fail(); + } } - } - // Makes expecting a function to not be called more explicit instead of calling same function and passing 0 in count - function expectNoCall(address target, bytes memory data) internal { - vm.expectCall(target, data, 0); - } + // Makes expecting a function to not be called more explicit instead of calling same function and passing 0 in count + function expectNoCall(address target, bytes memory data) internal { + vm.expectCall(target, data, 0); + } } diff --git a/contracts/debt-repayer-base-andromeda/test/DebtRepayerTest.t.sol b/contracts/debt-repayer-base-andromeda/test/DebtRepayerTest.t.sol index f7c93304..1051b8f7 100644 --- a/contracts/debt-repayer-base-andromeda/test/DebtRepayerTest.t.sol +++ b/contracts/debt-repayer-base-andromeda/test/DebtRepayerTest.t.sol @@ -37,9 +37,7 @@ contract DebtRepayerTest is BaseTest { vm.stopPrank(); vm.mockCall( - coreProxyAddress, - abi.encodeWithSelector(ISynthetixCore.getUsdToken.selector), - abi.encode(systemDebtToken) + coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.getUsdToken.selector), abi.encode(systemDebtToken) ); vm.mockCall( coreProxyAddress, @@ -73,17 +71,12 @@ contract DebtRepayerTest is BaseTest { ); vm.expectCall( - coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.mintUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** 18) + coreProxyAddress, + abi.encodeWithSelector(ISynthetixCore.mintUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** 18) ); debtRepayer.depositDebtToRepay( - coreProxyAddress, - spotMarketAddress, - accountProxyAddress, - accountId, - poolId, - address(USDC), - spotMarketId + coreProxyAddress, spotMarketAddress, accountProxyAddress, accountId, poolId, address(USDC), spotMarketId ); } @@ -97,23 +90,20 @@ contract DebtRepayerTest is BaseTest { ); vm.mockCall( coreProxyAddress, - abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** USDC.decimals()), + abi.encodeWithSelector( + ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** USDC.decimals() + ), abi.encode() ); vm.expectCall( - coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** 18) + coreProxyAddress, + abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** 18) ); expectNoCall(coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.deposit.selector)); debtRepayer.depositDebtToRepay( - coreProxyAddress, - spotMarketAddress, - accountProxyAddress, - accountId, - poolId, - address(USDC), - spotMarketId + coreProxyAddress, spotMarketAddress, accountProxyAddress, accountId, poolId, address(USDC), spotMarketId ); } @@ -141,7 +131,9 @@ contract DebtRepayerTest is BaseTest { ); vm.mockCall( spotMarketAddress, - abi.encodeWithSelector(ISpotMarket.sellExactOut.selector, spotMarketId, neededSynth, neededSynth, address(0)), + abi.encodeWithSelector( + ISpotMarket.sellExactOut.selector, spotMarketId, neededSynth, neededSynth, address(0) + ), abi.encode(0, 0, 0, 0, 0) ); vm.mockCall( @@ -151,7 +143,9 @@ contract DebtRepayerTest is BaseTest { ); vm.mockCall( coreProxyAddress, - abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** USDC.decimals()), + abi.encodeWithSelector( + ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** USDC.decimals() + ), abi.encode() ); vm.mockCall( @@ -161,24 +155,20 @@ contract DebtRepayerTest is BaseTest { ); vm.expectCall( - coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** 18) + coreProxyAddress, + abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** 18) ); vm.expectCall( - coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.deposit.selector, accountId, systemDebtToken, 900 * 10 ** 18) + coreProxyAddress, + abi.encodeWithSelector(ISynthetixCore.deposit.selector, accountId, systemDebtToken, 900 * 10 ** 18) ); debtRepayer.depositDebtToRepay( - coreProxyAddress, - spotMarketAddress, - accountProxyAddress, - accountId, - poolId, - address(USDC), - spotMarketId + coreProxyAddress, spotMarketAddress, accountProxyAddress, accountId, poolId, address(USDC), spotMarketId ); } - - function test_depositDebtToRepay_extremeNumbersTestRounding_success() public { + + function test_depositDebtToRepay_extremeNumbersTestRounding_success() public { vm.startPrank(user); vm.mockCall( @@ -202,7 +192,9 @@ contract DebtRepayerTest is BaseTest { ); vm.mockCall( spotMarketAddress, - abi.encodeWithSelector(ISpotMarket.sellExactOut.selector, spotMarketId, neededSynth, neededSynth, address(0)), + abi.encodeWithSelector( + ISpotMarket.sellExactOut.selector, spotMarketId, neededSynth, neededSynth, address(0) + ), abi.encode(0, 0, 0, 0, 0) ); vm.mockCall( @@ -212,7 +204,9 @@ contract DebtRepayerTest is BaseTest { ); vm.mockCall( coreProxyAddress, - abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** USDC.decimals()), + abi.encodeWithSelector( + ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** USDC.decimals() + ), abi.encode() ); vm.mockCall( @@ -222,20 +216,16 @@ contract DebtRepayerTest is BaseTest { ); vm.expectCall( - coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** 18) + coreProxyAddress, + abi.encodeWithSelector(ISynthetixCore.burnUsd.selector, accountId, poolId, address(USDC), 1000 * 10 ** 18) ); vm.expectCall( - coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.deposit.selector, accountId, systemDebtToken, 999999999999999999999) + coreProxyAddress, + abi.encodeWithSelector(ISynthetixCore.deposit.selector, accountId, systemDebtToken, 999999999999999999999) ); debtRepayer.depositDebtToRepay( - coreProxyAddress, - spotMarketAddress, - accountProxyAddress, - accountId, - poolId, - address(USDC), - spotMarketId + coreProxyAddress, spotMarketAddress, accountProxyAddress, accountId, poolId, address(USDC), spotMarketId ); } @@ -252,13 +242,7 @@ contract DebtRepayerTest is BaseTest { expectNoCall(coreProxyAddress, abi.encodeWithSelector(ISynthetixCore.burnUsd.selector)); debtRepayer.depositDebtToRepay( - coreProxyAddress, - spotMarketAddress, - accountProxyAddress, - accountId, - poolId, - address(USDC), - spotMarketId + coreProxyAddress, spotMarketAddress, accountProxyAddress, accountId, poolId, address(USDC), spotMarketId ); } @@ -282,13 +266,7 @@ contract DebtRepayerTest is BaseTest { vm.expectRevert(); debtRepayer.depositDebtToRepay( - coreProxyAddress, - spotMarketAddress, - accountProxyAddress, - accountId, - poolId, - address(USDC), - spotMarketId + coreProxyAddress, spotMarketAddress, accountProxyAddress, accountId, poolId, address(USDC), spotMarketId ); } }