Skip to content

Commit

Permalink
prettierfix squash this
Browse files Browse the repository at this point in the history
  • Loading branch information
ProblematicP authored and noisekit committed Nov 6, 2024
1 parent 9b71da0 commit dfadbcf
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 108 deletions.
9 changes: 4 additions & 5 deletions contracts/debt-repayer-base-andromeda/src/DebtRepayer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,15 +29,15 @@ contract DebtRepayer {

if (debt > 0) {
uint256 depositedAmount = coreProxy.getAccountAvailableCollateral(accountId, coreProxy.getUsdToken());

if (uint256(debt) > depositedAmount) {
uint256 remainingDebt = uint256(debt) - depositedAmount;

ISpotMarket spotMarket = ISpotMarket(spotMarketAddress);
(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);
Expand All @@ -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));
}
}
87 changes: 42 additions & 45 deletions contracts/debt-repayer-base-andromeda/test/BaseTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
94 changes: 36 additions & 58 deletions contracts/debt-repayer-base-andromeda/test/DebtRepayerTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
);
}

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

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

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

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

0 comments on commit dfadbcf

Please sign in to comment.