Skip to content

Commit

Permalink
all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
daopunk committed Jul 22, 2024
1 parent 3ef1e05 commit 8a65481
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 55 deletions.
41 changes: 36 additions & 5 deletions src/leverage/ParaswapSellAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import {IPoolAddressesProvider} from '@aave-core-v3/contracts/interfaces/IPoolAd
import {PercentageMath} from '@aave-core-v3/contracts/protocol/libraries/math/PercentageMath.sol';
import {IParaSwapAugustusRegistry} from '@aave-debt-swap/dependencies/paraswap/IParaSwapAugustusRegistry.sol';
import {ODProxy} from '@opendollar/contracts/proxies/ODProxy.sol';
import {Modifiable} from '@opendollar/contracts/utils/Modifiable.sol';
import {Authorizable} from '@opendollar/contracts/utils/Authorizable.sol';
import {
ISAFEEngine,
IODSafeManager,
ICollateralJoinFactory,
IOracleRelayer,
IVault721
} from '@opendollar/libraries/OpenDollarV1Arbitrum.sol';
import {Assertions} from '@opendollar/libraries/Assertions.sol';
import {Encoding} from '@opendollar/libraries/Encoding.sol';
import {Math} from '@opendollar/libraries/Math.sol';
import {IParaswapSellAdapter, InitSellAdapter} from 'src/leverage/interfaces/IParaswapSellAdapter.sol';
import {IParaswapAugustus} from 'src/leverage/interfaces/IParaswapAugustus.sol';
Expand All @@ -23,11 +27,12 @@ import {IExitActions} from 'src/leverage/interfaces/IExitActions.sol';
* TODO:
* - add access control
* - add modifiable contract for var updates
* - add withdraw function
*/
contract ParaswapSellAdapter is FlashLoanSimpleReceiverBase, IParaswapSellAdapter {
contract ParaswapSellAdapter is FlashLoanSimpleReceiverBase, IParaswapSellAdapter, Modifiable {
using PercentageMath for uint256;
using Math for uint256;
using Assertions for address;
using Encoding for bytes;

uint256 public constant MAX_SLIPPAGE_PERCENT = 0.03e4; // 3%

Expand All @@ -46,6 +51,7 @@ contract ParaswapSellAdapter is FlashLoanSimpleReceiverBase, IParaswapSellAdapte

constructor(InitSellAdapter memory _initSellAdapter)
FlashLoanSimpleReceiverBase(IPoolAddressesProvider(_initSellAdapter.poolProvider))
Authorizable(msg.sender)
{
AUGUSTUS_REGISTRY = IParaSwapAugustusRegistry(_initSellAdapter.augustusRegistry);
augustus = IParaswapAugustus(_initSellAdapter.augustusSwapper);
Expand All @@ -67,7 +73,7 @@ contract ParaswapSellAdapter is FlashLoanSimpleReceiverBase, IParaswapSellAdapte
function getLeveragedDebt(
bytes32 _cType,
uint256 _initCapital
) external returns (uint256 _cTypeLoanAmount, uint256 _leveragedDebt) {
) external view returns (uint256 _cTypeLoanAmount, uint256 _leveragedDebt) {
(_cTypeLoanAmount, _leveragedDebt) = _getLeveragedDebt(_cType, _initCapital, 0);
}

Expand All @@ -76,7 +82,7 @@ contract ParaswapSellAdapter is FlashLoanSimpleReceiverBase, IParaswapSellAdapte
bytes32 _cType,
uint256 _initCapital,
uint256 _percentageBuffer
) external returns (uint256 _cTypeLoanAmount, uint256 _leveragedDebt) {
) external view returns (uint256 _cTypeLoanAmount, uint256 _leveragedDebt) {
(_cTypeLoanAmount, _leveragedDebt) = _getLeveragedDebt(_cType, _initCapital, _percentageBuffer);
}

Expand All @@ -90,6 +96,10 @@ contract ParaswapSellAdapter is FlashLoanSimpleReceiverBase, IParaswapSellAdapte
_deposit(_onBehalfOf, _asset, _amount);
}

function withdraw(address _asset, uint256 _amount) external {
_withdraw(msg.sender, _asset, _amount);
}

/// @dev exact-in sell swap on ParaSwap
function sellOnParaSwap(
SellParams memory _sellParams,
Expand Down Expand Up @@ -249,7 +259,7 @@ contract ParaswapSellAdapter is FlashLoanSimpleReceiverBase, IParaswapSellAdapte
bytes32 _cType,
uint256 _initCapital,
uint256 _percentageBuffer
) internal returns (uint256 _cTypeLoanAmount, uint256 _leveragedDebt) {
) internal view returns (uint256 _cTypeLoanAmount, uint256 _leveragedDebt) {
(uint256 _accumulatedRate, uint256 _safetyPrice) = _getCData(_cType);

uint256 _percent = getSafetyRatio(_cType) + _percentageBuffer;
Expand All @@ -258,4 +268,25 @@ contract ParaswapSellAdapter is FlashLoanSimpleReceiverBase, IParaswapSellAdapte
_cTypeLoanAmount = (_initCapital * _multiplier / 10) - _initCapital;
_leveragedDebt = _initCapital.wmul(_safetyPrice).wdiv(_accumulatedRate) * _multiplier / 10;
}

/// @notice overridden function from Modifiable to modify parameters
function _modifyParameters(bytes32 _param, bytes memory _data) internal override {
address _addr = _data.toAddress();

if (_param == 'augustus') {
augustus = IParaswapAugustus(_addr.assertNonNull());
} else if (_param == 'safeManager') {
safeManager = IODSafeManager(_addr.assertNonNull());
} else if (_param == 'oracleRelayer') {
oracleRelayer = IOracleRelayer(_addr.assertNonNull());
} else if (_param == 'collateralJoinFactory') {
collateralJoinFactory = ICollateralJoinFactory(_addr.assertNonNull());
} else if (_param == 'exitActions') {
exitActions = IExitActions(_addr.assertNonNull());
} else if (_param == 'coinJoin') {
coinJoin = _addr.assertNonNull();
} else {
revert UnrecognizedParam();
}
}
}
3 changes: 2 additions & 1 deletion src/leverage/interfaces/IParaswapSellAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.20;

import {IPoolAddressesProvider} from '@aave-core-v3/contracts/interfaces/IPoolAddressesProvider.sol';

Check warning on line 4 in src/leverage/interfaces/IParaswapSellAdapter.sol

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

imported name IPoolAddressesProvider is not used
import {IPool} from '@aave-core-v3/contracts/interfaces/IPool.sol';

Check warning on line 5 in src/leverage/interfaces/IParaswapSellAdapter.sol

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

imported name IPool is not used
import {IModifiable} from '@opendollar/interfaces/utils/IModifiable.sol';

/**
* _augustusRegistry address of Paraswap AugustusRegistry
Expand All @@ -24,7 +25,7 @@ struct InitSellAdapter {
address coinJoin;
}

interface IParaswapSellAdapter {
interface IParaswapSellAdapter is IModifiable {
/**
* @dev emitted after a sell of an asset is made
* @param _fromAsset address of the asset sold
Expand Down
59 changes: 18 additions & 41 deletions test/e2e/E2ESwapExit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,65 +74,40 @@ contract E2ESwapExit is CommonTest {
vm.stopPrank();
}

/// @notice deposit 0.00001 Ether RETH/WSTETH
function testRequestFlashloan0RETH() public {
_requestFlashLoan(0.00001 ether, RETH);
}

function testRequestFlashloan0WSTETH() public {
_requestFlashLoan(0.00001 ether, WSTETH);
}
/**
* @dev DO NOT Fuzz Test w/ ParaSwap SDK calls
*/

/// @notice deposit 0.1 Ether RETH/WSTETH
function testRequestFlashloan1RETH() public {
_requestFlashLoan(0.1 ether, RETH);
/// @notice deposit 0.00001 Ether (RETH)
function testRequestFlashloan0() public {
_requestFlashLoan(0.00001 ether, RETH);
}

function testRequestFlashloan1WSTETH() public {
/// @notice deposit 0.1 Ether (WSTETH)
function testRequestFlashloan1() public {
_requestFlashLoan(0.1 ether, WSTETH);
}

/// @notice deposit 2 Ether RETH/WSTETH
function testRequestFlashloan2RETH() public {
/// @notice deposit 2 Ether (RETH)
function testRequestFlashloan2() public {
_requestFlashLoan(2 ether, RETH);
}

function testRequestFlashloan2WSTETH() public {
_requestFlashLoan(2 ether, WSTETH);
}

/// @notice deposit 4 Ether RETH/WSTETH
function testRequestFlashloan3RETH() public {
_requestFlashLoan(4 ether, RETH);
}

function testRequestFlashloan3WSTETH() public {
/// @notice deposit 4 Ether (WSTETH)
function testRequestFlashloan3() public {
_requestFlashLoan(4 ether, WSTETH);
}

/// @notice deposit 8 Ether RETH/WSTETH
function testRequestFlashloan4RETH() public {
/// @notice deposit 8 Ether (RETH)
function testRequestFlashloan4() public {
_requestFlashLoan(8 ether, RETH);
}

function testRequestFlashloan4WSTETH() public {
_requestFlashLoan(8 ether, WSTETH);
}

/// @notice deposit 16 Ether RETH/WSTETH
function testRequestFlashloan5RETH() public {
_requestFlashLoan(16 ether, RETH);
}

function testRequestFlashloan5WSTETH() public {
/// @notice deposit 16 Ether (WSTETH)
function testRequestFlashloan5() public {
_requestFlashLoan(16 ether, WSTETH);
}

/// @notice deposit 32 Ether RETH
function testRequestFlashloan6RETH() public {
_requestFlashLoan(32 ether, RETH);
}

function _requestFlashLoanAndAssertValues(uint256 _initCapital, bytes32 _cType) internal {
assertEq(collateral[_cType].balanceOf(sellAdapterAddr), 0);
assertEq(systemCoin.balanceOf(sellAdapterAddr), 0);
Expand Down Expand Up @@ -197,6 +172,8 @@ contract E2ESwapExit is CommonTest {
function _logFinalValues(uint256 _deposit, bytes32 _cType) internal {
(uint256 _c, uint256 _d) = _getSAFE(_cType, userNFV.safeHandler);
emit log_named_bytes32('COLLATERAL TYPE', _cType);
if (_cType == RETH) emit log_named_string('RETH ORACLE PRICE', _floatingPointWad(_readCTypePrice(RETH)));
if (_cType == WSTETH) emit log_named_string('WSTETH ORACLE PRICE', _floatingPointWad(_readCTypePrice(WSTETH)));
emit log_named_string('--------------------', '');
emit log_named_string('ORIGINAL DEBT', '0');
emit log_named_string('FINAL DEBT', _floatingPointWad(_d));
Expand Down
8 changes: 0 additions & 8 deletions test/e2e/common/BaseTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,3 @@ contract BaseTest is Test {
_result = vm.ffi(inputs);
}
}

// function _borrow(address _adapter, address _asset, uint256 _amount) internal {
// _adapter.borrow(_asset, _amount, 2, 0, USER);
// }

// function _withdraw(address _adapter, address _asset, uint256 _amount) internal {
// _adapter.withdraw(_asset, _amount, USER);
// }

0 comments on commit 8a65481

Please sign in to comment.