diff --git a/src/util/ALE.sol b/src/util/ALE.sol deleted file mode 100644 index 4866d448..00000000 --- a/src/util/ALE.sol +++ /dev/null @@ -1,740 +0,0 @@ -//SPDX-License-Identifier: None -pragma solidity ^0.8.0; - -import "src/interfaces/IMarket.sol"; -import "src/interfaces/ITransformHelper.sol"; -import {CurveDBRHelper} from "src/util/CurveDBRHelper.sol"; -import {Ownable} from "lib/openzeppelin-contracts/contracts/access/Ownable.sol"; -import {ReentrancyGuard} from "lib/openzeppelin-contracts/contracts/utils/ReentrancyGuard.sol"; -import {IERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; -import {SafeERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol"; - -interface IDBR { - function markets(address) external view returns (bool); -} - -interface IERC3156FlashBorrower { - /** - * @dev Receive a flash loan. - * @param initiator The initiator of the loan. - * @param token The loan currency. - * @param amount The amount of tokens lent. - * @param fee The additional amount of tokens to repay. - * @param data Arbitrary data structure, intended to contain user-defined parameters. - * @return The keccak256 hash of "ERC3156FlashBorrower.onFlashLoan" - */ - function onFlashLoan( - address initiator, - address token, - uint256 amount, - uint256 fee, - bytes calldata data - ) external returns (bytes32); -} - -interface IERC3156FlashLender { - /** - * @dev Initiate a flash loan. - * @param receiver The receiver of the tokens in the loan, and the receiver of the callback. - * @param token The loan currency. - * @param value The amount of tokens lent. - * @param data Arbitrary data structure, intended to contain user-defined parameters. - */ - function flashLoan( - IERC3156FlashBorrower receiver, - address token, - uint256 value, - bytes calldata data - ) external returns (bool); -} - -// Accelerated leverage engine -contract ALE is - Ownable, - ReentrancyGuard, - CurveDBRHelper, - IERC3156FlashBorrower -{ - using SafeERC20 for IERC20; - error CollateralNotSet(); - error MarketNotSet(address market); - error SwapFailed(); - error DOLAInvalidBorrow(uint256 expected, uint256 actual); - error DOLAInvalidRepay(uint256 expected, uint256 actual); - error InvalidProxyAddress(); - error InvalidHelperAddress(); - error InvalidAction(bytes32 action); - error NotFlashMinter(address caller); - error NotALE(address caller); - error NothingToDeposit(); - error DepositFailed(uint256 expected, uint256 actual); - error WithdrawFailed(uint256 expected, uint256 actual); - error TotalSupplyChanged(uint256 expected, uint256 actual); - error CollateralIsZero(); - error NoMarket(address market); - error MarketSetupFailed( - address market, - address buySellToken, - address collateral, - address helper - ); - - // 1Inch ExchangeProxy address. - address payable public exchangeProxy; - - IDBR public constant DBR = IDBR(0xAD038Eb671c44b853887A7E32528FaB35dC5D710); - - IERC3156FlashLender public constant flash = - IERC3156FlashLender(0x6C5Fdc0c53b122Ae0f15a863C349f3A481DE8f1F); - - bytes32 public constant CALLBACK_SUCCESS = - keccak256("ERC3156FlashBorrower.onFlashLoan"); - - bytes32 public constant LEVERAGE = keccak256("LEVERAGE"); - bytes32 public constant DELEVERAGE = keccak256("DELEVERAGE"); - - struct Market { - IERC20 buySellToken; - IERC20 collateral; - ITransformHelper helper; - bool useProxy; - } - - struct Permit { - uint256 deadline; - uint8 v; - bytes32 r; - bytes32 s; - } - - struct DBRHelper { - uint256 amountIn; // DOLA or DBR - uint256 minOut; // DOLA or DBR - uint256 dola; // DOLA to extra borrow or extra repay - } - - event LeverageUp( - address indexed market, - address indexed account, - uint256 dolaFlashMinted, // DOLA flash minted for buying collateral only - uint256 collateralDeposited, // amount of collateral deposited into the escrow - uint256 dolaBorrowed, // amount of DOLA borrowed on behalf of the user - uint256 dolaForDBR // amount of DOLA used for buying DBR - ); - - event LeverageDown( - address indexed market, - address indexed account, - uint256 dolaFlashMinted, // Flash minted DOLA for repaying leverage only - uint256 collateralSold, // amount of collateral/underlying sold - uint256 dolaUserRepaid, // amount of DOLA deposited by the user as part of the repay - uint256 dbrSoldForDola // amount of DBR sold for DOLA - ); - - event Deposit( - address indexed market, - address indexed account, - address indexed token, // token used for initial deposit (could be collateral or buySellToken) - uint256 depositAmount - ); - - event NewMarket( - address indexed market, - address indexed buySellToken, - address collateral, - address indexed helper - ); - - event NewHelper(address indexed market, address indexed helper); - - // Mapping of market to Market structs - // NOTE: in normal cases sellToken/buyToken is the collateral token, - // in other cases it could be different (eg. st-yCRV is collateral, yCRV is the token to be swapped from/to DOLA) - // or with DOLA curve LPs, LP token is the collateral and DOLA is the token to be swapped from/to - mapping(address => Market) public markets; - - modifier dolaSupplyUnchanged() { - uint256 totalSupply = dola.totalSupply(); - _; - if (totalSupply != dola.totalSupply()) - revert TotalSupplyChanged(totalSupply, dola.totalSupply()); - } - - constructor( - address _exchangeProxy, - address _pool - ) Ownable(msg.sender) CurveDBRHelper(_pool) { - exchangeProxy = payable(address(_exchangeProxy)); - _approveDola(address(flash), type(uint).max); - } - - function setExchangeProxy(address _exchangeProxy) external onlyOwner { - if (_exchangeProxy == address(0)) revert InvalidProxyAddress(); - exchangeProxy = payable(_exchangeProxy); - } - - /// @notice Set the market for a collateral token - /// @param _buySellToken The token which will be bought/sold (usually the collateral token), probably underlying if there's a helper - /// @param _market The market contract - /// @param _helper Optional helper contract to transform collateral to buySelltoken and viceversa - /// @param useProxy Whether to use the Exchange Proxy or not - function setMarket( - address _market, - address _buySellToken, - address _helper, - bool useProxy - ) external onlyOwner { - if (!DBR.markets(_market)) revert NoMarket(_market); - - if (_helper == address(0)) { - if (_buySellToken != IMarket(_market).collateral()) { - revert MarketSetupFailed( - _market, - _buySellToken, - IMarket(_market).collateral(), - _helper - ); - } - } - - address collateral = IMarket(_market).collateral(); - markets[_market].buySellToken = IERC20(_buySellToken); - markets[_market].collateral = IERC20(collateral); - markets[_market].buySellToken.approve(_market, type(uint256).max); - - if (_buySellToken != collateral) { - markets[_market].collateral.approve(_market, type(uint256).max); - } - - if (_helper != address(0)) { - markets[_market].helper = ITransformHelper(_helper); - - markets[_market].buySellToken.approve(_helper, type(uint256).max); - markets[_market].collateral.approve(_helper, type(uint256).max); - } - - markets[_market].useProxy = useProxy; - emit NewMarket(_market, _buySellToken, collateral, _helper); - } - - /// @notice Update the helper contract - /// @param _market The market we want to update the helper contract for - /// @param _helper The helper contract - function updateMarketHelper( - address _market, - address _helper - ) external onlyOwner { - if (address(markets[_market].buySellToken) == address(0)) - revert MarketNotSet(_market); - - address oldHelper = address(markets[_market].helper); - if (oldHelper != address(0)) { - markets[_market].buySellToken.approve(oldHelper, 0); - markets[_market].collateral.approve(oldHelper, 0); - } - - markets[_market].helper = ITransformHelper(_helper); - - if (_helper != address(0)) { - markets[_market].buySellToken.approve(_helper, type(uint256).max); - markets[_market].collateral.approve(_helper, type(uint256).max); - } - - emit NewHelper(_market, _helper); - } - - /// @notice Leverage user position by minting DOLA, buying collateral, deposting into the user escrow and borrow DOLA on behalf to repay the minted DOLA - /// @dev Requires user to sign message to permit the contract to borrow DOLA on behalf - /// @param value Amount of DOLA to flash mint/burn - /// @param market The market contract - /// @param spender The `allowanceTarget` field from the API response. - /// @param swapCallData The `data` field from the API response. - /// @param permit Permit data - /// @param helperData Optional helper data in case the collateral needs to be transformed - /// @param dbrData Optional data in case the user wants to buy DBR and also withdraw some DOLA - function leveragePosition( - uint256 value, - address market, - address spender, - bytes calldata swapCallData, - Permit calldata permit, - bytes calldata helperData, - DBRHelper calldata dbrData - ) public payable nonReentrant dolaSupplyUnchanged { - if (address(markets[market].buySellToken) == address(0)) - revert MarketNotSet(market); - - bytes memory data = abi.encode( - LEVERAGE, - msg.sender, - market, - 0, // unused - spender, - swapCallData, - permit, - helperData, - dbrData - ); - - flash.flashLoan( - IERC3156FlashBorrower(address(this)), - address(dola), - value, - data - ); - } - - /// @notice Deposit collateral and instantly leverage user position by minting DOLA, buying collateral, deposting into the user escrow and borrow DOLA on behalf to repay the minted DOLA - /// @dev Requires user to sign message to permit the contract to borrow DOLA on behalf - /// @param initialDeposit Amount of collateral or underlying (in case of helper) to deposit - /// @param value Amount of DOLA to borrow - /// @param market The market address - /// @param spender The `allowanceTarget` field from the API response. - /// @param swapCallData The `data` field from the API response. - /// @param permit Permit data - /// @param helperData Optional helper data in case the collateral needs to be transformed - /// @param dbrData Optional data in case the user wants to buy DBR and also withdraw some DOLA - /// @param depositCollateral Whether the initialDeposit is the collateral or the underlying entry asset - function depositAndLeveragePosition( - uint256 initialDeposit, - uint256 value, - address market, - address spender, - bytes calldata swapCallData, - Permit calldata permit, - bytes calldata helperData, - DBRHelper calldata dbrData, - bool depositCollateral - ) external payable { - if (initialDeposit == 0) revert NothingToDeposit(); - - IERC20 depositToken; - - if (depositCollateral) { - depositToken = markets[market].collateral; - } else { - depositToken = markets[market].buySellToken; - } - - depositToken.safeTransferFrom( - msg.sender, - address(this), - initialDeposit - ); - emit Deposit(market, msg.sender, address(depositToken), initialDeposit); - - leveragePosition( - value, - market, - spender, - swapCallData, - permit, - helperData, - dbrData - ); - } - - /// @notice Repay a DOLA loan and withdraw collateral from the escrow - /// @dev Requires user to sign message to permit the contract to withdraw collateral from the escrow - /// @param value Amount of DOLA to repay - /// @param market The market contract - /// @param collateralAmount Collateral amount to withdraw from the escrow - /// @param spender The `allowanceTarget` field from the API response. - /// @param swapCallData The `data` field from the API response. - /// @param permit Permit data - /// @param helperData Optional helper data in case collateral needs to be transformed - /// @param dbrData Optional data in case the user wants to sell DBR - function deleveragePosition( - uint256 value, - address market, - uint256 collateralAmount, - address spender, - bytes calldata swapCallData, - Permit calldata permit, - bytes calldata helperData, - DBRHelper calldata dbrData - ) external payable nonReentrant dolaSupplyUnchanged { - if (address(markets[market].buySellToken) == address(0)) - revert MarketNotSet(market); - - bytes memory data = abi.encode( - DELEVERAGE, - msg.sender, - market, - collateralAmount, - spender, - swapCallData, - permit, - helperData, - dbrData - ); - - flash.flashLoan( - IERC3156FlashBorrower(address(this)), - address(dola), - value, - data - ); - } - - function onFlashLoan( - address initiator, - address, - uint256 amount, - uint256, - bytes calldata data - ) external returns (bytes32) { - if (initiator != address(this)) revert NotALE(initiator); - if (msg.sender != address(flash)) revert NotFlashMinter(msg.sender); - - (bytes32 ACTION, , , , , , , , ) = abi.decode( - data, - ( - bytes32, - address, - address, - uint256, - address, - bytes, - Permit, - bytes, - DBRHelper - ) - ); - - if (ACTION == LEVERAGE) _onFlashLoanLeverage(amount, data); - else if (ACTION == DELEVERAGE) _onFlashLoanDeleverage(amount, data); - else revert InvalidAction(bytes32(ACTION)); - - return CALLBACK_SUCCESS; - } - - function _onFlashLoanLeverage(uint256 _value, bytes memory data) internal { - ( - , - address _user, - address _market, - , - address _spender, - bytes memory _swapCallData, - Permit memory _permit, - bytes memory _helperData, - DBRHelper memory _dbrData - ) = abi.decode( - data, - ( - bytes32, - address, - address, - uint256, - address, - bytes, - Permit, - bytes, - DBRHelper - ) - ); - // Call the encoded swap function call on the contract at `swapTarget`, - // passing along any ETH attached to this function call to cover protocol fees. - if (markets[_market].useProxy) { - _approveDola(_spender, _value); - (bool success, ) = exchangeProxy.call{value: msg.value}( - _swapCallData - ); - if (!success) revert SwapFailed(); - } - - // Actual collateral/buyToken bought - uint256 collateralAmount = markets[_market].buySellToken.balanceOf( - address(this) - ); - if (collateralAmount == 0) revert CollateralIsZero(); - - // If there's a helper contract, the buyToken has to be transformed - if (address(markets[_market].helper) != address(0)) { - collateralAmount = _convertToCollateral( - collateralAmount, - _market, - _helperData - ); - } - - // Deposit and borrow on behalf - IMarket(_market).deposit( - _user, - markets[_market].collateral.balanceOf(address(this)) - ); - - _borrowDola(_user, _value, _permit, _dbrData, IMarket(_market)); - - if (_dbrData.dola != 0) dola.transfer(_user, _dbrData.dola); - - if (_dbrData.amountIn != 0) - _buyDbr(_dbrData.amountIn, _dbrData.minOut, _user); - // Scope to avoid stack too deep error - { - uint256 balance = dola.balanceOf(address(this)); - - if (balance > _value) dola.transfer(_user, balance - _value); - } - - // Refund any possible unspent fees to the sender. - if (address(this).balance > 0) - payable(_user).transfer(address(this).balance); - - emit LeverageUp( - _market, - _user, - _value, - collateralAmount, - _dbrData.dola, - _dbrData.amountIn - ); - } - - function _onFlashLoanDeleverage( - uint256 _value, - bytes memory data - ) internal { - ( - , - address _user, - address _market, - uint256 _collateralAmount, - address _spender, - bytes memory _swapCallData, - Permit memory _permit, - bytes memory _helperData, - DBRHelper memory _dbrData - ) = abi.decode( - data, - ( - bytes32, - address, - address, - uint256, - address, - bytes, - Permit, - bytes, - DBRHelper - ) - ); - - _repayAndWithdraw( - _user, - _value, - _collateralAmount, - _permit, - _dbrData, - IMarket(_market) - ); - - IERC20 sellToken = markets[_market].buySellToken; - - // If there's a helper contract, the collateral has to be transformed - if (address(markets[_market].helper) != address(0)) { - _collateralAmount = _convertToAsset( - _collateralAmount, - _market, - sellToken, - _helperData - ); - // Reimburse leftover collateral from conversion if any - uint256 collateralLeft = markets[_market].collateral.balanceOf( - address(this) - ); - - if (collateralLeft != 0) { - markets[_market].collateral.safeTransfer(_user, collateralLeft); - } - } - - // Call the encoded swap function call on the contract at `swapTarget`, - // passing along any ETH attached to this function call to cover protocol fees. - // NOTE: This will swap the collateral or helperCollateral for DOLA - if (markets[_market].useProxy) { - // Approve sellToken for spender - sellToken.approve(_spender, 0); - sellToken.approve(_spender, _collateralAmount); - (bool success, ) = exchangeProxy.call{value: msg.value}( - _swapCallData - ); - if (!success) revert SwapFailed(); - } - - if (address(markets[_market].helper) == address(0)) { - uint256 collateralAvailable = markets[_market].collateral.balanceOf( - address(this) - ); - - if (collateralAvailable != 0) { - markets[_market].collateral.safeTransfer( - _user, - collateralAvailable - ); - } - } else if (address(sellToken) != address(dola)) { - uint256 sellTokenBal = sellToken.balanceOf(address(this)); - // Send any leftover sellToken to the sender - if (sellTokenBal != 0) sellToken.safeTransfer(_user, sellTokenBal); - } - - // Scope to avoid stack too deep error - { - uint256 balance = dola.balanceOf(address(this)); - if (balance < _value) revert DOLAInvalidRepay(_value, balance); - // Send any extra DOLA to the sender (in case the collateral withdrawn and swapped exceeds the value to burn) - if (balance > _value) dola.transfer(_user, balance - _value); - } - - if (_dbrData.amountIn != 0) { - dbr.transferFrom(_user, address(this), _dbrData.amountIn); - _sellDbr(_dbrData.amountIn, _dbrData.minOut, _user); - } - - // Refund any unspent protocol fees to the sender. - if (address(this).balance > 0) - payable(_user).transfer(address(this).balance); - - emit LeverageDown( - _market, - _user, - _value, - _collateralAmount, - _dbrData.dola, - _dbrData.amountIn - ); - } - - /// @notice Mint DOLA to this contract and approve the spender - /// @param spender The spender address - /// @param _value Amount of DOLA to mint and approve - function _approveDola(address spender, uint256 _value) internal { - dola.approve(spender, _value); - } - - /// @notice Borrow DOLA on behalf of the user - /// @param _value Amount of DOLA to borrow - /// @param _permit Permit data - /// @param _dbrData DBR data - /// @param market The market contract - function _borrowDola( - address _user, - uint256 _value, - Permit memory _permit, - DBRHelper memory _dbrData, - IMarket market - ) internal { - uint256 dolaToBorrow = _value; - - if (_dbrData.dola != 0) { - dolaToBorrow += _dbrData.dola; - } - - if (_dbrData.amountIn != 0) { - dolaToBorrow += _dbrData.amountIn; - } - // We borrow the amount of DOLA we minted before plus the amount for buying DBR if any - market.borrowOnBehalf( - _user, - dolaToBorrow, - _permit.deadline, - _permit.v, - _permit.r, - _permit.s - ); - - if (dola.balanceOf(address(this)) < dolaToBorrow) - revert DOLAInvalidBorrow( - dolaToBorrow, - dola.balanceOf(address(this)) - ); - } - - /// @notice Repay DOLA loan and withdraw collateral from the escrow - /// @param _value Amount of DOLA to repay - /// @param _collateralAmount Collateral amount to withdraw from the escrow - /// @param _permit Permit data - /// @param _dbrData DBR data - /// @param market The market contract - function _repayAndWithdraw( - address _user, - uint256 _value, - uint256 _collateralAmount, - Permit memory _permit, - DBRHelper memory _dbrData, - IMarket market - ) internal { - if (_dbrData.dola != 0) { - dola.transferFrom(_user, address(this), _dbrData.dola); - _approveDola(address(market), _value + _dbrData.dola); - market.repay(_user, _value + _dbrData.dola); - } else { - _approveDola(address(market), _value); - market.repay(_user, _value); - } - - // withdraw amount from ZERO EX quote - market.withdrawOnBehalf( - _user, - _collateralAmount, - _permit.deadline, - _permit.v, - _permit.r, - _permit.s - ); - } - - /// @notice convert a collateral amount into the underlying asset - /// @param _collateralAmount Collateral amount to convert - /// @param _market The market contract - /// @param sellToken The sell token (the underlying asset) - /// @param _helperData Optional helper data - /// @return assetAmount The amount of sellToken/underlying after the conversion - function _convertToAsset( - uint256 _collateralAmount, - address _market, - IERC20 sellToken, - bytes memory _helperData - ) internal returns (uint256) { - // Collateral amount is now transformed into sellToken - uint256 assetAmount = markets[_market].helper.transformFromCollateral( - _collateralAmount, - _helperData - ); - uint256 actualAssetAmount = sellToken.balanceOf(address(this)); - - if (actualAssetAmount < assetAmount) - revert WithdrawFailed(assetAmount, actualAssetAmount); - - return actualAssetAmount; - } - - /// @notice convert the underlying asset amount into the collateral - /// @param _assetAmount The amount of sellToken/underlying to convert - /// @param _market The market contract - /// @param _helperData Optional helper data - /// @return collateralAmount The amount of collateral after the conversion - function _convertToCollateral( - uint256 _assetAmount, - address _market, - bytes memory _helperData - ) internal returns (uint256) { - // Collateral amount is now transformed - uint256 collateralAmount = markets[_market] - .helper - .transformToCollateral(_assetAmount, _helperData); - - uint256 actualCollateralAmount = markets[_market].collateral.balanceOf( - address(this) - ); - if (actualCollateralAmount < collateralAmount) - revert DepositFailed(collateralAmount, actualCollateralAmount); - - return actualCollateralAmount; - } - - // solhint-disable-next-line no-empty-blocks - receive() external payable {} -} diff --git a/src/util/ALEV2.sol b/src/util/ALEV2.sol index 37894e71..fa37e827 100644 --- a/src/util/ALEV2.sol +++ b/src/util/ALEV2.sol @@ -3,8 +3,7 @@ pragma solidity ^0.8.0; import "src/interfaces/IMarket.sol"; import "src/interfaces/IPendleHelper.sol"; -import {CurveDBRHelper} from "src/util/CurveDBRHelper.sol"; -import {Ownable} from "lib/openzeppelin-contracts/contracts/access/Ownable.sol"; +import {CurveHelper} from "src/util/CurveHelper.sol"; import {ReentrancyGuard} from "lib/openzeppelin-contracts/contracts/utils/ReentrancyGuard.sol"; import {IERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol"; @@ -50,9 +49,8 @@ interface IERC3156FlashLender { // Accelerated leverage engine contract ALEV2 is - Ownable, ReentrancyGuard, - CurveDBRHelper, + CurveHelper, IERC3156FlashBorrower { using SafeERC20 for IERC20; @@ -81,8 +79,6 @@ contract ALEV2 is mapping(address => bool) public isExchangeProxy; - IDBR public constant DBR = IDBR(0xAD038Eb671c44b853887A7E32528FaB35dC5D710); - IERC3156FlashLender public constant flash = IERC3156FlashLender(0x6C5Fdc0c53b122Ae0f15a863C349f3A481DE8f1F); @@ -153,21 +149,22 @@ contract ALEV2 is mapping(address => Market) public markets; constructor( - address _pool - ) Ownable(msg.sender) CurveDBRHelper(_pool) { - dola.approve(address(flash), type(uint).max); + address _pool, + address _gov + ) CurveHelper(_pool, _gov) { + DOLA.approve(address(flash), type(uint).max); } /// @notice Allow an exchange proxy /// @param _proxy The proxy address - function allowProxy(address _proxy) external onlyOwner { + function allowProxy(address _proxy) external onlyGov { if (_proxy == address(0)) revert InvalidProxyAddress(); isExchangeProxy[_proxy] = true; } /// @notice Deny an exchange proxy /// @param _proxy The proxy address - function denyProxy(address _proxy) external onlyOwner { + function denyProxy(address _proxy) external onlyGov { if (_proxy == address(0)) revert InvalidProxyAddress(); isExchangeProxy[_proxy] = false; } @@ -182,8 +179,8 @@ contract ALEV2 is address _buySellToken, address _helper, bool useProxy - ) external onlyOwner { - if (!DBR.markets(_market)) revert NoMarket(_market); + ) external onlyGov { + if (!IDBR(address(DBR)).markets(_market)) revert NoMarket(_market); address collateral = IMarket(_market).collateral(); if (_helper == address(0) && _buySellToken != collateral) { @@ -220,7 +217,7 @@ contract ALEV2 is function updateMarketHelper( address _market, address _helper - ) external onlyOwner { + ) external onlyGov { if (address(markets[_market].buySellToken) == address(0)) revert MarketNotSet(_market); if (_helper == address(0)) revert InvalidHelperAddress(); @@ -271,7 +268,7 @@ contract ALEV2 is flash.flashLoan( IERC3156FlashBorrower(address(this)), - address(dola), + address(DOLA), value, data ); @@ -364,7 +361,7 @@ contract ALEV2 is flash.flashLoan( IERC3156FlashBorrower(address(this)), - address(dola), + address(DOLA), value, data ); @@ -431,7 +428,7 @@ contract ALEV2 is // passing along any ETH attached to this function call to cover protocol fees. if (markets[_market].useProxy) { if(!isExchangeProxy[_proxy]) revert InvalidProxyAddress(); - dola.approve(_proxy, _value); + DOLA.approve(_proxy, _value); (bool success, ) = payable(_proxy).call{value: msg.value}( _swapCallData ); @@ -462,7 +459,7 @@ contract ALEV2 is _borrowDola(_user, _value, _permit, _dbrData, IMarket(_market)); - if (_dbrData.dola != 0) dola.transfer(_user, _dbrData.dola); + if (_dbrData.dola != 0) DOLA.transfer(_user, _dbrData.dola); if (_dbrData.amountIn > 0 && _dbrData.minOut > 0) _buyDbr(_dbrData.amountIn, _dbrData.minOut, _user); @@ -563,14 +560,14 @@ contract ALEV2 is collateralAvailable ); } - } else if (address(sellToken) != address(dola)) { + } else if (address(sellToken) != address(DOLA)) { uint256 sellTokenBal = sellToken.balanceOf(address(this)); // Send any leftover sellToken to the sender if (sellTokenBal != 0) sellToken.safeTransfer(_user, sellTokenBal); } if (_dbrData.amountIn > 0 && _dbrData.minOut > 0) { - dbr.transferFrom(_user, address(this), _dbrData.amountIn); + DBR.transferFrom(_user, address(this), _dbrData.amountIn); _sellDbr(_dbrData.amountIn, _dbrData.minOut, _user); } @@ -609,10 +606,10 @@ contract ALEV2 is _permit.s ); - if (dola.balanceOf(address(this)) < dolaToBorrow) + if (DOLA.balanceOf(address(this)) < dolaToBorrow) revert DOLAInvalidBorrow( dolaToBorrow, - dola.balanceOf(address(this)) + DOLA.balanceOf(address(this)) ); } @@ -631,10 +628,10 @@ contract ALEV2 is IMarket market ) internal { if (_dbrData.dola != 0) { - dola.transferFrom(_user, address(this), _dbrData.dola); + DOLA.transferFrom(_user, address(this), _dbrData.dola); _value += _dbrData.dola; } - dola.approve(address(market), _value); + DOLA.approve(address(market), _value); market.repay(_user, _value); // withdraw amount from ZERO EX quote @@ -708,15 +705,12 @@ contract ALEV2 is /// @param _user The user address /// @param _value The amount of flash borrowed DOLA to be repaid function _refundExcess(address _user, uint256 _value) internal { - uint256 balance = dola.balanceOf(address(this)); + uint256 balance = DOLA.balanceOf(address(this)); if (balance < _value) revert DOLAInvalidRepay(_value, balance); // Send any extra DOLA to the sender - if (balance > _value) dola.transfer(_user, balance - _value); + if (balance > _value) DOLA.transfer(_user, balance - _value); // Refund any unspent protocol fees to the sender. if (address(this).balance > 0) payable(_user).transfer(address(this).balance); } - - // solhint-disable-next-line no-empty-blocks - receive() external payable {} } diff --git a/src/util/CurveDBRHelper.sol b/src/util/CurveDBRHelper.sol deleted file mode 100644 index 37c4d066..00000000 --- a/src/util/CurveDBRHelper.sol +++ /dev/null @@ -1,132 +0,0 @@ -pragma solidity ^0.8.13; -//import "src/util/OffchainAbstractHelper.sol"; -import "src/interfaces/IERC20.sol"; -import "src/interfaces/IDola.sol"; - -interface ICurvePool { - function coins(uint index) external view returns (address); - - function get_dy(uint i, uint j, uint dx) external view returns (uint); - - function exchange( - uint i, - uint j, - uint dx, - uint min_dy, - bool use_eth - ) external payable returns (uint); - - function exchange( - uint i, - uint j, - uint dx, - uint min_dy, - bool use_eth, - address receiver - ) external payable returns (uint); -} - -contract CurveDBRHelper { - ICurvePool public immutable curvePool; - IDola constant dola = IDola(0x865377367054516e17014CcdED1e7d814EDC9ce4); - IERC20 constant dbr = IERC20(0xAD038Eb671c44b853887A7E32528FaB35dC5D710); - - uint dbrIndex; - uint dolaIndex; - - constructor(address _pool) { - curvePool = ICurvePool(_pool); - dola.approve(_pool, type(uint).max); - dbr.approve(_pool, type(uint).max); - if (ICurvePool(_pool).coins(0) == address(dola)) { - dolaIndex = 0; - dbrIndex = 1; - } else { - dolaIndex = 1; - dbrIndex = 0; - } - } - - /** - @notice Sells an exact amount of DBR for DOLA in a curve pool - @param amount Amount of DBR to sell - @param minOut minimum amount of DOLA to receive - */ - function _sellDbr(uint amount, uint minOut, address receiver) internal { - if (amount > 0) { - curvePool.exchange( - dbrIndex, - dolaIndex, - amount, - minOut, - false, - receiver - ); - } - } - - /** - @notice Buys an exact amount of DBR for DOLA in a curve pool - @param amount Amount of DOLA to sell - @param minOut minimum amount of DBR out - */ - function _buyDbr(uint amount, uint minOut, address receiver) internal { - if (amount > 0) { - curvePool.exchange( - dolaIndex, - dbrIndex, - amount, - minOut, - false, - receiver - ); - } - } - - /** - @notice Approximates the total amount of dola and dbr needed to borrow a dolaBorrowAmount while also borrowing enough to buy the DBR needed to cover for the borrowing period - @dev Uses a binary search to approximate the amounts needed. Should only be called as part of generating transaction parameters. - @param dolaBorrowAmount Amount of dola the user wishes to end up with - @param period Amount of time in seconds the loan will last - @param iterations Number of approximation iterations. The higher the more precise the result - */ - function approximateDolaAndDbrNeeded( - uint dolaBorrowAmount, - uint period, - uint iterations - ) public view returns (uint dolaForDbr, uint dbrNeeded) { - uint amountIn = dolaBorrowAmount; - uint stepSize = amountIn / 2; - uint dbrReceived = curvePool.get_dy(dolaIndex, dbrIndex, amountIn); - uint dbrToBuy = ((amountIn + dolaBorrowAmount) * period) / 365 days; - uint dist = dbrReceived > dbrToBuy - ? dbrReceived - dbrToBuy - : dbrToBuy - dbrReceived; - for (uint i; i < iterations; ++i) { - uint newAmountIn = amountIn; - if (dbrReceived > dbrToBuy) { - newAmountIn -= stepSize; - } else { - newAmountIn += stepSize; - } - uint newDbrReceived = curvePool.get_dy( - dolaIndex, - dbrIndex, - newAmountIn - ); - uint newDbrToBuy = ((newAmountIn + dolaBorrowAmount) * period) / - 365 days; - uint newDist = newDbrReceived > newDbrToBuy - ? newDbrReceived - newDbrToBuy - : newDbrToBuy - newDbrReceived; - if (newDist < dist) { - dbrReceived = newDbrReceived; - dbrToBuy = newDbrToBuy; - dist = newDist; - amountIn = newAmountIn; - } - stepSize /= 2; - } - return (amountIn, ((dolaBorrowAmount + amountIn) * period) / 365 days); - } -} diff --git a/src/util/CurveHelper.sol b/src/util/CurveHelper.sol index 099e7831..b0d2feeb 100644 --- a/src/util/CurveHelper.sol +++ b/src/util/CurveHelper.sol @@ -1,30 +1,36 @@ pragma solidity ^0.8.13; import "src/util/OffchainAbstractHelper.sol"; +import {Ownable} from "src/util/Ownable.sol"; interface ICurvePool { function coins(uint index) external view returns(address); function get_dy(uint i, uint j, uint dx) external view returns(uint); - function exchange(uint i, uint j, uint dx, uint min_dy, bool use_eth) external payable returns(uint); - function exchange(uint i, uint j, uint dx, uint min_dy, bool use_eth, address receiver) external payable returns(uint); + function exchange(uint i, uint j, uint dx, uint min_dy, address receiver) external returns(uint); + function exchange(uint i, uint j, uint dx, uint min_dy) external returns(uint); } -contract CurveHelper is OffchainAbstractHelper{ +contract CurveHelper is Ownable, OffchainAbstractHelper { - ICurvePool public immutable curvePool; - uint dbrIndex; - uint dolaIndex; + ICurvePool public curvePool; + + uint public dbrIndex = type(uint).max; + uint public dolaIndex = type(uint).max; - constructor(address _pool) { + event NewCurvePool(address indexed newPool, uint256 dolaIndex, uint256 dbrIndex); + + constructor(address _pool, address _gov) Ownable(_gov) { curvePool = ICurvePool(_pool); + for(uint i; i < 3; ++i){ + if(curvePool.coins(i) == address(DOLA)){ + dolaIndex = i; + } + else if(curvePool.coins(i) == address(DBR)){ + dbrIndex = i; + } + } + require(dolaIndex != type(uint).max && dbrIndex != type(uint).max, "CurveHelper: pool missing DOLA or DBR"); DOLA.approve(_pool, type(uint).max); DBR.approve(_pool, type(uint).max); - if(ICurvePool(_pool).coins(0) == address(DOLA)){ - dolaIndex = 0; - dbrIndex = 1; - } else { - dolaIndex = 1; - dbrIndex = 0; - } } /** @@ -32,9 +38,9 @@ contract CurveHelper is OffchainAbstractHelper{ @param amount Amount of DBR to sell @param minOut minimum amount of DOLA to receive */ - function _sellDbr(uint amount, uint minOut) internal override { + function _sellDbr(uint amount, uint minOut, address receiver) internal override { if(amount > 0){ - curvePool.exchange(dbrIndex, dolaIndex, amount, minOut, false); + curvePool.exchange(dbrIndex, dolaIndex, amount, minOut, receiver); } } @@ -45,7 +51,7 @@ contract CurveHelper is OffchainAbstractHelper{ */ function _buyDbr(uint amount, uint minOut, address receiver) internal override { if(amount > 0) { - curvePool.exchange(dolaIndex, dbrIndex, amount, minOut, false, receiver); + curvePool.exchange(dolaIndex, dbrIndex, amount, minOut, receiver); } } @@ -82,4 +88,25 @@ contract CurveHelper is OffchainAbstractHelper{ } return (amountIn, (dolaBorrowAmount + amountIn) * period / 365 days); } + + /** + @notice Sets a new curve pool + @dev Can only be called by the gov + @param _pool Address of the new curve pool + @param _dolaIndex Index of DOLA in the new curve pool + @param _dbrIndex Index of DBR in the new curve pool + */ + function setCurvePool(address _pool, uint256 _dolaIndex, uint256 _dbrIndex) external onlyGov { + ICurvePool newPool = ICurvePool(_pool); + require(newPool.coins(_dolaIndex) == address(DOLA), "Wrong dola index"); + require(newPool.coins(_dbrIndex) == address(DBR), "Wrong dbr index"); + DOLA.approve(address(curvePool), 0); + DBR.approve(address(curvePool), 0); + curvePool = newPool; + DOLA.approve(_pool, type(uint).max); + DBR.approve(_pool, type(uint).max); + dolaIndex = _dolaIndex; + dbrIndex = _dbrIndex; + emit NewCurvePool(_pool, _dolaIndex, _dbrIndex); + } } diff --git a/src/util/DbrHelper.sol b/src/util/DbrHelper.sol index b3b354c5..d9f42421 100644 --- a/src/util/DbrHelper.sol +++ b/src/util/DbrHelper.sol @@ -3,7 +3,7 @@ pragma solidity ^0.8.20; import "src/interfaces/IERC20.sol"; import "src/interfaces/IMarket.sol"; import {ReentrancyGuard} from "openzeppelin-contracts/contracts/utils/ReentrancyGuard.sol"; -import {Ownable} from "openzeppelin-contracts/contracts/access/Ownable.sol"; +import {Ownable} from "src/util/Ownable.sol"; interface ICurvePool { function exchange( @@ -11,9 +11,10 @@ interface ICurvePool { uint j, uint dx, uint min_dy, - bool use_eth, address receiver - ) external payable returns (uint); + ) external returns (uint); + + function coins(uint index) external view returns (address); } interface IINVEscrow { @@ -44,8 +45,6 @@ contract DbrHelper is Ownable, ReentrancyGuard { IMarket public constant INV_MARKET = IMarket(0xb516247596Ca36bf32876199FBdCaD6B3322330B); - ICurvePool public constant CURVE_POOL = - ICurvePool(0xC7DE47b9Ca2Fc753D6a2F167D8b3e19c6D18b19a); IERC20 public constant DOLA = IERC20(0x865377367054516e17014CcdED1e7d814EDC9ce4); IERC20 public constant DBR = @@ -53,11 +52,13 @@ contract DbrHelper is Ownable, ReentrancyGuard { IERC20 public constant INV = IERC20(0x41D5D79431A913C4aE7d69a668ecdfE5fF9DFB68); - uint256 public constant DOLA_INDEX = 0; - uint256 public constant DBR_INDEX = 1; - uint256 public constant INV_INDEX = 2; + uint256 public dolaIndex = type(uint).max; + uint256 public dbrIndex = type(uint).max; + uint256 public invIndex = type(uint).max; uint256 public constant DENOMINATOR = 10000; // 100% in basis points + ICurvePool public curvePool; + event Sell( address indexed claimer, uint amountIn, @@ -77,9 +78,23 @@ contract DbrHelper is Ownable, ReentrancyGuard { uint invAmount ); event MarketApproved(address indexed market); + event NewCurvePool(address indexed newPool, uint256 dolaIndex, uint256 dbrIndex, uint256 invIndex); - constructor() Ownable(msg.sender) { - DBR.approve(address(CURVE_POOL), type(uint).max); + constructor(address _curvePool, address _gov) Ownable(_gov) { + curvePool = ICurvePool(_curvePool); + for(uint i; i < 3; i++){ + if(curvePool.coins(i) == address(DOLA)){ + dolaIndex = i; + } + else if(curvePool.coins(i) == address(DBR)){ + dbrIndex = i; + } + else if(curvePool.coins(i) == address(INV)){ + invIndex = i; + } + } + require(dolaIndex + dbrIndex + invIndex == 3, "Incorrect indices"); + DBR.approve(address(curvePool), type(uint).max); INV.approve(address(INV_MARKET), type(uint).max); } @@ -174,7 +189,7 @@ contract DbrHelper is Ownable, ReentrancyGuard { dolaAmount = _sellDbr( sellAmountForDola, params.minOutDola, - DOLA_INDEX, + dolaIndex, params.toDola ); } @@ -209,7 +224,7 @@ contract DbrHelper is Ownable, ReentrancyGuard { address to ) internal returns (uint256 invAmount) { // Sell DBR for INV - _sellDbr(amount, minOutInv, INV_INDEX, address(this)); + _sellDbr(amount, minOutInv, invIndex, address(this)); // Deposit INV invAmount = INV.balanceOf(address(this)); INV_MARKET.deposit(to, invAmount); @@ -229,7 +244,7 @@ contract DbrHelper is Ownable, ReentrancyGuard { Repay calldata repay ) internal returns (uint256 dolaAmount, uint256 repaidAmount) { // Sell DBR for DOLA - dolaAmount = _sellDbr(amount, minOutDola, DOLA_INDEX, address(this)); + dolaAmount = _sellDbr(amount, minOutDola, dolaIndex, address(this)); // Repay debt repaidAmount = _repay(repay, dolaAmount); } @@ -282,12 +297,11 @@ contract DbrHelper is Ownable, ReentrancyGuard { uint indexOut, address receiver ) internal returns (uint256 amountOut) { - amountOut = CURVE_POOL.exchange( - DBR_INDEX, + amountOut = curvePool.exchange( + dbrIndex, indexOut, amountIn, minOut, - false, receiver ); emit Sell(msg.sender, amountIn, amountOut, indexOut, receiver); @@ -325,4 +339,30 @@ contract DbrHelper is Ownable, ReentrancyGuard { revert SellPercentageTooHigh(); if (repay.percentage > DENOMINATOR) revert RepayPercentageTooHigh(); } + + /** + @notice Sets a new curve pool + @dev Can only be called by the gov + @param _pool Address of the new curve pool + @param _dolaIndex Index of DOLA in the new curve pool + @param _dbrIndex Index of DBR in the new curve pool + @param _invIndex Index of INV in the new curve pool + */ + function setCurvePool(address _pool, uint256 _dolaIndex, uint256 _dbrIndex, uint256 _invIndex) external onlyGov { + ICurvePool newPool = ICurvePool(_pool); + require(newPool.coins(_dolaIndex) == address(DOLA), "Wrong dola index"); + require(newPool.coins(_dbrIndex) == address(DBR), "Wrong dbr index"); + require(newPool.coins(_invIndex) == address(INV), "Wrong inv index"); + + DBR.approve(address(curvePool), 0); + + curvePool = ICurvePool(_pool); + + DBR.approve(_pool, type(uint).max); + + dolaIndex = _dolaIndex; + dbrIndex = _dbrIndex; + invIndex = _invIndex; + emit NewCurvePool(_pool, _dolaIndex, _dbrIndex, _invIndex); + } } diff --git a/src/util/Guardable.sol b/src/util/Guardable.sol new file mode 100644 index 00000000..62812c26 --- /dev/null +++ b/src/util/Guardable.sol @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import "src/util/Ownable.sol"; + +/** + * @title Guardable + * @notice This contract adds Guardian and Operator roles functionality + */ +contract Guardable is Ownable { + error NotGuardian(); + error NotOperator(); + error NotGuardianOrGov(); + error NotOperatorOrGov(); + + address public guardian; + address public operator; + + event NewGuardian(address guardian); + event NewOperator(address operator); + + /** @dev Constructor + @param _gov The address of Inverse Finance governance + @param _guardian The address of the guardian + @param _operator The address of the operator + **/ + constructor(address _gov, address _guardian, address _operator) Ownable(_gov) { + guardian = _guardian; + operator = _operator; + } + + modifier onlyGuardian() { + if (msg.sender != guardian) revert NotGuardian(); + _; + } + + modifier onlyOperator() { + if (msg.sender != operator) revert NotOperator(); + _; + } + + modifier onlyGuardianOrGov() { + if (msg.sender != guardian && msg.sender != gov) + revert NotGuardianOrGov(); + _; + } + + modifier onlyOperatorOrGov() { + if (msg.sender != operator && msg.sender != gov) + revert NotOperatorOrGov(); + _; + } + + + /** + * @notice Sets the guardian role + * @dev Only callable by gov + * @param _guardian The address of the guardian + */ + function setGuardian(address _guardian) external onlyGov { + guardian = _guardian; + emit NewGuardian(_guardian); + } + + /** + * @notice Sets the operator role + * @dev Only callable by gov + * @param _operator The address of the operator + */ + function setOperator(address _operator) external onlyGov { + operator = _operator; + emit NewOperator(_operator); + } +} diff --git a/src/util/Migration.sol b/src/util/Migration.sol new file mode 100644 index 00000000..b61f6a10 --- /dev/null +++ b/src/util/Migration.sol @@ -0,0 +1,94 @@ +pragma solidity ^0.8.13; + +import {ALEV2, IERC20, IPendleHelper} from "src/util/ALEV2.sol"; +import {DbrHelper} from "src/util/DbrHelper.sol"; + +contract Migration { + address[] public markets = [0x63Df5e23Db45a2066508318f172bA45B9CD37035, // WETH + 0x27b6c301Fd441f3345d61B7a4245E1F823c3F9c4, // yvyCRV + 0x3474ad0e3a9775c9F68B415A7a9880B0CAB9397a, //cvxCRV + 0xdc2265cBD15beD67b5F2c0B82e23FcE4a07ddF6b, //CVX + 0x3FD3daBB9F9480621C8A111603D3Ba70F17550BC, // wstETH + 0x48BA574Edf0bc4E2E40B529863aaA6a67c264E7C, // WBTC + 0x0c0bb843FAbda441edeFB93331cFff8EC92bD168, // styETH + 0x79eF6d28C41e47A588E2F2ffB4140Eb6d952AEc4, //sUSDe + 0x2A256306D8ba899E33B01e495982656884Ac77FF,// cbBTC + 0xb427fC22561f3963B04202F9bb5BCEbd76c14A99, //sUSDe-DOLA + 0x4E264618dC015219CD83dbc53B31251D73c2db1a, //yv-sUSDe-DOLA + 0xD68d3a44d46dd50BFeBa8Cca544717B76e7C4b29, //sUSDS-DOLA + 0x4A33baFA8a31E4ec9649f65646022cAD1957808b, //yv-sUSDS-DOLA + 0x2fed508aAc87c0e6f0b647Fe83164A7AA6eb2FC9, //scrvUSD-DOLA + 0x5bb8f6aAcFF2971B42F9fE6945D24726A2541CF2, //yv-scrvUSD-DOLA + 0x63D27fC9d463Ed727676367D3F818999962737E8, //scrvUSD-sDOLA + 0xb8bc1E9c0a2d445bc39d2A745F47619E954dD565, // yv-scrvUSD-sDOLA + 0x3Ac5CEbC7A417DB619B85660E4f284f5643DFd5e, //USR-DOLA + 0xC0086Ff652c67f43F00F0F9C69Ef6c33640C8cCF, //yv-USR-DOLA + 0xe4D47Ef77AC2C3FA4019Cd169Ac1Dd9E27cb12E4, //wstUSR-DOLA + 0x28684485369f7478f42aAA62660123AB5D573537, //yv-wstUSR-DOLA + 0x63fAd99705a255fE2D500e498dbb3A9aE5AA1Ee8, // CRV + 0xb516247596Ca36bf32876199FBdCaD6B3322330B // INV + ]; + + ALEV2 public constant OLD_ALE = ALEV2(payable(0x4dF2EaA1658a220FDB415B9966a9ae7c3d16e240)); + address public constant GOV = address(0x926dF14a23BE491164dCF93f4c468A50ef659D5B); + + ALEV2 public immutable ale; + DbrHelper public immutable dbrHelper; + + + constructor(address _ale, address _dbrHelper) { + ale = ALEV2(payable(_ale)); + dbrHelper = DbrHelper(_dbrHelper); + } + + modifier onlyGov() { + require(msg.sender == GOV, "Only gov"); + _; + } + + function migrate() external onlyGov { + ale.claimPendingGov(); + dbrHelper.claimPendingGov(); + + _setALEMarkets(); + _setDbrMarkets(); + + ale.setPendingGov(GOV); + dbrHelper.setPendingGov(GOV); + } + + function _setALEMarkets() internal { + for (uint i = 0; i < markets.length; i++) { + (IERC20 buySellToken, + , + IPendleHelper helper, + bool useProxy) = getMarket(markets[i]); + + ale.setMarket(markets[i], address(buySellToken), address(helper), useProxy); + } + + } + + function _setDbrMarkets() internal { + for (uint i = 0; i < markets.length; i++) { + dbrHelper.approveMarket(markets[i]); + } + } + + function getMarket(address market) internal view returns (IERC20 buySellToken, + IERC20 collateral, + IPendleHelper helper, + bool useProxy) { + + (buySellToken, + collateral, + helper, + useProxy) = OLD_ALE.markets(market); + + return (buySellToken, collateral, helper, useProxy); + } + + function getMarkets() external view returns (address[] memory) { + return markets; + } +} \ No newline at end of file diff --git a/src/util/OffchainAbstractHelper.sol b/src/util/OffchainAbstractHelper.sol index bc735f4b..8b0e0337 100644 --- a/src/util/OffchainAbstractHelper.sol +++ b/src/util/OffchainAbstractHelper.sol @@ -26,6 +26,7 @@ abstract contract OffchainAbstractHelper { @notice Buys DBR for an amount of Dola @param amount Amount of Dola to spend on DBR @param minOut minimum amount of DBR to receive + @param receiver Address to send purchased DBR to */ function _buyDbr(uint amount, uint minOut, address receiver) internal virtual; @@ -33,8 +34,9 @@ abstract contract OffchainAbstractHelper { @notice Sells an exact amount of DBR for DOLA @param amount Amount of DBR to sell @param minOut minimum amount of DOLA to receive + @param receiver Address to send purchased DOLA to */ - function _sellDbr(uint amount, uint minOut) internal virtual; + function _sellDbr(uint amount, uint minOut, address receiver) internal virtual; /** @notice Approximates the amount of additional DOLA and DBR needed to sustain dolaBorrowAmount over the period @@ -172,10 +174,10 @@ abstract contract OffchainAbstractHelper { //If user has less DBR than ordered, sell what's available if(dbrAmountToSell > dbrBal){ DBR.transferFrom(msg.sender, address(this), dbrBal); - _sellDbr(dbrBal, minDolaFromDbr); + _sellDbr(dbrBal, minDolaFromDbr, address(this)); } else { DBR.transferFrom(msg.sender, address(this), dbrAmountToSell); - _sellDbr(dbrAmountToSell, minDolaFromDbr); + _sellDbr(dbrAmountToSell, minDolaFromDbr, address(this)); } uint debt = market.debts(msg.sender); diff --git a/src/util/Ownable.sol b/src/util/Ownable.sol new file mode 100644 index 00000000..83b93edc --- /dev/null +++ b/src/util/Ownable.sol @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +/** + * @title Ownable + * @notice This contract adds Ownable functionality to the contract + */ +contract Ownable { + error NotGov(); + error NotPendingGov(); + + address public gov; + address public pendingGov; + + event NewGov(address indexed gov); + event NewPendingGov(address indexed pendingGov); + + /** @dev Constructor + @param _gov The address of Inverse Finance governance + **/ + constructor(address _gov) { + gov = _gov; + } + + modifier onlyGov() { + if (msg.sender != gov) revert NotGov(); + _; + } + + /** + * @notice Sets the pendingGov, which can claim gov role. + * @dev Only callable by gov + * @param _pendingGov The address of the pendingGov + */ + function setPendingGov(address _pendingGov) external onlyGov { + pendingGov = _pendingGov; + emit NewPendingGov(_pendingGov); + } + + /** + * @notice Claims the gov role + * @dev Only callable by pendingGov + */ + function claimPendingGov() external { + if (msg.sender != pendingGov) revert NotPendingGov(); + gov = pendingGov; + pendingGov = address(0); + emit NewGov(gov); + } +} diff --git a/src/util/PendlePTHelper.sol b/src/util/PendlePTHelper.sol index df65a1fa..1bd7a6a6 100644 --- a/src/util/PendlePTHelper.sol +++ b/src/util/PendlePTHelper.sol @@ -60,10 +60,11 @@ contract PendlePTHelper is Sweepable, IPendleHelper { ); event YTOut(uint256 amount, address indexed from, address indexed to); event YTIn(uint256 amount, address indexed from); + event NewALE(address oldALE, address newALE); IERC20 public immutable DOLA; address public immutable router; - address public immutable ale; + address public ale; /// @notice Mapping of market addresses to their associated PT and YT tokens. mapping(address => PT) public markets; @@ -373,4 +374,14 @@ contract PendlePTHelper is Sweepable, IPendleHelper { delete markets[market]; emit MarketRemoved(market); } + + /** + * @notice Sets the ALE address. + * @dev Only callable by gov + * @param _ale The address of the new ALE + */ + function setALE(address _ale) external onlyGov { + emit NewALE(ale, _ale); + ale = _ale; + } } diff --git a/test/ConfigAddr.sol b/test/ConfigAddr.sol index 281acc36..53b7c505 100644 --- a/test/ConfigAddr.sol +++ b/test/ConfigAddr.sol @@ -8,6 +8,7 @@ contract ConfigAddr { address pauseGuardian = address(0xE3eD95e130ad9E15643f5A5f232a3daE980784cd); address dolaAddr = address(0x865377367054516e17014CcdED1e7d814EDC9ce4); address dbrAddr = address(0xAD038Eb671c44b853887A7E32528FaB35dC5D710); + address invAddr = address(0x41D5D79431A913C4aE7d69a668ecdfE5fF9DFB68); address oracleAddr = address(0xaBe146CF570FD27ddD985895ce9B138a7110cce8); address borrowControllerAddr = address(0x44B7895989Bc7886423F06DeAa844D413384b0d6); @@ -57,6 +58,8 @@ contract ConfigAddr { // Curve Pools address triDBRAddr = address(0xC7DE47b9Ca2Fc753D6a2F167D8b3e19c6D18b19a); + address newTriDBRAddr = + address(0x66da369fC5dBBa0774Da70546Bd20F2B242Cd34d); // Balancer Pools // FiRM Escrows diff --git a/test/DbrHelper.t.sol b/test/DbrHelper.t.sol index 5e4cb9bb..b36f7d0a 100644 --- a/test/DbrHelper.t.sol +++ b/test/DbrHelper.t.sol @@ -57,7 +57,7 @@ contract DbrHelperForkTest is MarketBaseForkTest { function setUp() public { //This will fail if there's no mainnet variable in foundry.toml string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 18586960); + vm.createSelectFork(url); distributor = DbrDistributor( 0xdcd2D918511Ba39F2872EB731BB88681AE184244 ); @@ -67,7 +67,7 @@ contract DbrHelperForkTest is MarketBaseForkTest { dbr.addMinter(address(distributor)); vm.stopPrank(); - helper = new DbrHelper(); + helper = new DbrHelper(newTriDBRAddr, gov); INV = helper.INV(); vm.expectEmit(true, false, false, true); @@ -81,6 +81,7 @@ contract DbrHelperForkTest is MarketBaseForkTest { DOLA.allowance(address(helper), marketAddr), type(uint256).max ); + assertEq(dbr.balanceOf(address(helper)), 0); } function _depositAllowAndWarp() internal { @@ -751,7 +752,7 @@ contract DbrHelperForkTest is MarketBaseForkTest { DbrHelper.Repay memory repay; (sell, repay) = _getArguments( - address(0), + user, user, user, 5000, @@ -768,8 +769,9 @@ contract DbrHelperForkTest is MarketBaseForkTest { uint256 dbrAmount ) = helper.claimAndSell(sell, repay); - assertEq(dbrAmount, 0); - assertEq(dbr.balanceOf(user), 0); + // If DBR amount is odd, user gets 1 wei DBR from rounding down in percentage calc + assertApproxEqAbs(dbrAmount, 0, 1); + assertApproxEqAbs(dbr.balanceOf(user), 0, 1); assertApproxEqAbs(dolaAmount, dolaRepaid * 2, 1); assertGt(dolaAmount, 0); assertGt(invAmount, 0); @@ -808,7 +810,7 @@ contract DbrHelperForkTest is MarketBaseForkTest { DbrHelper.Repay memory repay; (sell, repay) = _getArguments( - address(0), + user, user, user, 5000, @@ -824,9 +826,9 @@ contract DbrHelperForkTest is MarketBaseForkTest { uint256 dolaRepaid, uint256 dbrAmount ) = helper.claimAndSell(sell, repay); - - assertEq(dbrAmount, 0); - assertEq(dbr.balanceOf(user), 0); + // If DBR amount is odd, user gets 1 wei DBR from rounding down in percentage calc + assertApproxEqAbs(dbrAmount, 0, 1); + assertApproxEqAbs(dbr.balanceOf(user), 0, 1); assertEq(dolaAmount, dolaRepaid); assertGt(dolaAmount, 0); assertGt(invAmount, 0); @@ -873,9 +875,9 @@ contract DbrHelperForkTest is MarketBaseForkTest { uint256 dolaRepaid, uint256 dbrAmount ) = helper.claimAndSell(sell, repay); - - assertEq(dbrAmount, 0); - assertEq(dbr.balanceOf(user), 0); + // If DBR amount is odd, user gets 1 wei DBR from rounding down in percentage calc + assertApproxEqAbs(dbrAmount, 0, 1); + assertApproxEqAbs(dbr.balanceOf(user), 0, 1); assertGt(dolaAmount, 0); assertGt(invAmount, 0); @@ -1312,6 +1314,48 @@ contract DbrHelperForkTest is MarketBaseForkTest { assertEq(DOLA.balanceOf(user), dolaBeforeUser); } + function test_setPendingGov() public { + assertEq(helper.gov(), gov); + + vm.prank(gov); + helper.setPendingGov(user2); + assertEq(helper.pendingGov(), user2); + vm.prank(user2); + helper.claimPendingGov(); + assertEq(helper.gov(), user2); + + vm.prank(user2); + helper.setPendingGov(gov); + assertEq(helper.pendingGov(), gov); + vm.prank(gov); + helper.claimPendingGov(); + + assertEq(helper.gov(), gov); + } + + function test_fail_setPendingGov_not_gov() public { + vm.prank(user); + vm.expectRevert( + abi.encodeWithSelector(Ownable.NotGov.selector, user) + ); + helper.setPendingGov(user2); + } + + function test_setCurvePool() public { + assertEq(address(helper.curvePool()), address(newTriDBRAddr)); + vm.prank(gov); + helper.setCurvePool(triDBRAddr, 0,1,2); + assertEq(address(helper.curvePool()), triDBRAddr); + } + + function test_fail_setCurvePool_not_gov() public { + vm.prank(user); + vm.expectRevert( + abi.encodeWithSelector(Ownable.NotGov.selector, user) + ); + helper.setCurvePool(triDBRAddr, 0,1,2); + } + function _getArguments( address toDbr, address toDola, diff --git a/test/OffchainHelper.t.sol b/test/LatestOffchainHelper.t.sol similarity index 76% rename from test/OffchainHelper.t.sol rename to test/LatestOffchainHelper.t.sol index 6874d0cf..0e2eefde 100644 --- a/test/OffchainHelper.t.sol +++ b/test/LatestOffchainHelper.t.sol @@ -1,52 +1,58 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; -import "./FiRMBaseTest.sol"; +import "forge-std/Test.sol"; +import "forge-std/console2.sol"; +//import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; import {CurveHelper} from "src/util/CurveHelper.sol"; - +import {ConfigAddr} from "test/ConfigAddr.sol"; +import {IChainlinkFeed} from "src/interfaces/IChainlinkFeed.sol"; +import {DolaBorrowingRights} from "src/DBR.sol"; +import "src/Market.sol"; +import {IMarket} from "src/interfaces/IMarket.sol"; +import {BorrowController} from "src/BorrowController.sol"; interface IWeth is IERC20 { function withdraw(uint wad) external; function deposit() payable external; } - +interface IDOLA { + function mint(address to, uint256 amount) external; +} //This test must be run as a mainnet fork, to work correctly -contract OffchainHelperTest is FiRMBaseTest { +contract LatestOffchainHelperTest is Test, ConfigAddr { CurveHelper helper; bytes32 borrowHash; address userPk; uint maxBorrowAmount; - IWeth weth; - + IWeth weth = IWeth(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); + IChainlinkFeed ethFeed = IChainlinkFeed(0x22390B88C53D1631f673b8Dcd91860267137b2c8); + IERC20 DOLA = IERC20(dolaAddr); + DolaBorrowingRights dbr = DolaBorrowingRights(dbrAddr); + Market market = Market(0x63Df5e23Db45a2066508318f172bA45B9CD37035); + uint256 wethTestAmount = 10 ether; + BorrowController borrowController = BorrowController(0x01ECA33e20a4c379Bd8A5361f896A7dd2bAE4ce8); + uint256 privKey = 0x989944; function setUp() public { //This will fail if there's no mainnet variable in foundry.toml string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 18484330); // Block at which there's enough DOLA liquidity in WETH market - - - initialize(replenishmentPriceBps, collateralFactorBps, replenishmentIncentiveBps, liquidationBonusBps, callOnDepositCallback); - - vm.warp(block.timestamp - 7 days); - - vm.startPrank(chair); - fed.expansion(IMarket(address(market)), 1_000_000e18); - vm.stopPrank(); + vm.createSelectFork(url); + vm.label(address(DOLA),"DOLA"); + vm.label(address(dbr),"DBR"); + + address newTriDBRAddr = + address(0x66da369fC5dBBa0774Da70546Bd20F2B242Cd34d); + helper = new CurveHelper{salt: bytes32(uint256(1))}(newTriDBRAddr, gov); - dbr = DolaBorrowingRights(0xAD038Eb671c44b853887A7E32528FaB35dC5D710); - address pool = 0xC7DE47b9Ca2Fc753D6a2F167D8b3e19c6D18b19a; - helper = new CurveHelper(pool); - userPk = vm.addr(1); + userPk = vm.addr(privKey); + vm.startPrank(gov); - borrowController = BorrowController(0x44B7895989Bc7886423F06DeAa844D413384b0d6); borrowController.allow(address(helper)); vm.stopPrank(); - - market = Market(0x63Df5e23Db45a2066508318f172bA45B9CD37035); - weth = IWeth(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); - - vm.prank(0xF04a5cC80B1E94C69B48f5ee68a08CD2F09A7c3E); - weth.transfer(userPk, wethTestAmount); + deal(address(weth), address(userPk), wethTestAmount); + vm.prank(gov); + borrowController.setMinDebt(address(market), 1 ether); maxBorrowAmount = getMaxBorrowAmount(wethTestAmount); vm.startPrank(userPk, userPk); @@ -55,13 +61,14 @@ contract OffchainHelperTest is FiRMBaseTest { DOLA.approve(address(helper), type(uint).max); dbr.approve(address(helper), type(uint).max); vm.stopPrank(); - + } + function testDepositAndBorrowOnBehalf() public { uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, 365 days, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); vm.startPrank(userPk, userPk); assertEq(borrowController.isPriceStale(address(market)), false); @@ -79,7 +86,7 @@ contract OffchainHelperTest is FiRMBaseTest { uint duration = 365 days; uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, duration, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); uint prevBal = weth.balanceOf(userPk); vm.deal(userPk, wethTestAmount); @@ -101,7 +108,7 @@ contract OffchainHelperTest is FiRMBaseTest { vm.startPrank(userPk, userPk); uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, 365 days, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); deposit(wethTestAmount); vm.stopPrank(); @@ -120,7 +127,7 @@ contract OffchainHelperTest is FiRMBaseTest { vm.startPrank(userPk, userPk); uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, 365 days, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); gibDOLA(userPk, 10000 ether); @@ -141,7 +148,7 @@ contract OffchainHelperTest is FiRMBaseTest { vm.startPrank(userPk, userPk); uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, 365 days, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); gibDOLA(userPk, 10000 ether); @@ -158,17 +165,18 @@ contract OffchainHelperTest is FiRMBaseTest { assertEq(dbr.balanceOf(userPk), 0, "Did not sell DBR"); } - function testSellDbrAndRepayOnBehalf_EarnMoreFromDBRSellThanRepay() public { + function testSellDbrAndRepayOnBehalf_EarnMoreFromDBRSellThanRepay() public { vm.startPrank(userPk, userPk); uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, 365 days, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); gibDOLA(userPk, 10000 ether); deposit(wethTestAmount); helper.buyDbrAndBorrowOnBehalf(IMarket(address(market)), borrowAmount, dolaForDbr, dbrNeeded * 99 / 100, block.timestamp, v, r, s); //Reduce debt to 1 + DOLA.approve(address(market), type(uint256).max); market.repay(userPk, market.debts(userPk) - 1 ether); uint dolaBalanceBefore = DOLA.balanceOf(userPk); helper.sellDbrAndRepayOnBehalf(IMarket(address(market)), market.debts(userPk), dbr.balanceOf(userPk) / 100,dbr.balanceOf(userPk)); @@ -185,14 +193,14 @@ contract OffchainHelperTest is FiRMBaseTest { gibDOLA(userPk, 10000 ether); uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, 365 days, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); vm.startPrank(userPk, userPk); deposit(wethTestAmount); helper.buyDbrAndBorrowOnBehalf(IMarket(address(market)), borrowAmount, dolaForDbr, dbrNeeded * 99 / 100, block.timestamp, v, r, s); bytes32 withdrawHash = getWithdrawHash(wethTestAmount, 1); - (v, r, s) = vm.sign(1, withdrawHash); + (v, r, s) = vm.sign(privKey, withdrawHash); uint pkBalanceBefore = weth.balanceOf(userPk); helper.sellDbrRepayAndWithdrawOnBehalf( @@ -215,14 +223,14 @@ contract OffchainHelperTest is FiRMBaseTest { gibDOLA(userPk, 10000 ether); uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, 365 days, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); vm.startPrank(userPk, userPk); deposit(wethTestAmount); helper.buyDbrAndBorrowOnBehalf(IMarket(address(market)), borrowAmount, dolaForDbr, dbrNeeded * 99 / 100, block.timestamp, v, r, s); bytes32 withdrawHash = getWithdrawHash(wethTestAmount, 1); - (v, r, s) = vm.sign(1, withdrawHash); + (v, r, s) = vm.sign(privKey, withdrawHash); uint pkBalanceBefore = userPk.balance; helper.sellDbrRepayAndWithdrawNativeEthOnBehalf( @@ -243,7 +251,7 @@ contract OffchainHelperTest is FiRMBaseTest { function testWithdrawNativeEthOnBehalf() public { bytes32 withdrawHash = getWithdrawHash(wethTestAmount, 0); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, withdrawHash); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, withdrawHash); vm.startPrank(userPk, userPk); deposit(wethTestAmount); @@ -270,7 +278,7 @@ contract OffchainHelperTest is FiRMBaseTest { gibDOLA(userPk, 10000 ether); uint borrowAmount = maxBorrowAmount / 2; (uint dolaForDbr, uint dbrNeeded) = helper.approximateDolaAndDbrNeeded(borrowAmount, 365 days, 18); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount + dolaForDbr, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount + dolaForDbr, 0)); vm.startPrank(userPk, userPk); @@ -278,27 +286,29 @@ contract OffchainHelperTest is FiRMBaseTest { helper.buyDbrAndBorrowOnBehalf(IMarket(address(market)), borrowAmount, dolaForDbr, dbrNeeded * 99 / 100, block.timestamp, v, r, s); bytes32 withdrawHash = getWithdrawHash(wethTestAmount, 1); - (v, r, s) = vm.sign(1, withdrawHash); + (v, r, s) = vm.sign(privKey, withdrawHash); uint pkBalanceBefore = userPk.balance; + helper.repayAndWithdrawNativeEthOnBehalf( IMarket(address(market)), market.debts(userPk), wethTestAmount, block.timestamp, v, r, s); - vm.stopPrank(); - + assertEq(weth.balanceOf(address(market.predictEscrow(userPk))), 0, "failed to withdraw weth"); assertEq(userPk.balance - pkBalanceBefore, wethTestAmount, "failed to withdraw weth"); - assertEq(market.debts(userPk), 0, "Did not repay debt"); + assertEq(market.debts(userPk), 0, "Did not repay debt"); + vm.stopPrank(); } function testDepositNativeEthAndBorrowOnBehalf() public { uint borrowAmount = maxBorrowAmount / 2; - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, getBorrowHash(borrowAmount, 0)); + (uint8 v, bytes32 r, bytes32 s) = vm.sign(privKey, getBorrowHash(borrowAmount, 0)); vm.startPrank(userPk, userPk); + weth.approve(address(weth), type(uint).max); weth.withdraw(wethTestAmount); assertEq(weth.balanceOf(address(market.predictEscrow(userPk))), 0); @@ -354,4 +364,49 @@ contract OffchainHelperTest is FiRMBaseTest { ); return hash; } + + function deposit(uint amount) internal { + weth.approve(address(market), amount); + market.deposit(amount); + } + + function convertWethToDola(uint amount) public view returns (uint) { + return (amount * uint(ethFeed.latestAnswer())) / 1e18; + } + + function convertDolaToWeth(uint amount) public view returns (uint) { + return (amount * 1e18) / uint(ethFeed.latestAnswer()); + } + + function getMaxBorrowAmount(uint amountWeth) public view returns (uint) { + return + (convertWethToDola(amountWeth) * market.collateralFactorBps()) / + 10_000; + } + + // function gibWeth(address _address, uint _amount) internal { + // vm.deal(_address, _amount); + // vm.startPrank(_address); + // WETH.deposit{value: _amount}(); + // vm.stopPrank(); + // } + + // function gibDBR(address _address, uint _amount) internal { + // vm.startPrank(gov); + // dbr.mint(_address, _amount); + // vm.stopPrank(); + // } + + function gibDOLA(address _address, uint _amount) internal { + bytes32 slot; + assembly { + mstore(0, _address) + mstore(0x20, 0x6) + slot := keccak256(0, 0x40) + } + + vm.store(address(DOLA), slot, bytes32(_amount)); + } + + receive() external payable {} } diff --git a/test/escrowForkTests/DolaDeUSDEscrowConvexFork.t.sol b/test/escrowForkTests/DolaDeUSDEscrowConvexFork.t.sol deleted file mode 100644 index f08b2bf7..00000000 --- a/test/escrowForkTests/DolaDeUSDEscrowConvexFork.t.sol +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import {BaseEscrowLPConvexTest} from "test/escrowForkTests/BaseEscrowLPConvexTest.t.sol"; - -contract DolaDeUSDEscrowConvexForkTest is BaseEscrowLPConvexTest { - // Curve - address _dolaDeUSD = 0x6691DBb44154A9f23f8357C56FC9ff5548A8bdc4; - address _lpHolder = address(0xcb4a7b790eDB7Fa3e2731Efd7ED85275f92Fc74A); - address _gauge = 0xa48A3c91b062ca06Fd0d0569695432EB066f8c7E; - - // Convex - uint256 _pid = 419; - address _rewardPool = 0xD30E66cBc869Aa808eB9c81f8Aad8408767E3a3E; - address _stash = 0x074297Bf0dEA6925c27526E6E3E3151D8cE4edc7; - - function setUp() public { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 21826229); - BaseEscrowLPConvexTest.ConvexInfo memory convexParams = ConvexInfo( - _pid, - _rewardPool, - _stash - ); - - init(_dolaDeUSD, _lpHolder, _gauge, convexParams, true); - } -} diff --git a/test/feedForkTests/DolaDeUSDFeedFork.t.sol b/test/feedForkTests/DolaDeUSDFeedFork.t.sol deleted file mode 100644 index 3e8318e5..00000000 --- a/test/feedForkTests/DolaDeUSDFeedFork.t.sol +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.19; - -import "forge-std/Test.sol"; -import "src/feeds/ChainlinkBasePriceFeed.sol"; -import {ChainlinkCurveFeed} from "src/feeds/ChainlinkCurveFeed.sol"; -import {ChainlinkCurve2CoinsFeed} from "src/feeds/ChainlinkCurve2CoinsFeed.sol"; -import "src/feeds/CurveLPPessimisticFeed.sol"; -import {DolaCurveLPPessimsticFeedBaseTest} from "test/feedForkTests/DolaCurveLPPessimsticFeedBaseTest.t.sol"; -import {ConfigAddr} from "test/ConfigAddr.sol"; - -contract DolaDeUSDFeedFork is DolaCurveLPPessimsticFeedBaseTest, ConfigAddr { - address clDeUSDFeed = address(0x471a6299C027Bd81ed4D66069dc510Bd0569f4F8); - uint256 deUSDHeartbeat = 86400; - - ICurvePool public constant dolaDeUSD = - ICurvePool(0x6691DBb44154A9f23f8357C56FC9ff5548A8bdc4); - - function setUp() public { - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 21826229); - - ChainlinkBasePriceFeed deUSDFeed = new ChainlinkBasePriceFeed( - gov, - clDeUSDFeed, - address(0), - deUSDHeartbeat - ); - init(address(0), address(deUSDFeed), address(dolaDeUSD)); - } -} diff --git a/test/feedForkTests/SdeUSDFeedFork.t.sol b/test/feedForkTests/SdeUSDFeedFork.t.sol deleted file mode 100644 index 94a11690..00000000 --- a/test/feedForkTests/SdeUSDFeedFork.t.sol +++ /dev/null @@ -1,192 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.19; - -import "forge-std/Test.sol"; -import "src/feeds/ERC4626Feed.sol"; -import "src/interfaces/IChainlinkFeed.sol"; -import {ChainlinkCurveFeed, ICurvePool} from "src/feeds/ChainlinkCurveFeed.sol"; -import "forge-std/console.sol"; -import {ERC20 as ERC20Mock} from "test/mocks/ERC20.sol"; -import {ERC4626, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol"; -import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; - -contract Mock4626 is ERC4626 { - constructor(IERC20 _asset) ERC20("MOCK", "MOCK") ERC4626(_asset) {} - - function _decimalsOffset() internal view override returns (uint8) { - return 12; - } -} - -contract SdeUSDFeedForkTest is Test { - ChainlinkCurveFeed curveFeed; - ERC4626Feed feed; - address curvePool = address(0x82202CAEC5E6d85014eADC68D4912F3C90093e7C); - uint256 k = 0; - uint256 targetIndex = 1; - address sdeUSD = address(0x5C5b196aBE0d54485975D1Ec29617D42D9198326); - address dolaFeed = address(0x6255981e2a1EBeA600aFC506185590eD383517be); - - ERC20Mock mock6Decimals; - Mock4626 mock6Vault; - ERC4626Feed feed6Decimals; - - function setUp() public { - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url); - curveFeed = new ChainlinkCurveFeed(dolaFeed, curvePool, k, targetIndex); - feed = new ERC4626Feed(sdeUSD, address(curveFeed)); - } - - function test_6Decimals_underlying_asset_Feed_returns_18() public { - mock6Decimals = new ERC20Mock("Mock6", "M6", 6); - mock6Vault = new Mock4626(IERC20(address(mock6Decimals))); - assertEq(mock6Vault.decimals(), 18); - - feed6Decimals = new ERC4626Feed( - address(mock6Vault), - address(curveFeed) - ); - uint256 amount = 10000e6; - assertEq(feed6Decimals.decimals(), 18); - assertEq(feed6Decimals.assetScale(), 1e6); - - mock6Decimals.mint(address(this), amount); - mock6Decimals.approve(address(mock6Vault), amount); - mock6Vault.deposit(amount, address(this)); - - assertEq(mock6Vault.convertToAssets(1e18), 1e6); - assertEq(mock6Vault.previewRedeem(1e18), 1e6); - assertEq(mock6Vault.convertToShares(1e6), 1e18); - assertEq(mock6Vault.previewDeposit(1e6), 1e18); - - uint256 sdeUSDNormalizedToDola = ICurvePool(curvePool).price_oracle( - curveFeed.assetOrTargetK() - ); - - int256 dolaToUsdPrice = curveFeed.assetToUsd().latestAnswer(); - int256 sdeUSDNormalizedToUsdPrice = int256( - (sdeUSDNormalizedToDola * uint(dolaToUsdPrice)) / 1e18 - ); - - uint256 sdeUSDToDeUSDRate = mock6Vault.previewRedeem(1e18); - - uint256 price = (uint(sdeUSDNormalizedToUsdPrice) * sdeUSDToDeUSDRate) / - 1e6; - assertEq(feed6Decimals.latestAnswer(), int256(price)); - } - - function test_decimals() public { - assertEq(feed.decimals(), 18); - assertEq(feed.assetScale(), 1e18); - } - - function test_description() public { - assertEq(feed.description(), "sdeUSD / USD using sdeUSD vault rate"); - } - - function test_latestRoundData() public { - ( - uint80 roundId, - int256 sdeUSDToUsdPrice, - uint256 startedAt, - uint256 updatedAt, - uint80 answeredInRound - ) = feed.latestRoundData(); - assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); - ( - uint80 roundId2, - , - uint256 startedAt2, - uint256 updatedAt2, - uint80 answeredInRound2 - ) = IChainlinkBasePriceFeed(dolaFeed).latestRoundData(); - // Data are - assertEq(roundId, roundId2); - assertEq(sdeUSDToUsdPrice, _calculateSdeUSDPrice()); - assertEq(startedAt, startedAt2); - assertEq(updatedAt, updatedAt2); - assertEq(answeredInRound, answeredInRound2); - } - - function test_sdeUSD_upward_depeg() public { - int256 answer = feed.latestAnswer(); - assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); - uint256 mockRate = 2e18; - _mockVaultRate(sdeUSD, mockRate); - assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); - assertGt(feed.latestAnswer(), answer); - } - - function test_sdeUSD_downward_depeg() public { - int256 answer = feed.latestAnswer(); - assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); - uint256 mockRate = 0.5e18; - _mockVaultRate(sdeUSD, mockRate); - assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); - assertLt(feed.latestAnswer(), answer); - } - - function test_previewRedeemRevert_useConvertToAssets() public { - // Mock preview redeem rate - _mockVaultRate(sdeUSD, 2e18); - // Answer is equal to preview redeem rate but not equal to convert to assets - assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); - assertGt(feed.latestAnswer(), _calculateSdeUSDPriceConvertToAssets()); - // Mock preview redeem revert to use convert to assets - _mockPreviewRevert(sdeUSD); - assertEq(feed.latestAnswer(), _calculateSdeUSDPriceConvertToAssets()); - } - - function _calculateSdeUSDPrice() internal view returns (int256) { - uint256 sdeUSDNormalizedToDola = ICurvePool(curvePool).price_oracle( - curveFeed.assetOrTargetK() - ); - - int256 dolaToUsdPrice = curveFeed.assetToUsd().latestAnswer(); - int256 sdeUSDNormalizedToUsdPrice = int256( - (sdeUSDNormalizedToDola * uint(dolaToUsdPrice)) / 1e18 - ); - - uint256 sdeUSDToDeUSDRate = IERC4626(sdeUSD).previewRedeem(1e18); - return - (sdeUSDNormalizedToUsdPrice * int(sdeUSDToDeUSDRate)) / - int256(1e18); - } - - function _calculateSdeUSDPriceConvertToAssets() - internal - view - returns (int256) - { - uint256 sdeUSDNormalizedToDola = ICurvePool(curvePool).price_oracle( - curveFeed.assetOrTargetK() - ); - - int256 dolaToUsdPrice = curveFeed.assetToUsd().latestAnswer(); - int256 sdeUSDNormalizedToUsdPrice = int256( - (sdeUSDNormalizedToDola * uint(dolaToUsdPrice)) / 1e18 - ); - - uint256 sdeUSDToDeUSDRate = IERC4626(sdeUSD).convertToAssets(1e18); - return - (sdeUSDNormalizedToUsdPrice * int(sdeUSDToDeUSDRate)) / - int256(1e18); - } - - function _mockVaultRate(address vault, uint256 mockRate) internal { - vm.mockCall( - vault, - abi.encodeWithSelector(IERC4626.previewRedeem.selector, 1e18), - abi.encode(mockRate) - ); - } - - function _mockPreviewRevert(address vault) internal { - vm.mockCallRevert( - vault, - abi.encodeWithSelector(IERC4626.previewRedeem.selector, 1e18), - "mock revert" - ); - } -} diff --git a/test/marketForkTests/CrvUSDDolaYearnV2MarketForkTest.t.sol b/test/marketForkTests/CrvUSDDolaYearnV2MarketForkTest.t.sol deleted file mode 100644 index 52002627..00000000 --- a/test/marketForkTests/CrvUSDDolaYearnV2MarketForkTest.t.sol +++ /dev/null @@ -1,104 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import {MarketBaseForkTest, IOracle, IDolaBorrowingRights, IERC20} from "./MarketBaseForkTest.sol"; -import {Market} from "src/Market.sol"; -import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; -import {CurveLPYearnV2Feed} from "src/feeds/CurveLPYearnV2Feed.sol"; -import {ChainlinkCurve2CoinsFeed} from "src/feeds/ChainlinkCurve2CoinsFeed.sol"; -import {ChainlinkCurveFeed, ICurvePool} from "src/feeds/ChainlinkCurveFeed.sol"; -import "src/feeds/ChainlinkBasePriceFeed.sol"; -import "src/feeds/CurveLPYearnV2Feed.sol"; -import {console} from "forge-std/console.sol"; -import {YearnVaultV2Helper, IYearnVaultV2} from "src/util/YearnVaultV2Helper.sol"; - -interface IYearnVaultFactory { - function createNewVaultsAndStrategies( - address _gauge - ) - external - returns ( - address vault, - address convexStrategy, - address curveStrategy, - address convexFraxStrategy - ); -} - -contract CrvUSDDolaYearnV2MarketForkTest is MarketBaseForkTest { - SimpleERC20Escrow escrow; - CurveLPYearnV2Feed feedCrvUSDDolaYearnV2; - - ChainlinkBasePriceFeed mainCrvUSDFeed; - ChainlinkBasePriceFeed baseFraxToUsd; - ChainlinkCurve2CoinsFeed crvUSDFallback; - - ICurvePool public constant dolaCrvUSD = - ICurvePool(0x8272E1A3dBef607C04AA6e5BD3a1A134c8ac063B); - - IChainlinkFeed public constant fraxToUsd = - IChainlinkFeed(0xB9E1E3A9feFf48998E45Fa90847ed4D467E8BcfD); - - uint256 public fraxHeartbeat = 1 hours; - - // For CrvUSD fallback - ICurvePool public constant crvUSDFrax = - ICurvePool(0x0CD6f267b2086bea681E922E19D40512511BE538); - - IChainlinkFeed public constant crvUSDToUsd = - IChainlinkFeed(0xEEf0C605546958c1f899b6fB336C20671f9cD49F); - uint256 public crvUSDHeartbeat = 24 hours; - - uint256 crvUSDIndex = 1; - - address public yearn = address(0xfb5137Aa9e079DB4b7C2929229caf503d0f6DA96); - address yearnHolder = address(0x8B5b1D02AAB4e10e49507e89D2bE10A382D52b57); //update - - SimpleERC20Escrow userEscrow; - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 22582376); - - escrow = new SimpleERC20Escrow(); - - _advancedInit( - crvUSDDolaYearnAddr, - address(yearnCrvUSDDolaFeedAddr), - true - ); - - userEscrow = SimpleERC20Escrow( - address(Market(crvUSDDolaYearnAddr).predictEscrow(user)) - ); - } - - function _deployCrvUSDDolaYearnV2Feed() - internal - returns (CurveLPYearnV2Feed feed) - { - // CrvUSD fallback - baseFraxToUsd = new ChainlinkBasePriceFeed( - gov, - address(fraxToUsd), - address(0), - fraxHeartbeat - ); - crvUSDFallback = new ChainlinkCurve2CoinsFeed( - address(baseFraxToUsd), - address(crvUSDFrax), - crvUSDIndex - ); - - // Main feed - mainCrvUSDFeed = new ChainlinkBasePriceFeed( - gov, - address(crvUSDToUsd), - address(crvUSDFallback), - crvUSDHeartbeat - ); - - feed = new CurveLPYearnV2Feed(address(yearn), address(mainCrvUSDFeed)); - } -} diff --git a/test/marketForkTests/DolaDeUSDConvexMarketForkTest.t.sol b/test/marketForkTests/DolaDeUSDConvexMarketForkTest.t.sol deleted file mode 100644 index d64d50f5..00000000 --- a/test/marketForkTests/DolaDeUSDConvexMarketForkTest.t.sol +++ /dev/null @@ -1,106 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import {MarketBaseForkTest, IOracle, IDolaBorrowingRights, IERC20} from "./MarketBaseForkTest.sol"; -import {Market} from "src/Market.sol"; - -import {ConvexEscrowV2} from "src/escrows/ConvexEscrowV2.sol"; -import {CurveLPPessimisticFeed} from "src/feeds/CurveLPPessimisticFeed.sol"; -import {ChainlinkCurve2CoinsFeed, ICurvePool} from "src/feeds/ChainlinkCurve2CoinsFeed.sol"; -import {ChainlinkCurveFeed} from "src/feeds/ChainlinkCurveFeed.sol"; -import "src/feeds/ChainlinkBasePriceFeed.sol"; -import {console} from "forge-std/console.sol"; -import {YearnVaultV2Helper, IYearnVaultV2} from "src/util/YearnVaultV2Helper.sol"; -import {DolaFixedPriceFeed} from "src/feeds/DolaFixedPriceFeed.sol"; -import {ChainlinkBasePriceFeed} from "src/feeds/ChainlinkBasePriceFeed.sol"; - -contract DolaDeUSDConvexMarketForkTest is MarketBaseForkTest { - ConvexEscrowV2 escrow; - - CurveLPPessimisticFeed feedDolaDeUSD; - DolaFixedPriceFeed dolaFeed; - - ICurvePool public constant dolaDeUSD = - ICurvePool(0x6691DBb44154A9f23f8357C56FC9ff5548A8bdc4); - - address deUSDFeed = address(0x471a6299C027Bd81ed4D66069dc510Bd0569f4F8); - ChainlinkBasePriceFeed deUSDWrapper; - - address rewardPool = address(0xD30E66cBc869Aa808eB9c81f8Aad8408767E3a3E); - - address booster = address(0xF403C135812408BFbE8713b5A23a04b3D48AAE31); - - uint256 pid = 419; - - IERC20 public cvx = IERC20(0x4e3FBD56CD56c3e72c1403e103b45Db9da5B9D2B); - IERC20 public crv = IERC20(0xD533a949740bb3306d119CC777fa900bA034cd52); - - ConvexEscrowV2 userEscrow; - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url); - escrow = new ConvexEscrowV2( - rewardPool, - booster, - address(cvx), - address(crv), - pid - ); - feedDolaDeUSD = _deployDolaDeUSDFeed(); - market = new Market( - gov, - fedAddr, - pauseGuardian, - address(escrow), - IDolaBorrowingRights(address(dbrAddr)), - IERC20(address(dolaDeUSD)), - IOracle(address(oracleAddr)), - 5000, - 5000, - 1000, - true - ); - _advancedInit(address(market), address(feedDolaDeUSD), true); - - userEscrow = ConvexEscrowV2( - address(Market(address(market)).predictEscrow(user)) - ); - } - - function test_escrow_immutables() public { - testDeposit(); - assertEq( - address(userEscrow.rewardPool()), - address(rewardPool), - "Reward pool not set" - ); - assertEq( - address(userEscrow.booster()), - address(booster), - "Booster not set" - ); - - assertEq(address(userEscrow.cvx()), address(cvx), "CVX not set"); - assertEq(address(userEscrow.crv()), address(crv), "CRV not set"); - } - - function _deployDolaDeUSDFeed() - internal - returns (CurveLPPessimisticFeed feed) - { - deUSDWrapper = new ChainlinkBasePriceFeed( - gov, - address(deUSDFeed), - address(0), - 86400 - ); - feed = new CurveLPPessimisticFeed( - address(dolaDeUSD), - address(deUSDWrapper), - address(dolaFixedFeedAddr), - false - ); - } -} diff --git a/test/marketForkTests/DolaDeUSDYearnV2MarketForkTest.t.sol b/test/marketForkTests/DolaDeUSDYearnV2MarketForkTest.t.sol deleted file mode 100644 index df63652b..00000000 --- a/test/marketForkTests/DolaDeUSDYearnV2MarketForkTest.t.sol +++ /dev/null @@ -1,72 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import {MarketBaseForkTest, IOracle, IDolaBorrowingRights, IERC20} from "./MarketBaseForkTest.sol"; -import {Market} from "src/Market.sol"; -import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; -import {CurveLPYearnV2Feed} from "src/feeds/CurveLPYearnV2Feed.sol"; -import {ChainlinkCurve2CoinsFeed} from "src/feeds/ChainlinkCurve2CoinsFeed.sol"; -import {ChainlinkCurveFeed, ICurvePool} from "src/feeds/ChainlinkCurveFeed.sol"; -import "src/feeds/ChainlinkBasePriceFeed.sol"; -import "src/feeds/CurveLPYearnV2Feed.sol"; -import {console} from "forge-std/console.sol"; -import {YearnVaultV2Helper, IYearnVaultV2} from "src/util/YearnVaultV2Helper.sol"; -import {CurveLPPessimisticFeed} from "src/feeds/CurveLPPessimisticFeed.sol"; -import {MockFeedDescription} from "test/mocks/MockFeedDescription.sol"; -import {ChainlinkBasePriceFeed} from "src/feeds/ChainlinkBasePriceFeed.sol"; - -contract DolaDeUSDYearnV2MarketForkTest is MarketBaseForkTest { - CurveLPYearnV2Feed yearnFeed; - CurveLPPessimisticFeed lpFeed; - - address deUSDFeed = address(0x471a6299C027Bd81ed4D66069dc510Bd0569f4F8); - ChainlinkBasePriceFeed deUSDWrapper; - ICurvePool public constant dolaDeUSD = - ICurvePool(0x6691DBb44154A9f23f8357C56FC9ff5548A8bdc4); - - address public constant yearn = - address(0xc7C1B907BCD3194C0D9bFA2125251af98BdDAfbb); - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url); - - Market market = new Market( - gov, - lender, - pauseGuardian, - address(simpleERC20EscrowAddr), - IDolaBorrowingRights(address(dbr)), - IERC20(address(yearn)), - IOracle(address(oracle)), - 5000, - 5000, - 1000, - false - ); - yearnFeed = _deployDolaDeUSDYearnV2Feed(); - _advancedInit(address(market), address(yearnFeed), true); - } - - function _deployDolaDeUSDYearnV2Feed() - internal - returns (CurveLPYearnV2Feed feed) - { - deUSDWrapper = new ChainlinkBasePriceFeed( - gov, - address(deUSDFeed), - address(0), - 86400 - ); - - lpFeed = new CurveLPPessimisticFeed( - address(dolaDeUSD), - address(deUSDWrapper), - address(dolaFixedFeedAddr), - false - ); - - feed = new CurveLPYearnV2Feed(address(yearn), address(lpFeed)); - } -} diff --git a/test/marketForkTests/PendlePTUSDe25Sep25MarketForkTest.t.sol b/test/marketForkTests/PendlePTUSDe25Sep25MarketForkTest.t.sol deleted file mode 100644 index 02ff1a7d..00000000 --- a/test/marketForkTests/PendlePTUSDe25Sep25MarketForkTest.t.sol +++ /dev/null @@ -1,79 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "./MarketBaseForkTest.sol"; -import {NavBeforeMaturityFeed} from "src/feeds/NavBeforeMaturityFeed.sol"; -import {ChainlinkBasePriceFeed} from "src/feeds/ChainlinkBasePriceFeed.sol"; -import {FeedSwitch} from "src/util/FeedSwitch.sol"; -import {PendleNAVFeed} from "src/feeds/PendleNAVFeed.sol"; - -contract PendlePTUSDe25Sep25MarketForkTest is MarketBaseForkTest { - address USDeFeed = address(0xa569d910839Ae8865Da8F8e70FfFb0cBA869F961); - address pendlePT = address(0xBC6736d346a5eBC0dEbc997397912CD9b8FAe10a); // PT USDe 25 Sep 25 - address pendlePTHolder = - address(0x4352Cc849b33a936Ad93bB109aFDec1c89653b4f); - - ChainlinkBasePriceFeed usdeWrappedFeed = ChainlinkBasePriceFeed(0xB3C1D801A02d88adC96A294123c2Daa382345058); // USDe/USD wrapped - NavBeforeMaturityFeed beforeMaturityFeed; - ChainlinkBasePriceFeed afterMaturityFeed = ChainlinkBasePriceFeed(0xB3C1D801A02d88adC96A294123c2Daa382345058); - address navFeed; - - uint256 baseDiscount = 0.2 ether; // 20% - FeedSwitch feedSwitch; - - address feedAddr; //FeedSwitch - address marketAddr; - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 23132371); - - feedAddr = _deployFeed(); - - marketAddr = address( - new Market( - gov, - lender, - pauseGuardian, - simpleERC20EscrowAddr, - IDolaBorrowingRights(dbrAddr), - IERC20(pendlePT), - IOracle(oracleAddr), - 9150, - 5000, - 500, - false - ) - ); - _advancedInit(marketAddr, feedAddr, false); - } - - function _deployFeed() internal returns (address feed) { - navFeed = address(new PendleNAVFeed(pendlePT, baseDiscount)); - beforeMaturityFeed = new NavBeforeMaturityFeed( - address(usdeWrappedFeed), - address(navFeed) - ); - - feedSwitch = new FeedSwitch( - address(navFeed), - address(beforeMaturityFeed), - address(afterMaturityFeed), - 18 hours, - pendlePT, - pauseGuardian - ); - return address(feedSwitch); - } - - // Override the function to use the PendlePTHolder to avoid error revert: stdStorage find(StdStorage): Slot(s) not found - function gibCollateral( - address _address, - uint _amount - ) internal virtual override { - vm.prank(pendlePTHolder); - IERC20(pendlePT).transfer(_address, _amount); - } -} diff --git a/test/marketForkTests/PendlePTsUSDe24Sep25MarketForkTest.t.sol b/test/marketForkTests/PendlePTsUSDe24Sep25MarketForkTest.t.sol deleted file mode 100644 index e55899a6..00000000 --- a/test/marketForkTests/PendlePTsUSDe24Sep25MarketForkTest.t.sol +++ /dev/null @@ -1,94 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "./MarketBaseForkTest.sol"; -import {USDeNavBeforeMaturityFeed} from "src/feeds/USDeNavBeforeMaturityFeed.sol"; -import {ChainlinkBasePriceFeed} from "src/feeds/ChainlinkBasePriceFeed.sol"; -import {FeedSwitch} from "src/util/FeedSwitch.sol"; -import {PendleNAVFeed} from "src/feeds/PendleNAVFeed.sol"; - -contract PendlePTsUSDe24Sep25MarketForkTest is MarketBaseForkTest { - address USDeFeed = address(0xa569d910839Ae8865Da8F8e70FfFb0cBA869F961); - address sUSDeFeed = address(0xFF3BC18cCBd5999CE63E788A1c250a88626aD099); - address sUSDe = address(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497); - address pendlePT = address(0x9F56094C450763769BA0EA9Fe2876070c0fD5F77); // PT sUSDe 24 Sep 25 - address pendlePTHolder = - address(0x5c14F9573697176b1cBd8af8378Cff9583DE4166); - - ChainlinkBasePriceFeed sUSDeWrappedFeed; - USDeNavBeforeMaturityFeed beforeMaturityFeed; - ChainlinkBasePriceFeed afterMaturityFeed; - address navFeed; - - uint256 baseDiscount = 0.2 ether; // 20% - FeedSwitch feedSwitch; - - address feedAddr; //FeedSwitch - address marketAddr; - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url,22590519); - - feedAddr = _deployFeed(); - - marketAddr = address( - new Market( - gov, - lender, - pauseGuardian, - simpleERC20EscrowAddr, - IDolaBorrowingRights(dbrAddr), - IERC20(pendlePT), - IOracle(oracleAddr), - 9150, - 5000, - 500, - false - ) - ); - _advancedInit(marketAddr, feedAddr, false); - } - - function _deployFeed() internal returns (address feed) { - sUSDeWrappedFeed = new ChainlinkBasePriceFeed( - gov, - sUSDeFeed, - address(0), - 24 hours - ); - navFeed = address(new PendleNAVFeed(pendlePT, baseDiscount)); - beforeMaturityFeed = new USDeNavBeforeMaturityFeed( - address(sUSDeWrappedFeed), - address(sUSDe), - address(navFeed) - ); - afterMaturityFeed = new ChainlinkBasePriceFeed( - gov, - USDeFeed, - address(0), - 24 hours - ); - - feedSwitch = new FeedSwitch( - address(navFeed), - address(beforeMaturityFeed), - address(afterMaturityFeed), - 18 hours, - pendlePT, - pauseGuardian - ); - return address(feedSwitch); - } - - // Override the function to use the PendlePTHolder to avoid error revert: stdStorage find(StdStorage): Slot(s) not found - function gibCollateral( - address _address, - uint _amount - ) internal virtual override { - vm.prank(pendlePTHolder); - IERC20(pendlePT).transfer(_address, _amount); - } -} diff --git a/test/marketForkTests/PendlePTsUSDe27Mar25MarketForkTest.t.sol b/test/marketForkTests/PendlePTsUSDe27Mar25MarketForkTest.t.sol deleted file mode 100644 index 52587d75..00000000 --- a/test/marketForkTests/PendlePTsUSDe27Mar25MarketForkTest.t.sol +++ /dev/null @@ -1,72 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "./MarketBaseForkTest.sol"; -import {USDeBeforeMaturityFeed} from "src/feeds/USDeBeforeMaturityFeed.sol"; -import {ChainlinkBasePriceFeed} from "src/feeds/ChainlinkBasePriceFeed.sol"; -import {DolaFixedPriceFeed} from "src/feeds/DolaFixedPriceFeed.sol"; -import {FeedSwitch} from "src/util/FeedSwitch.sol"; - -contract PendlePTsUSDe27Mar25MarketForkTest is MarketBaseForkTest { - address USDeFeed = address(0xa569d910839Ae8865Da8F8e70FfFb0cBA869F961); - address sUSDeFeed = address(0xFF3BC18cCBd5999CE63E788A1c250a88626aD099); - address sUSDe = address(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497); - address pendlePT = address(0xE00bd3Df25fb187d6ABBB620b3dfd19839947b81); // PT sUSDe 27 MAR 2025 - address pendlePTHolder = - address(0x9Dc53706C02c63Cf149F18978D478d4A3454B964); - - ChainlinkBasePriceFeed sUSDeWrappedFeed; - USDeBeforeMaturityFeed beforeMaturityFeed; - ChainlinkBasePriceFeed afterMaturityFeed; - DolaFixedPriceFeed initialFeed; - FeedSwitch feedSwitch; - address marketAddr = address(0x0DFE3D04536a74Dd532dd0cEf5005bA14c5f4112); - address feedAddr = address(0xddB5653FaC7a215139141863B2FAd021D44d7Ee4); - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 21573122); - - _advancedInit(address(marketAddr), feedAddr, false); - } - - function _deployFeed() internal returns (address feed) { - sUSDeWrappedFeed = new ChainlinkBasePriceFeed( - gov, - sUSDeFeed, - address(0), - 24 hours - ); - beforeMaturityFeed = new USDeBeforeMaturityFeed( - address(sUSDeWrappedFeed), - address(sUSDe) - ); - afterMaturityFeed = new ChainlinkBasePriceFeed( - gov, - USDeFeed, - address(0), - 24 hours - ); - initialFeed = new DolaFixedPriceFeed(); - feedSwitch = new FeedSwitch( - address(initialFeed), - address(beforeMaturityFeed), - address(afterMaturityFeed), - 18 hours, - pendlePT, - pauseGuardian - ); - return address(feedSwitch); - } - - // Override the function to use the PendlePTHolder to avoid error revert: stdStorage find(StdStorage): Slot(s) not found - function gibCollateral( - address _address, - uint _amount - ) internal virtual override { - vm.prank(pendlePTHolder); - IERC20(pendlePT).transfer(_address, _amount); - } -} diff --git a/test/marketForkTests/PendlePTsUSDe29May25MarketForkTest.t.sol b/test/marketForkTests/PendlePTsUSDe29May25MarketForkTest.t.sol deleted file mode 100644 index 4e63f749..00000000 --- a/test/marketForkTests/PendlePTsUSDe29May25MarketForkTest.t.sol +++ /dev/null @@ -1,76 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "./MarketBaseForkTest.sol"; -import {USDeNavBeforeMaturityFeed} from "src/feeds/USDeNavBeforeMaturityFeed.sol"; -import {ChainlinkBasePriceFeed} from "src/feeds/ChainlinkBasePriceFeed.sol"; -import {FeedSwitch} from "src/util/FeedSwitch.sol"; -import {PendleNAVFeed} from "src/feeds/PendleNAVFeed.sol"; -contract PendlePTsUSDe29May25MarketForkTest is MarketBaseForkTest { - address USDeFeed = address(0xa569d910839Ae8865Da8F8e70FfFb0cBA869F961); - address sUSDeFeed = address(0xFF3BC18cCBd5999CE63E788A1c250a88626aD099); - address sUSDe = address(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497); - address pendlePT = address(0xb7de5dFCb74d25c2f21841fbd6230355C50d9308); // PT sUSDe 29 May 25 - address pendlePTHolder = - address(0x8C0824fFccBE9A3CDda4c3d409A0b7447320F364); - - ChainlinkBasePriceFeed sUSDeWrappedFeed; - USDeNavBeforeMaturityFeed beforeMaturityFeed; - ChainlinkBasePriceFeed afterMaturityFeed; - address navFeed; - - uint256 baseDiscount = 0.2 ether; // 20% - FeedSwitch feedSwitch; - - address feedAddr = 0x8f5d8A77e6C1943218854B1eef22401760D4ca10; //FeedSwitch - address marketAddr = 0x2D4788893DE7a4fB42106D9Db36b65463428FBD9; - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url,22241889); - - _advancedInit(marketAddr, feedAddr, false); - } - - function _deployFeed() internal returns (address feed) { - sUSDeWrappedFeed = new ChainlinkBasePriceFeed( - gov, - sUSDeFeed, - address(0), - 24 hours - ); - navFeed = address(new PendleNAVFeed(pendlePT, baseDiscount)); - beforeMaturityFeed = new USDeNavBeforeMaturityFeed( - address(sUSDeWrappedFeed), - address(sUSDe), - address(navFeed) - ); - afterMaturityFeed = new ChainlinkBasePriceFeed( - gov, - USDeFeed, - address(0), - 24 hours - ); - - feedSwitch = new FeedSwitch( - address(navFeed), - address(beforeMaturityFeed), - address(afterMaturityFeed), - 18 hours, - pendlePT, - pauseGuardian - ); - return address(feedSwitch); - } - - // Override the function to use the PendlePTHolder to avoid error revert: stdStorage find(StdStorage): Slot(s) not found - function gibCollateral( - address _address, - uint _amount - ) internal virtual override { - vm.prank(pendlePTHolder); - IERC20(pendlePT).transfer(_address, _amount); - } -} diff --git a/test/marketForkTests/PendlePTsUSDe31July25MarketForkTest.t.sol b/test/marketForkTests/PendlePTsUSDe31July25MarketForkTest.t.sol deleted file mode 100644 index 6b860001..00000000 --- a/test/marketForkTests/PendlePTsUSDe31July25MarketForkTest.t.sol +++ /dev/null @@ -1,97 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "./MarketBaseForkTest.sol"; -import {USDeNavBeforeMaturityFeed} from "src/feeds/USDeNavBeforeMaturityFeed.sol"; -import {ChainlinkBasePriceFeed} from "src/feeds/ChainlinkBasePriceFeed.sol"; -import {FeedSwitch} from "src/util/FeedSwitch.sol"; -import {PendleNAVFeed} from "src/feeds/PendleNAVFeed.sol"; - -contract PendlePTsUSDe31July25MarketForkTest is MarketBaseForkTest { - address USDeFeed = address(0xa569d910839Ae8865Da8F8e70FfFb0cBA869F961); - address sUSDeFeed = address(0xFF3BC18cCBd5999CE63E788A1c250a88626aD099); - address sUSDe = address(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497); - address pendlePT = address(0x3b3fB9C57858EF816833dC91565EFcd85D96f634); // PT sUSDe 31 July 25 - address pendlePTHolder = - address(0x520A816aA9E220a24590862bed50E91047349142); - - ChainlinkBasePriceFeed sUSDeWrappedFeed; - USDeNavBeforeMaturityFeed beforeMaturityFeed; - ChainlinkBasePriceFeed afterMaturityFeed; - address navFeed; - - uint256 baseDiscount = 0.2 ether; // 20% - FeedSwitch feedSwitch; - - address feedAddr; //FeedSwitch - address marketAddr; - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url,22582482); - - - address marketAddr = address( - new Market( - gov, - lender, - pauseGuardian, - simpleERC20EscrowAddr, - IDolaBorrowingRights(dbrAddr), - IERC20(pendlePT), - IOracle(oracleAddr), - 9150, - 5000, - 500, - false - ) - ); - - address feedAddr = _deployFeed(); - - _advancedInit(marketAddr, feedAddr, false); - } - - function _deployFeed() internal returns (address feed) { - sUSDeWrappedFeed = new ChainlinkBasePriceFeed( - gov, - sUSDeFeed, - address(0), - 24 hours - ); - navFeed = address(new PendleNAVFeed(pendlePT, baseDiscount)); - beforeMaturityFeed = new USDeNavBeforeMaturityFeed( - address(sUSDeWrappedFeed), - address(sUSDe), - navFeed - ); - - afterMaturityFeed = new ChainlinkBasePriceFeed( - gov, - USDeFeed, - address(0), - 24 hours - ); - - feedSwitch = new FeedSwitch( - address(navFeed), - address(beforeMaturityFeed), - address(afterMaturityFeed), - 18 hours, - pendlePT, - pauseGuardian - ); - return address(feedSwitch); - } - - // Override the function to use the PendlePTHolder to avoid error revert: stdStorage find(StdStorage): Slot(s) not found - function gibCollateral( - address _address, - uint _amount - ) internal virtual override { - vm.prank(pendlePTHolder); - IERC20(pendlePT).transfer(_address, _amount); - } -} diff --git a/test/marketForkTests/SdeUSDMarketForkTest.t.sol b/test/marketForkTests/SdeUSDMarketForkTest.t.sol deleted file mode 100644 index adcfe610..00000000 --- a/test/marketForkTests/SdeUSDMarketForkTest.t.sol +++ /dev/null @@ -1,43 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "./MarketBaseForkTest.sol"; -import "src/feeds/ERC4626Feed.sol"; -import "src/Market.sol"; -import {ChainlinkCurveFeed} from "src/feeds/ChainlinkCurveFeed.sol"; - -contract SdeUSDMarketForkTest is MarketBaseForkTest { - address curvePool = address(0x82202CAEC5E6d85014eADC68D4912F3C90093e7C); - uint256 k = 0; - uint256 targetIndex = 1; - address sdeUSD = address(0x5C5b196aBE0d54485975D1Ec29617D42D9198326); - address dolaFeed = address(0x6255981e2a1EBeA600aFC506185590eD383517be); - - function setUp() public virtual { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 21880983); - address curveFeed = address( - new ChainlinkCurveFeed(dolaFeed, curvePool, k, targetIndex) - ); - address feedAddr = address(new ERC4626Feed(sdeUSD, curveFeed)); - - address marketAddr = address( - new Market( - gov, - lender, - pauseGuardian, - simpleERC20EscrowAddr, - IDolaBorrowingRights(dbrAddr), - IERC20(sdeUSD), - IOracle(oracleAddr), - 5000, - 1000, - 1000, - false - ) - ); - _advancedInit(marketAddr, feedAddr, false); - } -} diff --git a/test/marketForkTests/StYEthMarketForkTest.t.sol b/test/marketForkTests/StYEthMarketForkTest.t.sol index 6ad472e4..03b878aa 100644 --- a/test/marketForkTests/StYEthMarketForkTest.t.sol +++ b/test/marketForkTests/StYEthMarketForkTest.t.sol @@ -18,7 +18,7 @@ contract StYEthMarketForkTest is MarketBaseForkTest { function setUp() public { //This will fail if there's no mainnet variable in foundry.toml string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 19015193); + vm.createSelectFork(url); escrow = new SimpleERC20Escrow(); stYEthFeed = new StYEthPriceFeed(); diff --git a/test/util/FeedSwitchNavSUSDe24Sep25.t.sol b/test/util/FeedSwitchNavSUSDe24Sep25.t.sol deleted file mode 100644 index 114e54a3..00000000 --- a/test/util/FeedSwitchNavSUSDe24Sep25.t.sol +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.18; - -import {FeedSwitch, IChainlinkFeed} from "src/util/FeedSwitch.sol"; -import {BaseFeedSwitchNavForkTest} from "test/util/BaseFeedSwitchNavFork.t.sol"; -import {USDeNavBeforeMaturityFeed} from "src/feeds/USDeNavBeforeMaturityFeed.sol"; -import {PendleNAVFeed} from "src/feeds/PendleNAVFeed.sol"; - -contract FeedSwitchNavSUSDe24Sep25Test is BaseFeedSwitchNavForkTest { - address _beforeMaturityFeed; - address _afterMaturityFeed = address(0xB3C1D801A02d88adC96A294123c2Daa382345058); // USDe Chainlink Wrapper - address _pendlePT = address(0x9F56094C450763769BA0EA9Fe2876070c0fD5F77); // PT sUSDe 24 Sep 25 - uint256 _baseDiscount = 0.2 ether; // 20% - address sUSDeWrapper = address(0xD723a0910e261de49A90779d38A94aFaAA028F15); - address sUSDe = address(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497); - - function setUp() public { - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 22590519); - - PendleNAVFeed _navFeed = new PendleNAVFeed(_pendlePT, _baseDiscount); - _beforeMaturityFeed = address(new USDeNavBeforeMaturityFeed(sUSDeWrapper,sUSDe,address(_navFeed))); // USDeBeforeMaturityFeed: USDe/USD Feed using sUSDe Chainlink feed and sUSDe/USDe rate and NAV - - initialize(address(_beforeMaturityFeed), address(_afterMaturityFeed), _pendlePT , _baseDiscount, address(_navFeed)); - } -} diff --git a/test/util/FeedSwitchNavSUSDe29May25.t.sol b/test/util/FeedSwitchNavSUSDe29May25.t.sol deleted file mode 100644 index dcbdc014..00000000 --- a/test/util/FeedSwitchNavSUSDe29May25.t.sol +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.18; - -import {FeedSwitch, IChainlinkFeed} from "src/util/FeedSwitch.sol"; -import {BaseFeedSwitchNavForkTest} from "test/util/BaseFeedSwitchNavFork.t.sol"; -import {USDeNavBeforeMaturityFeed} from "src/feeds/USDeNavBeforeMaturityFeed.sol"; -import {PendleNAVFeed} from "src/feeds/PendleNAVFeed.sol"; - -contract FeedSwitchNavSUSDe29May25Test is BaseFeedSwitchNavForkTest { - address _beforeMaturityFeed; - address _afterMaturityFeed = address(0xB3C1D801A02d88adC96A294123c2Daa382345058); // USDe Chainlink Wrapper - address _pendlePT = address(0xb7de5dFCb74d25c2f21841fbd6230355C50d9308); // PT sUSDe 29 May 25 - uint256 _baseDiscount = 0.2 ether; // 20% - address sUSDeWrapper = address(0xD723a0910e261de49A90779d38A94aFaAA028F15); - address sUSDe = address(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497); - - function setUp() public { - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 22018716); - - PendleNAVFeed _navFeed = new PendleNAVFeed(_pendlePT, _baseDiscount); - _beforeMaturityFeed = address(new USDeNavBeforeMaturityFeed(sUSDeWrapper,sUSDe,address(_navFeed))); // USDeBeforeMaturityFeed: USDe/USD Feed using sUSDe Chainlink feed and sUSDe/USDe rate and NAV - - initialize(address(_beforeMaturityFeed), address(_afterMaturityFeed), _pendlePT , _baseDiscount, address(_navFeed)); - } -} diff --git a/test/util/FeedSwitchNavSUSDe31July25.t.sol b/test/util/FeedSwitchNavSUSDe31July25.t.sol deleted file mode 100644 index ebd7db08..00000000 --- a/test/util/FeedSwitchNavSUSDe31July25.t.sol +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.18; - -import {FeedSwitch, IChainlinkFeed} from "src/util/FeedSwitch.sol"; -import {BaseFeedSwitchNavForkTest} from "test/util/BaseFeedSwitchNavFork.t.sol"; -import {USDeNavBeforeMaturityFeed} from "src/feeds/USDeNavBeforeMaturityFeed.sol"; -import {PendleNAVFeed} from "src/feeds/PendleNAVFeed.sol"; - -contract FeedSwitchNavSUSDe31July25Test is BaseFeedSwitchNavForkTest { - address _beforeMaturityFeed; - address _afterMaturityFeed = address(0xB3C1D801A02d88adC96A294123c2Daa382345058); // USDe Chainlink Wrapper - address _pendlePT = address(0x3b3fB9C57858EF816833dC91565EFcd85D96f634); // PT sUSDe 31 July 25 - uint256 _baseDiscount = 0.2 ether; // 20% - address sUSDeWrapper = address(0xD723a0910e261de49A90779d38A94aFaAA028F15); - address sUSDe = address(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497); - - function setUp() public { - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 22582482); - - PendleNAVFeed _navFeed = new PendleNAVFeed(_pendlePT, _baseDiscount); - _beforeMaturityFeed = address(new USDeNavBeforeMaturityFeed(sUSDeWrapper,sUSDe,address(_navFeed))); // USDeBeforeMaturityFeed: USDe/USD Feed using sUSDe Chainlink feed and sUSDe/USDe rate and NAV - - initialize(address(_beforeMaturityFeed), address(_afterMaturityFeed), _pendlePT , _baseDiscount, address(_navFeed)); - } -} diff --git a/test/util/FeedSwitchNavUSDe25Sep25.t.sol b/test/util/FeedSwitchNavUSDe25Sep25.t.sol deleted file mode 100644 index 33c1479c..00000000 --- a/test/util/FeedSwitchNavUSDe25Sep25.t.sol +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.18; - -import {FeedSwitch, IChainlinkFeed} from "src/util/FeedSwitch.sol"; -import {BaseFeedSwitchNavForkTest} from "test/util/BaseFeedSwitchNavFork.t.sol"; -import {NavBeforeMaturityFeed} from "src/feeds/NavBeforeMaturityFeed.sol"; -import {PendleNAVFeed} from "src/feeds/PendleNAVFeed.sol"; -import {console2} from "forge-std/console2.sol"; - -contract FeedSwitchNavUSDe25Sep25Test is BaseFeedSwitchNavForkTest { - address usdeWrapper = address(0xB3C1D801A02d88adC96A294123c2Daa382345058); - NavBeforeMaturityFeed _beforeMaturityFeed; - address _afterMaturityFeed = address(0xB3C1D801A02d88adC96A294123c2Daa382345058); // USDe Chainlink Wrapper - address _pendlePT = address(0x9F56094C450763769BA0EA9Fe2876070c0fD5F77); // PT sUSDe 24 Sep 25 - uint256 _baseDiscount = 0.2 ether; // 20% - - function setUp() public { - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 22590519); - - PendleNAVFeed _navFeed = new PendleNAVFeed(_pendlePT, _baseDiscount); - _beforeMaturityFeed = new NavBeforeMaturityFeed(usdeWrapper,address(_navFeed)); // USDeBeforeMaturityFeed: USDe/USD Feed using USDe Chainlink wrapper and NAV - - initialize(address(_beforeMaturityFeed), address(_afterMaturityFeed), _pendlePT , _baseDiscount, address(_navFeed)); - } -} diff --git a/test/util/MIgration.t.sol b/test/util/MIgration.t.sol new file mode 100644 index 00000000..ab48123a --- /dev/null +++ b/test/util/MIgration.t.sol @@ -0,0 +1,45 @@ +pragma solidity ^0.8.13; + +import "forge-std/Test.sol"; +import {Migration} from "src/util/Migration.sol"; +import {ALEV2, IERC20, IPendleHelper} from "src/util/ALEV2.sol"; +import {DbrHelper} from "src/util/DbrHelper.sol"; +import {ConfigAddr} from "test/ConfigAddr.sol"; +contract MigrationTest is Test, ConfigAddr { + + Migration migration; + ALEV2 ale; + DbrHelper dbrHelper; + + function setUp() public { + string memory url = vm.rpcUrl("mainnet"); + vm.createSelectFork(url); + + ale = new ALEV2(newTriDBRAddr, address(this)); + dbrHelper = new DbrHelper(newTriDBRAddr, address(this)); + migration = new Migration(address(ale),address(dbrHelper)); + ale.setPendingGov(address(migration)); + dbrHelper.setPendingGov(address(migration)); + } + + function test_migrateMarkets() public { + vm.prank(gov); + migration.migrate(); + address[] memory markets = migration.getMarkets(); + ALEV2 oldALE = migration.OLD_ALE(); + for (uint i = 0; i < markets.length; ++i) { + (IERC20 buySellToken, , IPendleHelper helper, bool useProxy) = oldALE.markets(markets[i]); + (IERC20 buySellToken2, , IPendleHelper helper2, bool useProxy2) = ale.markets(markets[i]); + // ALE migration checks + assertEq(address(buySellToken), address(buySellToken2)); + assertEq(address(helper), address(helper2)); + assertEq(useProxy,useProxy2); + // DbrHelper migration checks + IERC20 dola = IERC20(dolaAddr); + assertEq(dola.allowance(address(dbrHelper),address(markets[i])), type(uint).max); + } + + assertEq(ale.pendingGov(), gov); + assertEq(dbrHelper.pendingGov(), gov); + } +} \ No newline at end of file diff --git a/test/util/aleTests/ALEDolaCrvUSD.t.sol b/test/util/aleTests/ALEDolaCrvUSD.t.sol deleted file mode 100644 index ce413ae7..00000000 --- a/test/util/aleTests/ALEDolaCrvUSD.t.sol +++ /dev/null @@ -1,457 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {CurveDolaLPHelper} from "src/util/CurveDolaLPHelper.sol"; -import "test/marketForkTests/CrvUSDDolaConvexMarketForkTest.t.sol"; -import {console} from "forge-std/console.sol"; -import {IMultiMarketTransformHelper} from "src/interfaces/IMultiMarketTransformHelper.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; - -interface IFlashMinter { - function setMaxFlashLimit(uint256 limit) external; - - function flashFee( - address _token, - uint256 _value - ) external view returns (uint256); -} - -contract ALEDolaCrvUSDTest is CrvUSDDolaConvexMarketForkTest { - ALEV2 ale; - IFlashMinter flash; - address userPk = vm.addr(1); - CurveDolaLPHelper helper; - address userPkEscrow; - ICurvePool curvePool; - - function setUp() public override { - super.setUp(); - curvePool = dolaCrvUSD; - - helper = CurveDolaLPHelper(curveDolaLPHelperAddr); - - vm.startPrank(gov); - DOLA.mint(address(this), 100000 ether); - helper.setMarket(address(market), address(curvePool), 0, 2, address(0)); - ale = ALEV2(payable(aleV2Addr)); - ale.setMarket(address(market), address(DOLA), address(helper), false); - - borrowController.allow(address(ale)); - vm.stopPrank(); - userPkEscrow = address(market.predictEscrow(userPk)); - } - - function test_leveragePosition() public { - vm.prank(gov); - DOLA.mint(userPk, 10000 ether); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 10000 ether); - helper.convertToCollateralAndDeposit( - 10000 ether, - userPk, - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - uint256 lpAmount = ConvexEscrowV2(address(userPkEscrow)).balance(); - console.log("lpAmount", lpAmount); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - uint256[2] memory amounts = [maxBorrowAmount, 0]; - uint256 lpAmountAdded = curvePool.calc_token_amount(amounts, true); - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq( - ConvexEscrowV2(address(userPkEscrow)).balance(), - lpAmount + lpAmountAdded - ); - } - - function test_leveragePosition_buyDBR() public { - vm.prank(gov); - DOLA.mint(userPk, 10000 ether); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 10000 ether); - helper.convertToCollateralAndDeposit( - 10000 ether, - userPk, - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - uint256 lpAmount = ConvexEscrowV2(address(userPkEscrow)).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 365 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 97) / 100, - 0 - ); - - uint256[2] memory amounts = [maxBorrowAmount, 0]; - uint256 lpAmountAdded = curvePool.calc_token_amount(amounts, true); - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq( - ConvexEscrowV2(address(userPkEscrow)).balance(), - lpAmount + lpAmountAdded - ); - assertGt(dbr.balanceOf(userPk), (dbrAmount * 97) / 100); - } - - function test_depositAndLeveragePosition_DOLA() public { - vm.prank(gov); - DOLA.mint(userPk, 11000 ether); - uint256 initialDolaDeposit = 1000 ether; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 10000 ether); - helper.convertToCollateralAndDeposit( - 10000 ether, - userPk, - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - uint256 lpAmount = ConvexEscrowV2(address(userPkEscrow)).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - uint256[2] memory amounts = [maxBorrowAmount + initialDolaDeposit, 0]; - uint256 lpAmountAdded = curvePool.calc_token_amount(amounts, true); - - vm.startPrank(userPk); - DOLA.approve(address(ale), initialDolaDeposit); - ale.depositAndLeveragePosition( - initialDolaDeposit, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData, - false - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq( - ConvexEscrowV2(address(userPkEscrow)).balance(), - lpAmount + lpAmountAdded - ); - } - - function test_depositAndLeveragePosition_LP() public { - vm.prank(gov); - DOLA.mint(userPk, 11000 ether); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 11000 ether); - uint256 initialLpAmount = helper.convertToCollateral( - address(0), - 1000 ether, - abi.encode(address(market), 0) - ); - helper.convertToCollateralAndDeposit( - 10000 ether, - userPk, - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - uint256 lpAmount = ConvexEscrowV2(address(userPkEscrow)).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - uint256[2] memory amounts = [maxBorrowAmount, 0]; - uint256 lpAmountAdded = curvePool.calc_token_amount(amounts, true); - - vm.startPrank(userPk); - IERC20(address(curvePool)).approve(address(ale), initialLpAmount); - ale.depositAndLeveragePosition( - initialLpAmount, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData, - true - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq( - ConvexEscrowV2(address(userPkEscrow)).balance(), - lpAmount + lpAmountAdded + initialLpAmount - ); - } - - function test_deleveragePosition() public { - test_leveragePosition(); - uint256 lpAmount = ConvexEscrowV2(address(userPkEscrow)).balance(); - uint256 amountToWithdraw = lpAmount / 2; - - uint256 dolaRedeemed = curvePool.calc_withdraw_one_coin( - amountToWithdraw, - 0 - ); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.prank(userPk); - ale.deleveragePosition( - dolaRedeemed / 2, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq( - ConvexEscrowV2(address(userPkEscrow)).balance(), - lpAmount - amountToWithdraw - ); - assertApproxEqAbs(DOLA.balanceOf(userPk), dolaRedeemed / 2, 1); - } - - function test_deleveragePosition_sellDBR() public { - test_leveragePosition(); - uint256 lpAmount = ConvexEscrowV2(address(userPkEscrow)).balance(); - uint256 amountToWithdraw = lpAmount; - - uint256 dolaRedeemed = curvePool.calc_withdraw_one_coin( - amountToWithdraw, - 0 - ); - uint256 debt = market.debts(address(userPk)); - - assertGt(debt, 0); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 1, - 0 - ); // sell all DBR - bytes memory swapData; - - vm.startPrank(userPk); - dbr.approve(address(ale), dbr.balanceOf(userPk)); - ale.deleveragePosition( - debt, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq( - ConvexEscrowV2(address(userPkEscrow)).balance(), - lpAmount - amountToWithdraw - ); - // Dbrs have also been sold - assertGt(DOLA.balanceOf(userPk), dolaRedeemed - debt); - assertEq(dbr.balanceOf(userPk), 0); - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (amountCollat * - oracle.viewPrice(address(curvePool), 0) * - market.collateralFactorBps()) / - 10_000 / - 1e18; - } -} diff --git a/test/util/aleTests/ALEDolaCrvUSDYearnV2.t.sol b/test/util/aleTests/ALEDolaCrvUSDYearnV2.t.sol deleted file mode 100644 index 8f837404..00000000 --- a/test/util/aleTests/ALEDolaCrvUSDYearnV2.t.sol +++ /dev/null @@ -1,539 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {CurveDolaLPHelper} from "src/util/CurveDolaLPHelper.sol"; -import "test/marketForkTests/CrvUSDDolaYearnV2MarketForkTest.t.sol"; -import {console} from "forge-std/console.sol"; -import {IMultiMarketTransformHelper} from "src/interfaces/IMultiMarketTransformHelper.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {YearnVaultV2Helper, IYearnVaultV2} from "src/util/YearnVaultV2Helper.sol"; - -interface IFlashMinter { - function setMaxFlashLimit(uint256 _maxFlashLimit) external; - - function flashFee( - address _token, - uint256 _value - ) external view returns (uint256); -} - -contract ALEDolaCrvUSDYearnV2Test is CrvUSDDolaYearnV2MarketForkTest { - ALEV2 ale; - IFlashMinter flash; - address userPk = vm.addr(1); - CurveDolaLPHelper helper; - address userPkEscrow; - IYearnVaultV2 vault = IYearnVaultV2(yearn); - - function setUp() public override { - super.setUp(); - - helper = CurveDolaLPHelper(curveDolaLPHelperAddr); - - vm.startPrank(gov); - DOLA.mint(address(this), 100000 ether); - helper.setMarket(address(market), address(dolaCrvUSD), 0, 2, yearn); - ale = ALEV2(payable(aleV2Addr)); - ale.setMarket(address(market), address(DOLA), address(helper), false); - - borrowController.allow(address(ale)); - vm.stopPrank(); - userPkEscrow = address(market.predictEscrow(userPk)); - } - - function test_leveragePosition() public { - vm.prank(gov); - DOLA.mint(userPk, 1000000 ether); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 1000000 ether); - helper.convertToCollateralAndDeposit( - 1000000 ether, - userPk, - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - uint256 sharesAmount = vault.balanceOf(userPkEscrow); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(sharesAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - uint256[2] memory amounts = [maxBorrowAmount, 0]; - uint256 lpAmountAdded = dolaCrvUSD.calc_token_amount(amounts, true); - uint256 sharesAdded = YearnVaultV2Helper.assetToCollateral( - vault, - lpAmountAdded - ); - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq(vault.balanceOf(userPkEscrow), sharesAmount + sharesAdded); - } - - function test_leveragePosition_buyDBR() public { - vm.prank(gov); - DOLA.mint(userPk, 10000 ether); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 10000 ether); - helper.convertToCollateralAndDeposit( - 10000 ether, - userPk, - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - uint256 sharesAmount = vault.balanceOf(userPkEscrow); - - uint maxBorrowAmount = _getMaxBorrowAmount(sharesAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 365 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 98) / 100, - 0 - ); - - uint256[2] memory amounts = [maxBorrowAmount, 0]; - - uint256 sharesAdded = YearnVaultV2Helper.assetToCollateral( - vault, - dolaCrvUSD.calc_token_amount(amounts, true) - ); - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq(vault.balanceOf(userPkEscrow), sharesAmount + sharesAdded); - assertGt(dbr.balanceOf(userPk), (dbrAmount * 98) / 100); - } - - function test_depositAndLeveragePosition_DOLA() public { - vm.prank(gov); - DOLA.mint(userPk, 11000 ether); - uint256 initialDolaDeposit = 1000 ether; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 10000 ether); - helper.convertToCollateralAndDeposit( - 10000 ether, - userPk, - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - uint256 sharesAmount = vault.balanceOf(userPkEscrow); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(sharesAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - uint256[2] memory amounts = [maxBorrowAmount + initialDolaDeposit, 0]; - uint256 lpAmountAdded = dolaCrvUSD.calc_token_amount(amounts, true); - uint256 sharesAdded = YearnVaultV2Helper.assetToCollateral( - vault, - lpAmountAdded - ); - vm.startPrank(userPk); - DOLA.approve(address(ale), initialDolaDeposit); - ale.depositAndLeveragePosition( - initialDolaDeposit, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData, - false - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq(vault.balanceOf(userPkEscrow), sharesAmount + sharesAdded); - } - - function test_depositAndLeveragePosition_LP() public { - vm.prank(gov); - DOLA.mint(userPk, 11000 ether); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 11000 ether); - uint256 initialSharesAmount = helper.convertToCollateral( - address(0), - 1000 ether, - abi.encode(address(market), 0) - ); - helper.convertToCollateralAndDeposit( - 10000 ether, - userPk, - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - uint256 sharesAmount = vault.balanceOf(userPkEscrow); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(sharesAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - uint256[2] memory amounts = [maxBorrowAmount, 0]; - uint256 lpAmountAdded = dolaCrvUSD.calc_token_amount(amounts, true); - uint256 sharesAdded = YearnVaultV2Helper.assetToCollateral( - vault, - lpAmountAdded - ); - vm.startPrank(userPk); - IERC20(address(vault)).approve(address(ale), initialSharesAmount); - ale.depositAndLeveragePosition( - initialSharesAmount, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData, - true - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq( - vault.balanceOf(userPkEscrow), - sharesAmount + sharesAdded + initialSharesAmount - ); - } - - function test_deleveragePosition(uint sharesAmount) public { - test_leveragePosition(); - uint256 totalSharesAmount = vault.balanceOf(userPkEscrow); - vm.assume(sharesAmount > 0.00001 ether); - vm.assume(sharesAmount <= totalSharesAmount); - uint256 amountToWithdraw = sharesAmount / 2; - - uint256 dolaRedeemed = dolaCrvUSD.calc_withdraw_one_coin( - YearnVaultV2Helper.collateralToAsset(vault, amountToWithdraw), - 0 - ); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.prank(userPk); - ale.deleveragePosition( - dolaRedeemed / 2, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq( - vault.balanceOf(userPkEscrow), - totalSharesAmount - amountToWithdraw - ); - assertApproxEqAbs(DOLA.balanceOf(userPk), dolaRedeemed / 2, 1); - } - - function test_deleveragePosition_Yearn_Leftover() public { - test_leveragePosition(); - // No leftover - assertEq(vault.balanceOf(address(helper)), 0); - - // Add some Yearn to simulate a leftover on the helper - vm.prank(gov); - DOLA.mint(userPk, 1000 ether); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), 1000 ether); - uint256 yearnLeftover = helper.convertToCollateral( - 1000 ether, - address(helper), - abi.encode(address(market), 0) - ); - vm.stopPrank(); - - // Leftover is in helper - assertEq(vault.balanceOf(address(helper)), yearnLeftover); - assertEq(vault.balanceOf(userPk), 0); - - uint256 sharesAmount = vault.balanceOf(userPkEscrow); - uint256 amountToWithdraw = sharesAmount / 2; - - uint256 dolaRedeemed = dolaCrvUSD.calc_withdraw_one_coin( - YearnVaultV2Helper.collateralToAsset(vault, amountToWithdraw), - 0 - ); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.prank(userPk); - ale.deleveragePosition( - dolaRedeemed / 2, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq( - vault.balanceOf(userPkEscrow), - sharesAmount - amountToWithdraw - ); - assertApproxEqAbs(DOLA.balanceOf(userPk), dolaRedeemed / 2, 1); - - // Leftover is in user balance - assertEq(vault.balanceOf(address(helper)), 0); - assertEq(vault.balanceOf(userPk), yearnLeftover); - } - - function test_deleveragePosition_sellDBR() public { - test_leveragePosition(); - uint256 sharesAmount = vault.balanceOf(userPkEscrow); - uint256 amountToWithdraw = sharesAmount / 2; - - uint256 dolaRedeemed = dolaCrvUSD.calc_withdraw_one_coin( - YearnVaultV2Helper.collateralToAsset(vault, amountToWithdraw), - 0 - ); - uint256 debt = market.debts(address(userPk)); - - assertGt(debt, 0); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 1, - 0 - ); // sell all DBR - bytes memory swapData; - - vm.startPrank(userPk); - dbr.approve(address(ale), dbr.balanceOf(userPk)); - ale.deleveragePosition( - debt, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(0)), - dbrData - ); - - assertEq( - vault.balanceOf(userPkEscrow), - sharesAmount - amountToWithdraw - ); - // Dbrs have also been sold - assertGt(DOLA.balanceOf(userPk), dolaRedeemed - debt); - assertEq(dbr.balanceOf(userPk), 0); - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (amountCollat * - oracle.viewPrice(address(yearn), 0) * - market.collateralFactorBps()) / - 10_000 / - 1e18; - } -} diff --git a/test/util/aleTests/ALEDolaDeUSD.t.sol b/test/util/aleTests/ALEDolaDeUSD.t.sol deleted file mode 100644 index 8e9909d2..00000000 --- a/test/util/aleTests/ALEDolaDeUSD.t.sol +++ /dev/null @@ -1,31 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {CurveDolaLPHelperDynamic} from "src/util/CurveDolaLPHelperDynamic.sol"; -import "test/marketForkTests/DolaDeUSDConvexMarketForkTest.t.sol"; -import {console} from "forge-std/console.sol"; -import {IMultiMarketTransformHelper} from "src/interfaces/IMultiMarketTransformHelper.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {ALEBaseDolaLPDynTest, IFlashMinter} from "test/util/aleTests/ALEBaseDolaLPDyn.sol"; - -contract ALEDolaDeUSDTest is - ALEBaseDolaLPDynTest, - DolaDeUSDConvexMarketForkTest -{ - function setUp() public override { - super.setUp(); - curvePool = dolaDeUSD; - - helper = CurveDolaLPHelperDynamic(curveDolaLPHelperDynamicAddr); - - vm.startPrank(gov); - DOLA.mint(address(this), 100000 ether); - helper.setMarket(address(market), address(curvePool), 0, 2, address(0)); - ale = ALEV2(payable(aleV2Addr)); - ale.setMarket(address(market), address(DOLA), address(helper), false); - - borrowController.allow(address(ale)); - vm.stopPrank(); - userPkEscrow = address(market.predictEscrow(userPk)); - } -} diff --git a/test/util/aleTests/ALEDolaDeUSDYearnV2.t.sol b/test/util/aleTests/ALEDolaDeUSDYearnV2.t.sol deleted file mode 100644 index 858405a8..00000000 --- a/test/util/aleTests/ALEDolaDeUSDYearnV2.t.sol +++ /dev/null @@ -1,33 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {CurveDolaLPHelperDynamic} from "src/util/CurveDolaLPHelperDynamic.sol"; -import "test/marketForkTests/DolaDeUSDYearnV2MarketForkTest.t.sol"; -import {console} from "forge-std/console.sol"; -import {IMultiMarketTransformHelper} from "src/interfaces/IMultiMarketTransformHelper.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {YearnVaultV2Helper, IYearnVaultV2} from "src/util/YearnVaultV2Helper.sol"; -import {ALEBaseDolaLPDynYearnV2Test, IFlashMinter} from "test/util/aleTests/ALEBaseDolaLPDynYearnV2.sol"; - -contract ALEDolaDeUSDeYearnV2Test is - ALEBaseDolaLPDynYearnV2Test, - DolaDeUSDYearnV2MarketForkTest -{ - function setUp() public override { - super.setUp(); - - helper = CurveDolaLPHelperDynamic(curveDolaLPHelperDynamicAddr); - - curvePool = dolaDeUSD; - vault = IYearnVaultV2(yearn); - vm.startPrank(gov); - DOLA.mint(address(this), 100000 ether); - helper.setMarket(address(market), address(curvePool), 0, 2, yearn); - ale = ALEV2(payable(aleV2Addr)); - ale.setMarket(address(market), address(DOLA), address(helper), false); - - borrowController.allow(address(ale)); - vm.stopPrank(); - userPkEscrow = address(market.predictEscrow(userPk)); - } -} diff --git a/test/util/aleTests/ALEDolaUSR.t.sol b/test/util/aleTests/ALEDolaUSR.t.sol index fe2f548a..f53a3441 100644 --- a/test/util/aleTests/ALEDolaUSR.t.sol +++ b/test/util/aleTests/ALEDolaUSR.t.sol @@ -21,11 +21,31 @@ contract ALEDolaUSRTest is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, address(0)); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); + ale.setCurvePool(address(newTriDBRAddr),0,1); + vm.stopPrank(); userPkEscrow = address(market.predictEscrow(userPk)); + + _seedTriDbrPool(); + } + + function _seedTriDbrPool() internal { + vm.prank(gov); + DOLA.mint(address(this), 600000 ether); + DOLA.approve(address(newTriDBRAddr), 600000 ether); + deal(address(dbrAddr), address(this), 10000000 ether); + deal(address(invAddr), address(this), 20000 ether); + IERC20(dbrAddr).approve(address(newTriDBRAddr), 10000000 ether); + IERC20(invAddr).approve(address(newTriDBRAddr), 20000 ether); + uint[3] memory amounts; + amounts[0] = 600000 ether; + amounts[1] = 10000000 ether; + amounts[2] = 20000 ether; + + ICurvePool(newTriDBRAddr).add_liquidity(amounts, 0); } } diff --git a/test/util/aleTests/ALEDolaUSRYearnV2.t.sol b/test/util/aleTests/ALEDolaUSRYearnV2.t.sol index 5c162d56..e3b6dbeb 100644 --- a/test/util/aleTests/ALEDolaUSRYearnV2.t.sol +++ b/test/util/aleTests/ALEDolaUSRYearnV2.t.sol @@ -22,7 +22,7 @@ contract ALEDolaUSRYearnV2Test is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, yearn); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); diff --git a/test/util/aleTests/ALEDolaWstUSR.t.sol b/test/util/aleTests/ALEDolaWstUSR.t.sol index 5adf69bf..4e6b37ba 100644 --- a/test/util/aleTests/ALEDolaWstUSR.t.sol +++ b/test/util/aleTests/ALEDolaWstUSR.t.sol @@ -23,7 +23,7 @@ contract ALEDolaWstUSRTest is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, address(0)); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); diff --git a/test/util/aleTests/ALEDolaWstUSRYearnV2.t.sol b/test/util/aleTests/ALEDolaWstUSRYearnV2.t.sol index 9d489b19..e9901cc4 100644 --- a/test/util/aleTests/ALEDolaWstUSRYearnV2.t.sol +++ b/test/util/aleTests/ALEDolaWstUSRYearnV2.t.sol @@ -24,7 +24,7 @@ contract ALEDolaWstUSRYearnV2Test is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, yearn); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); diff --git a/test/util/aleTests/ALEDolasUSDe.t.sol b/test/util/aleTests/ALEDolasUSDe.t.sol index f1a51ccc..974bb7b2 100644 --- a/test/util/aleTests/ALEDolasUSDe.t.sol +++ b/test/util/aleTests/ALEDolasUSDe.t.sol @@ -21,7 +21,7 @@ contract ALEDolasUSDeTest is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, address(0)); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); diff --git a/test/util/aleTests/ALEDolasUSDeYearnV2.t.sol b/test/util/aleTests/ALEDolasUSDeYearnV2.t.sol index f42ddaa5..43a546c5 100644 --- a/test/util/aleTests/ALEDolasUSDeYearnV2.t.sol +++ b/test/util/aleTests/ALEDolasUSDeYearnV2.t.sol @@ -23,7 +23,7 @@ contract ALEDolasUSDeYearnV2Test is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, yearn); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); diff --git a/test/util/aleTests/ALEDolasUSDs.t.sol b/test/util/aleTests/ALEDolasUSDs.t.sol index a41b00d1..ab1c3dff 100644 --- a/test/util/aleTests/ALEDolasUSDs.t.sol +++ b/test/util/aleTests/ALEDolasUSDs.t.sol @@ -21,7 +21,7 @@ contract ALEDolasUSDsTest is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, address(0)); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); diff --git a/test/util/aleTests/ALEDolasUSDsYearnV2.t.sol b/test/util/aleTests/ALEDolasUSDsYearnV2.t.sol index 9ce32966..0be3632d 100644 --- a/test/util/aleTests/ALEDolasUSDsYearnV2.t.sol +++ b/test/util/aleTests/ALEDolasUSDsYearnV2.t.sol @@ -21,7 +21,7 @@ contract ALEDolasUSDsYearnV2Test is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, yearn); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); diff --git a/test/util/aleTests/ALEDolascrvUSD.t.sol b/test/util/aleTests/ALEDolascrvUSD.t.sol index 0119f898..e662dbaa 100644 --- a/test/util/aleTests/ALEDolascrvUSD.t.sol +++ b/test/util/aleTests/ALEDolascrvUSD.t.sol @@ -21,7 +21,7 @@ contract ALEDolascrvUSDTest is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, address(0)); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); vm.stopPrank(); diff --git a/test/util/aleTests/ALEDolascrvUSDYearnV2.t.sol b/test/util/aleTests/ALEDolascrvUSDYearnV2.t.sol index 2f3b3ded..0de48c81 100644 --- a/test/util/aleTests/ALEDolascrvUSDYearnV2.t.sol +++ b/test/util/aleTests/ALEDolascrvUSDYearnV2.t.sol @@ -24,7 +24,7 @@ contract ALEDolascrvUSDYearnV2Test is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 0, 2, yearn); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); vm.stopPrank(); diff --git a/test/util/aleTests/ALEForkTest.t.sol b/test/util/aleTests/ALEForkTest.t.sol deleted file mode 100644 index 57970af6..00000000 --- a/test/util/aleTests/ALEForkTest.t.sol +++ /dev/null @@ -1,1213 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "test/marketForkTests/MarketForkTest.sol"; -import "src/DBR.sol"; -import "test/mocks/ERC20.sol"; -import {ALE} from "src/util/ALE.sol"; -import {ConfigAddr} from "test/ConfigAddr.sol"; - -contract MockExchangeProxy { - IOracle oracle; - IERC20 dola; - - constructor(address _oracle, address _dola) { - oracle = IOracle(_oracle); - dola = IERC20(_dola); - } - - function swapDolaIn( - IERC20 collateral, - uint256 dolaAmount - ) external returns (bool success, bytes memory ret) { - dola.transferFrom(msg.sender, address(this), dolaAmount); - uint256 collateralAmount = (dolaAmount * 1e18) / - oracle.viewPrice(address(collateral), 0); - collateral.transfer(msg.sender, collateralAmount); - success = true; - } - - function swapDolaOut( - IERC20 collateral, - uint256 collateralAmount - ) external returns (bool success, bytes memory ret) { - collateral.transferFrom(msg.sender, address(this), collateralAmount); - uint256 dolaAmount = (collateralAmount * - oracle.viewPrice(address(collateral), 0)) / 1e18; - dola.transfer(msg.sender, dolaAmount); - success = true; - } -} - -interface IFlashMinter { - function setMaxFlashLimit(uint256 limit) external; -} - -contract ALEForkTest is MarketForkTest { - bytes onlyGovUnpause = "Only governance can unpause"; - bytes onlyPauseGuardianOrGov = - "Only pause guardian or governance can pause"; - bytes exceededLimit = "Exceeded credit limit"; - bytes repaymentGtThanDebt = "Repayment greater than debt"; - - error NothingToDeposit(); - - MockExchangeProxy exchangeProxy; - ALE ale; - address triDBR = 0xC7DE47b9Ca2Fc753D6a2F167D8b3e19c6D18b19a; - IFlashMinter flash; - - function setUp() public { - //This will fail if there's no mainnet variable in foundry.toml - string memory url = vm.rpcUrl("mainnet"); - vm.createSelectFork(url, 20590050); - init(crvMarketAddr, crvUsdFeedAddr); - - vm.prank(gov); - market.pauseBorrows(false); - - vm.startPrank(chair, chair); - fed.expansion(IMarket(address(market)), 100_000e18); - vm.stopPrank(); - - exchangeProxy = new MockExchangeProxy( - address(market.oracle()), - address(DOLA) - ); - - ale = new ALE(address(exchangeProxy), triDBR); - // ALE setup - vm.prank(gov); - DOLA.addMinter(address(ale)); - - ale.setMarket( - address(market), - address(market.collateral()), - address(0), - true - ); - - // Allow contract - vm.startPrank(gov); - borrowController.allow(address(ale)); - - flash = IFlashMinter(address(ale.flash())); - DOLA.addMinter(address(flash)); - flash.setMaxFlashLimit(1000000e18); - vm.stopPrank(); - } - - function getMaxLeverageBorrowAmount( - uint256 collateralAmount, - uint256 iterations - ) internal view returns (uint256) { - uint256 maxDolaAmount = getMaxBorrowAmount(collateralAmount); - uint256 totalDola = maxDolaAmount; - for (uint i = 0; i < iterations; i++) { - uint256 dolaAmount = getMaxBorrowAmount( - convertDolaToCollat(maxDolaAmount) - ); - maxDolaAmount = dolaAmount; - totalDola += dolaAmount; - } - return totalDola; - } - - function test_depositAndLeveragePosition_buyDBR( - uint256 crvTestAmount - ) public { - vm.assume(crvTestAmount < 50000 ether); - vm.assume(crvTestAmount > 0.000001 ether); - // We are going to deposit and leverage the position - // uint crvTestAmount = 13606; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - - uint maxBorrowAmount = getMaxBorrowAmount(crvTestAmount) / 10; // we want to borrow only 10% of the max amount to exchange - - // recharge mocked proxy for swap, we need to swap DOLA to collateral - deal( - address(market.collateral()), - address(exchangeProxy), - convertDolaToCollat(maxBorrowAmount) - ); - - vm.startPrank(userPk, userPk); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 365 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData = ALE.DBRHelper( - dolaForDBR, - (dbrAmount * 98) / 100, - 0 - ); // DBR buy - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - collateral, - maxBorrowAmount - ); - - assertEq(dbr.balanceOf(userPk), 0); - - collateral.approve(address(ale), crvTestAmount); - - // We set crvTestAmount as initial deposit - ale.depositAndLeveragePosition( - crvTestAmount, - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData, - false - ); - - // Balance in escrow is equal to the collateral deposited + the extra collateral swapped from the leverage - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - crvTestAmount + convertDolaToCollat(maxBorrowAmount) - ); - assertEq(DOLA.balanceOf(userPk), 0); - - assertGt(dbr.balanceOf(userPk), (dbrAmount * 98) / 100); - } - - function test_fail_depositAndLeveragePosition_buyDBR_with_ZERO_deposit() - public - { - // We are going to deposit and leverage the position - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - - uint maxBorrowAmount = getMaxBorrowAmount(crvTestAmount) / 10; // we want to borrow only 10% of the max amount to exchange - - // recharge mocked proxy for swap, we need to swap DOLA to collateral - deal( - address(market.collateral()), - address(exchangeProxy), - convertDolaToCollat(maxBorrowAmount) - ); - - vm.startPrank(userPk, userPk); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 365 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData = ALE.DBRHelper( - dolaForDBR, - (dbrAmount * 98) / 100, // DBR buy, - 0 // Dola to borrow and withdraw after leverage - ); - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - collateral, - maxBorrowAmount - ); - - assertEq(dbr.balanceOf(userPk), 0); - - collateral.approve(address(ale), crvTestAmount); - - // We try to set 0 as initial deposit, reverts - vm.expectRevert(NothingToDeposit.selector); - ale.depositAndLeveragePosition( - 0, - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData, - false - ); - } - - function test_leveragePosition_buyDBR_withdrawDOLA() public { - // We are going to deposit some CRV, then leverage the position - uint crvTestAmount = 1000 ether; - uint dolaToWithdraw = 100 ether; - - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - - uint maxBorrowAmount = getMaxBorrowAmount(crvTestAmount); - - // recharge mocked proxy for swap, we need to swap DOLA to collateral - deal( - address(market.collateral()), - address(exchangeProxy), - convertDolaToCollat(maxBorrowAmount + dolaToWithdraw) - ); - - vm.startPrank(userPk, userPk); - // Initial CRV deposit - deposit(crvTestAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 365 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR + dolaToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData = ALE.DBRHelper( - dolaForDBR, - (dbrAmount * 98) / 100, // DBR buy - dolaToWithdraw // Dola to borrow and withdraw after leverage - ); - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - collateral, - maxBorrowAmount - ); - - assertEq(dbr.balanceOf(userPk), 0); - - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - // Balance in escrow is equal to the collateral deposited + the extra collateral swapped from the leverage - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - crvTestAmount + convertDolaToCollat(maxBorrowAmount) - ); - assertEq(DOLA.balanceOf(userPk), dolaToWithdraw); - - assertGt(dbr.balanceOf(userPk), (dbrAmount * 98) / 100); - } - - function test_leveragePosition_buyDBR() public { - // We are going to deposit some CRV, then leverage the position - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - - uint maxBorrowAmount = getMaxBorrowAmount(crvTestAmount); - - // recharge mocked proxy for swap, we need to swap DOLA to collateral - deal( - address(market.collateral()), - address(exchangeProxy), - convertDolaToCollat(maxBorrowAmount) - ); - - vm.startPrank(userPk, userPk); - // Initial CRV deposit - deposit(crvTestAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 365 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData = ALE.DBRHelper( - dolaForDBR, - (dbrAmount * 98) / 100, // DBR buy - 0 // Dola to borrow and withdraw after leverage - ); - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - collateral, - maxBorrowAmount - ); - - assertEq(dbr.balanceOf(userPk), 0); - - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - // Balance in escrow is equal to the collateral deposited + the extra collateral swapped from the leverage - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - crvTestAmount + convertDolaToCollat(maxBorrowAmount) - ); - assertEq(DOLA.balanceOf(userPk), 0); - - assertGt(dbr.balanceOf(userPk), (dbrAmount * 98) / 100); - } - - function test_deleveragePosition_sellDBR() public { - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - // Max Amount borrowable is the one available from collateral amount + - // the extra borrow amount from the max borrow amount swapped and re-deposited as collateral - uint borrowAmount = getMaxBorrowAmount(crvTestAmount) / 2; - - // recharge mocked proxy for swap, we need to swap collateral to DOLA - vm.startPrank(gov); - DOLA.mint( - address(exchangeProxy), - convertCollatToDola(crvTestAmount / 10) - ); - vm.stopPrank(); - - vm.startPrank(userPk, userPk); - // CRV deposit and DOLA borrow - deposit(crvTestAmount); - market.borrow(borrowAmount); - - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - crvTestAmount - ); - assertEq(DOLA.balanceOf(userPk), borrowAmount); - - // We are going to withdraw only 1/10 of the collateral to deleverage - uint256 amountToWithdraw = collateral.balanceOf( - address(market.predictEscrow(userPk)) - ) / 10; - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData = ALE.DBRHelper( - dbr.balanceOf(userPk), - 0, - 0 - ); // Sell DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaOut.selector, - collateral, - amountToWithdraw - ); - - dbr.approve(address(ale), dbr.balanceOf(userPk)); - - ale.deleveragePosition( - convertCollatToDola(amountToWithdraw), - address(market), - amountToWithdraw, - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - // Some collateral has been withdrawn - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - crvTestAmount - amountToWithdraw - ); - - // User still has dola and actually he has more bc he sold his DBRs - assertGt(DOLA.balanceOf(userPk), borrowAmount); - - assertEq(dbr.balanceOf(userPk), 0); - } - - function test_deleveragePosition_withdrawALL_sellDBR() public { - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - // Max Amount borrowable is the one available from collateral amount + - // the extra borrow amount from the max borrow amount swapped and re-deposited as collateral - uint borrowAmount = getMaxBorrowAmount(crvTestAmount) / 2; - - // recharge mocked proxy for swap, we need to swap collateral to DOLA - vm.startPrank(gov); - DOLA.mint(address(exchangeProxy), convertCollatToDola(crvTestAmount)); - vm.stopPrank(); - - vm.startPrank(userPk, userPk); - // CRV deposit and DOLA borrow - deposit(crvTestAmount); - market.borrow(borrowAmount); - - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - crvTestAmount - ); - assertEq(DOLA.balanceOf(userPk), borrowAmount); - - // We are going to withdraw ALL the collateral to deleverage - uint256 amountToWithdraw = collateral.balanceOf( - address(market.predictEscrow(userPk)) - ); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData = ALE.DBRHelper( - dbr.balanceOf(userPk), - 0, - 0 - ); // Sell DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaOut.selector, - collateral, - amountToWithdraw / 2 - ); - - dbr.approve(address(ale), dbr.balanceOf(userPk)); - - assertEq(collateral.balanceOf(userPk), 0); - - ale.deleveragePosition( - borrowAmount, - address(market), - amountToWithdraw, - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - // No collateral left in the escrow - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - 0 - ); - - // User still has dola and actually he has more bc he sold his DBRs - assertGt(DOLA.balanceOf(userPk), borrowAmount); - - assertEq(dbr.balanceOf(userPk), 0); - - assertEq(collateral.balanceOf(userPk), amountToWithdraw / 2); - } - - function test_max_leveragePosition() public { - // We are going to deposit some CRV, then fully leverage the position - - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - // Max Amount borrowable is the one available from collateral amount + - // the extra borrow amount from the max borrow amount swapped and re-deposited as collateral - uint maxBorrowAmount = getMaxLeverageBorrowAmount(crvTestAmount, 100); - - // recharge mocked proxy for swap, we need to swap DOLA to collateral - deal( - address(market.collateral()), - address(exchangeProxy), - convertDolaToCollat(maxBorrowAmount) - ); - - vm.startPrank(userPk, userPk); - // Initial CRV deposit - deposit(crvTestAmount); - - // We are going to leverage the max amount we can borrow - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData; // NO DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - collateral, - maxBorrowAmount - ); - - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - // Balance in escrow is equal to the collateral deposited + the extra collateral swapped from the leverage - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - crvTestAmount + convertDolaToCollat(maxBorrowAmount) - ); - assertEq(DOLA.balanceOf(userPk), 0); - } - - function test_max_deleveragePosition(uint crvTestAmount) public { - // We are going to deposit some CRV, then fully leverage the position - vm.assume(crvTestAmount < 40000 ether); - vm.assume(crvTestAmount > 0.00000001 ether); - - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - // Max Amount borrowable is the one available from collateral amount + - // the extra borrow amount from the max borrow amount swapped and re-deposited as collateral - uint maxBorrowAmount = getMaxBorrowAmount(crvTestAmount); - - // recharge mocked proxy for swap, we need to swap collateral to DOLA - vm.startPrank(gov); - DOLA.mint(address(exchangeProxy), convertCollatToDola(crvTestAmount)); - vm.stopPrank(); - - vm.startPrank(userPk, userPk); - // Initial CRV deposit - deposit(crvTestAmount); - market.borrow(maxBorrowAmount); - - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - crvTestAmount - ); - assertEq(DOLA.balanceOf(userPk), maxBorrowAmount); - - // We are going to deleverage and withdraw ALL collateral - uint256 amountToWithdraw = collateral.balanceOf( - address(market.predictEscrow(userPk)) - ); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData; // NO DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaOut.selector, - collateral, - amountToWithdraw - ); - - ale.deleveragePosition( - maxBorrowAmount, - address(market), - amountToWithdraw, - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - // No collateral in the escrow - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - 0 - ); - // All collateral is swapped to DOLA and sent to the user - assertEq(DOLA.balanceOf(userPk), convertCollatToDola(crvTestAmount)); - } - - function test_max_leverageAndDeleveragePosition( - uint256 crvTestAmount - ) public { - // We are going to deposit some CRV, then fully leverage the position - // and then fully deleverage it (withdrawing ALL the collateral) - vm.assume(crvTestAmount < 40000 ether); - vm.assume(crvTestAmount > 0.00000001 ether); - - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - // Max Amount borrowable is the one available from collateral amount + - // the extra borrow amount from the max borrow amount swapped and re-deposited as collateral - uint maxBorrowAmount = getMaxLeverageBorrowAmount(crvTestAmount, 100); - - // recharge proxy for swap, we need to swap DOLA to collateral - deal( - address(market.collateral()), - address(exchangeProxy), - convertDolaToCollat(maxBorrowAmount) - ); - // we also need to mint DOLA into the swap mock bc we will swap ALL the collateral, not only the one added from the leverage - vm.startPrank(gov); - DOLA.mint(address(exchangeProxy), convertCollatToDola(crvTestAmount)); - vm.stopPrank(); - - vm.startPrank(userPk, userPk); - // Initial CRV deposit - deposit(crvTestAmount); - - // We are going to leverage the max amount we can borrow - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData; // NO DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - collateral, - maxBorrowAmount - ); - - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - // We now deleverage and withdraw ALL the collateral (which will be swapped for DOLA) - uint256 amountToWithdraw = collateral.balanceOf( - address(market.predictEscrow(userPk)) - ); - - hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (v, r, s) = vm.sign(1, hash); - - permit = ALE.Permit(block.timestamp, v, r, s); - - swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaOut.selector, - collateral, - amountToWithdraw - ); - - ale.deleveragePosition( - maxBorrowAmount, - address(market), - amountToWithdraw, - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - // We have fully deleveraged the position (no collateral left in the escrow) - // extra DOLA swapped is sent to the user (after burning) - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - 0 - ); - assertEq( - DOLA.balanceOf(userPk), - convertCollatToDola(amountToWithdraw) - maxBorrowAmount - ); - } - - function test_deleveragePosition_if_collateral_no_debt() public { - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - // recharge mocked proxy for swap, we need to swap collateral to DOLA - vm.startPrank(gov); - DOLA.mint(address(exchangeProxy), convertCollatToDola(crvTestAmount)); - vm.stopPrank(); - - vm.startPrank(userPk, userPk); - deposit(crvTestAmount); - - uint256 amountToWithdraw = crvTestAmount; - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData; // NO DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaOut.selector, - collateral, - amountToWithdraw - ); - - // vm.expectRevert(repaymentGtThanDebt); - // WE can deleverage even if we have no debt, will be swapped to DOLA and sent to the user - ale.deleveragePosition( - 0, - address(market), - amountToWithdraw, - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - - assertEq( - collateral.balanceOf(address(market.predictEscrow(userPk))), - 0 - ); - assertEq(DOLA.balanceOf(userPk), convertCollatToDola(crvTestAmount)); - } - - function test_fail_leveragePosition_if_no_collateral() public { - // We are going to deposit some CRV, then leverage the position - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - uint maxBorrowAmount = getMaxBorrowAmount(crvTestAmount); - - // recharge mocked proxy for swap, we need to swap DOLA to collateral - deal( - address(market.collateral()), - address(exchangeProxy), - convertDolaToCollat(maxBorrowAmount) - ); - - vm.startPrank(userPk, userPk); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData; // NO DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - collateral, - maxBorrowAmount - ); - - vm.expectRevert(exceededLimit); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - } - - function test_fail_deleveragePosition_if_no_collateral() public { - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - // recharge mocked proxy for swap, we need to swap collateral to DOLA - vm.startPrank(gov); - DOLA.mint(address(exchangeProxy), convertCollatToDola(crvTestAmount)); - vm.stopPrank(); - - vm.startPrank(userPk, userPk); - - uint256 amountToWithdraw = crvTestAmount; - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData; // NO DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaOut.selector, - collateral, - amountToWithdraw - ); - - // Cannot make a repayment without debt - vm.expectRevert(repaymentGtThanDebt); - ale.deleveragePosition( - 1 ether, - address(market), - amountToWithdraw, - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - } - - function test_fail_max_leveragePosition_buyDBR() public { - // We are going to deposit some CRV, then fully leverage the position - - uint crvTestAmount = 1 ether; - address userPk = vm.addr(1); - deal(address(market.collateral()), userPk, crvTestAmount); - gibDBR(userPk, crvTestAmount); - - // Max Amount borrowable is the one available from collateral amount + - // all redeposited amount as collateral - uint maxBorrowAmount = getMaxLeverageBorrowAmount(crvTestAmount, 100); - - // recharge mocked proxy for swap, we need to swap DOLA to collateral - deal( - address(market.collateral()), - address(exchangeProxy), - convertDolaToCollat(maxBorrowAmount) - ); - - vm.startPrank(userPk, userPk); - // Initial CRV deposit - deposit(crvTestAmount); - - // Calculate the amount of DOLA needed to buy the DBR to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 365 days, 8); - - // We are going to leverage the max amount we can borrow + the amount needed to buy the DBR - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); - - ALE.DBRHelper memory dbrData = ALE.DBRHelper( - dolaForDBR, - (dbrAmount * 99) / 100, - 0 - ); // buy DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - collateral, - maxBorrowAmount - ); - - // Cannot MAX leverage a position and buying DBR at the same time - vm.expectRevert(exceededLimit); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - bytes(""), - dbrData - ); - } - - function test_fail_setMarket_NoMarket() public { - address fakeMarket = address(0x69); - - vm.expectRevert( - abi.encodeWithSelector(ALE.NoMarket.selector, fakeMarket) - ); - ale.setMarket(fakeMarket, address(0), address(0), true); - } - - function test_fail_setMarket_Wrong_BuySellToken_Without_Helper() public { - ale.updateMarketHelper(address(market), address(0)); - - address fakeBuySellToken = address(0x69); - - vm.expectRevert( - abi.encodeWithSelector( - ALE.MarketSetupFailed.selector, - address(market), - fakeBuySellToken, - address(collateral), - address(0) - ) - ); - ale.setMarket(address(market), fakeBuySellToken, address(0), true); - - vm.expectRevert(); - ale.setMarket(address(market), address(0), address(0), true); - } - - function test_fail_updateMarketHelper_NoMarket() public { - address wrongMarket = address(0x69); - address newHelper = address(0x70); - - vm.expectRevert( - abi.encodeWithSelector(ALE.MarketNotSet.selector, wrongMarket) - ); - ale.updateMarketHelper(wrongMarket, newHelper); - } -} diff --git a/test/util/aleTests/ALEPendlePTUSDe25Sep25.t.sol b/test/util/aleTests/ALEPendlePTUSDe25Sep25.t.sol deleted file mode 100644 index 1a86a349..00000000 --- a/test/util/aleTests/ALEPendlePTUSDe25Sep25.t.sol +++ /dev/null @@ -1,1034 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {PendlePTHelper, IPendleHelper} from "src/util/PendlePTHelper.sol"; -import "test/marketForkTests/PendlePTUSDe25Sep25MarketForkTest.t.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; - -/* - User 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf - ALE 0x4dF2EaA1658a220FDB415B9966a9ae7c3d16e240 - helper 0x4809fE7d314c2AE5b2Eb7fa19C1B166434D29141 - DOLA 0x865377367054516e17014CcdED1e7d814EDC9ce4 - pendlePT 0xBC6736d346a5eBC0dEbc997397912CD9b8FAe10a -*/ -contract ALEV2PTUSDe25Sep25Test is PendlePTUSDe25Sep25MarketForkTest { - ALEV2 ale; - address userPk = vm.addr(1); - PendlePTHelper helper; - address userPkEscrow; - - address pendleRouter = address(0x888888888889758F76e7103c6CbF23ABbF58F946); - address pendleYT = address(0x48bbbEdc4d2491cc08915D7a5c7cc8A8EdF165da); - address marketPT = address(0x6d98a2b6CDbF44939362a3E99793339Ba2016aF4); - - // Pendle Router Mock - address pendleYTHolder = - address(0x9b17f918b9df3E661eC68a4ACcdC1CBC621Af2f8); - - // Get initial PT - // Swap 100K DOLA for PT (recipient helper) - bytes pendleDataInitial = - hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af40000000000000000000000000000000000000000000014665ea16925e4a75c15000000000000000000000000000000000000000000000254c904d1a7e0d68045000000000000000000000000000000000000000000001ad7778f8a5f4435297a0000000000000000000000000000000000000000000004a99209a34fc1ad008b000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000fe0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d44e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000008200000000000000000000000000000000000000000000000000000000000000a600000000000000000000000000000000000000000000000000000000000000760000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000013df3fa56382d600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004076dcc3ef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000004444c5dc75cb358380d2e3de08a900000000000000000000000000000000000000000000000000000001738ccf5ba00000000000000000000000000000000000000000000000000000000000000800000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000000000000001738ccf5ba0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000003f0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000fffd8963efd1fc6a506488495d951d5263988d25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000016213e3e066d4b2000000000000151acbffded913000000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000140ca83313b49ed999980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002877b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239393738382e39363735363134393736222c22416d6f756e744f7574555344223a2239393734332e3931323835393830303236222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223939363634303131313938393131363636343534353237222c2254696d657374616d70223a313735353039363134312c22526f7574654944223a2230636139323630392d626565362d343663392d626633342d3239376137343838653335323a39363863313561612d323666312d343935612d613665332d313435323233353463653838222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2263516c31645830644d2f79366379445265526e7345394434727472703776496565563971786137743247527639664b69384b5139425a326b2b5669455a48664345506d566873367a627a686f4c4f6a5a4e6d546e4d52684737524139774938703050774f77594e66622b4471712b33734d354134412f5079455644576d79424f6749396d4253473153494c72582b626854317155686176726241577436666c746f6d536e397353784d37796b5a43546846497470592b4b4f7132344f4a544d32596f714d7964643670766f755a746231317842462f4e58446f6f5a72526f772f556d356e697a76446e747249735453756d6d77354e434a61796b5237323742424872744242716e33336168647075794541446e707534726765494e6653664759302f53383853462f47684d7a786f6c627a6961525a6d345a4536594947357235444f586f7659364f6c4e4d72314152655370454847413d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000004b513ccc178b5be0a02b30d1439af11548fe47788e90c600eb90529c75e3fc35396f8a7c4baa117a9f00000000000000000000000000000000000000000000000000000000689df56e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b300000000000000000000000048bbbedc4d2491cc08915d7a5c7cc8a8edf165da000000000000000000000000bb38a4a1c1883694700c3a2ce465f4ffffd47c98000000000000000000000000bb38a4a1c1883694700c3a2ce465f4ffffd47c98000000000000000000000000000000000000000000000fe168ac77c474e79def000000000000000000000000000000000000000000000000021c22425d7e4bba0000000000000000000000000000000000000000000000000c7d713b49da00000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000415d305398b54b596551055f61a8d130759891af7da2697160c6528760b4f2063b6d46a1ec220e2f4c1dd3f6fb3ef28111d70826271203ed57c25508a9649f18bb1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - // Swap maxBorrowAmount from 100k DOLA to PT deposit (recipient helper) - bytes pendleSwapData = - hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af400000000000000000000000000000000000000000000127dea1fcc1f017b0fc30000000000000000000000000000000000000000000009bb88b26b6ea276a9fb000000000000000000000000000000000000000000001854d5be0c949628a8f30000000000000000000000000000000000000000000013771164d6dd44ed53f6000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000fe0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001330895df47fbcb8f89b0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d44e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000008200000000000000000000000000000000000000000000000000000000000000a600000000000000000000000000000000000000000000000000000000000000760000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001330895df47fbcb8f89b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001202147c44369900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004076dcc3ef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000004444c5dc75cb358380d2e3de08a90000000000000000000000000000000000000000000000000000000150b30de7b00000000000000000000000000000000000000000000000000000000000000800000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000150b30de7b0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000003f0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000fffd8963efd1fc6a506488495d951d5263988d250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000140e4aebf68f1a10000000000001320716b702f11000000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001330895df47fbcb8f89b00000000000000000000000000000000000000000000122b9ef2ddc6502666650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001330895df47fbcb8f89b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002877b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238383234302e3536343730383730383834222c22416d6f756e744f7574555344223a2238393937352e37373935313232363236222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223930333233343331373334313738323039353935333931222c2254696d657374616d70223a313735353039363331332c22526f7574654944223a2235356433316364622d656633322d343564642d613366612d3032386131313165336534613a63623938663164622d306237342d343361312d613964362d393266383839613833646130222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22445350734679634857613575516958556d426f6e6d71743653644d4c68386d5150714471386859795a395344543575313547486e786a32706949636d4677547671637a4a41506b633639694b385279787a5946665436394b5142792f79414a377530464743784837456f59564a2f582f7a2f5642544b646a644f64565579636337757974596376794955737937426a546b6e7a6a583332634b587949745258526f4364564257744b574946414e57325046486d6e467266457034554a656b76574243646d58364251656d336347737a685242503345544b56696350535130446f64447379694c694d376c524162446f784f71333059676b34416552312f644b337164436f42464e4545627655446e354837503465703535596b49707577444d624a4e762b75354371716c7779654346324638584e4d736b7a584e502b6f706b5673694f5475306e7936536a626f4c77446265326973413d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - uint256 initialDolaAmount = 100000 ether; // 100K DOLA - - function setUp() public override { - super.setUp(); - - vm.startPrank(gov); - - ale = ALEV2(payable(aleV2Addr)); - - helper = PendlePTHelper( - pendlePTHelperAddr - ); - ale.setMarket(address(market), address(DOLA), address(helper), false); - helper.setMarket(address(market), address(pendlePT), pendleYT); - borrowController.allow(address(ale)); - DOLA.mint(address(market), 1000000 ether); - vm.stopPrank(); - - userPkEscrow = address(market.predictEscrow(userPk)); - - vm.prank(userPk); - DOLA.approve(address(helper), initialDolaAmount); - } - - function _mintInitialDolaAmountToUser() internal { - vm.prank(gov); - DOLA.mint(userPk, initialDolaAmount); - } - function test_leveragePosition_Mint_PT_and_YT_with_DOLA_router() public { - _mintInitialDolaAmountToUser(); - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Mint PT and YT with maxBorrowAmount (swap recipient is the helper, then distribute PT to ALE and YT to userPk) - bytes - memory pendleMintData = hex"d0f423850000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d2914100000000000000000000000048bbbedc4d2491cc08915d7a5c7cc8a8edf165da00000000000000000000000000000000000000000000122a9502fa251d8ccccc0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001330895df47fbcb8f89b0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e84e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000ba000000000000000000000000000000000000000000000000000000000000008a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000840000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001330895df47fbcb8f89b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012021581f795c400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004076dcc3ef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000004444c5dc75cb358380d2e3de08a90000000000000000000000000000000000000000000000000000000150abf858c00000000000000000000000000000000000000000000000000000000000000800000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000150abf858c0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000010000000000000000000000004440854b2d02c57a0dc5c58b7a884562d875c0c400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000001198cc16abb535c0f718000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000019401906a800000000000000000000000000000000000000e8a4ff67d40e73127d13ec16340000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000689caf6900000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000041e6dc089ef7be2566561716d9c299af5fba17248b7465271190dfd4a89f5adfa90a26be265550e291afae4e14c4d6f405a21983c46dbf6a015060f0a2ca8d99bd1c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000140d2563d167bf6000000000000131f597c65a055000000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001330895df47fbcb8f89b00000000000000000000000000000000000000000000122a9502fa251d8ccccb0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001330895df47fbcb8f89b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239303433372e3532323332303630303835222c22416d6f756e744f7574555344223a2239303437362e3533343834303939343733222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223930333033323630333831333134303338313034303633222c2254696d657374616d70223a313735353039383830352c22526f7574654944223a2233373339373237332d613134352d343462622d623233392d3936643237373736366331643a61666539636630622d633264662d343633352d626334322d346239353666393430313932222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224b413349594f34505767466c35346e4a4e766c5a2f3638374b6c4f705156713345747263724e724b62734666585065626b6c496469514d364d5363755257786650596f694c3776516d67523469734d302b7358715a69466862323845783077774f4f554550386874335542397573314e6c33485849755a6436707149446b4c2b4e52454272584c6c536a6f2f48697159336f4a48796d42766163736332506b43465071484e4f686b4e58794b6c2f7735512f2b646c6478653949715a5775514761694c2b384f7a4668465651785663495170746571726d583363344444554f66566e41705345636357475945543361714c53434d77524544423735742f532f41442f48793264734e4d685751744a2b434f635366666371745966646959566d625134544b483357694437737a474674706d794967534e5742323146714555466a35704c6a4342322f4d316c687a705475536f755777773d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleMintData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 0.9963 ether) / 1 ether), //0.9963 ratio minted PT and YT to DOLA - 1e14 // 0.01% Delta - ); - - assertApproxEqRel( - IERC20(pendleYT).balanceOf(userPk), - ((maxBorrowAmount * 0.9963 ether) / 1 ether), // 0.9963 ratio minted PT and YT to DOLA - 1e14 // 0.01% Delta - ); - } - - function test_leveragePosition_Swap_DOLA_for_PT_router() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.0135 ether) / 1 ether), // approx 1.0135 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_leveragePosition_buyDBR_Swap_DOLA_for_PT() public { - _mintInitialDolaAmountToUser(); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 15 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 90) / 100, - 0 - ); - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.0135 ether) / 1 ether), // approx 1.0135 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - assertGt(dbr.balanceOf(userPk), (dbrAmount * 90) / 100); - } - - function test_depositAndLeveragePosition_DOLA() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - // Swap DOLA for PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleInitialDepositData = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af400000000000000000000000000000000000000000000125c7954241ed01dd3860000000000000000000000000000000000000000000009a9ef03dd1db0e746e8000000000000000000000000000000000000000000001828d589a8ca3a423145000000000000000000000000000000000000000000001353de07ba3b61ce8dd1000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000e60000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bc4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000006a000000000000000000000000000000000000000000000000000000000000008e000000000000000000000000000000000000000000000000000000000000005e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000011e284035cd0580000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000404c134a970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000e0e0e08a6a4b9dc7bd67bcb7aade5cf48157d44400000000000000000000000000000000000000000000000000000014e5df31680000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000346dc5d638866000000640000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000013ea3d5d296dafd00000000000012fe0f6d6c965f000000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000120af50e5a5ba70ccccb0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393639372e3033343531323239323833222c22416d6f756e744f7574555344223a2238393634352e3433303539373331373234222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223839363839313831333530343531393635303637323633222c2254696d657374616d70223a313735353039393631392c22526f7574654944223a2238623362646531652d393965302d346238312d623764352d3035303966373861303963393a30326130616533362d363639642d343734332d393136382d316234653564373565376435222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224f39502f742f67486c52686d48366155392f5961384a315279314d366e42697041503278414a71426f4941556b646c777030565366754661637356572f6e456c566358304c4b6c47435755436145586b3363484e4861556c666e6e4559584c76445256534649497a314362397a36724c64684e31425452756768755945583073652f46684d714f36493878325637366330356339347544577844737171725a6e682f5976312f3751744f613763524a382b644c4761445a4f4450597439385072526e5678694c44562f614e326d567949324857726f373749794445313069596c3675304a617372767161716350305250684d39487472696b4a786952456b5430644f5531507846322f556856414d2b653256696736686e676679325853536b687a6a3957464b74434636316869726579523734757768513975794f31506646483764525868656c474b464474333936624e55616646513d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount - initialDolaDeposit); - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleInitialDepositData) - ); - assertEq(DOLA.balanceOf(address(helper)), 0); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - // Swap DOLA for PT (maxBorrowAmount + initialDolaDeposit) (receiver is the helper) - bytes - memory pendleSwapDataDeposit = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af40000000000000000000000000000000000000000000012ac6cbfd03b42a999830000000000000000000000000000000000000000000009d403577b11b74bca0f000000000000000000000000000000000000000000001892085ab3ac4a3d79260000000000000000000000000000000000000000000013a806aef6236e97941f000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000e60000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000136231e7e6db2aec13fd0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bc4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000006a000000000000000000000000000000000000000000000000000000000000008e000000000000000000000000000000000000000000000000000000000000005e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000136231e7e6db2aec13fd000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001230aaec996f4c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000404c134a970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000e0e0e08a6a4b9dc7bd67bcb7aade5cf48157d44400000000000000000000000000000000000000000000000000000015412e6af40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000346dc5d638866000000640000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000014413f5a6e3f0ed0000000000001351098f19cb9b000000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000136231e7e6db2aec13fd000000000000000000000000000000000000000000001259c914bee7d33fffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000136231e7e6db2aec13fd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002867b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239313138322e32373930313239303133222c22416d6f756e744f7574555344223a2239313139322e32393930313634353639222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223931323139383338323432313234303133323430333139222c2254696d657374616d70223a313735353039393639362c22526f7574654944223a2263633735356232332d616166632d343535342d383563352d6537393939393165373963613a32326331356330632d623730332d343638622d383837352d616533323537343636316566222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2249446d583773416379594f445461726c79742f777362504669746b4e4933436379737141486b6a436c414858495641382b6268532b6a4969454f5979794a4d43313834373031506863626d78793246533649734135586c36632b6e66376234325a4b334f44594f472b597376762b416b6c57324c366161516c35565475774e72363043587842682b6a525262496a2b64357935522b57426174724932722f722f72534e657931324c45586a416b524344686430426b72623177397159504d78416a6c3944786e79475730523071514471652b525337422f305770627558475848357253364d4e6156483954754c6e77626d434d375833386c52364c45306a636e4d5a486f64463261394e305053444d523334782f313342545378792f794c43696c3541737932493149637777497675455a2f504f6a6f696c632b6e3941474e4c4c4165397130355452437957546d3149424452676c513d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(userPk); - DOLA.approve(address(ale), initialDolaDeposit); - ale.depositAndLeveragePosition( - initialDolaDeposit, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapDataDeposit), - dbrData, - false - ); - - assertEq(DOLA.balanceOf(userPk), 0); - - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + - (((maxBorrowAmount + initialDolaDeposit) * 1.0135 ether) / - 1 ether), // approx 1.0135 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - } - - function test_depositAndLeveragePosition_PT() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - // Swap DOLA for PT (initialDolaDeposit) (receiver is the helper, recipient is the user which will be depositing them) - bytes - memory pendleDataInitialPT = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af400000000000000000000000000000000000000000000020a4f8eece8b0374d59000000000000000000000000000000000000000000000112e6811e5f852a94800000000000000000000000000000000000000000000002af4042cbeeccea7340000000000000000000000000000000000000000000000225cd023cbf0a552900000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001120000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e84e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000ba000000000000000000000000000000000000000000000000000000000000008a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000840000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000021e19e0c9bab2400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000001fcbde6c0711b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004076dcc3ef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000004444c5dc75cb358380d2e3de08a90000000000000000000000000000000000000000000000000000000025274e1cd00000000000000000000000000000000000000000000000000000000000000800000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000025274e1cd0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000010000000000000000000000004440854b2d02c57a0dc5c58b7a884562d875c0c400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000001198cc16abb535c0f718000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000002c9590ef600000000000000000000000000000000000000e8a441526685aee63bdd4a73000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000689cb77600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004159f888f5e29a6d2aecaffa28369a679f45617d995ea4f420f96faf51c5080177271137423ebe89da952a6333bf06f7a194a26d8c7098cf20c4dee15e1b28ae831b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000002367685398f1e6000000000000021c389fa59881200000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000021e19e0c9bab240000000000000000000000000000000000000000000000000020135cadd50e11199980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000021e19e0c9bab240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002857b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a22393937342e38333430383431393137222c22416d6f756e744f7574555344223a22393936302e383530303031303532343636222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a2239393635333231393631363634393938383031343037222c2254696d657374616d70223a313735353130303836362c22526f7574654944223a2238326439623835632d613730642d346631342d396438372d3464383562646230343463663a33613664366139322d306133372d343132612d613731612d633066376133643730383732222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224130682f767750567636594d4c537237364f6b54324b4e62332b6c4d7335396e72693036413165516774412f4f51616b6b4e5262394c4132643347376c6b6b462f332b6c7066682b73374c6752535a77524430643572394d596f7770324875784861653457762f697930534853317362694a55776e516c355566396134305a68326a2f6563782b452f412b344934306f4f5855636f582f686a4a75324f7766506c6a7777393770414f4756777a515642764c77334f78383938336341504632516d6e676c79585a5a5378727a716d32566774654867374f6a4e36697031444538766d6931794d316664516f42313153417a59413558706479726b447051776c6946426948527862454764726738304d4e465179744a536d4867446149496433584c4d4d48774c41516d615a656f2f4e7073756e2f737a544837515773416a526a5275776f696856335641624871487030364765324b513d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - uint256 initialLpAmount = helper.convertToCollateral( - initialDolaDeposit, - abi.encode(address(market), 0, pendleDataInitialPT) - ); - - // Swap DOLA to PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleDataInitialDeposit = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af400000000000000000000000000000000000000000000125be74affdb97615cdb0000000000000000000000000000000000000000000009a9a227793034b9fafa0000000000000000000000000000000000000000000018281562aef883d0f372000000000000000000000000000000000000000000001353444ef2606973f5f5000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001120000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e84e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000ba000000000000000000000000000000000000000000000000000000000000008a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000840000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000011e280b13eef5e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004076dcc3ef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000004444c5dc75cb358380d2e3de08a9000000000000000000000000000000000000000000000000000000014e5df9f1400000000000000000000000000000000000000000000000000000000000000800000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000014e5df9f140000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000010000000000000000000000004440854b2d02c57a0dc5c58b7a884562d875c0c400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000001198cc16abb535c0f71800000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000001913d9254b00000000000000000000000000000000000000e8a56db656546328db4d86583d0000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000689cb3f800000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000041c1ec34cc89de6d3254dcf4edb84ff6da4ad97d4aeb5089bf0927a22322ccb7031b46938b1ec3d0aa372698a1bb167ade8006c4727003dad9f9fcce7a44ca382d1c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000013ea0b74cd1efc700000000000012fddfd4efee61000000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000120ac7d7172275bfffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393838362e3738333331373333363939222c22416d6f756e744f7574555344223a2238393838332e3631343432323538353633222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223839363835373531373232323734373539363339303339222c2254696d657374616d70223a313735353039393937332c22526f7574654944223a2261353637306539352d346339662d343665312d623133382d3530666434333864316333303a37323163383533362d333166632d343637622d616336322d373736353637336364653965222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a225a5774737634626873574f6d3550684b4d454e466f5876706164446b7133476b6d674b4934796777426f724a56693158744357625939797854657462544e79763864506457462f664f72554f7578446e6462612f6844456e677a5464756f4e65686f774667395558744f3955624e717430584b6b633659487353357478796e3657577952756a7a31736f522b6b742b66512f364b765177366f426b444c6a68756b5371374f684d34634a2b555235514f3151665748664d4b773678765347594f2b56483377504f56475a41666e704c4a6b5361655841346b436a66636333546659444957744b427143374171452f4a4336456d676c70612b46437a42554f7177454267787a7531784138746c54442f6a597755496c73692f6f4e424e703575725241597843326977626c6e44764d687878584b6e79735a617a647a6d75694a37654a652b3733357635457768723772597671673668773d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleDataInitialDeposit) - ); - vm.stopPrank(); - - uint256 lpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Swap DOLA Max borrow amount to PT (receiver is the helper) - bytes - memory pendleDataLeverage = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af40000000000000000000000000000000000000000000010a0f21b3f5fb88f5a8d0000000000000000000000000000000000000000000008c07f6ca8176e9c4a9b0000000000000000000000000000000000000000000015e13e8fa43a9486ba84000000000000000000000000000000000000000000001180fed9502edd389537000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000fe0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001143224c1562c6d1b23f0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d44e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000008200000000000000000000000000000000000000000000000000000000000000a600000000000000000000000000000000000000000000000000000000000000760000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001143224c1562c6d1b23f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001033102954112100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004076dcc3ef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000004444c5dc75cb358380d2e3de08a9000000000000000000000000000000000000000000000000000000012edc31dbf00000000000000000000000000000000000000000000000000000000000000800000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000012edc31dbf0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000003f0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000fffd8963efd1fc6a506488495d951d5263988d2500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000001209aa55f015e9e0000000000001133bfb5dceaa6000000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001143224c1562c6d1b23f0000000000000000000000000000000000000000000010578fb991deea7fffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001143224c1562c6d1b23f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238313239372e3530333933333532373936222c22416d6f756e744f7574555344223a2238313332302e3832323838343436383833222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223831323334383238333436383735353530313035353939222c2254696d657374616d70223a313735353130303035342c22526f7574654944223a2266306439393161352d353637342d346136612d616237652d6161323864313964386164663a36393538356339622d386631642d346434382d626366632d383339383431383133616134222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224274534844346b69706a5a576f6251424376482f76684e5a6e32656d59414e702f61547a386863744e6e78315357565747792b796e673652726a4841374e4733764846736478417a2b305857646d4d3662482b4a6a34464471446145636d67754b642f795177356f456d54572f5858346a4b3359534669527536685a554f5634504f582f707a4e556e6e5a2b597177322b52456c6b307a584b4f4676625a366f38686d427372634236436667494142386a49694e7250384c466866397356367a4d3045706f45577a4c70564c717534314b36597468517444655530495958787668366d7078726843574a584d474864353950416a416357453131694165454e4b44635332716c376179686256526775575a4c66704b3767467670724978517a32714744474b3365785944696e7432456c555132413070547955705347497147664c533242446e774c3970506579372f2f54346a4664673d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(userPk); - IERC20(pendlePT).approve(address(ale), initialLpAmount); - ale.depositAndLeveragePosition( - initialLpAmount, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleDataLeverage), - dbrData, - true - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - lpAmount + - initialLpAmount + - ((maxBorrowAmount * 1.0135 ether) / 1 ether), - 1e14 // 0.01% Delta - ); - } - - function test_deleveragePosition_Redeem_with_PT_and_YT_router() public { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e24000000000000000000000000048bbbedc4d2491cc08915d7a5c7cc8a8edf165da000000000000000000000000000000000000000000002896eba4e14d0d2919a40000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000026a6db813891d65a5a5a0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000bc4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000006a000000000000000000000000000000000000000000000000000000000000008e000000000000000000000000000000000000000000000000000000000000005e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000404c134a970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000e0e0e08a6a4b9dc7bd67bcb7aade5cf48157d444000000000000000000000000000000000000000000002966bd282d7cac24cd5a0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000346dc5d63886600000064000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002d8d4f4add000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000026fbc0f2627668000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b84057400b606b000000000000297ff3c354fd980000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000002966bd282d7cac24cd5a00000000000000000000000000000000000000000000276cc12cc3f0e9ffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000002966bd282d7cac24cd5a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028a7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139353936392e3531303533393636383837222c22416d6f756e744f7574555344223a223139373236352e32333937393136323639222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313935393737333237323731343336333935343739303339222c2254696d657374616d70223a313735353039393138322c22526f7574654944223a2232343534373562332d636530632d343138612d383738352d6536313563323361656261613a64316664326431302d323039332d346561322d626236642d626463616335386539383932222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2263334e7334596f35655063517176747a7855366134465874426732666953532f47775146574e7346483139592b664d504c696371745639723263523745696f4f617067324e4c7a32627457416a4f496a30505546496a796d6c5364304b715243552b33387147453241504c737774714a4a6f784e392f6579762b6c77425975534b584d6d73324e6d726754775a6b525a383073467549714b316850525558634c2b714c457a376541526854436336616e6353662f513055577a6f38792f3561656875723570392b6c6c704f4e51775855625568754162774f63376b664977344b354455503249515a754343396b347a41782b54375a77426539704e79466d31616f43766876537444394762343763754256766f676a624667324946746276662b77354f305a6159777744717361555661386e78712f38734b35524f33434b66785065396f685a58747a6a4352686a50726d4f446268413d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - - assertApproxEqRel( - DOLA.balanceOf(userPk), - 192134634579839603410824 - debt, - 0.01 ether - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - assertEq(market.debts(userPk), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_AFTER_DEADLINE_deleveragePosition_Redeem_with_PT_and_YT_router() - public - { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e24000000000000000000000000048bbbedc4d2491cc08915d7a5c7cc8a8edf165da000000000000000000000000000000000000000000002896eba4e14d0d2919a40000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000026a6db813891d65a5a5a0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000bc4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000006a000000000000000000000000000000000000000000000000000000000000008e000000000000000000000000000000000000000000000000000000000000005e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000404c134a970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000e0e0e08a6a4b9dc7bd67bcb7aade5cf48157d444000000000000000000000000000000000000000000002966bd282d7cac24cd5a0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000346dc5d63886600000064000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002d8d4f4add000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000026fbc0f2627668000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b84057400b606b000000000000297ff3c354fd980000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000002966bd282d7cac24cd5a00000000000000000000000000000000000000000000276cc12cc3f0e9ffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000002966bd282d7cac24cd5a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028a7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139353936392e3531303533393636383837222c22416d6f756e744f7574555344223a223139373236352e32333937393136323639222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313935393737333237323731343336333935343739303339222c2254696d657374616d70223a313735353039393138322c22526f7574654944223a2232343534373562332d636530632d343138612d383738352d6536313563323361656261613a64316664326431302d323039332d346561322d626236642d626463616335386539383932222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2263334e7334596f35655063517176747a7855366134465874426732666953532f47775146574e7346483139592b664d504c696371745639723263523745696f4f617067324e4c7a32627457416a4f496a30505546496a796d6c5364304b715243552b33387147453241504c737774714a4a6f784e392f6579762b6c77425975534b584d6d73324e6d726754775a6b525a383073467549714b316850525558634c2b714c457a376541526854436336616e6353662f513055577a6f38792f3561656875723570392b6c6c704f4e51775855625568754162774f63376b664977344b354455503249515a754343396b347a41782b54375a77426539704e79466d31616f43766876537444394762343763754256766f676a624667324946746276662b77354f305a6159777744717361555661386e78712f38734b35524f33434b66785065396f685a58747a6a4352686a50726d4f446268413d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - uint256 ytBalBefore = IERC20(pendleYT).balanceOf(userPk); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), ytBalBefore); - } - - function test_deleveragePosition_Swap_PT_to_DOLA_router() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e2400000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af40000000000000000000000000000000000000000000028ec19effad1719650eb00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000d80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000002642029ca4a9e9b9b9b90000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000b84e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000066000000000000000000000000000000000000000000000000000000000000008a000000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000862fc0a67623a4e6f0776103340836c91728b06d0000000000000000000000000000000000000000000028fbbf5e5abc233d279a0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b300000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f0000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000004628f13651ead6793f8d838b34b8f8522fb0cc5200000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000290600c1364218000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000290353fe1904d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b127bc6870a5ef0000000000002913ac92a0a0e00000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000028fbbf5e5abc233d279a000000000000000000000000000000000000000000002705e3f1b2326e6666650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000028fbbf5e5abc233d279a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028a7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139333936372e3635313138313031393434222c22416d6f756e744f7574555344223a223139363738332e34333039313432303531222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313933393739393439313133313839383731393130393131222c2254696d657374616d70223a313735353039363733372c22526f7574654944223a2232316166653731382d633965322d343261662d626438632d6630636538393233343130373a61393630333332652d613630632d343463342d383739642d663363366438386163336239222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22474d4d547779384e674733614d3771355543356846444e542b6a30394837362f352b544f625a6e534c66794c71747545427772594c395447466e2f5035422f7653536668526152662f7a624f6158575831544343554735552f2b4455776a4b4e714d595369636233706a4551473230644448744b36795139784548757a31507773394a6b384f4f526b55744a686c6a38754b724232303779377459746f4f615764546e716c534862714f324e33472f5459583144436d5870625a386c76326a4d67524c4a623335386b51676b757a4e304173444c4c65527557683263556277654a61794c6e4c36584563384f773967654870392b615742593933496a7869626a4454335a6973305a5852736b6f764b5336425452566a6d2f596838306a79774a63765776694334345963556163464f3670792f6b424176467432383735713061444c36436b49356578306c746c2b75553733726371513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - uint256 debt = market.debts(address(userPk)); - vm.startPrank(userPk); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - - // Dola balance is equal to collateral sold for DOLA minus debt - assertEq(DOLA.balanceOf(userPk), 190273106254383237796973- debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_AFTER_DEADLINE_deleveragePosition_Swap_PT_to_DOLA_router() - public - { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e2400000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af40000000000000000000000000000000000000000000028ec19effad1719650eb00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000d80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000002642029ca4a9e9b9b9b90000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000b84e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000066000000000000000000000000000000000000000000000000000000000000008a000000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000862fc0a67623a4e6f0776103340836c91728b06d0000000000000000000000000000000000000000000028fbbf5e5abc233d279a0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b300000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f0000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000004628f13651ead6793f8d838b34b8f8522fb0cc5200000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000290600c1364218000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000290353fe1904d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b127bc6870a5ef0000000000002913ac92a0a0e00000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000028fbbf5e5abc233d279a000000000000000000000000000000000000000000002705e3f1b2326e6666650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000028fbbf5e5abc233d279a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028a7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139333936372e3635313138313031393434222c22416d6f756e744f7574555344223a223139363738332e34333039313432303531222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313933393739393439313133313839383731393130393131222c2254696d657374616d70223a313735353039363733372c22526f7574654944223a2232316166653731382d633965322d343261662d626438632d6630636538393233343130373a61393630333332652d613630632d343463342d383739642d663363366438386163336239222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22474d4d547779384e674733614d3771355543356846444e542b6a30394837362f352b544f625a6e534c66794c71747545427772594c395447466e2f5035422f7653536668526152662f7a624f6158575831544343554735552f2b4455776a4b4e714d595369636233706a4551473230644448744b36795139784548757a31507773394a6b384f4f526b55744a686c6a38754b724232303779377459746f4f615764546e716c534862714f324e33472f5459583144436d5870625a386c76326a4d67524c4a623335386b51676b757a4e304173444c4c65527557683263556277654a61794c6e4c36584563384f773967654870392b615742593933496a7869626a4454335a6973305a5852736b6f764b5336425452566a6d2f596838306a79774a63765776694334345963556163464f3670792f6b424176467432383735713061444c36436b49356578306c746c2b75553733726371513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - - vm.startPrank(userPk); - vm.expectRevert(PendlePTHelper.PendleSwapFailed.selector); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - } - - function test_deleveragePosition_sellDBR() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - uint256 debt = market.debts(address(userPk)); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 10000, - 0 - ); // sell all DBR - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e2400000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af40000000000000000000000000000000000000000000028ec19effad1719650eb00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000d80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000002642029ca4a9e9b9b9b90000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000b84e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000066000000000000000000000000000000000000000000000000000000000000008a000000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000862fc0a67623a4e6f0776103340836c91728b06d0000000000000000000000000000000000000000000028fbbf5e5abc233d279a0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b300000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f0000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000004628f13651ead6793f8d838b34b8f8522fb0cc5200000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000290600c1364218000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000290353fe1904d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b127bc6870a5ef0000000000002913ac92a0a0e00000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000028fbbf5e5abc233d279a000000000000000000000000000000000000000000002705e3f1b2326e6666650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000028fbbf5e5abc233d279a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028a7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139333936372e3635313138313031393434222c22416d6f756e744f7574555344223a223139363738332e34333039313432303531222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313933393739393439313133313839383731393130393131222c2254696d657374616d70223a313735353039363733372c22526f7574654944223a2232316166653731382d633965322d343261662d626438632d6630636538393233343130373a61393630333332652d613630632d343463342d383739642d663363366438386163336239222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22474d4d547779384e674733614d3771355543356846444e542b6a30394837362f352b544f625a6e534c66794c71747545427772594c395447466e2f5035422f7653536668526152662f7a624f6158575831544343554735552f2b4455776a4b4e714d595369636233706a4551473230644448744b36795139784548757a31507773394a6b384f4f526b55744a686c6a38754b724232303779377459746f4f615764546e716c534862714f324e33472f5459583144436d5870625a386c76326a4d67524c4a623335386b51676b757a4e304173444c4c65527557683263556277654a61794c6e4c36584563384f773967654870392b615742593933496a7869626a4454335a6973305a5852736b6f764b5336425452566a6d2f596838306a79774a63765776694334345963556163464f3670792f6b424176467432383735713061444c36436b49356578306c746c2b75553733726371513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - dbr.approve(address(ale), dbr.balanceOf(userPk)); - ale.deleveragePosition( - debt, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - - assertEq(dbr.balanceOf(userPk), 0); - // Dola balance is greater than collateral sold for DOLA minus debt because we also sold DBR - assertGt(DOLA.balanceOf(userPk), 190176420699205756775404 - debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_InsufficientPT_after_swap() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode( - address(market), - (initialDolaAmount * 1.1 ether) / 1 ether, - pendleDataInitial - ) - ); - } - - function test_fail_InsufficientPT_after_mint() public { - _mintInitialDolaAmountToUser(); - bytes - memory pendleMintData = hex"d0f423850000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d2914100000000000000000000000048bbbedc4d2491cc08915d7a5c7cc8a8edf165da00000000000000000000000000000000000000000000122a9502fa251d8ccccc0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001330895df47fbcb8f89b0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e84e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000ba000000000000000000000000000000000000000000000000000000000000008a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000840000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001330895df47fbcb8f89b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012021581f795c400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004076dcc3ef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000004444c5dc75cb358380d2e3de08a90000000000000000000000000000000000000000000000000000000150abf858c00000000000000000000000000000000000000000000000000000000000000800000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000150abf858c0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000010000000000000000000000004440854b2d02c57a0dc5c58b7a884562d875c0c400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000001198cc16abb535c0f718000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000019401906a800000000000000000000000000000000000000e8a4ff67d40e73127d13ec16340000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000689caf6900000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000041e6dc089ef7be2566561716d9c299af5fba17248b7465271190dfd4a89f5adfa90a26be265550e291afae4e14c4d6f405a21983c46dbf6a015060f0a2ca8d99bd1c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000140d2563d167bf6000000000000131f597c65a055000000000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001330895df47fbcb8f89b00000000000000000000000000000000000000000000122a9502fa251d8ccccb0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001330895df47fbcb8f89b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239303433372e3532323332303630303835222c22416d6f756e744f7574555344223a2239303437362e3533343834303939343733222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223930333033323630333831333134303338313034303633222c2254696d657374616d70223a313735353039383830352c22526f7574654944223a2233373339373237332d613134352d343462622d623233392d3936643237373736366331643a61666539636630622d633264662d343633352d626334322d346239353666393430313932222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224b413349594f34505767466c35346e4a4e766c5a2f3638374b6c4f705156713345747263724e724b62734666585065626b6c496469514d364d5363755257786650596f694c3776516d67523469734d302b7358715a69466862323845783077774f4f554550386874335542397573314e6c33485849755a6436707149446b4c2b4e52454272584c6c536a6f2f48697159336f4a48796d42766163736332506b43465071484e4f686b4e58794b6c2f7735512f2b646c6478653949715a5775514761694c2b384f7a4668465651785663495170746571726d583363344444554f66566e41705345636357475945543361714c53434d77524544423735742f532f41442f48793264734e4d685751744a2b434f635366666371745966646959566d625134544b483357694437737a474674706d794967534e5742323146714555466a35704c6a4342322f4d316c687a705475536f755777773d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateral( - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleMintData) - ); - } - - function test_withdrawAndConvertFromCollateral_SWAP() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf0000000000000000000000006d98a2b6cdbf44939362a3e99793339ba2016af4000000000000000000000000000000000000000000001578bcba763409622d1100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000f40000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000014152a5c864491fd7d7d0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000d44e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000008200000000000000000000000000000000000000000000000000000000000000a600000000000000000000000000000000000000000000000000000000000000760000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000000000000000004076dcc3ef0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000004444c5dc75cb358380d2e3de08a90000000000000000000000000000000000000000000001582f62445aeea67de3c00000000000000000000000000000000000000000000000000000000000000800000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000001582f62445aeea67de3c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000003f0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000001000276a400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000017aab7c9f6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001441169d63d83600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000169c208c3e87410000000000000158ffcfa3f51b50000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001582f62445aeea67de3c00000000000000000000000000000000000000000000147bfd20ef5a6bf333320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001582f62445aeea67de3c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028a7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130313536362e3133303638383837353037222c22416d6f756e744f7574555344223a223130323232302e33393338333435323232222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313031383235383039343934383634393132343439353335222c2254696d657374616d70223a313735353039373939342c22526f7574654944223a2261653464333634632d613762662d343161352d383265312d3930643064303962613838333a66636530333932302d373761612d346538642d393964622d386565366332396366326561222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22524c7a56552f566f4d792b394e4b2b414b416f736856584e30446d68422f55765439764254473235774172463274716e724c67644b5237693877616b6c734e474470586f4e2f65552f612b4f49535a5a446f39374e6b4f676b42525253376d504d6a6f30526a4f664533596f756435726d41796b63416d536e31316d695257536250483534787751624f326669697838773249463348616e345070366d315a46394f6264552f627772504b6d636c417931732b55724152757a5645414c306b51426c756c55343247763038572b6a376b43414d4f55375042564930364f54636177494144326a50756746716e4658374b56446443496964673178636c755075436a65794971746765544b50796f4b6a7244445566656f51464463746e674a76537766594b3861415a6555554e434d426a502f354c4f77506379354f333250527346746c4f637631795a467a77784d6c67622b734d68413d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - assertEq(DOLA.balanceOf(userPk), 0); - - vm.startPrank(userPk); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode(address(market), 99000 ether, pendleSwapToDolaData) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - } - - function test_withdrawAndConvertFromCollateral_REDEEM() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf00000000000000000000000048bbbedc4d2491cc08915d7a5c7cc8a8edf165da000000000000000000000000000000000000000000001578bcba763409622d110000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000147299906ca62bc646460000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000bc4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000006a000000000000000000000000000000000000000000000000000000000000008e000000000000000000000000000000000000000000000000000000000000005e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000404c134a970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000e0e0e08a6a4b9dc7bd67bcb7aade5cf48157d4440000000000000000000000000000000000000000000015e6ac05def7a32bd1cf0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000346dc5d63886600000064000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001818d3d14a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000149f5013d3059d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000170551bb1f8608800000000000015f44ea5d85c2f0000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000015e6ac05def7a32bd1cf0000000000000000000000000000000000000000000014db4ab727245fd999980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000015e6ac05def7a32bd1cf000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130333537352e3033353837393837303332222c22416d6f756e744f7574555344223a223130343335362e3434373437393435373834222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313033363736333638383637383434323332383339313637222c2254696d657374616d70223a313735353039393435322c22526f7574654944223a2263303463643639312d373561662d343237302d623965662d6664306263303163346464643a33336135633565642d386335642d343537652d623438642d613132373665363862333134222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22565139673869384c3835425579704c6857624b446552796763343456627050626b75792f564f59426f467853486e61464549752f5442467339324f4b2b3430553856704853376b334a423730554b4a46397659425075687230715049714e696c7759426b6b74766a58494b506e77654457793763303632716b4570435a534455515262634677425a44697a5141596a624a6643513352625356344a73486f6c394e7a6f35326a53666e6c3568456c6661654339635a73704b6f58767636434c53343659614c674c787654766a413068546374794b36434f5648596844655263706f2b4a373359365a507a4676556a49474142577470417069565347594c427356507647586430516765462b4e50427152644e4558533452634a4666396142694761534f70365147684e4d317734724568434e47676548634846736848737357526d6556374a7a2b346266757574486a4f78656a6949773d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - } - - function test_withdrawAndConvertFromCollateral_REDEEM_AFTER_DEADLINE() - public - { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 1000000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf00000000000000000000000048bbbedc4d2491cc08915d7a5c7cc8a8edf165da000000000000000000000000000000000000000000001578bcba763409622d110000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000147299906ca62bc646460000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000d4f480965d2347d421f1bec7f545682e5ec2151d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000bc4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000006a000000000000000000000000000000000000000000000000000000000000008e000000000000000000000000000000000000000000000000000000000000005e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000404c134a970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000e0e0e08a6a4b9dc7bd67bcb7aade5cf48157d4440000000000000000000000000000000000000000000015e6ac05def7a32bd1cf0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000346dc5d63886600000064000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000836fe54625be242bcfa286207795405ca4fd10000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001818d3d14a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000149f5013d3059d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000170551bb1f8608800000000000015f44ea5d85c2f0000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000015e6ac05def7a32bd1cf0000000000000000000000000000000000000000000014db4ab727245fd999980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000015e6ac05def7a32bd1cf000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130333537352e3033353837393837303332222c22416d6f756e744f7574555344223a223130343335362e3434373437393435373834222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313033363736333638383637383434323332383339313637222c2254696d657374616d70223a313735353039393435322c22526f7574654944223a2263303463643639312d373561662d343237302d623965662d6664306263303163346464643a33336135633565642d386335642d343537652d623438642d613132373665363862333134222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22565139673869384c3835425579704c6857624b446552796763343456627050626b75792f564f59426f467853486e61464549752f5442467339324f4b2b3430553856704853376b334a423730554b4a46397659425075687230715049714e696c7759426b6b74766a58494b506e77654457793763303632716b4570435a534455515262634677425a44697a5141596a624a6643513352625356344a73486f6c394e7a6f35326a53666e6c3568456c6661654339635a73704b6f58767636434c53343659614c674c787654766a413068546374794b36434f5648596844655263706f2b4a373359365a507a4676556a49474142577470417069565347594c427356507647586430516765462b4e50427152644e4558533452634a4666396142694761534f70365147684e4d317734724568434e47676548634846736848737357526d6556374a7a2b346266757574486a4f78656a6949773d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertGt(DOLA.balanceOf(userPk), (initialDolaAmount * 98) / 100); - // Didn't transfer token even if approval granted because after maturity - assertEq(IERC20(pendleYT).balanceOf(userPk), amountToWithdraw); - } - function test_Access_Control_convertToCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertToCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_Access_Control_convertFromCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertFromCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_updateMarketHelper_if_helper_not_address_zero() public { - vm.prank(gov); - ale.updateMarketHelper(address(market), address(1)); - (, , IPendleHelper helper, ) = ale.markets(address(market)); - assertEq(address(helper), address(1)); - } - - function test_updateMarketHelper_if_market_not_set() public { - vm.expectRevert( - abi.encodeWithSelector(ALEV2.MarketNotSet.selector, address(2)) - ); - vm.prank(gov); - ale.updateMarketHelper(address(2), address(1)); - } - - function test_updateMarketHelper_fails_if_helper_not_set() public { - vm.expectRevert(ALEV2.InvalidHelperAddress.selector); - vm.prank(gov); - ale.updateMarketHelper(address(market), address(0)); - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (amountCollat * - oracle.viewPrice(address(pendlePT), 0) * - market.collateralFactorBps()) / - 10_000 / - 1e18; - } -} diff --git a/test/util/aleTests/ALEPendlePTsUSDe.t.sol b/test/util/aleTests/ALEPendlePTsUSDe.t.sol deleted file mode 100644 index 070cf742..00000000 --- a/test/util/aleTests/ALEPendlePTsUSDe.t.sol +++ /dev/null @@ -1,1043 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {PendlePTHelper, IPendleHelper} from "src/util/PendlePTHelper.sol"; -import "test/marketForkTests/PendlePTsUSDe27Mar25MarketForkTest.t.sol"; -import {console} from "forge-std/console.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; - -/* - User 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf - ALE 0x9123ef9b7dB2e3D968B01C6FF99839aCb242dA13 - helper 0xcc39FC2eD370a4373FF4B8eBd507cd26906A80E8 - DOLA 0x865377367054516e17014CcdED1e7d814EDC9ce4 - address pendlePT = 0xE00bd3Df25fb187d6ABBB620b3dfd19839947b81 -*/ -contract ALEV2PTsUSDeTest is PendlePTsUSDe27Mar25MarketForkTest { - ALEV2 ale; - address userPk = vm.addr(1); - PendlePTHelper helper; - address userPkEscrow; - - address pendleRouter = address(0x888888888889758F76e7103c6CbF23ABbF58F946); - address pendleYT = address(0x96512230bF0Fa4E20Cf02C3e8A7d983132cd2b9F); - address marketPT = address(0xcDd26Eb5EB2Ce0f203a84553853667aE69Ca29Ce); - - // Pendle Router Mock - address pendleYTHolder = - address(0x9844c3688dAaA98De18fBe52499A6B152236896b); - - // Get initial PT - // Swap 100K DOLA for PT (recipient helper) - bytes pendleDataInitial = - hex"c81f847a000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e8000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce0000000000000000000000000000000000000000000015bbb7d43d50adbe774d000000000000000000000000000000000000000000000af9f5689b16a553440300000000000000000000000000000000000000000000170ce9c2127c5b2edb9f0000000000000000000000000000000000000000000015f3ead1362d4aa68806000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000380000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cf5540fffcdc3d510b18bfca6d2b9987b07725590000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f083bd37f90001865377367054516e17014ccded1e7d814edc9ce400019d39a5de30e57443bff2a8307a4256c8797a34970a152d02c7e14af68000000a1258ca4b9af3b300000000c49b0001b28ca7e465c452ce4252598e0bc96aeba553cf820001744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0001888888888889758f76e7103c6cbf23abbf58f94635d39ebf03010203000a01010001020001ff00000000000000000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - // Swap maxBorrowAmount from 100k DOLA to PT deposit (recipient helper) - bytes pendleSwapData = - hex"c81f847a000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e8000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce00000000000000000000000000000000000000000000139a329b323cc73797320000000000000000000000000000000000000000000009e6717a576edb9070900000000000000000000000000000000000000000000014ca54b41e0266af52c90000000000000000000000000000000000000000000013cce2f4aeddb720e121000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b20000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000131953f892f952ddaea90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000131953f892f952ddaea9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000001159fb5d7cb8462000000000000108c343c74052cc41998000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000131953f892f952ddaea9000000000000000000000000000000000000000000001061d78dc4c7aee0dbe5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000131953f892f952ddaea900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002317b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393334352e3131323431363431323732222c22416d6f756e744f7574555344223a2238393232312e32323435333932373334222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223738313434313731393037313837383130323435303136222c2254696d657374616d70223a313733363236303632312c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224d39474c677835524a3863396c7a6d4e44585571304159354a7157416769364234696269346c306f4f514f317a586248777952743945533748313250514b71734837566b66484f493850756e63647a6c535651426a654e745a2b752f62754e352f4775303038497a334141686b543950312b342f6b2b394678542f534d503350614f3642444c344678664b6273326c5171336c58664473716a7a4f326a426475784277415557624c5734756c777a4e654352587a304c494865676c6c50756a5359536b554e365745724e3056425043346c78565754492b3856574d70507971446e4e6a4666513043414c61504c6562437a413250576276756d5358465062627a734f534d78624f42756a7038756f6d7a594877585279574453472f3867466c6a7047524c756e5671736e474a34647861344f372b44544a3245437134457a57736a613837417a354576563868396779487a6976466b513d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - address aleAddress = 0x5233f4C2515ae21B540c438862Abb5603506dEBC; - address userPkAddr = 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf; - address helperAddr = 0x2e234DAe75C793f67A35089C9d99245E1C58470b; - uint256 initialDolaAmount = 100000 ether; // 100K DOLA - - function setUp() public override { - super.setUp(); - - vm.startPrank(gov); - - ale = new ALEV2(triDBRAddr); - - helper = new PendlePTHelper( - gov, - pauseGuardian, - address(DOLA), - address(pendleRouter), - address(ale) - ); - ale.setMarket(address(market), address(DOLA), address(helper), false); - helper.setMarket(address(market), address(pendlePT), pendleYT); - borrowController.allow(address(ale)); - DOLA.mint(address(market), 1000000 ether); - vm.stopPrank(); - - userPkEscrow = address(market.predictEscrow(userPk)); - - vm.prank(userPk); - DOLA.approve(address(helper), initialDolaAmount); - } - - function _mintInitialDolaAmountToUser() internal { - vm.prank(gov); - DOLA.mint(userPk, initialDolaAmount); - } - function test_leveragePosition_Mint_PT_and_YT_with_DOLA_router() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Mint PT and YT with maxBorrowAmount (swap recipient is the helper, then distribute PT to ALE and YT to userPk) - bytes - memory pendleMintData = hex"d0f42385000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e800000000000000000000000096512230bf0fa4e20cf02c3e8a7d983132cd2b9f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000131953f892f952ddaea90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000131953f892f952ddaea900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000115a0ce892f03fa000000000000108c44f77cc1a67bcd16000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000131953f892f952ddaea9000000000000000000000000000000000000000000000d3d03f9309aeb963dab000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000131953f892f952ddaea900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002327b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393333342e3633343132373134353838222c22416d6f756e744f7574555344223a2238393536302e3431363532383733343938222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223738313435333737343734313138363530393430363934222c2254696d657374616d70223a313733363236313338322c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22464f70686550495230465553777469393367654f72525a74427a616f674f3649684368615a476f44654c4f6f6d6d536c774a65307342627555717a63612f78634e2f4b784374697a4a79386949316777325a2b34444469384c52746e71744e476a495230635468546f4539495469594d37336b5843446f38683566654d6147695656686c2f4156356e772f2f354363595046577552765668797538524d65587279413778774e34774949636376567757616555416245434d7941496d48457874577362747849437061486c35562f39624a616c326552486b414b5173654547744c4e764d6842576e7a654a3047656958426e76352b664974574d4f6750764e6d56464d4779665276343054714735544252714c43794a674d61463654537173566d6d5646674165567a43426c634c4b6e43476e56706831784d326c736464574c78536342495a39616764696a5745536e6654386235513d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleMintData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1 ether) / 1.0081 ether), // 1.0081 ratio DOLA/mint PT and YT - 1e14 // 0.01% Delta - ); - assertApproxEqRel( - IERC20(pendleYT).balanceOf(userPk), - ((maxBorrowAmount * 1 ether) / 1.0081 ether), // 1.0081 ratio DOLA/mint PT and YT - 1e14 // 0.01% Delta - ); - } - - function test_leveragePosition_Swap_DOLA_for_PT_router() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.0365 ether) / 1 ether), // approx 1.0365 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_leveragePosition_buyDBR_Swap_DOLA_for_PT() public { - _mintInitialDolaAmountToUser(); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 15 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 95) / 100, - 0 - ); - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.0365 ether) / 1 ether), // approx 1.0365 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - assertGt(dbr.balanceOf(userPk), (dbrAmount * 95) / 100); - } - - function test_depositAndLeveragePosition_DOLA() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - // Swap DOLA for PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleInitialDepositData = hex"c81f847a000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e8000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce00000000000000000000000000000000000000000000138eda3d96845d63d2380000000000000000000000000000000000000000000009e0b6a065e09699d95d0000000000000000000000000000000000000000000014be4c50d5f13c4315430000000000000000000000000000000000000000000013c16d40cbc12d33b2ba000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b20000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000001150943d0f8806600000000000010833c9f0b63d53eaf22000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000001058f6e52250ea27046b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002327b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393433332e3230373638373333383937222c22416d6f756e744f7574555344223a2238383835362e3232373332303239303339222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223737393738373535343232323537323534363734323130222c2254696d657374616d70223a313733363236313937372c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224c4e79374f644e562b44456532484d376c7666596569413364384136326632363674506e55444263794e3549457542454941576a66326358533866573547704c767054336a6a74327552785039426e787939374b326a416c724b5438775338492b6f446f2f67347a4c58776441412b7355336d38684c383066475138726a303766324473565244627554363949575473626570546b4c736d696f666f792f513253415156762f6e685570347856364e62684d2b616e7731537a68326654492f737769344e527953303858636772444f50452b48713145776e757442673333743663716e3431446c7572684438597a7579754e7265433776314e7252395835377a4e6d76646c312b5450765a534c57616475523958426e78736a6e654a754f426f2b64344979646b4d4444317a75324c694b5278584158764836316a6d326d7353426a32786f432b72506c6859793831384364347156413d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount - initialDolaDeposit); - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleInitialDepositData) - ); - assertEq(DOLA.balanceOf(address(helper)), 0); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - // Swap DOLA for PT (maxBorrowAmount + initialDolaDeposit) (receiver is the helper) - bytes - memory pendleSwapDataDeposit = hex"c81f847a000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e8000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce0000000000000000000000000000000000000000000013d06077e362288d7229000000000000000000000000000000000000000000000a01ce75704117112262000000000000000000000000000000000000000000001503cb29d2224a0a61ce0000000000000000000000000000000000000000000014039ceae0822e2244c5000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b20000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000134ed0d8997aa99551a70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000134ed0d8997aa99551a700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000118aa0f8f492b7b00000000000010ba98ebec1e3bbabc78000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000134ed0d8997aa99551a700000000000000000000000000000000000000000000108fc578ec5121883a95000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000134ed0d8997aa99551a700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002327b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239303630342e3634313736303439323534222c22416d6f756e744f7574555344223a2239303031392e3930323233393335373938222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223738393939393737323834313532313837333337383438222c2254696d657374616d70223a313733363236323035322c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22514f58653035617433644e353470337558563842326c655638504a63754f7632626862536d612f47544e46376e362f71677273567a5a4f7a503532724c49786d3162535848744c6533527a6c2f4d48564d5776444a4172725a6c754b746c4379723162755535522b4766776a4b6d597365567a427739326a503566654f75443272694d78346f734c6154706d3669533446544e646441342b78302b474a692f6f4d737049576e344442734f526451452b613435737950395874674f6f6f364e79426a732b63484d636853744d79592b37374b374671625a425a6f4a2f726733524631486b6d2b73715468324e714d776c4944723039415531457a4f72546d6b4542647a4c4d7051692f694a304573686c4d5763773958353833516645436553416d63593850534d567039673946666f33346553534d582b413043442f6c3837442b4a75664b376d432b755a3548446266597243475a413d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - DOLA.approve(address(ale), initialDolaDeposit); - ale.depositAndLeveragePosition( - initialDolaDeposit, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapDataDeposit), - dbrData, - false - ); - - assertEq(DOLA.balanceOf(userPk), 0); - - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + - (((maxBorrowAmount + initialDolaDeposit) * 1.0365 ether) / - 1 ether), // approx 1.0365 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - } - - function test_depositAndLeveragePosition_PT() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - // Swap DOLA for PT (initialDolaDeposit) (receiver is the helper, recipient is the user which will be depositing them) - bytes - memory pendleDataInitialPT = hex"c81f847a000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e8000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce00000000000000000000000000000000000000000000022c6c35d8ad8daca5320000000000000000000000000000000000000000000001190585372159a75b3100000000000000000000000000000000000000000000024e25315a2c6f790c4d0000000000000000000000000000000000000000000002320b0a6e42b34eb662000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000380000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cf5540fffcdc3d510b18bfca6d2b9987b07725590000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f083bd37f90001865377367054516e17014ccded1e7d814edc9ce400019d39a5de30e57443bff2a8307a4256c8797a34970a021e19e0c9bab24000000a01d5c122dc0d96c0000000c49b0001b28ca7e465c452ce4252598e0bc96aeba553cf820001744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0001888888888889758f76e7103c6cbf23abbf58f94635d39ebf03010203000a01010001020001ff00000000000000000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 initialLpAmount = helper.convertToCollateral( - initialDolaDeposit, - abi.encode(address(market), 0, pendleDataInitialPT) - ); - - // Swap DOLA to PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleDataInitialDeposit = hex"c81f847a000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e8000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce00000000000000000000000000000000000000000000138ed8e845253511ea5f0000000000000000000000000000000000000000000009e0b5f403e43f014a690000000000000000000000000000000000000000000014be4ae6d4f8eab5e90f0000000000000000000000000000000000000000000013c16be807c87e0294d2000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b20000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000001150901e57c62d6000000000000108338b12fed3b2a14cc000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000001058f30155d8f0552e30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002327b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393338342e3831393337343337323537222c22416d6f756e744f7574555344223a2238383031322e3830343138353832313739222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223737393738343732323938363033323232373939353634222c2254696d657374616d70223a313733363236333233362c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2245474174712f717344474c6b6e337835425a576a7942647766772b6b4f34395a6874337370424578726b65526a333963727166745758737067374532494a575869316f616c5272633244416473314139485a2f73765a4233527952674f565a3363315a304c644c5231563146437a4948352f353150533233784e5a4e545368574d5047544d7854446d43464457686951676f304172306362676d4141486f4b4174472f565a49327276456a687634597661324c3843526d624e347448765a446e4f3557334e2b6f58732f754e39446a6b38776b3345443353546b4872414179447772436b4f7657756b37464a6474777345434b6c6b6b6363666867684242375470496a455331685a5666362f344447524f7856796f37514e4e6b475538706151324a6e356f596f6d675245496e696536324b7a687669653734616b47454d4b753353427a6d424b44774b5858466963766c794c7a6d773d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleDataInitialDeposit) - ); - vm.stopPrank(); - - uint256 lpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Swap DOLA Max borrow amount to PT (receiver is the helper) - bytes - memory pendleDataLeverage = hex"c81f847a000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e8000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce0000000000000000000000000000000000000000000011a3e508cbe6b250fc9f0000000000000000000000000000000000000000000008e8c13feadbf29fda170000000000000000000000000000000000000000000012b595d306cde3e949fe0000000000000000000000000000000000000000000011d1827fd5b7e53fb42f000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b20000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000113087cb02f1ef7694960000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000113087cb02f1ef769496000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000f9e0216be43db00000000000000ee4cc35f88b6e5592b7000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000113087cb02f1ef769496000000000000000000000000000000000000000000000ebeab72df04eaabc1e3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000113087cb02f1ef76949600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002327b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238303537312e3632323437313439373435222c22416d6f756e744f7574555344223a2237393536312e3330383336363338343934222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223730333333373033333439363136303438393638333735222c2254696d657374616d70223a313733363236333239392c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224774442f472b2f6f352b6c434a6279303836394736364d37624f2b42664670645542725a71724f5234754b564336446f396c6d704d46556e4d4857446b324c464e7355714a32414a47612f49485050416d74614833756942364c753144493161414877696a4a5146547777393563685a4d6658642b546f5057532b4e56777a754949556d657973765547716f337a47546e584e784f49616d4575323550554447787263374f373376626e693050795a674c47677564387a5746764f2f3867784f354c694b3061776b4b413145653243776262766e464746457354614347386c6e574978355879314e37445358697569537435544a445a6a3038367463354577684b6f3848627352486b4d3636534946434e6a45325535756f373974694f34636742774b4a783139306b7365544e62636b58454d426c7a7854545a494a4c5551666f38654d64464b4f2f7a61564a5349435073463154513d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - IERC20(pendlePT).approve(address(ale), initialLpAmount); - ale.depositAndLeveragePosition( - initialLpAmount, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleDataLeverage), - dbrData, - true - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - lpAmount + - initialLpAmount + - ((maxBorrowAmount * 1.0365 ether) / 1 ether), - 1e14 // 0.01% Delta - ); - } - - function test_deleveragePosition_Redeem_with_PT_and_YT_router() public { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000009123ef9b7db2e3d968b01c6ff99839acb242da1300000000000000000000000096512230bf0fa4e20cf02c3e8a7d983132cd2b9f0000000000000000000000000000000000000000000028e5a7091f3bc94d48c90000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000028bfaa0a0bcb95c851600000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023b5bfb83f6d82c58500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b28e1ad68ad8fd000000000000292908d90428258485eb0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000023b5bfb83f6d82c585000000000000000000000000000000000000000000000028bfaa0a0bcb95c85160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd6700000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000023b5bfb83f6d82c5850000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002347b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139313833372e3933323739313839363633222c22416d6f756e744f7574555344223a223139313837332e38393134333935343531222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313934333733393739383530303730323639363538363033222c2254696d657374616d70223a313733363236343039382c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2261436846646739663758313472586e61736f364d4c42336874696e396765564d544c4946734543786b3130524932626c7a3341444c4a2f704754764a596a59664b432f36384b59565974706d32576a69724d4a7a70546f64526a394169356a55357552454e5a4c5a7a4d4a46494b662b554f7a714b653168515938544d5243417969574e5931543076505761307475766477334664396949376b426f5333706c796278334330754a2b324947692f304c4b784b644862365275486a66686e372b5742536a52326b43674531735666506d34415a784463585571373663696c654563734e6f677963757a7170774858475555754a79657753793470552b676751334250455a72344f324c743058754675304d797350746d336155764d663654375232364971726c48536e4f6a4f6d616766433639445a58354d466f466249324e793956577361436e625a676b6b66617679694b734639513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - assertApproxEqRel( - DOLA.balanceOf(userPk), - 194373979850070269658603 - debt, - 0.01 ether - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - assertEq(market.debts(userPk), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_AFTER_DEADLINE_deleveragePosition_Redeem_with_PT_and_YT_router() - public - { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000009123ef9b7db2e3d968b01c6ff99839acb242da1300000000000000000000000096512230bf0fa4e20cf02c3e8a7d983132cd2b9f0000000000000000000000000000000000000000000028e5a7091f3bc94d48c90000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000028bfaa0a0bcb95c851600000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023b5bfb83f6d82c58500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b28e1ad68ad8fd000000000000292908d90428258485eb0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000023b5bfb83f6d82c585000000000000000000000000000000000000000000000028bfaa0a0bcb95c85160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd6700000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000023b5bfb83f6d82c5850000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002347b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139313833372e3933323739313839363633222c22416d6f756e744f7574555344223a223139313837332e38393134333935343531222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313934333733393739383530303730323639363538363033222c2254696d657374616d70223a313733363236343039382c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2261436846646739663758313472586e61736f364d4c42336874696e396765564d544c4946734543786b3130524932626c7a3341444c4a2f704754764a596a59664b432f36384b59565974706d32576a69724d4a7a70546f64526a394169356a55357552454e5a4c5a7a4d4a46494b662b554f7a714b653168515938544d5243417969574e5931543076505761307475766477334664396949376b426f5333706c796278334330754a2b324947692f304c4b784b644862365275486a66686e372b5742536a52326b43674531735666506d34415a784463585571373663696c654563734e6f677963757a7170774858475555754a79657753793470552b676751334250455a72344f324c743058754675304d797350746d336155764d663654375232364971726c48536e4f6a4f6d616766433639445a58354d466f466249324e793956577361436e625a676b6b66617679694b734639513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - uint256 ytBalBefore = IERC20(pendleYT).balanceOf(userPk); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), ytBalBefore); - } - - function test_deleveragePosition_Swap_PT_to_DOLA_router() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000009123ef9b7db2e3d968b01c6ff99839acb242da13000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce0000000000000000000000000000000000000000000029bed86f3d5430f5df9b00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000a80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000027c9826c343c05e8faee0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022ddf92a7faedbd903b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002a242992bba1475000000000000283064b5b3781aa819e90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000022ddf92a7faedbd903b00000000000000000000000000000000000000000000027c9826c343c05e8faee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd6700000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000022ddf92a7faedbd903b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002357b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223138373631352e3330393836373435393831222c22416d6f756e744f7574555344223a223138373834302e3931353135393932343432222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313839373837333539393334303236383639313139343635222c2254696d657374616d70223a313733363236333933322c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224b61594b3637504f6544415066746e36514148516857526a6f4b796d6c34484f586c564a78473967323532695a2f2b702b674b4f30556853306d67466b476e5a326d4c4b554558783536362f4c2b4c6f58572b544a434d46785067336c7874624f6b4771504434786f73704c336e65337a56446d46372b4b7a2f766235663630437366746a557832366875743930585434496c6e2f324155714d50745a4f6e4a3034534a6571656f33384b4c44736e38544436314f6e346637733343716348336b3555776d666a316576346777714256656c484665417771796b6436626146706d6e6e676e5452376435394248516e6256653952767843454e7359574d537a2b524948326573754c71725333584a727058546b4f726a466637796f56793636556e746f6e6a2f4646744b456b4e5936326e504e3954464d6c714c58587762726c67757544644d79413843494435472b666b72625738773d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - vm.startPrank(userPk); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - // Dola balance is equal to collateral sold for DOLA minus debt - assertEq(DOLA.balanceOf(userPk), 189795305864399402361976 - debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_AFTER_DEADLINE_deleveragePosition_Swap_PT_to_DOLA_router() - public - { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000009123ef9b7db2e3d968b01c6ff99839acb242da13000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce0000000000000000000000000000000000000000000029bed86f3d5430f5df9b00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000a80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000027c9826c343c05e8faee0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022ddf92a7faedbd903b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002a242992bba1475000000000000283064b5b3781aa819e90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000022ddf92a7faedbd903b00000000000000000000000000000000000000000000027c9826c343c05e8faee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd6700000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000022ddf92a7faedbd903b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002357b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223138373631352e3330393836373435393831222c22416d6f756e744f7574555344223a223138373834302e3931353135393932343432222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313839373837333539393334303236383639313139343635222c2254696d657374616d70223a313733363236333933322c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224b61594b3637504f6544415066746e36514148516857526a6f4b796d6c34484f586c564a78473967323532695a2f2b702b674b4f30556853306d67466b476e5a326d4c4b554558783536362f4c2b4c6f58572b544a434d46785067336c7874624f6b4771504434786f73704c336e65337a56446d46372b4b7a2f766235663630437366746a557832366875743930585434496c6e2f324155714d50745a4f6e4a3034534a6571656f33384b4c44736e38544436314f6e346637733343716348336b3555776d666a316576346777714256656c484665417771796b6436626146706d6e6e676e5452376435394248516e6256653952767843454e7359574d537a2b524948326573754c71725333584a727058546b4f726a466637796f56793636556e746f6e6a2f4646744b456b4e5936326e504e3954464d6c714c58587762726c67757544644d79413843494435472b666b72625738773d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - - vm.startPrank(userPk); - vm.expectRevert(PendlePTHelper.PendleSwapFailed.selector); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - } - - function test_deleveragePosition_sellDBR() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - uint256 debt = market.debts(address(userPk)); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 10000, - 0 - ); // sell all DBR - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000009123ef9b7db2e3d968b01c6ff99839acb242da13000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce0000000000000000000000000000000000000000000029bed86f3d5430f5df9b00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000a80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000027c9826c343c05e8faee0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022ddf92a7faedbd903b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002a242992bba1475000000000000283064b5b3781aa819e90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000022ddf92a7faedbd903b00000000000000000000000000000000000000000000027c9826c343c05e8faee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd6700000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000022ddf92a7faedbd903b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002357b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223138373631352e3330393836373435393831222c22416d6f756e744f7574555344223a223138373834302e3931353135393932343432222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313839373837333539393334303236383639313139343635222c2254696d657374616d70223a313733363236333933322c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224b61594b3637504f6544415066746e36514148516857526a6f4b796d6c34484f586c564a78473967323532695a2f2b702b674b4f30556853306d67466b476e5a326d4c4b554558783536362f4c2b4c6f58572b544a434d46785067336c7874624f6b4771504434786f73704c336e65337a56446d46372b4b7a2f766235663630437366746a557832366875743930585434496c6e2f324155714d50745a4f6e4a3034534a6571656f33384b4c44736e38544436314f6e346637733343716348336b3555776d666a316576346777714256656c484665417771796b6436626146706d6e6e676e5452376435394248516e6256653952767843454e7359574d537a2b524948326573754c71725333584a727058546b4f726a466637796f56793636556e746f6e6a2f4646744b456b4e5936326e504e3954464d6c714c58587762726c67757544644d79413843494435472b666b72625738773d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - dbr.approve(address(ale), dbr.balanceOf(userPk)); - ale.deleveragePosition( - debt, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - - assertEq(dbr.balanceOf(userPk), 0); - // Dola balance is greater than collateral sold for DOLA minus debt because we also sold DBR - assertGt(DOLA.balanceOf(userPk), 189795305864399402361976 - debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_InsufficientPT_after_swap() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode( - address(market), - (initialDolaAmount * 1.1 ether) / 1 ether, - pendleDataInitial - ) - ); - } - - function test_fail_InsufficientPT_after_mint() public { - _mintInitialDolaAmountToUser(); - bytes - memory pendleMintData = hex"d0f42385000000000000000000000000cc39fc2ed370a4373ff4b8ebd507cd26906a80e800000000000000000000000096512230bf0fa4e20cf02c3e8a7d983132cd2b9f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000131953f892f952ddaea90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000131953f892f952ddaea900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000115a0ce892f03fa000000000000108c44f77cc1a67bcd16000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000131953f892f952ddaea9000000000000000000000000000000000000000000000d3d03f9309aeb963dab000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000131953f892f952ddaea900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002327b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393333342e3633343132373134353838222c22416d6f756e744f7574555344223a2238393536302e3431363532383733343938222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223738313435333737343734313138363530393430363934222c2254696d657374616d70223a313733363236313338322c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22464f70686550495230465553777469393367654f72525a74427a616f674f3649684368615a476f44654c4f6f6d6d536c774a65307342627555717a63612f78634e2f4b784374697a4a79386949316777325a2b34444469384c52746e71744e476a495230635468546f4539495469594d37336b5843446f38683566654d6147695656686c2f4156356e772f2f354363595046577552765668797538524d65587279413778774e34774949636376567757616555416245434d7941496d48457874577362747849437061486c35562f39624a616c326552486b414b5173654547744c4e764d6842576e7a654a3047656958426e76352b664974574d4f6750764e6d56464d4779665276343054714735544252714c43794a674d61463654537173566d6d5646674165567a43426c634c4b6e43476e56706831784d326c736464574c78536342495a39616764696a5745536e6654386235513d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateral( - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleMintData) - ); - } - - function test_withdrawAndConvertFromCollateral_SWAP() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf000000000000000000000000cdd26eb5eb2ce0f203a84553853667ae69ca29ce0000000000000000000000000000000000000000000015f3ead1362d4aa6880600000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000dc0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001507b990cd0c30827dd00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000bc4e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000009400000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000005e00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000057064f49ad7123c92560882a45518374ad982e850000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043bc8c40ec139fd0c7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000008272e1a3dbef607c04aa6e5bd3a1a134c8ac063b000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004d2def47b1f5943d27800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e2c523752c9abdfdbe70000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000016464471999e099000000000000153e1b1de6643b57d9970000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012681afb618ae5dce864000000000000000000000000000000000000000000001507b990cd0c30827dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd6700000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012681afb618ae5dce86400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002327b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239393633332e3033333633363636393938222c22416d6f756e744f7574555344223a2239393839372e30383937363034373237222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313030333135333438323433393634303537303832323633222c2254696d657374616d70223a313733363435373037312c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22576b5455384c5a3837586861774575536f573755736a59784f69756848336e386d4753337359505758517239324f302b374b414e4464774667366e43533535785a76453264304474494a346841652b6c706c3365576f6e33726464614f7a4a3748324c53375561464d654a79306b614269564a366c334c66452b4548463930644a326f5761622b4f706b47456667527369434b574e476d775a6673614f756f6b4e5764566a4939644e64796e74464656654e66684235575a6c6631697737666a2f304555507552785239524a557850346e2f7a3136624467666c536f4f466562634d5544464c2b5569597775675a6737747456582f68777a6d6d77733745504d437a717131737a473536585730747277726d6a5456763144674565666534674a7a702f3971783062787249484a4b35306f74683752594b54483478656b6e68624f6e6f655a614f676a6341755153697a5536384978513d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode(address(market), 98000 ether, pendleSwapToDolaData) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - } - - function test_withdrawAndConvertFromCollateral_REDEEM() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf00000000000000000000000096512230bf0fa4e20cf02c3e8a7d983132cd2b9f0000000000000000000000000000000000000000000015f3ead1362d4aa688060000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000015e2465534fd239f4bc90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000132853f8cb988597b6f800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000172dbf91cd0e9da000000000000161add05e7f2c44e285a0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000132853f8cb988597b6f80000000000000000000000000000000000000000000015e2465534fd239f4bc9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000132853f8cb988597b6f800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002347b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130333638382e36343736393437303934222c22416d6f756e744f7574555344223a223130333832312e3634333939303035313037222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313034333837363034333539373335373730323238383236222c2254696d657374616d70223a313733363435373335322c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22536b5a6751506656577232654e484278436c51507555506b63363843356c6753557166464d36704736373176547937316b425730794e5271666f70574d6b4d627a3343334b555164672f61756a6f62684e64553131694164484c6558685131433578436c586b73704162456f566a4c62306c38423439744a6645416b7778705635785039464e735a47532f78426a78414153364c6948763232574758494841312b4f50324b7a6778533656646b776a45714b4a7269427a4d6c3864373538796947793049312f4f2b522b6d5471766349492b75324374643879495a476d376d50523779445051514856536246357944665852515934654e6755724f3549652b76362f2b747234497869736e345961567a477a4977546a4a793073687434372b6f734e35397259725a49446f4758554330364d4d70714f6479302b6133445849394835722b39723734506e4b57626d6b6647422b5773513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - } - - function test_withdrawAndConvertFromCollateral_REDEEM_AFTER_DEADLINE() - public - { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 1000000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf00000000000000000000000096512230bf0fa4e20cf02c3e8a7d983132cd2b9f0000000000000000000000000000000000000000000015f3ead1362d4aa688060000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000015e2465534fd239f4bc90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000007ae5c4cf5cddfbda870a80d021997d8a04f4ff1100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000884e21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000132853f8cb988597b6f800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000172dbf91cd0e9da000000000000161add05e7f2c44e285a0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000132853f8cb988597b6f80000000000000000000000000000000000000000000015e2465534fd239f4bc9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000132853f8cb988597b6f800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002347b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130333638382e36343736393437303934222c22416d6f756e744f7574555344223a223130333832312e3634333939303035313037222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313034333837363034333539373335373730323238383236222c2254696d657374616d70223a313733363435373335322c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22536b5a6751506656577232654e484278436c51507555506b63363843356c6753557166464d36704736373176547937316b425730794e5271666f70574d6b4d627a3343334b555164672f61756a6f62684e64553131694164484c6558685131433578436c586b73704162456f566a4c62306c38423439744a6645416b7778705635785039464e735a47532f78426a78414153364c6948763232574758494841312b4f50324b7a6778533656646b776a45714b4a7269427a4d6c3864373538796947793049312f4f2b522b6d5471766349492b75324374643879495a476d376d50523779445051514856536246357944665852515934654e6755724f3549652b76362f2b747234497869736e345961567a477a4977546a4a793073687434372b6f734e35397259725a49446f4758554330364d4d70714f6479302b6133445849394835722b39723734506e4b57626d6b6647422b5773513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertGt(DOLA.balanceOf(userPk), (initialDolaAmount * 98) / 100); - // Didn't transfer token even if approval granted because after maturity - assertEq(IERC20(pendleYT).balanceOf(userPk), amountToWithdraw); - } - function test_Access_Control_convertToCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertToCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_Access_Control_convertFromCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertFromCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_updateMarketHelper_if_helper_not_address_zero() public { - vm.prank(gov); - ale.updateMarketHelper(address(market), address(1)); - (, , IPendleHelper helper, ) = ale.markets(address(market)); - assertEq(address(helper), address(1)); - } - - function test_updateMarketHelper_if_market_not_set() public { - vm.expectRevert( - abi.encodeWithSelector(ALEV2.MarketNotSet.selector, address(2)) - ); - vm.prank(gov); - ale.updateMarketHelper(address(2), address(1)); - } - - function test_updateMarketHelper_fails_if_helper_not_set() public { - vm.expectRevert(ALEV2.InvalidHelperAddress.selector); - vm.prank(gov); - ale.updateMarketHelper(address(market), address(0)); - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (amountCollat * - oracle.viewPrice(address(pendlePT), 0) * - market.collateralFactorBps()) / - 10_000 / - 1e18; - } -} diff --git a/test/util/aleTests/ALEPendlePTsUSDe24Sep25.t.sol b/test/util/aleTests/ALEPendlePTsUSDe24Sep25.t.sol deleted file mode 100644 index fed2bcf9..00000000 --- a/test/util/aleTests/ALEPendlePTsUSDe24Sep25.t.sol +++ /dev/null @@ -1,1037 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {PendlePTHelper, IPendleHelper} from "src/util/PendlePTHelper.sol"; -import "test/marketForkTests/PendlePTsUSDe24Sep25MarketForkTest.t.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; - -/* - User 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf - ALE 0x4dF2EaA1658a220FDB415B9966a9ae7c3d16e240 - helper 0x4809fE7d314c2AE5b2Eb7fa19C1B166434D29141 - DOLA 0x865377367054516e17014CcdED1e7d814EDC9ce4 - pendlePT 0x9F56094C450763769BA0EA9Fe2876070c0fD5F77 -*/ -contract ALEV2PTsUSDe24Sep25Test is PendlePTsUSDe24Sep25MarketForkTest { - ALEV2 ale; - address userPk = vm.addr(1); - PendlePTHelper helper; - address userPkEscrow; - - address pendleRouter = address(0x888888888889758F76e7103c6CbF23ABbF58F946); - address pendleYT = address(0x029d6247ADb0A57138c62E3019C92d3dfC9c1840); - address marketPT = address(0xA36b60A14A1A5247912584768C6e53E1a269a9F7); - - // Pendle Router Mock - address pendleYTHolder = - address(0xa1026c4bbc0180F2cDF791922ee0917C66b42A3f); - - // Get initial PT - // Swap 100K DOLA for PT (recipient helper) - bytes pendleDataInitial = - hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f700000000000000000000000000000000000000000000150f5abc9373de725d6700000000000000000000000000000000000000000000001bc6cbaadb2fa8b89c0000000000000000000000000000000000000000000018e983cbb84f7d46469a0000000000000000000000000000000000000000000000378d9755b65f517139000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001140000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ea4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000117962af24129874f204000000000000000000000000000000000000000000001203bcd43a4d3b85c214386f66ccc06e4377aad8f98b7aec1dae0000000000000000000000000158b4a5000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000640000000000000000000000000000016a8865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c482fb15ed47040000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000064000000000000000000000000000001068865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4d3168276864000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000783492568c71c080192a3748d85f6299e92915a1b017109d972d5667524a3c4a99c8a5b4e42fa6744400000000000000000000000000000000000000000000000000000000683ca686000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed15000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed150000000000000000000000000000000000000000000006e26d7bea5aa1a41a35000000000000000000000000000000000000000000000000011d45d20df5178f0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041acc8da241f15320228c305a5b1e75bc967d420566392e5ad41936353477633983689476d7d195ce5b1059e9666bd883666fbcccf9acc2f85d6d6b8719d6a580b1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - // Swap maxBorrowAmount from 100k DOLA to PT deposit (recipient helper) - bytes pendleSwapData = - hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f700000000000000000000000000000000000000000000127a5cf2854ed8a757f40000000000000000000000000000000000000000000000185ef38e0b988a81710000000000000000000000000000000000000000000015dc8d6999266c7a3787000000000000000000000000000000000000000000000030bde71c17311502e2000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001140000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000129453510050812215cb0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ea4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000129453510050812215cb000000000000000000000000000000000000000000000f54f0829b1b85d505ee000000000000000000000000000000000000000000000fce5451dc9b0dee15f3d1b88e478a3242289c7b48ff8a89ede70000000000000000000000000158b4ad000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000640000000000000000000000000000015e0865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a678ad133604850871600000000000000000000000000000000000000000000000000000000000000010000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000064000000000000000000000000000001130865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000082cc87fccf038d18eb50000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000006977a288b9c3c12ba52a3748d85f6299e92915a1b017109d972d5667524a3c4a99c8a5b4e42fa6744400000000000000000000000000000000000000000000000000000000683ca686000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed15000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed150000000000000000000000000000000000000000000006e26d7bea5aa1a41a35000000000000000000000000000000000000000000000000011d45d20df5178f0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041acc8da241f15320228c305a5b1e75bc967d420566392e5ad41936353477633983689476d7d195ce5b1059e9666bd883666fbcccf9acc2f85d6d6b8719d6a580b1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 initialDolaAmount = 100000 ether; // 100K DOLA - - function setUp() public override { - super.setUp(); - - vm.startPrank(gov); - - ale = ALEV2(payable(aleV2Addr)); - - helper = PendlePTHelper( - pendlePTHelperAddr - ); - ale.setMarket(address(market), address(DOLA), address(helper), false); - helper.setMarket(address(market), address(pendlePT), pendleYT); - borrowController.allow(address(ale)); - DOLA.mint(address(market), 1000000 ether); - vm.stopPrank(); - - userPkEscrow = address(market.predictEscrow(userPk)); - - vm.prank(userPk); - DOLA.approve(address(helper), initialDolaAmount); - } - - function _mintInitialDolaAmountToUser() internal { - vm.prank(gov); - DOLA.mint(userPk, initialDolaAmount); - } - function test_leveragePosition_Mint_PT_and_YT_with_DOLA_router() public { - _mintInitialDolaAmountToUser(); - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Mint PT and YT with maxBorrowAmount (swap recipient is the helper, then distribute PT to ALE and YT to userPk) - bytes - memory pendleMintData = hex"d0f423850000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c18400000000000000000000000000000000000000000000012031a972f118d2e7fd40000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000129453510050812215cb0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000be4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000006c000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000129453510050812215cb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000004628f13651ead6793f8d838b34b8f8522fb0cc5200000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e11000000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000128fa37bdcda2bef2f86000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000670a72e6d22b0956c0d2573288f82dcc5d6e3a6100000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f0000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012948f89ed7942f80e83000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000001378a1bdfa8adae0000000000001291b71fd18394302bb2000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000129453510050812215cb0000000000000000000000000000000000000000000012031a972f118d2eb9be0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000129453510050812215cb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238373730322e3832373731323534323432222c22416d6f756e744f7574555344223a2238373631332e3136343933343937393035222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223837363930353730303738313337373734373739333134222c2254696d657374616d70223a313734383535303134392c22526f7574654944223a2263336564366630632d326437382d343536322d623033322d3866633535663238666234383a33376663383863372d316331352d343636632d386662302d343364363464373335323038222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224874323950594931484844525a74776476546848516561437a513779593931506a5930327a7379786b7362372b4a33614762694534425264342f75386653497744686a56374c672f466a5a656951387271447063375865776e42685a474774316c783646517a505944644a43706d444b4b452b2b2b7a49384e373971444a3977747157466f36707a4c59505a3858366e394f65594d78543569755137735a7444364b772b2b5746454f4468566e526a595a37724c6f646e487a4b62786a696232595a736f71743459714378355757655736576c6669556873744d465474312b49675051463855386e3344452b49544d6852505438734c745a622b544a46626271776a5a517631373633744878496a4e7854496e30496374435068414f73506f486758557566756956694a63452b33686c45394f5531427165624e51566456766f3670414a77535a572f6a72316a656f6d4b786d7875413d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleMintData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 0.999 ether) / 1 ether), //0.999 ratio minted PT and YT to DOLA - 1e14 // 0.01% Delta - ); - - assertApproxEqRel( - IERC20(pendleYT).balanceOf(userPk), - ((maxBorrowAmount * 0.999 ether) / 1 ether), // 0.999 ratio minted PT and YT to DOLA - 1e14 // 0.01% Delta - ); - } - - function test_leveragePosition_Swap_DOLA_for_PT_router() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.0253 ether) / 1 ether), // approx 1.0253 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_leveragePosition_buyDBR_Swap_DOLA_for_PT() public { - _mintInitialDolaAmountToUser(); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 15 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 90) / 100, - 0 - ); - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.0253 ether) / 1 ether), // approx 1.0253 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - assertGt(dbr.balanceOf(userPk), (dbrAmount * 90) / 100); - } - - function test_depositAndLeveragePosition_DOLA() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - // Swap DOLA for PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleInitialDepositData = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f70000000000000000000000000000000000000000000012f44310a796b4021463000000000000000000000000000000000000000000000018ff3c17af0a5b586e00000000000000000000000000000000000000000000166c539948879ad016ec000000000000000000000000000000000000000000000031fe782f5e14b6b0dd000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001140000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ea4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000fba16245079e9cbf25d0000000000000000000000000000000000000000000010369ac910fc5d3bd246f320f8a7b4eb46f580676448f1d09da80000000000000000000000000158b4b4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000640000000000000000000000000000015e0865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aac3081695b0780000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000064000000000000000000000000000001130865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000862b865ae353cc000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000006c2f1b16b89a31a1242a3748d85f6299e92915a1b017109d972d5667524a3c4a99c8a5b4e42fa6744400000000000000000000000000000000000000000000000000000000683ca686000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed15000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed150000000000000000000000000000000000000000000006e26d7bea5aa1a41a35000000000000000000000000000000000000000000000000011d45d20df5178f0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041acc8da241f15320228c305a5b1e75bc967d420566392e5ad41936353477633983689476d7d195ce5b1059e9666bd883666fbcccf9acc2f85d6d6b8719d6a580b1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount - initialDolaDeposit); - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleInitialDepositData) - ); - assertEq(DOLA.balanceOf(address(helper)), 0); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - // Swap DOLA for PT (maxBorrowAmount + initialDolaDeposit) (receiver is the helper) - bytes - memory pendleSwapDataDeposit = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f70000000000000000000000000000000000000000000012bc7f5fc8ff69bcae5f000000000000000000000000000000000000000000000018b58771a8ad64044e00000000000000000000000000000000000000000000162a711ca009482016330000000000000000000000000000000000000000000000316b0ee3515ac8089c000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001140000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000012d6d60a192d6362ed820000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ea4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000012d6d60a192d6362ed82000000000000000000000000000000000000000000000f8bd16cfb434ded452f000000000000000000000000000000000000000000001006e7bce35d23789139b64c5bee7f2e44fbb74ce9a80aa8fbde0000000000000000000000000158b4bb000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000640000000000000000000000000000015e0865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a8cc9c8370f2d6a997c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000064000000000000000000000000000001130865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084a0c41e21e35f854060000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000006af0aa9594b6802d632a3748d85f6299e92915a1b017109d972d5667524a3c4a99c8a5b4e42fa6744400000000000000000000000000000000000000000000000000000000683ca686000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed15000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed150000000000000000000000000000000000000000000006e26d7bea5aa1a41a35000000000000000000000000000000000000000000000000011d45d20df5178f0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041acc8da241f15320228c305a5b1e75bc967d420566392e5ad41936353477633983689476d7d195ce5b1059e9666bd883666fbcccf9acc2f85d6d6b8719d6a580b1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - DOLA.approve(address(ale), initialDolaDeposit); - ale.depositAndLeveragePosition( - initialDolaDeposit, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapDataDeposit), - dbrData, - false - ); - - assertEq(DOLA.balanceOf(userPk), 0); - - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + - (((maxBorrowAmount + initialDolaDeposit) * 1.0253 ether) / - 1 ether), // approx 1.0253 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - } - - function test_depositAndLeveragePosition_PT() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - // Swap DOLA for PT (initialDolaDeposit) (receiver is the helper, recipient is the user which will be depositing them) - bytes - memory pendleDataInitialPT = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f700000000000000000000000000000000000000000000021b487ef1e6ed41e925000000000000000000000000000000000000000000000002c73d36f5726a065d00000000000000000000000000000000000000000000027eec1cf20d3a656ce20000000000000000000000000000000000000000000000058e7a6deae4d40cba000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000009a0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000040000000000000000000000006088d94c5a40cecd3ae2d4e0710ca687b91c61d000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000070403b87e5f000000000000000000000000000000000000000000000000000000000001a663000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000000000000000000000000001bf7626be989635ac00000000000000000000000000000000000000000000000000000000006838cc5c0000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000006e0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000021e19e0c9bab2400000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000160000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ecd7eef15713997528896cb5db7ec316db4c21010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ecd7eef15713997528896cb5db7ec316db4c21010000000000000000000000000000000000000000000000000000000000000001000000000000000000002710ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000655977feb2f289a4ab78af67bab0d17aab8436700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000655977feb2f289a4ab78af67bab0d17aab843670000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ecd7eef15713997528896cb5db7ec316db4c21010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ecd7eef15713997528896cb5db7ec316db4c21010000000000000000000000000000000000000000000000000000000000000001000000000000000000002710d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000655977feb2f289a4ab78af67bab0d17aab843670000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000c05f0becd796b62532a3748d85f6299e92915a1b017109d972d5667524a3c4a99c8a5b4e42fa6744400000000000000000000000000000000000000000000000000000000683ca686000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed15000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed150000000000000000000000000000000000000000000006e26d7bea5aa1a41a35000000000000000000000000000000000000000000000000011d45d20df5178f0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041acc8da241f15320228c305a5b1e75bc967d420566392e5ad41936353477633983689476d7d195ce5b1059e9666bd883666fbcccf9acc2f85d6d6b8719d6a580b1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 initialLpAmount = helper.convertToCollateral( - initialDolaDeposit, - abi.encode(address(market), 0, pendleDataInitialPT) - ); - - // Swap DOLA to PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleDataInitialDeposit = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f70000000000000000000000000000000000000000000012f442853a3d66ed5f72000000000000000000000000000000000000000000000018ff98156e55a6d8ab00000000000000000000000000000000000000000000166c5ed0609a5dd93cc5000000000000000000000000000000000000000000000031ff302adcab4db156000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001140000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ea4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000fba156ae018c870eb460000000000000000000000000000000000000000000010369a09e4637244e7ff8830ff64617f448b885eb7dc801956e10000000000000000000000000158b4d5000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000640000000000000000000000000000015e0865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aac3081695b0780000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000064000000000000000000000000000001130865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000862b865ae353cc000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000006c2deebadfae912c1c2a3748d85f6299e92915a1b017109d972d5667524a3c4a99c8a5b4e42fa6744400000000000000000000000000000000000000000000000000000000683ca686000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed15000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed150000000000000000000000000000000000000000000006e26d7bea5aa1a41a35000000000000000000000000000000000000000000000000011d45d20df5178f0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041acc8da241f15320228c305a5b1e75bc967d420566392e5ad41936353477633983689476d7d195ce5b1059e9666bd883666fbcccf9acc2f85d6d6b8719d6a580b1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleDataInitialDeposit) - ); - vm.stopPrank(); - - uint256 lpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Swap DOLA Max borrow amount to PT (receiver is the helper) - bytes - memory pendleDataLeverage = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f70000000000000000000000000000000000000000000010a1439035df49d3dfdd000000000000000000000000000000000000000000000015eec73c3c41abce840000000000000000000000000000000000000000000013adab109238c8dafa2e00000000000000000000000000000000000000000000002bdd8e787883579d09000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001140000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000010b898845dd48c9c525f0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ea4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000010b898845dd48c9c525f000000000000000000000000000000000000000000000dcc656c4e983a44c1db000000000000000000000000000000000000000000000e39a542ca6d6e370c79ca5eaf43cdae4e23bc6a4bbd1aaed9250000000000000000000000000158b4dc000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000064000000000000000000000000000001450865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008b1e8e8ababf732913c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000640000000000000000000000000000012c0865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000806af9bb2289569c1230000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000005ee9e2bbe0a548bab92a3748d85f6299e92915a1b017109d972d5667524a3c4a99c8a5b4e42fa6744400000000000000000000000000000000000000000000000000000000683ca686000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed15000000000000000000000000d0076b85c236d38628f692bc5aa9e8b77edfed150000000000000000000000000000000000000000000006e26d7bea5aa1a41a35000000000000000000000000000000000000000000000000011d45d20df5178f0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041acc8da241f15320228c305a5b1e75bc967d420566392e5ad41936353477633983689476d7d195ce5b1059e9666bd883666fbcccf9acc2f85d6d6b8719d6a580b1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - IERC20(pendlePT).approve(address(ale), initialLpAmount); - ale.depositAndLeveragePosition( - initialLpAmount, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleDataLeverage), - dbrData, - true - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - lpAmount + - initialLpAmount + - ((maxBorrowAmount * 1.0253 ether) / 1 ether), - 1e14 // 0.01% Delta - ); - } - - function test_deleveragePosition_Redeem_with_PT_and_YT_router() public { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000000000000000000000000284600bbee32bf1ddfe20000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000002711253ab26ec3d426a30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022f86f84a539cc5252f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b1386ac905050f0000000000002914ab1b69d0d87acc420000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000022f86f84a539cc5252f20000000000000000000000000000000000000000000027d92b1807ebdc39ac870000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000022f86f84a539cc5252f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139343230342e3536343439393733383334222c22416d6f756e744f7574555344223a223139343233322e3238383934373839323038222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313933393938323930323433383830323037363939303130222c2254696d657374616d70223a313734383535303336362c22526f7574654944223a2238646433393962392d626236622d346665352d386238392d3162653463396166393837623a35313438353436642d376665302d343564352d386232342d353130363439333638666662222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a225042333556714c6e3150424f463936676c47547053456c7677364b6630756a316a70344462472f714978414e6f4b624744662b653158316a7a726c46396669442b53756c3671412b6559396435677763735677795142414650614b444d7533796f76314d34526446564f5a7549506e324d507333666d57587267394c63666a7764327448396939676a36666f5374654c51774a6b42703451545236587a684a6e614b787343476850426c57734b3978412b2b795a6a43616b336c704a636265684148666539687679364232435078766f664977374d4c2b435a72722b6e50566d5a2f4d4c78714330416e5965707577594a4c664c345641554f635732386e785959596f45774a416131493247586b5862306d5275566b73366f6362382b69574f61383375593851553748432b5a31324a66594451713178706a33577943526d7438615748382b544e7178524c78363338565341434d513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - assertApproxEqRel( - DOLA.balanceOf(userPk), - 190637534582110192477893 - debt, - 0.01 ether - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - assertEq(market.debts(userPk), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_AFTER_DEADLINE_deleveragePosition_Redeem_with_PT_and_YT_router() - public - { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000000000000000000000000284600bbee32bf1ddfe20000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000002711253ab26ec3d426a30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022f86f84a539cc5252f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b1386ac905050f0000000000002914ab1b69d0d87acc420000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000022f86f84a539cc5252f20000000000000000000000000000000000000000000027d92b1807ebdc39ac870000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000022f86f84a539cc5252f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139343230342e3536343439393733383334222c22416d6f756e744f7574555344223a223139343233322e3238383934373839323038222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313933393938323930323433383830323037363939303130222c2254696d657374616d70223a313734383535303336362c22526f7574654944223a2238646433393962392d626236622d346665352d386238392d3162653463396166393837623a35313438353436642d376665302d343564352d386232342d353130363439333638666662222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a225042333556714c6e3150424f463936676c47547053456c7677364b6630756a316a70344462472f714978414e6f4b624744662b653158316a7a726c46396669442b53756c3671412b6559396435677763735677795142414650614b444d7533796f76314d34526446564f5a7549506e324d507333666d57587267394c63666a7764327448396939676a36666f5374654c51774a6b42703451545236587a684a6e614b787343476850426c57734b3978412b2b795a6a43616b336c704a636265684148666539687679364232435078766f664977374d4c2b435a72722b6e50566d5a2f4d4c78714330416e5965707577594a4c664c345641554f635732386e785959596f45774a416131493247586b5862306d5275566b73366f6362382b69574f61383375593851553748432b5a31324a66594451713178706a33577943526d7438615748382b544e7178524c78363338565341434d513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - uint256 ytBalBefore = IERC20(pendleYT).balanceOf(userPk); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), ytBalBefore); - } - - function test_deleveragePosition_Swap_PT_to_DOLA_router() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f70000000000000000000000000000000000000000000028c1ca9f2d1998aaf7d100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ae0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000026711092836155f603900000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002a630452d30967d000000000000286c560b8ccaa449f3ea0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000002735e2d2e22af66674460000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139303834342e3133303038333736373332222c22416d6f756e744f7574555344223a223139303833382e3930303034323335303933222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313930383933313037383738383630343133333334353036222c2254696d657374616d70223a313734383534393633392c22526f7574654944223a2239633436643532322d326637652d343466662d613832342d3733316662373366623866633a66633333653538382d383265322d343735622d623337632d383164306561326632346166222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224661535865575a6e6759684748684b504f496978436465493650563251692b6e36466564593761733931335364722b75527a30793061764c6d7966424c5543624752466168685262523158714f795a575236565138376e3556587341416d4e41395254346e4c4b354b585346672f356f416557396c396a574f4274644e30513537424c334a52306b624b7569365a30435554597a546f59324a5947784f434f45762f6a37744639734b4539393532514f67392b36426967783930725759627767517153483962427a743936644c65786c4d684238413244354d487665637a4e6a7a6248732f49595134476334524c4d6148742f336251696364763142504f634a79776263676470554b71364633434b48386233307a2f53584973584c397144596b65675550534c434c6537466254766c6668767034552b682b71785575552b7939337457514e512b38474e41674e3631783274706c513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000b600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000058000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000005a559f153cb0c4e6e700f751289048c5812bce3995777aca289edaf464883b77f4d4601515ca205dd9000000000000000000000000000000000000000000000000000000006838f4fc000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000093c725e0bc1de94ee286e81fb297cfb5b6f4b59500000000000000000000000093c725e0bc1de94ee286e81fb297cfb5b6f4b59500000000000000000000000000000000000000000000028ffecaeb77efb7ba240000000000000000000000000000000000000000000000000121cd058f0167e80000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004111c8464d538209ee62f5baacb00df1fda5008e95faa9ce26a1605f805a6800b42305e996ec4dfa6d2fe331e7eec349eb6597194219cf228dba819c25124c63c91b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000005e114dc5b3ff76f11202f8c0073beecab4687b906381727d92f248cb82578afae175080255bf47d101900000000000000000000000000000000000000000000000000000000683c1160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c18400000000000000000000000001df75aaab7c791af659574d0d32aacff0c30cea00000000000000000000000001df75aaab7c791af659574d0d32aacff0c30cea00000000000000000000000000000000000000000000005e114dc5b3ff76f1120000000000000000000000000000000000000000000000000012ee760584b47fa0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041ec6486944a202d7742b342ca0bdf4f0cc0cb1ebef8c4e196cc0242accd1969201548a0c818cb356913d0b77bf0c9f6f8ca01d171ec3e16113f2cf7dff56f40dd1b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000669ff1749a9ec6b992710fe0a053571d2e7d8184c7a644f692d2a248df6e89b8d565f10ebd7fe33a9d200000000000000000000000000000000000000000000000000000000685fe7e5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000338fe1a22b55ca5f10b6f391310d47c9bee06a2b000000000000000000000000338fe1a22b55ca5f10b6f391310d47c9bee06a2b000000000000000000000000000000000000000000000669ff1749a9ec6b9927000000000000000000000000000000000000000000000000013b477aaa681c540000000000000000000000000000000000000000000000000c7d713b49da00000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000411510defc489ea4ebd49563639c04720f8c8669192a59f4fe9bfd7b867a5c418e746866923c1c0fd88d53ef713046534a718ce3683dfd5c765e86504217ccdf081b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000038e9bc8584848db8d7223df1a987e34aff8a65087d58e4299ff9a6c96be77af76a5f4955434d40ab51100000000000000000000000000000000000000000000000000000000685f46ee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000088a4bfd368c062c0baa4108e7085e8222d09a89000000000000000000000000088a4bfd368c062c0baa4108e7085e8222d09a89000000000000000000000000000000000000000000000174d77603c1d7b16047d000000000000000000000000000000000000000000000000013f2da4f04749960000000000000000000000000000000000000000000000000c7d713b49da00000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000412e450127348134ce17317d68a12e14681b404e32ae6319b1b8ce34cffe44710f54b518d4196430491eace3542ef5155b7d08f8a21bbbfb552e4a53a84fbf80201b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - vm.startPrank(userPk); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - // Dola balance is equal to collateral sold for DOLA minus debt - assertEq(DOLA.balanceOf(userPk), 187145404691119732289444- debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_AFTER_DEADLINE_deleveragePosition_Swap_PT_to_DOLA_router() - public - { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f70000000000000000000000000000000000000000000028c1ca9f2d1998aaf7d100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ae0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000026711092836155f603900000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002a630452d30967d000000000000286c560b8ccaa449f3ea0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000002735e2d2e22af66674460000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139303834342e3133303038333736373332222c22416d6f756e744f7574555344223a223139303833382e3930303034323335303933222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313930383933313037383738383630343133333334353036222c2254696d657374616d70223a313734383534393633392c22526f7574654944223a2239633436643532322d326637652d343466662d613832342d3733316662373366623866633a66633333653538382d383265322d343735622d623337632d383164306561326632346166222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224661535865575a6e6759684748684b504f496978436465493650563251692b6e36466564593761733931335364722b75527a30793061764c6d7966424c5543624752466168685262523158714f795a575236565138376e3556587341416d4e41395254346e4c4b354b585346672f356f416557396c396a574f4274644e30513537424c334a52306b624b7569365a30435554597a546f59324a5947784f434f45762f6a37744639734b4539393532514f67392b36426967783930725759627767517153483962427a743936644c65786c4d684238413244354d487665637a4e6a7a6248732f49595134476334524c4d6148742f336251696364763142504f634a79776263676470554b71364633434b48386233307a2f53584973584c397144596b65675550534c434c6537466254766c6668767034552b682b71785575552b7939337457514e512b38474e41674e3631783274706c513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000b600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000058000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000005a559f153cb0c4e6e700f751289048c5812bce3995777aca289edaf464883b77f4d4601515ca205dd9000000000000000000000000000000000000000000000000000000006838f4fc000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000093c725e0bc1de94ee286e81fb297cfb5b6f4b59500000000000000000000000093c725e0bc1de94ee286e81fb297cfb5b6f4b59500000000000000000000000000000000000000000000028ffecaeb77efb7ba240000000000000000000000000000000000000000000000000121cd058f0167e80000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004111c8464d538209ee62f5baacb00df1fda5008e95faa9ce26a1605f805a6800b42305e996ec4dfa6d2fe331e7eec349eb6597194219cf228dba819c25124c63c91b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000005e114dc5b3ff76f11202f8c0073beecab4687b906381727d92f248cb82578afae175080255bf47d101900000000000000000000000000000000000000000000000000000000683c1160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c18400000000000000000000000001df75aaab7c791af659574d0d32aacff0c30cea00000000000000000000000001df75aaab7c791af659574d0d32aacff0c30cea00000000000000000000000000000000000000000000005e114dc5b3ff76f1120000000000000000000000000000000000000000000000000012ee760584b47fa0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041ec6486944a202d7742b342ca0bdf4f0cc0cb1ebef8c4e196cc0242accd1969201548a0c818cb356913d0b77bf0c9f6f8ca01d171ec3e16113f2cf7dff56f40dd1b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000669ff1749a9ec6b992710fe0a053571d2e7d8184c7a644f692d2a248df6e89b8d565f10ebd7fe33a9d200000000000000000000000000000000000000000000000000000000685fe7e5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000338fe1a22b55ca5f10b6f391310d47c9bee06a2b000000000000000000000000338fe1a22b55ca5f10b6f391310d47c9bee06a2b000000000000000000000000000000000000000000000669ff1749a9ec6b9927000000000000000000000000000000000000000000000000013b477aaa681c540000000000000000000000000000000000000000000000000c7d713b49da00000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000411510defc489ea4ebd49563639c04720f8c8669192a59f4fe9bfd7b867a5c418e746866923c1c0fd88d53ef713046534a718ce3683dfd5c765e86504217ccdf081b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000038e9bc8584848db8d7223df1a987e34aff8a65087d58e4299ff9a6c96be77af76a5f4955434d40ab51100000000000000000000000000000000000000000000000000000000685f46ee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000088a4bfd368c062c0baa4108e7085e8222d09a89000000000000000000000000088a4bfd368c062c0baa4108e7085e8222d09a89000000000000000000000000000000000000000000000174d77603c1d7b16047d000000000000000000000000000000000000000000000000013f2da4f04749960000000000000000000000000000000000000000000000000c7d713b49da00000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000412e450127348134ce17317d68a12e14681b404e32ae6319b1b8ce34cffe44710f54b518d4196430491eace3542ef5155b7d08f8a21bbbfb552e4a53a84fbf80201b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - - vm.startPrank(userPk); - vm.expectRevert(PendlePTHelper.PendleSwapFailed.selector); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - } - - function test_deleveragePosition_sellDBR() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - uint256 debt = market.debts(address(userPk)); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 10000, - 0 - ); // sell all DBR - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f70000000000000000000000000000000000000000000028c1ca9f2d1998aaf7d100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ae0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000026711092836155f603900000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002a630452d30967d000000000000286c560b8ccaa449f3ea0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000002735e2d2e22af66674460000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000226923147d61d62c300d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139303834342e3133303038333736373332222c22416d6f756e744f7574555344223a223139303833382e3930303034323335303933222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313930383933313037383738383630343133333334353036222c2254696d657374616d70223a313734383534393633392c22526f7574654944223a2239633436643532322d326637652d343466662d613832342d3733316662373366623866633a66633333653538382d383265322d343735622d623337632d383164306561326632346166222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224661535865575a6e6759684748684b504f496978436465493650563251692b6e36466564593761733931335364722b75527a30793061764c6d7966424c5543624752466168685262523158714f795a575236565138376e3556587341416d4e41395254346e4c4b354b585346672f356f416557396c396a574f4274644e30513537424c334a52306b624b7569365a30435554597a546f59324a5947784f434f45762f6a37744639734b4539393532514f67392b36426967783930725759627767517153483962427a743936644c65786c4d684238413244354d487665637a4e6a7a6248732f49595134476334524c4d6148742f336251696364763142504f634a79776263676470554b71364633434b48386233307a2f53584973584c397144596b65675550534c434c6537466254766c6668767034552b682b71785575552b7939337457514e512b38474e41674e3631783274706c513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000b600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000058000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000005a559f153cb0c4e6e700f751289048c5812bce3995777aca289edaf464883b77f4d4601515ca205dd9000000000000000000000000000000000000000000000000000000006838f4fc000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000093c725e0bc1de94ee286e81fb297cfb5b6f4b59500000000000000000000000093c725e0bc1de94ee286e81fb297cfb5b6f4b59500000000000000000000000000000000000000000000028ffecaeb77efb7ba240000000000000000000000000000000000000000000000000121cd058f0167e80000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004111c8464d538209ee62f5baacb00df1fda5008e95faa9ce26a1605f805a6800b42305e996ec4dfa6d2fe331e7eec349eb6597194219cf228dba819c25124c63c91b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000005e114dc5b3ff76f11202f8c0073beecab4687b906381727d92f248cb82578afae175080255bf47d101900000000000000000000000000000000000000000000000000000000683c1160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c18400000000000000000000000001df75aaab7c791af659574d0d32aacff0c30cea00000000000000000000000001df75aaab7c791af659574d0d32aacff0c30cea00000000000000000000000000000000000000000000005e114dc5b3ff76f1120000000000000000000000000000000000000000000000000012ee760584b47fa0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041ec6486944a202d7742b342ca0bdf4f0cc0cb1ebef8c4e196cc0242accd1969201548a0c818cb356913d0b77bf0c9f6f8ca01d171ec3e16113f2cf7dff56f40dd1b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000669ff1749a9ec6b992710fe0a053571d2e7d8184c7a644f692d2a248df6e89b8d565f10ebd7fe33a9d200000000000000000000000000000000000000000000000000000000685fe7e5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c1840000000000000000000000000338fe1a22b55ca5f10b6f391310d47c9bee06a2b000000000000000000000000338fe1a22b55ca5f10b6f391310d47c9bee06a2b000000000000000000000000000000000000000000000669ff1749a9ec6b9927000000000000000000000000000000000000000000000000013b477aaa681c540000000000000000000000000000000000000000000000000c7d713b49da00000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000411510defc489ea4ebd49563639c04720f8c8669192a59f4fe9bfd7b867a5c418e746866923c1c0fd88d53ef713046534a718ce3683dfd5c765e86504217ccdf081b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000038e9bc8584848db8d7223df1a987e34aff8a65087d58e4299ff9a6c96be77af76a5f4955434d40ab51100000000000000000000000000000000000000000000000000000000685f46ee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000088a4bfd368c062c0baa4108e7085e8222d09a89000000000000000000000000088a4bfd368c062c0baa4108e7085e8222d09a89000000000000000000000000000000000000000000000174d77603c1d7b16047d000000000000000000000000000000000000000000000000013f2da4f04749960000000000000000000000000000000000000000000000000c7d713b49da00000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000412e450127348134ce17317d68a12e14681b404e32ae6319b1b8ce34cffe44710f54b518d4196430491eace3542ef5155b7d08f8a21bbbfb552e4a53a84fbf80201b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - dbr.approve(address(ale), dbr.balanceOf(userPk)); - ale.deleveragePosition( - debt, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - - assertEq(dbr.balanceOf(userPk), 0); - // Dola balance is greater than collateral sold for DOLA minus debt because we also sold DBR - assertGt(DOLA.balanceOf(userPk), 187150105763588640524025 - debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_InsufficientPT_after_swap() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode( - address(market), - (initialDolaAmount * 1.1 ether) / 1 ether, - pendleDataInitial - ) - ); - } - - function test_fail_InsufficientPT_after_mint() public { - _mintInitialDolaAmountToUser(); - bytes - memory pendleMintData = hex"d0f423850000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c18400000000000000000000000000000000000000000000014873730979a6d2c1c8a0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ea4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000000000000000000000001179b249655b0520d7980000000000000000000000000000000000000000000012040ee4bcf1a0ff8c7333931ebd147448b49293fd0e11563e0a0000000000000000000000000158b7c2000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000064000000000000000000000000000001900865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d8d726b7177a800000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000064000000000000000000000000000000e10865377367054516e17014ccded1e7d814edc9ce40000006000000044ff00000000000000000000000000000000000000000000000000000000000000095ea7b300000000000000000000000016c6521dff6bab339122a0fe25a9116693265353ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff16c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000079f905c6fd34e8000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateral( - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleMintData) - ); - } - - function test_withdrawAndConvertFromCollateral_SWAP() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf000000000000000000000000a36b60a14a1a5247912584768c6e53e1a269a9f70000000000000000000000000000000000000000000015b61aa7f40146c376af00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ae0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000147f0d5946e4de2340080000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012588c55100f03bc1f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000169989a94d34633000000000000158d84ccb964c9eb015b0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012588c55100f03bc1f710000000000000000000000000000000000000000000014e7fe41766453386f650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012588c55100f03bc1f71000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130313732362e3135323537333539343536222c22416d6f756e744f7574555344223a223130313739382e3233363231343031353035222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313031373830323536323831383036333837323136373331222c2254696d657374616d70223a313734383535383036302c22526f7574654944223a2265383861663733302d636432652d343039642d613138342d3739383561376366633236623a65613932373065302d363165622d343235332d383163662d326165646464623366333237222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22497556692b47526c3676634861624f6672374a6c6456684f37767362507751727835753974537444643177396f357631614346514c3035345a3558744946567176577766765171616d65455a767a576c495947376233636e745276704e5968552f51567553462b69736662396f524c66695630797a6f306139786f436b6b546e346349354a566348784b43495430596c5a5631336a74486f6b5a564f58646c4e33304c2f3075676a593534752b4e3473424e77345155583546514a2b3168495969424b41576b6f376a794f736a545678446f3055316e343179492f4e733667572f38756c6768734a4f514166545146646a50553842774a4464442b6551543554785962736a4853434c4c3553646e71674731546d76354d4b70653552773048434b7a4d5a313451444a55756b377137684a796548554f30776574484b48504e4c5165626267556e4e46626b396c6f5a5a4f6d435459513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000062000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000002c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000005a559f153cb0c4e6e700f751289048c5812bce3995777aca289edaf464883b77f4d4601515ca205dd9000000000000000000000000000000000000000000000000000000006838f4fc000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c184000000000000000000000000093c725e0bc1de94ee286e81fb297cfb5b6f4b59500000000000000000000000093c725e0bc1de94ee286e81fb297cfb5b6f4b59500000000000000000000000000000000000000000000028ffecaeb77efb7ba240000000000000000000000000000000000000000000000000121cd058f0167e80000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004111c8464d538209ee62f5baacb00df1fda5008e95faa9ce26a1605f805a6800b42305e996ec4dfa6d2fe331e7eec349eb6597194219cf228dba819c25124c63c91b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000005e114dc5b3ff76f11202f8c0073beecab4687b906381727d92f248cb82578afae175080255bf47d101900000000000000000000000000000000000000000000000000000000683c1160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c18400000000000000000000000001df75aaab7c791af659574d0d32aacff0c30cea00000000000000000000000001df75aaab7c791af659574d0d32aacff0c30cea00000000000000000000000000000000000000000000005e114dc5b3ff76f1120000000000000000000000000000000000000000000000000012ee760584b47fa0000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041ec6486944a202d7742b342ca0bdf4f0cc0cb1ebef8c4e196cc0242accd1969201548a0c818cb356913d0b77bf0c9f6f8ca01d171ec3e16113f2cf7dff56f40dd1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - assertEq(DOLA.balanceOf(userPk), 0); - - vm.startPrank(userPk); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode(address(market), 99000 ether, pendleSwapToDolaData) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - } - - function test_withdrawAndConvertFromCollateral_REDEEM() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c18400000000000000000000000000000000000000000000015b61aa7f40146c376af0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000150fdd4eba993952301d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012da2d162d7ebecc8de3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000001739362dae3ff200000000000001625cbaf288173b2cbab0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012da2d162d7ebecc8de300000000000000000000000000000000000000000000157bb3a757ee3558f3a30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012da2d162d7ebecc8de3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130343732382e3830353334373333313037222c22416d6f756e744f7574555344223a223130343637312e3039383233363235363736222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313034353839323639313438313036353238383336353233222c2254696d657374616d70223a313734383535373933372c22526f7574654944223a2262376664323062322d653036632d343634322d623536312d6366623166386431323135623a66653730656433352d346336302d346363372d393633652d626538623332306336613862222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22627069366c7959644d2f304d6a4274653547596e32793034306c4a4a377a41674a5a6d6d49524242615173454d61665158764f743872376b3373754e6c4a3558682f44643052644345746b43647767445450655449627841342f6c6143396944526745416e554b744c2b4271763737325570476854654b6f32353651516636486d75386c4f653736674a79472f736a52706172737a7a64656d4f383665754b47694e36452f517931466d3935644969556757346354794c34704b6d767866735a763452704644432b4d6b4742304847774c57707953647a714c3375366b47362f4930754e68666f36504246565051706276555961453452414e5976426a646a35726c684e4d6f4333417a7237756e7877686e52526f55564b3677652b6a306e77364674514d77724c526e4273415263325170703679304b424169396774786c6a6a383664574e4655394f717147696d305532373170673d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - } - - function test_withdrawAndConvertFromCollateral_REDEEM_AFTER_DEADLINE() - public - { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 1000000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf000000000000000000000000029d6247adb0a57138c62e3019c92d3dfc9c18400000000000000000000000000000000000000000000015b61aa7f40146c376af0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000150fdd4eba993952301d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012da2d162d7ebecc8de3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000001739362dae3ff200000000000001625cbaf288173b2cbab0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012da2d162d7ebecc8de300000000000000000000000000000000000000000000157bb3a757ee3558f3a30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012da2d162d7ebecc8de3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028b7b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130343732382e3830353334373333313037222c22416d6f756e744f7574555344223a223130343637312e3039383233363235363736222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313034353839323639313438313036353238383336353233222c2254696d657374616d70223a313734383535373933372c22526f7574654944223a2262376664323062322d653036632d343634322d623536312d6366623166386431323135623a66653730656433352d346336302d346363372d393633652d626538623332306336613862222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22627069366c7959644d2f304d6a4274653547596e32793034306c4a4a377a41674a5a6d6d49524242615173454d61665158764f743872376b3373754e6c4a3558682f44643052644345746b43647767445450655449627841342f6c6143396944526745416e554b744c2b4271763737325570476854654b6f32353651516636486d75386c4f653736674a79472f736a52706172737a7a64656d4f383665754b47694e36452f517931466d3935644969556757346354794c34704b6d767866735a763452704644432b4d6b4742304847774c57707953647a714c3375366b47362f4930754e68666f36504246565051706276555961453452414e5976426a646a35726c684e4d6f4333417a7237756e7877686e52526f55564b3677652b6a306e77364674514d77724c526e4273415263325170703679304b424169396774786c6a6a383664574e4655394f717147696d305532373170673d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertGt(DOLA.balanceOf(userPk), (initialDolaAmount * 98) / 100); - // Didn't transfer token even if approval granted because after maturity - assertEq(IERC20(pendleYT).balanceOf(userPk), amountToWithdraw); - } - function test_Access_Control_convertToCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertToCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_Access_Control_convertFromCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertFromCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_updateMarketHelper_if_helper_not_address_zero() public { - vm.prank(gov); - ale.updateMarketHelper(address(market), address(1)); - (, , IPendleHelper helper, ) = ale.markets(address(market)); - assertEq(address(helper), address(1)); - } - - function test_updateMarketHelper_if_market_not_set() public { - vm.expectRevert( - abi.encodeWithSelector(ALEV2.MarketNotSet.selector, address(2)) - ); - vm.prank(gov); - ale.updateMarketHelper(address(2), address(1)); - } - - function test_updateMarketHelper_fails_if_helper_not_set() public { - vm.expectRevert(ALEV2.InvalidHelperAddress.selector); - vm.prank(gov); - ale.updateMarketHelper(address(market), address(0)); - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (amountCollat * - oracle.viewPrice(address(pendlePT), 0) * - market.collateralFactorBps()) / - 10_000 / - 1e18; - } -} diff --git a/test/util/aleTests/ALEPendlePTsUSDe29May25.t.sol b/test/util/aleTests/ALEPendlePTsUSDe29May25.t.sol deleted file mode 100644 index a1f8bcb1..00000000 --- a/test/util/aleTests/ALEPendlePTsUSDe29May25.t.sol +++ /dev/null @@ -1,1035 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {PendlePTHelper, IPendleHelper} from "src/util/PendlePTHelper.sol"; -import "test/marketForkTests/PendlePTsUSDe29May25MarketForkTest.t.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; - -/* - User 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf - ALE 0x4dF2EaA1658a220FDB415B9966a9ae7c3d16e240 - helper 0x4809fE7d314c2AE5b2Eb7fa19C1B166434D29141 - DOLA 0x865377367054516e17014CcdED1e7d814EDC9ce4 - address pendlePT = 0xb7de5dFCb74d25c2f21841fbd6230355C50d9308 -*/ -contract ALEV2PTsUSDe29May25Test is PendlePTsUSDe29May25MarketForkTest { - ALEV2 ale; - address userPk = vm.addr(1); - PendlePTHelper helper; - address userPkEscrow; - - address pendleRouter = address(0x888888888889758F76e7103c6CbF23ABbF58F946); - address pendleYT = address(0x1de6Ff19FDA7496DdC12f2161f6ad6427c52aBBe); - address marketPT = address(0xB162B764044697cf03617C2EFbcB1f42e31E4766); - - // Pendle Router Mock - address pendleYTHolder = - address(0x8607a7D180de23645Db594D90621d837749408d5); - - // Get initial PT - // Swap 100K DOLA for PT (recipient helper) - bytes pendleDataInitial = - hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e4766000000000000000000000000000000000000000000001460b0f643c093ac7dcf000000000000000000000000000000000000000000000ab9a0819cec17d40c52000000000000000000000000000000000000000000001ad01144084e3b921ece000000000000000000000000000000000000000000001573410339d82fa818a5000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001820000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001584e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000108000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000000fc0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000f600000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000004c000000000000000000000000000000000000000000000000000000000000008a00000000000000000000000000000000000000000000000000000000000000a400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000006691dbb44154a9f23f8357c56fc9ff5548a8bdc4000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000002544faa778090e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007c4e143b23d72e6938e06291f705b5ae3d5c7c7c00000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000002540906888aeac0ca4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000028f74f359000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002c00000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000e57180685e3348589e9521aa53af0bcd497e884d000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000d8d726b7177a800000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040301a40330000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000031373595f40ea48a7aab6cbcb0d377c6066e2dca00000000000000000000000000000000000000000000000000000000ee36ad82000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b00000000000000000000000000000000000000000000000000000000ee5af26c000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000e9c7f5bd65501200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000008272e1a3dbef607c04aa6e5bd3a1a134c8ac063b000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000003635c9adc5dea000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000390f3595bca2df7d23783dfd126427cceb997bf4000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003626711d3d231b859c500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b00000000000000000000000000000000000000000000000000000003b9385ef2000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca300000000000000000000000000000000000000000000000000000000000000200000000000000000013115d793a6549a000000000000122f3b88b0b4c077c590000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000000000000000000000001146788ea7deea0b62150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002657b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130303334352e3634393330303136353831222c22416d6f756e744f7574555344223a223130303032392e3938343630343537333234222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223835383733383833353336303533343032383436363038222c2254696d657374616d70223a313734343332393836372c22526f7574654944223a2231303832323436302d353063612d343066352d623330612d393136363964663463376564222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2255344a333730695546363465756d4d46524c442f57526e714e31464c6c4b6d76396c513635482f4f4d4636346e6b4d4365474e46715753617173612b6176527051615a474531656246643977337250646b67547277597154555864565979596e4456756f43484d4d614756437138304a566b4744346a623842323362364430704f313064575762746a6f74626e4a546c5a676a6a32642f4c4c3555477942776e4841744f3657703936733053555268507a633677644754792f3736446256564656526831422f31304f5044596f682b4a74446844775532682f31486e51335968326d64516d4f637a745a56653531743743434342335a47383648524a4d477566352b454e7677674d58686a4c2b6750746946714378375a654848704442754a546e74486741384e64722b734779534b74705a5a3461312f515561752b4c6c744d434d3434536971454d624f44363463476734356c71673d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - // Swap maxBorrowAmount from 100k DOLA to PT deposit (recipient helper) - bytes pendleSwapData = - hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e476600000000000000000000000000000000000000000000125ba211ba86559a51330000000000000000000000000000000000000000000009a97db87d1e480060a2000000000000000000000000000000000000000000001827ba4d38cbb400f195000000000000000000000000000000000000000000001352fb70fa3c9000c144000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001820000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001313a08445915c4095e30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001584e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000108000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000000fc0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000f600000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000004c000000000000000000000000000000000000000000000000000000000000008a00000000000000000000000000000000000000000000000000000000000000a400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000006691dbb44154a9f23f8357c56fc9ff5548a8bdc4000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000002dc8b470a6f67701685000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007c4e143b23d72e6938e06291f705b5ae3d5c7c7c00000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000002dc33fc842d828cc58700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000032529bd37000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002c00000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000e57180685e3348589e9521aa53af0bcd497e884d000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000c3585735fb930cd2cb000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040301a40330000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000031373595f40ea48a7aab6cbcb0d377c6066e2dca00000000000000000000000000000000000000000000000000000000d69b5341000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b00000000000000000000000000000000000000000000000000000000d6bbddd1000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000c665b892d3815906166000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000008272e1a3dbef607c04aa6e5bd3a1a134c8ac063b000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000030d615cd7ee4c334b2d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000390f3595bca2df7d23783dfd126427cceb997bf4000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030c859185c6f60a4f0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000035abc01ae000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000002000000000000000000112d844c64848140000000000001061cc7f6a8c6dd3c1f3000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001313a08445915c4095e3000000000000000000000000000000000000000000000f901bdf72056855f8400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001313a08445915c4095e300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002637b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239303239352e3938343134303330343232222c22416d6f756e744f7574555344223a2238393936342e3733393730363237343235222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223737333631393333353134373231333030303338313331222c2254696d657374616d70223a313734343333303039352c22526f7574654944223a2237383931383839352d616562312d343939302d613234642d346335336534323433343136222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224678524e7353517552586261792b4c68506e2b375a484f69677a4b2b4637766c3936334b7a6b5a6a335753526b6d694458466a314578463946667556336e566a35752f46544649576e3441514278625166355879656e346f7248704a4b486c7163334f57424f4f7774724144664e77307a4735493144757062367259334476497645617332786a5a3757687061342f62536445534866734f7036697464644d416b2f346f4b7645564b707854704f44584c7531596c4f42756357645a4d654c356c7938633668344a6452634b586639393870422b424d536f634b752b65515971534a674c6f544c684e30325530723449454150786b543479476e644a3451735472677252354c42665264494938416e66583043685738434e38536735705a673375556f635671592f5451664c526f4f594e3254335332546c364f346666424566472b5267484157634943416b475872486642687855513d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 initialDolaAmount = 100000 ether; // 100K DOLA - - function setUp() public override { - super.setUp(); - - vm.startPrank(gov); - - ale = ALEV2(payable(aleV2Addr)); - - helper = PendlePTHelper( - pendlePTHelperAddr - ); - ale.setMarket(address(market), address(DOLA), address(helper), false); - helper.setMarket(address(market), address(pendlePT), pendleYT); - borrowController.allow(address(ale)); - DOLA.mint(address(market), 1000000 ether); - vm.stopPrank(); - - userPkEscrow = address(market.predictEscrow(userPk)); - - vm.prank(userPk); - DOLA.approve(address(helper), initialDolaAmount); - } - - function _mintInitialDolaAmountToUser() internal { - vm.prank(gov); - DOLA.mint(userPk, initialDolaAmount); - } - function test_leveragePosition_Mint_PT_and_YT_with_DOLA_router() public { - _mintInitialDolaAmountToUser(); - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Mint PT and YT with maxBorrowAmount (swap recipient is the helper, then distribute PT to ALE and YT to userPk) - bytes - memory pendleMintData = hex"d0f423850000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe00000000000000000000000000000000000000000000122904f889feccff18fb0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001313a08445915c4095e30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001584e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000108000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000000fc0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000f600000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000004c000000000000000000000000000000000000000000000000000000000000008a00000000000000000000000000000000000000000000000000000000000000a400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000006691dbb44154a9f23f8357c56fc9ff5548a8bdc4000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000002dc8b470a6f67701685000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007c4e143b23d72e6938e06291f705b5ae3d5c7c7c00000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000002dc33fc842d828cc58700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000032529bd37000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002c00000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000e57180685e3348589e9521aa53af0bcd497e884d000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000c3585735fb930cd2cb000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040301a40330000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000031373595f40ea48a7aab6cbcb0d377c6066e2dca00000000000000000000000000000000000000000000000000000000d69b5341000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b00000000000000000000000000000000000000000000000000000000d6bbfea4000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000c665b892d3815906166000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000008272e1a3dbef607c04aa6e5bd3a1a134c8ac063b000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000030d615cd7ee4c334b2d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000390f3595bca2df7d23783dfd126427cceb997bf4000000000000000000000000f939e0a03fb07f59a73314e73794be0e57ac1b4e000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030c859185c6f60a4f0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000035abc01ae000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000002000000000000000000112d847edb9bddb0000000000001061ccaf8b27713fa896000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001313a08445915c4095e3000000000000000000000000000000000000000000000f901c0d2a98ab96135b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001313a08445915c4095e300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002637b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239303234352e3738393933393232323333222c22416d6f756e744f7574555344223a2238393930352e3432333638323832333333222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223737333631393437303631333730333331353732333734222c2254696d657374616d70223a313734343333303031322c22526f7574654944223a2262366461626362652d656238632d343430322d613636322d386566653139393063373563222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224b7156356c50556d357a4a374d6e38455731314636477938527a77336d446a4d5763507a707978376d65786a367138614a51546f323361794a437046506d596c6161393330436b4165366d73516758775064674152647864494c782b61634a65416832364430705969726531764c684f452f706f6158514d6a754d614362396f556b695578764150684846532b72544d5952334d504d6a5037782f496e7655506b2f326e4266704277564a5863556a327169545958626a56574c72595736394b5a567a50434a6d7771416f30684c5a32413970773379437a524b4e4c645562564c66553745566533676b5155694c4e43484d75593572726c6449682b6642522b392b6d332f66343858596b56735976654f706852734669517434734e744e47552b34564b7973504a616462585261373157384d477732757434497445362b4c33384445796a424342336232556f7061314c4a586165673d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleMintData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.0016512 ether) / 1 ether), //1.0016512 ratio minted PT and YT to DOLA - 1e14 // 0.01% Delta - ); - - assertApproxEqRel( - IERC20(pendleYT).balanceOf(userPk), - ((maxBorrowAmount * 1.0016512 ether) / 1 ether), // 1.0016512 ratio minted PT and YT to DOLA - 1e14 // 0.01% Delta - ); - } - - function test_leveragePosition_Swap_DOLA_for_PT_router() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.010968098 ether) / 1 ether), // approx 1.010968098 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_leveragePosition_buyDBR_Swap_DOLA_for_PT() public { - _mintInitialDolaAmountToUser(); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 15 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 90) / 100, - 0 - ); - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.010968098 ether) / 1 ether), // approx 1.010968098 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - assertGt(dbr.balanceOf(userPk), (dbrAmount * 90) / 100); - } - - function test_depositAndLeveragePosition_DOLA() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - // Swap DOLA for PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleInitialDepositData = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e4766000000000000000000000000000000000000000000001256c2ee948f0d275def0000000000000000000000000000000000000000000009a6221be0118c55103f0000000000000000000000000000000000000000000018214500c0c371f7530500000000000000000000000000000000000000000000134c4437c02318aa207f000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000012a0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001004e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000000000a40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000009e000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000032000000000000000000000000000000000000000000000000000000000000007600000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000001e7e4171bf4d3a0000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b00000000000000000000000000000000000000000000000000000002184cc397000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000006691dbb44154a9f23f8357c56fc9ff5548a8bdc4000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000001e7e4171bf4d3a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007c4e143b23d72e6938e06291f705b5ae3d5c7c7c00000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000001e7a435a54a7d35a9a600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000021846b9ff000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000f3f20b8dfa69d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000011291b191d98772000000000000105d979bbfbd7b093fd5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000f8c1cd3f62734e2630a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002637b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239303239302e3530393037373736393334222c22416d6f756e744f7574555344223a2238393934372e3838353339333836393936222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223737323834333335343630393130393632393137333333222c2254696d657374616d70223a313734343333313632392c22526f7574654944223a2266326638316532322d366537382d343238622d393331362d663837373061356564303434222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2245396c495461524145714f422b38307a567946656536325a79506d2f327469776b4349496f4665666a4763734e69436278585255487854546441546d374c6c79727231774a524d3970414e657a57364d4e624d4b64594678304857754767766e4a5030716656534d2f66647578484932523139507656646856696a365a4f76414f6c325446344c6a435a325661577875587748456e455a39444848664847377644524169546270376d4e7466537a4c795a646d4d68424b504c7a6f3737466d664443534f4e6a706e3267446a504c362b4e7a2f586b4b72624f66672b6a6573352b38513554704c743443563442324b7077556e317864344f524152674b4d705858786c38304e6d6c565556756c46534754686373323571555263773734446d667037374c5634336a3975726e586a7738456c6e6a653052497a4c37466d7a595261727768557a5a4e33686b592f7a2b6a6532694431413d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000414c455c6987b5c1a194fa1867dc570ce60a3f1a07414e05f937a93b7e5b843608780eb47561b50000000000000000000000000000000000000000000000000000000006812ba77000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b30000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000ae7519fc3ca24195aa6dbb3af77aea2d98afde9f000000000000000000000000ae7519fc3ca24195aa6dbb3af77aea2d98afde9f00000000000000000000000000000000000000000000001043561a88293000000000000000000000000000000000000000000000000000000110c337f720fa110000000000000000000000000000000000000000000000000ab423d28f346a54000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041c6fd5465d2bda6c6d88281e4a369e65767d1d580697ec60c87c179732a27af8664330b011f7cc353a9101033e9ecf7978f45154650ab2c52df378dfd93b5b5231c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount - initialDolaDeposit); - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleInitialDepositData) - ); - assertEq(DOLA.balanceOf(address(helper)), 0); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - // Swap DOLA for PT (maxBorrowAmount + initialDolaDeposit) (receiver is the helper) - bytes - memory pendleSwapDataDeposit = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e476600000000000000000000000000000000000000000000128fa162480494566d030000000000000000000000000000000000000000000009c4dbacfd7bac6361cc00000000000000000000000000000000000000000000186c253079b52ef8747e000000000000000000000000000000000000000000001389b759faf758c6c398000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000001480000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001349ba770daa32d334b70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ce00000000000000000000000000000000000000000000000000000000000000f200000000000000000000000000000000000000000000000000000000000000c20000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000bc000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000009400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000001edc5d8b49105151ee9000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af000000000000000000000000000000000000000000000000000000021e6d990f000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba3000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000021ec4e040000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000006691dbb44154a9f23f8357c56fc9ff5548a8bdc4000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000001edc5d8b49105151ede000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007c4e143b23d72e6938e06291f705b5ae3d5c7c7c00000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000001ed852e44df94d2ab6200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000021ebd8561000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000f6e2ec5a48828a8f6f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000115e111b245aaaa000000000000109019884677876b785e000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001349ba770daa32d334b7000000000000000000000000000000000000000000000fbc18417624c0a618bf0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001349ba770daa32d334b700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002627b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239313335332e30323639343533373433222c22416d6f756e744f7574555344223a2239313036332e3434373332393238323536222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223738323136303334363730343535343636373139333236222c2254696d657374616d70223a313734343333313735372c22526f7574654944223a2232643936383536652d386566392d343337332d626532302d386436316664323930623330222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22477a73762b4a71756c335670776433535a4168752b47776948466a6d3139315a79554f6251386d503675516c65484a35584e32365447496b58563756716b676a717a546f65374633415338475039554c57414964447764376b36765944344d394c76326350766d6979343644327156794a73366f6838554b36627641474169376f31394764336a367650465835644a79772f595857394a636d2b67635137584130672f6d324f652f44756f534e425a4241574544486978764a5856706d39686e38726b353244706e4d53585831435a5236666e75756f597045437a446471426546364a6c36597859716d375a75752b4e6d4f396b4a594f546868537769456c2b4259446b4d473755784259427a4b67584d634f5563574435346e764f31542f644368325a6d394c554f6c48465571627639446456714a4859527a5a367a32666c73414a5857342f713744762b4d6531322b54415847773d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - DOLA.approve(address(ale), initialDolaDeposit); - ale.depositAndLeveragePosition( - initialDolaDeposit, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapDataDeposit), - dbrData, - false - ); - - assertEq(DOLA.balanceOf(userPk), 0); - - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + - (((maxBorrowAmount + initialDolaDeposit) * 1.01082 ether) / - 1 ether), // approx 1.01082 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - } - - function test_depositAndLeveragePosition_PT() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - // Swap DOLA for PT (initialDolaDeposit) (receiver is the helper, recipient is the user which will be depositing them) - bytes - memory pendleDataInitialPT = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e4766000000000000000000000000000000000000000000000209bfa72d7dd4139fc100000000000000000000000000000000000000000000011101c3cd3b271e93970000000000000000000000000000000000000000000002ae82db4ebb9d0b63fe00000000000000000000000000000000000000000000022203879a764e3d272f000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000e60000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bc4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000006c000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000021e19e0c9bab2400000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af000000000000000000000000000000000000000000000000000000025381a5d9000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba3000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b0000000000000000000000000000000000000000000000000000000253e155b0000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca300000000000000000000000000000000000000000000000000000000000000200000000000000000001e82a7ce46c8ad00000000000001d18d2503479392f726000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000000000000000000000000001ba46165cb732986aca0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000021e19e0c9bab240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002617b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a22393938352e3436353732353237373934222c22416d6f756e744f7574555344223a22393934372e323032323830313534343234222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a2238353837393036353333323134333831303738333130222c2254696d657374616d70223a313734343333313832392c22526f7574654944223a2232303166366637392d623438352d346561642d613732392d646536646530303461363266222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2255337944366b4c3537426e43555574724d774830394d5a744b3036684a4c554458435a4a687250517a786375516759694653322b664b61324c6a4655305675554347467369464b3575626566314572586d353751726b534d6b39344d636b5169696f6c436e6468345a4274644c536e522f443677547a364255577951796241696f33364b68696646387935497a7133734d4e38717758306b32767733736b6b50392b566838656e497a43644e6a447447306169413030724e68666135476f71394649494147356b5574377250315a2f4d4e677253642b485864354e61683238446e70704f5861596b49574231337a37456c7243714c6e64346e61466b5362325a38474958716861785254734c503151464f6e67776e6e58674a4949774772372b794635306243306b6e58522b39314f41766b6536784164373251776f69635046302b643471462f6451377951792f58393177316475673d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000006cb79bf0598ef8013b793ff4d3e5b235cf6f84bdd08d5a0c31c8fd836b4cec5ad9821ca4a4dbba60000000000000000000000000000000000000000000000000000000067fdf664000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b30000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000731bfadaf268f6a388558fad07cfcc5ec9e9323f000000000000000000000000731bfadaf268f6a388558fad07cfcc5ec9e9323f000000000000000000000000000000000000000000000003849d4d6850881a6b00000000000000000000000000000000000000000000000000e17735d896e7ea0000000000000000000000000000000000000000000000000ab45f0c207219f80000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000418c032bfd8f33de1db5250a7d3ed86679ca55986075b4fcd6840a169156b3382168296f1ef69b54e304c309227d5c5e339bfa18eb99d08a206d491de0f0dcdad61b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 initialLpAmount = helper.convertToCollateral( - initialDolaDeposit, - abi.encode(address(market), 0, pendleDataInitialPT) - ); - - // Swap DOLA to PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleDataInitialDeposit = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e4766000000000000000000000000000000000000000000001256c2491fc7a406b21d0000000000000000000000000000000000000000000009a6220ec6a51d317d5400000000000000000000000000000000000000000000182144dffe935c97f08700000000000000000000000000000000000000000000134c441d8d4a3a62faa8000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000012a0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001004e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000000000a40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000009e000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000032000000000000000000000000000000000000000000000000000000000000007600000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000001e7e4171bf4d3a0000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b00000000000000000000000000000000000000000000000000000002184cc397000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000006691dbb44154a9f23f8357c56fc9ff5548a8bdc4000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000001e7e4171bf4d3a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007c4e143b23d72e6938e06291f705b5ae3d5c7c7c00000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000001e7a435a54a7d35a9a600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000021846b9ff000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000f3f20b8dfa69d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000011291ac671b1034000000000000105d974ce844b40071e2000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000f8c1c890fdade339f630000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002637b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393836392e3139313532373530313436222c22416d6f756e744f7574555344223a2238393531362e3839313437393430383934222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223737323834333133323638393439303434313935383130222c2254696d657374616d70223a313734343333313836322c22526f7574654944223a2231363230383834612d663430372d343565392d396264362d623231333665353862366462222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a225938716d6d624832794f66704544656a73587a5631415a4c6f2b7065392b536d4f7776765261782f516b794334765a656b596942645536794b704b6a476b304b50326c6d546b33506b4e7a4471373933506178704669483951472b4b513236595270674e624e48576f4c36776750583251446b576d4d427950376c7748306a72575a7a7a733663716a6c4334664e4934582b53334559434b673446525039324746776a634e6635354b72376a2b656c597350484b6c49525544764156587232364664305848665944646c473335374a692b5958436834596b52325259527776496f4253672b35342f436634464e4a504e786147375054524b336d5061564b3333477a6275797776714667326b5a3239466647537975616b6a41306d506f474e353232546c2f796c4e6c312f327033796c6c67536d53727447385a32594e456274793866425670433664644a3144786b467157714c77513d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000041338ab7d8f3d5f1a194fa1867dc570ce60a3f1a07414e05f937a93b7e5b843608780eb47561b50000000000000000000000000000000000000000000000000000000006812ba77000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b30000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000ae7519fc3ca24195aa6dbb3af77aea2d98afde9f000000000000000000000000ae7519fc3ca24195aa6dbb3af77aea2d98afde9f00000000000000000000000000000000000000000000001043561a88293000000000000000000000000000000000000000000000000000000110c337f720fa110000000000000000000000000000000000000000000000000ab423d28f346a54000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041c6fd5465d2bda6c6d88281e4a369e65767d1d580697ec60c87c179732a27af8664330b011f7cc353a9101033e9ecf7978f45154650ab2c52df378dfd93b5b5231c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleDataInitialDeposit) - ); - vm.stopPrank(); - - uint256 lpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Swap DOLA Max borrow amount to PT (receiver is the helper) - bytes - memory pendleDataLeverage = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e4766000000000000000000000000000000000000000000001085aab849fb41ffe1070000000000000000000000000000000000000000000008b223f534694b285b7d0000000000000000000000000000000000000000000015bd59e503073be4e4b800000000000000000000000000000000000000000000116447ea68d29650b6fa000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000012a0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000112b5f81ea34fa3888b90000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001004e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000000000a40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000009e000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000032000000000000000000000000000000000000000000000000000000000000007600000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000001e37db4b0cd81ec9e7500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b000000000000000000000000000000000000000000000000000000021376dd00000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000006691dbb44154a9f23f8357c56fc9ff5548a8bdc4000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000015700b564ca08d9439c58ca5053166e8317aa1380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000018b95654ad6b01eb024000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007c4e143b23d72e6938e06291f705b5ae3d5c7c7c00000000000000000000000015700b564ca08d9439c58ca5053166e8317aa138000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000018b61c9de47873bfa7a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000001dd125c32e4b5086c63cc13b3ca02c4a2a61fa9b00000000000000000000000000000000000000000000000000000001b2d42ee3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000dbc4c67ee90c82d3a20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000f75bed6a1069410000000000000ebe667367a8ea0e383e000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000112b5f81ea34fa3888b9000000000000000000000000000000000000000000000e01ae20d5ad44c0b56e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000112b5f81ea34fa3888b900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002627b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238313237392e36333533373138383836222c22416d6f756e744f7574555344223a2238303934392e3338353939383037363633222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223639363235333934343932333639323137313430373938222c2254696d657374616d70223a313734343333323033372c22526f7574654944223a2266366163356636322d363731362d346635352d393339372d623866633462663766336465222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2242764b6954762b4a34424a6a74324e7a4f7a4a5a546739434a53624c2b326544486143567a3537352f6b7155524b536e31687162677941457a6342386d2b4f563658394b32423831634f7632484132794563314671395772534f30396b6a724557426b4f36586748745070707a66774a4948366d7449557762643652506e6777634e7064514943636c6c63324c35706f636b624e6d733358707937342f556c31546b396a73706e766666744a7375764f305a6c2f3052446d386774637a67517337754b71525a5a46494436374147696e48386c6d51554a6d7a4d635a39726d43715a4c573244435a2f62636f765a4a36626c7765783842623655796c644f6f47694b51426a4e613767435053513630634d6d616b56746a52417766324c615750586c6c754b4c62737674535048376149727471526f6d35704450533366772b324e625445437178784e3133583264556a5976683135773d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - IERC20(pendlePT).approve(address(ale), initialLpAmount); - ale.depositAndLeveragePosition( - initialLpAmount, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleDataLeverage), - dbrData, - true - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - lpAmount + - initialLpAmount + - ((maxBorrowAmount * 1.01072 ether) / 1 ether), - 1e14 // 0.01% Delta - ); - } - - function test_deleveragePosition_Redeem_with_PT_and_YT_router() public { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e2400000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe00000000000000000000000000000000000000000000288531720083e475df5e0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000002661106360027ef6534d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000001244e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000000000f800000000000000000000000000000000000000000000000000000000000000c80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000c2000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e1b4827368ce9d15ebb0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af0000000000000000000000000000000000000000000001c55e98e517590c50dd000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040301a40330000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000031373595f40ea48a7aab6cbcb0d377c6066e2dca0000000000000000000000000000000000000000000000000000000243d552b7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002442dd2d30000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af00000000000000000000000000000000000000000000038abd31ca2eb218a1ba000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000c6fdfc08401113d02280e27f53f4f38a19fbccc90000000000000000000000000000000000000000000000000000000487a9b512000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041ecf7e8f9cf4c98d12000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b3571f297a85100000000000002935041f3ea9c0e8238c0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000236b63f1e5d2f4f6515200000000000000000000000000000000000000000000272590b748547742ee910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000236b63f1e5d2f4f6515200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002647b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139343330372e36353134353434323132222c22416d6f756e744f7574555344223a223139343833382e31393235323131343132222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313934353935303030323538353631323936353733333234222c2254696d657374616d70223a313734343333303337342c22526f7574654944223a2265306366333537362d653266312d343237642d383139302d396134303138396333613435222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22454b35577838614c4f34394f3675716a47736332505875564a653161424f466c56702f656d4261543843584a365a4c4a465a795037334b547143383732354739697548445168537035666c6565714a5652666c574949517a767631517641534a4f4d4733696d5447476c4b466f7267726c672f436d6c4c62313252626b52516f7a2b697267706748522f6237544b504f6c6459615141446579544d46316a6b6c4c667551356878346d45786d332f6342484d70597a38496c4859546e5a2f787a5945646a696e384139622b555a4d39445757743444454b515437642b687567426c356d4c2b5a6b47586b4e3732344b65625854667a2b6950382f52776c477637367941496779615136396c456d3439652f597150766e39346a2b63757155372f5347535239392b425431383436414346566f685951686a3743737662324347427863503145697a7676424d543453477073616a646d513d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - assertApproxEqRel( - DOLA.balanceOf(userPk), - 190637534582110192477893 - debt, - 0.01 ether - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - assertEq(market.debts(userPk), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_AFTER_DEADLINE_deleveragePosition_Redeem_with_PT_and_YT_router() - public - { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e2400000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe00000000000000000000000000000000000000000000288531720083e475df5e0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000002661106360027ef6534d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000001244e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000000000f800000000000000000000000000000000000000000000000000000000000000c80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000c2000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e1b4827368ce9d15ebb0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af0000000000000000000000000000000000000000000001c55e98e517590c50dd000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040301a40330000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000031373595f40ea48a7aab6cbcb0d377c6066e2dca0000000000000000000000000000000000000000000000000000000243d552b7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002442dd2d30000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af00000000000000000000000000000000000000000000038abd31ca2eb218a1ba000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000c6fdfc08401113d02280e27f53f4f38a19fbccc90000000000000000000000000000000000000000000000000000000487a9b512000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041ecf7e8f9cf4c98d12000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002b3571f297a85100000000000002935041f3ea9c0e8238c0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000236b63f1e5d2f4f6515200000000000000000000000000000000000000000000272590b748547742ee910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000236b63f1e5d2f4f6515200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002647b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139343330372e36353134353434323132222c22416d6f756e744f7574555344223a223139343833382e31393235323131343132222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313934353935303030323538353631323936353733333234222c2254696d657374616d70223a313734343333303337342c22526f7574654944223a2265306366333537362d653266312d343237642d383139302d396134303138396333613435222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22454b35577838614c4f34394f3675716a47736332505875564a653161424f466c56702f656d4261543843584a365a4c4a465a795037334b547143383732354739697548445168537035666c6565714a5652666c574949517a767631517641534a4f4d4733696d5447476c4b466f7267726c672f436d6c4c62313252626b52516f7a2b697267706748522f6237544b504f6c6459615141446579544d46316a6b6c4c667551356878346d45786d332f6342484d70597a38496c4859546e5a2f787a5945646a696e384139622b555a4d39445757743444454b515437642b687567426c356d4c2b5a6b47586b4e3732344b65625854667a2b6950382f52776c477637367941496779615136396c456d3439652f597150766e39346a2b63757155372f5347535239392b425431383436414346566f685951686a3743737662324347427863503145697a7676424d543453477073616a646d513d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - uint256 ytBalBefore = IERC20(pendleYT).balanceOf(userPk); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), ytBalBefore); - } - - function test_deleveragePosition_Swap_PT_to_DOLA_router() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e47660000000000000000000000000000000000000000000028b21966e97b0e57517a00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000001500000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000261d5b9887bee02dd28b0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000001304e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000010400000000000000000000000000000000000000000000000000000000000000d40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000ce00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000760000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001de6312b0ccd57caee1c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003e00000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af0000000000000000000000000000000000000000000001c23f20a6666e93776b000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af000000000000000000000000000000000000000000000000000000023fd6d94e000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba3000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002402fc75a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af0000000000000000000000000000000000000000000003847e414cccdd26eed6000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000c6fdfc08401113d02280e27f53f4f38a19fbccc9000000000000000000000000000000000000000000000000000000047facc58c000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004178b778d0424a735f4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002ae9380163eb9e200000000000028ec522d336e9fdaf79d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000232cee8d0000a385545d0000000000000000000000000000000000000000000026e081448a75e4a99e6e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000232cee8d0000a385545d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002637b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139333037372e373230303833393633222c22416d6f756e744f7574555344223a223139333739322e34393634323437383137222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313933323534303132333631383136353436313436323035222c2254696d657374616d70223a313734343333303734322c22526f7574654944223a2230656233346131612d636537382d343533302d613037312d393962616636313261633538222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22566a3369456546436d6b45784b6c35676a43337562353078576836724c647961733856755a3263507771772f31594f334b4662776c5355364d56314e794e4a5a74594e5a5147304a49657a6f494d2f7a4775345a466e74783739354e2b74646d6836315875536f77626c79704e754b3333796b4f6455376248436f4d2f62614b55467852753770417434672f3842332f46416d4f396f5731676c51676a6846504f476431324e5359727658343736666831526f6b467342732b2b55625455494c583255327154694679456b62664d7a77587a30386439787153427a71543777573638305053796336624332327268616f45762b6f50435a3253384d4c757152694e32796a4a5a3361487062437370757462525347742b6b504d62654c385155624d656f344165442f344759424c43592b47565a7354317165466f4e77576150324c71703556683350615650425a2b5663614b466157673d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000002849eb11672558700b8a242ea5f877a3a2756f8a6879c5bb885874668ff252fdea822bea50e6af214c630000000000000000000000000000000000000000000000000000000067f8e6cd000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000afbb30322d80b572a7ff5aee89896d555732051d000000000000000000000000afbb30322d80b572a7ff5aee89896d555732051d00000000000000000000000000000000000000000002ee152257ecad790d4cbc000000000000000000000000000000000000000000000000012a2337a255a4f30000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004187d8e83f8d3cf54b5be96da92d7f9be2eb61098ac118100edf2aabc8a3c9137a5b73e1bc74d2936c7210483899d1045fff5446de36a7d8a1a08b8cdfaa4128341c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - vm.startPrank(userPk); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - // Dola balance is equal to collateral sold for DOLA minus debt - assertEq(DOLA.balanceOf(userPk), 189467723788496711906807 - debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_AFTER_DEADLINE_deleveragePosition_Swap_PT_to_DOLA_router() - public - { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e47660000000000000000000000000000000000000000000028b21966e97b0e57517a00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000001500000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000261d5b9887bee02dd28b0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000001304e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000010400000000000000000000000000000000000000000000000000000000000000d40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000ce00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000760000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001de6312b0ccd57caee1c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003e00000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af0000000000000000000000000000000000000000000001c23f20a6666e93776b000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af000000000000000000000000000000000000000000000000000000023fd6d94e000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba3000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002402fc75a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af0000000000000000000000000000000000000000000003847e414cccdd26eed6000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000c6fdfc08401113d02280e27f53f4f38a19fbccc9000000000000000000000000000000000000000000000000000000047facc58c000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004178b778d0424a735f4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002ae9380163eb9e200000000000028ec522d336e9fdaf79d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000232cee8d0000a385545d0000000000000000000000000000000000000000000026e081448a75e4a99e6e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000232cee8d0000a385545d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002637b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139333037372e373230303833393633222c22416d6f756e744f7574555344223a223139333739322e34393634323437383137222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313933323534303132333631383136353436313436323035222c2254696d657374616d70223a313734343333303734322c22526f7574654944223a2230656233346131612d636537382d343533302d613037312d393962616636313261633538222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22566a3369456546436d6b45784b6c35676a43337562353078576836724c647961733856755a3263507771772f31594f334b4662776c5355364d56314e794e4a5a74594e5a5147304a49657a6f494d2f7a4775345a466e74783739354e2b74646d6836315875536f77626c79704e754b3333796b4f6455376248436f4d2f62614b55467852753770417434672f3842332f46416d4f396f5731676c51676a6846504f476431324e5359727658343736666831526f6b467342732b2b55625455494c583255327154694679456b62664d7a77587a30386439787153427a71543777573638305053796336624332327268616f45762b6f50435a3253384d4c757152694e32796a4a5a3361487062437370757462525347742b6b504d62654c385155624d656f344165442f344759424c43592b47565a7354317165466f4e77576150324c71703556683350615650425a2b5663614b466157673d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000002849eb11672558700b8a242ea5f877a3a2756f8a6879c5bb885874668ff252fdea822bea50e6af214c630000000000000000000000000000000000000000000000000000000067f8e6cd000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000afbb30322d80b572a7ff5aee89896d555732051d000000000000000000000000afbb30322d80b572a7ff5aee89896d555732051d00000000000000000000000000000000000000000002ee152257ecad790d4cbc000000000000000000000000000000000000000000000000012a2337a255a4f30000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004187d8e83f8d3cf54b5be96da92d7f9be2eb61098ac118100edf2aabc8a3c9137a5b73e1bc74d2936c7210483899d1045fff5446de36a7d8a1a08b8cdfaa4128341c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - - vm.startPrank(userPk); - vm.expectRevert(PendlePTHelper.PendleSwapFailed.selector); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - } - - function test_deleveragePosition_sellDBR() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - uint256 debt = market.debts(address(userPk)); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 10000, - 0 - ); // sell all DBR - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e47660000000000000000000000000000000000000000000028b21966e97b0e57517a00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000001500000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000261d5b9887bee02dd28b0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000001304e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000010400000000000000000000000000000000000000000000000000000000000000d40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000ce00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000760000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001de6312b0ccd57caee1c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003e00000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af0000000000000000000000000000000000000000000001c23f20a6666e93776b000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af000000000000000000000000000000000000000000000000000000023fd6d94e000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba3000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040d90ce4910000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000007d10a8734d985dbb3ad91fce9c48ccc78b9f8b94000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002402fc75a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000040f59b1df7000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000066a9893cc07d91d95644aedd05d03f95e1dba8af0000000000000000000000000000000000000000000003847e414cccdd26eed6000000000000000000000000000000000022d473030f116ddee9f6b43ac78ba30000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000012c0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000c6fdfc08401113d02280e27f53f4f38a19fbccc9000000000000000000000000000000000000000000000000000000047facc58c000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004178b778d0424a735f4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000002ae9380163eb9e200000000000028ec522d336e9fdaf79d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000232cee8d0000a385545d0000000000000000000000000000000000000000000026e081448a75e4a99e6e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000232cee8d0000a385545d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002637b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223139333037372e373230303833393633222c22416d6f756e744f7574555344223a223139333739322e34393634323437383137222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313933323534303132333631383136353436313436323035222c2254696d657374616d70223a313734343333303734322c22526f7574654944223a2230656233346131612d636537382d343533302d613037312d393962616636313261633538222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22566a3369456546436d6b45784b6c35676a43337562353078576836724c647961733856755a3263507771772f31594f334b4662776c5355364d56314e794e4a5a74594e5a5147304a49657a6f494d2f7a4775345a466e74783739354e2b74646d6836315875536f77626c79704e754b3333796b4f6455376248436f4d2f62614b55467852753770417434672f3842332f46416d4f396f5731676c51676a6846504f476431324e5359727658343736666831526f6b467342732b2b55625455494c583255327154694679456b62664d7a77587a30386439787153427a71543777573638305053796336624332327268616f45762b6f50435a3253384d4c757152694e32796a4a5a3361487062437370757462525347742b6b504d62654c385155624d656f344165442f344759424c43592b47565a7354317165466f4e77576150324c71703556683350615650425a2b5663614b466157673d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000002849eb11672558700b8a242ea5f877a3a2756f8a6879c5bb885874668ff252fdea822bea50e6af214c630000000000000000000000000000000000000000000000000000000067f8e6cd000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000afbb30322d80b572a7ff5aee89896d555732051d000000000000000000000000afbb30322d80b572a7ff5aee89896d555732051d00000000000000000000000000000000000000000002ee152257ecad790d4cbc000000000000000000000000000000000000000000000000012a2337a255a4f30000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004187d8e83f8d3cf54b5be96da92d7f9be2eb61098ac118100edf2aabc8a3c9137a5b73e1bc74d2936c7210483899d1045fff5446de36a7d8a1a08b8cdfaa4128341c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - dbr.approve(address(ale), dbr.balanceOf(userPk)); - ale.deleveragePosition( - debt, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - - assertEq(dbr.balanceOf(userPk), 0); - // Dola balance is greater than collateral sold for DOLA minus debt because we also sold DBR - assertGt(DOLA.balanceOf(userPk), 189384718117561736955569 - debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_InsufficientPT_after_swap() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode( - address(market), - (initialDolaAmount * 1.1 ether) / 1 ether, - pendleDataInitial - ) - ); - } - - function test_fail_InsufficientPT_after_mint() public { - _mintInitialDolaAmountToUser(); - bytes - memory pendleMintData = hex"d0f423850000000000000000000000006218d3d6c01f8077f06519d8f5d31f935f3c3c530000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000000000000000000000000a960112b065b45d415c0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b64e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000066000000000000000000000000000000000000000000000000000000000000008a000000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000540000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002c00000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000c6fdfc08401113d02280e27f53f4f38a19fbccc90000000000000000000000000000000000000000000015280b7c36b1c460d75b00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000002950460e2b9529d0e00284a5fa2d7bdf3fa4d72000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001744060f9f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000016334a784e6e8ef000000000000152c023123ac67d66fc5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000000000000000000000000010f001c0e956b978596a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002647b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130303034392e3539363630353032353434222c22416d6f756e744f7574555344223a2239393937322e3732313331353438373937222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223939393831353130383236313931303837383930333733222c2254696d657374616d70223a313734333730323332352c22526f7574654944223a2266313138373235332d356131632d343865352d393933342d326566393932653734393463222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2259524b6a7130684373542f625a774b4d72595267694f416d4d7039716e653470306335496776614a4e4b45625259565475574452594c57344d65386274572f58436167584637524249626b7430775868555039386c6c666f32644945714362544868456737656d4336754c7a69587334766b38656450717365586d6533465132696b7471564851536d576e6355564e2f5a56696550652f6230653539695263374a4c55752f6575312f626c71554d53694a684f62526d644b57716f3663362f642f72576e55474b447469595039547938344e4d674c5855554234634f6d324a4a66377759697956396231314c497875644d34367333742f6638776635516c67626c64544d4949755056334954396647626d794c36344d6754415550672b72765374635a49622f7732696a783678724c652f546f79355230795743686c59483068796a6154636a304838794a4175704d5867614d5672673d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateral( - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleMintData) - ); - } - - function test_withdrawAndConvertFromCollateral_SWAP() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf000000000000000000000000b162b764044697cf03617c2efbcb1f42e31e4766000000000000000000000000000000000000000000001568777a51f893b0d35300000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000ac0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001306bd7c7fac6e94e5eb0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008c4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012803ce5f8fd8089d15500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000169c51f16ceb20b00000000000015902c15a1c36c4259e80000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012803ce5f8fd8089d15500000000000000000000000000000000000000000000136827ad119647d550ea0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012803ce5f8fd8089d15500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002667b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130313838332e3236373639303730313531222c22416d6f756e744f7574555344223a223130313930382e3731333137303936343237222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313031383239323033393039383439363131363535363536222c2254696d657374616d70223a313734333730323730362c22526f7574654944223a2231383566623136362d633136622d343238322d626362302d306163623338626639643331222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2258747579587a36366c49574e69624f4c68436f4c446f336d614e5a4f384853726b4e6f7173627732495a6b536169393037384b547a2f4f6b776553664672615a4236754c43717934324b463442682f4c4454556a367a4a6533744b4a2f4a576b504248764846747644694b4b6a6332337373305561307a79474933687544626330547552386f516645624c2b614a4575776765334d30654444616c30526561537a3546586b4c3265594a4d6b6151717546712f6947775a2f6d6c394e62393453765a704d6a5a6b5244725643426850334a4a66563130787450667a306f4c52737a37575a3333324c73726e3735684d4e756f6b6443354233507a4c6855684b446b6876754d6a4657696a5a4572386d66784c565a6f425a67467676744b344650355443497671315952527170567934715075626d4b644b676a2f6331695a414d53433533312b662b307a6664506f3732617648516d413d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - assertEq(DOLA.balanceOf(userPk), 0); - - vm.startPrank(userPk); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode(address(market), 99000 ether, pendleSwapToDolaData) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - } - - function test_withdrawAndConvertFromCollateral_REDEEM() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf0000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000000000000000000000001568777a51f893b0d3530000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000ab32e707b061de22ac10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008c4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012ba5fb7e19fc3b2689b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000016e35e8e0553a6700000000000015d3ee18a90c7a66fb0f0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012ba5fb7e19fc3b2689b0000000000000000000000000000000000000000000011765813ba7061ebfc0c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012ba5fb7e19fc3b2689b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002667b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130333037332e3730303636333839323535222c22416d6f756e744f7574555344223a223130333037382e3034393837323436383733222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313033303739313135373838343636373931303534303935222c2254696d657374616d70223a313734333730323930302c22526f7574654944223a2233353936393166362d616466392d343435632d383166632d323336363432353264636133222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22594e6159306479445a574176524466743951662b424a49676b31747258372b524d59586a6f6a7577386550593158694470684a30626262756e7043745262447144534935377a7541384f63544f306f67356f38665744664d477535653959484c486450434668502f3050352b4a357a594931656b36747449615153695332673034716b51447451717834454166672b57483876305a5936306c315a3952662f6c326d555577506657584b4c496435616a313665447a5274306b6b7173493776344e7a397641533245367662446268613455502f356f386848413167615067532f7a377477686a7a6a35774c5a594b62705a454c5a5a31524175632f374a4a3133664b37593439737744424e6e656d474a637a784e7a786b7570495941646c7950626d724e783962623731797a2b77686c68744446617435427651456e434a674b6b573258726f514b4576764a4a53556b6973587648773d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - } - - function test_withdrawAndConvertFromCollateral_REDEEM_AFTER_DEADLINE() - public - { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 1000000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf0000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000000000000000000000001568777a51f893b0d3530000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000ab32e707b061de22ac10000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000008c4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012ba5fb7e19fc3b2689b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000016e35e8e0553a6700000000000015d3ee18a90c7a66fb0f0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012ba5fb7e19fc3b2689b0000000000000000000000000000000000000000000011765813ba7061ebfc0c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012ba5fb7e19fc3b2689b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002667b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130333037332e3730303636333839323535222c22416d6f756e744f7574555344223a223130333037382e3034393837323436383733222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a22313033303739313135373838343636373931303534303935222c2254696d657374616d70223a313734333730323930302c22526f7574654944223a2233353936393166362d616466392d343435632d383166632d323336363432353264636133222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22594e6159306479445a574176524466743951662b424a49676b31747258372b524d59586a6f6a7577386550593158694470684a30626262756e7043745262447144534935377a7541384f63544f306f67356f38665744664d477535653959484c486450434668502f3050352b4a357a594931656b36747449615153695332673034716b51447451717834454166672b57483876305a5936306c315a3952662f6c326d555577506657584b4c496435616a313665447a5274306b6b7173493776344e7a397641533245367662446268613455502f356f386848413167615067532f7a377477686a7a6a35774c5a594b62705a454c5a5a31524175632f374a4a3133664b37593439737744424e6e656d474a637a784e7a786b7570495941646c7950626d724e783962623731797a2b77686c68744446617435427651456e434a674b6b573258726f514b4576764a4a53556b6973587648773d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertGt(DOLA.balanceOf(userPk), (initialDolaAmount * 98) / 100); - // Didn't transfer token even if approval granted because after maturity - assertEq(IERC20(pendleYT).balanceOf(userPk), amountToWithdraw); - } - function test_Access_Control_convertToCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertToCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_Access_Control_convertFromCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertFromCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_updateMarketHelper_if_helper_not_address_zero() public { - vm.prank(gov); - ale.updateMarketHelper(address(market), address(1)); - (, , IPendleHelper helper, ) = ale.markets(address(market)); - assertEq(address(helper), address(1)); - } - - function test_updateMarketHelper_if_market_not_set() public { - vm.expectRevert( - abi.encodeWithSelector(ALEV2.MarketNotSet.selector, address(2)) - ); - vm.prank(gov); - ale.updateMarketHelper(address(2), address(1)); - } - - function test_updateMarketHelper_fails_if_helper_not_set() public { - vm.expectRevert(ALEV2.InvalidHelperAddress.selector); - vm.prank(gov); - ale.updateMarketHelper(address(market), address(0)); - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (amountCollat * - oracle.viewPrice(address(pendlePT), 0) * - market.collateralFactorBps()) / - 10_000 / - 1e18; - } -} diff --git a/test/util/aleTests/ALEPendlePTsUSDe31July25.t.sol b/test/util/aleTests/ALEPendlePTsUSDe31July25.t.sol deleted file mode 100644 index f9484f86..00000000 --- a/test/util/aleTests/ALEPendlePTsUSDe31July25.t.sol +++ /dev/null @@ -1,1036 +0,0 @@ -pragma solidity ^0.8.13; - -import {ICurvePool} from "src/interfaces/ICurvePool.sol"; -import {PendlePTHelper, IPendleHelper} from "src/util/PendlePTHelper.sol"; -import "test/marketForkTests/PendlePTsUSDe31July25MarketForkTest.t.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; - -/* - User 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf - ALE 0x4dF2EaA1658a220FDB415B9966a9ae7c3d16e240 - helper 0x4809fE7d314c2AE5b2Eb7fa19C1B166434D29141 - DOLA 0x865377367054516e17014CcdED1e7d814EDC9ce4 - pendlePT 0x3b3fB9C57858EF816833dC91565EFcd85D96f634; -*/ -contract ALEV2PTsUSDe31July25Test is PendlePTsUSDe31July25MarketForkTest { - ALEV2 ale; - address userPk = vm.addr(1); - PendlePTHelper helper; - address userPkEscrow; - - address pendleRouter = address(0x888888888889758F76e7103c6CbF23ABbF58F946); - address pendleYT = address(0xb7E51D15161C49C823f3951D579DEd61cD27272B); - address marketPT = address(0x4339Ffe2B7592Dc783ed13cCE310531aB366dEac); - - // Pendle Router Mock - address pendleYTHolder = - address(0x1BE3d6c73E7Af74a0AB818C4a95bD72f058F320A); - - // Get initial PT - // Swap 100K DOLA for PT (recipient helper) - bytes pendleDataInitial = - hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000014d66f4d5232d4008432000000000000000000000000000000000000000000000abdb569d5eab1e5dfda0000000000000000000000000000000000000000000018b454736c0232c3e94300000000000000000000000000000000000000000000157b6ad3abd563cbbfb5000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000012e288cc00a8b6d0000000000001202923dad50e6051b72000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000000000000000000000001178410dbf281251c1050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002897b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130303030342e3232393736373231323739222c22416d6f756e744f7574555344223a2239393937342e3432323537343434333836222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223835303530303237393439303637313137383631373436222c2254696d657374616d70223a313734383435313332382c22526f7574654944223a2261616638386166382d613331302d343862312d383738652d6463333362633965386231623a63383533393666332d343337662d346164332d386665322d323134663366356538613633222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2243483851742f4c7059775878734b464245754a48366f6d61734f7873306d7163334e7467333968793035487435784649726441752b477343465a52386e45306a41733461736a446b47586343795673735333554a4c70496f645247375a6c54776b6a3457514e4e5939684d6244776466314d543769616c776b504167565a467574706a5167302b4c52452f6466396b66673735774f5056417533524b574e567a4c4f4462482b6938476e7354415866313864736555776f3348644f7a374b56524f4750744d6e4b5634386d6b6a4d52664731756565314a35703361335a4e4a7173657047615349564a4c7737466f41596265306d766d36572b7669664d70613638723850586e6b30314a50562f6b733776397476646f7a31365a41534676672b4c74654a465254363744516d49744e416c5630422b78507773367133427179525a79334d6e71746c4d615873673878513664676475513d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - // Swap maxBorrowAmount from 100k DOLA to PT deposit (recipient helper) - bytes pendleSwapData = - hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000012ab8bf02765e948e8aa00000000000000000000000000000000000000000000099faf0f95a0bcde52fb000000000000000000000000000000000000000000001622790a3e8b4bff587500000000000000000000000000000000000000000000133f5e1f2b4179bca5f7000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000012f979fb35c93edf349d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012f979fb35c93edf349d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000010ebfc7bbc75e6200000000000010234dd57106dfec65e1000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012f979fb35c93edf349d000000000000000000000000000000000000000000000fa75d6b32c18c67ddb30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012f979fb35c93edf349d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238373637372e3630343435393930393337222c22416d6f756e744f7574555344223a2238393537342e3438373632353636323237222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223736323039313038323831363739343538313030373035222c2254696d657374616d70223a313734383435323032362c22526f7574654944223a2233636161396166372d366539622d343735642d616130342d3866393030663333346564303a34383163326530642d643261632d343835652d623439332d633764386536623064303937222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22567a7469556e64744d675479354d4962455370377950646d316d3355523651444b7238475539616269716c4e784236417a56776c6b356a5942416f693562497a464558314c2b444838542b5663476a656459696a3656646854336467384c396c2b346659664e71796d78764d683842545a52344747304e7355663764394e394f484f66754f565537674d72544b68334551764e4f504e6d4c476a636d544334784e4d575948594f684441324b654d4b42547a2b397448354941447632304d46705346303767433433764a4f6d344e74506a563558794f42565a32353265747a5070366e303544585933664c394b412f78477776647237644c79412f66704a425046714a57426f74682b4365775768343067487a494e2f69766e6b4e45463751346e50745869343949696e564f4a7455356e62536c69694145436a7173727a435a476a6f496c714a6a6e4d3647726d756f7461735352413d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 initialDolaAmount = 100000 ether; // 100K DOLA - - function setUp() public override { - super.setUp(); - - vm.startPrank(gov); - - ale = ALEV2(payable(aleV2Addr)); - - helper = PendlePTHelper( - pendlePTHelperAddr - ); - ale.setMarket(address(market), address(DOLA), address(helper), false); - helper.setMarket(address(market), address(pendlePT), pendleYT); - borrowController.allow(address(ale)); - DOLA.mint(address(market), 1000000 ether); - vm.stopPrank(); - - userPkEscrow = address(market.predictEscrow(userPk)); - - vm.prank(userPk); - DOLA.approve(address(helper), initialDolaAmount); - } - - function _mintInitialDolaAmountToUser() internal { - vm.prank(gov); - DOLA.mint(userPk, initialDolaAmount); - } - function test_leveragePosition_Mint_PT_and_YT_with_DOLA_router() public { - _mintInitialDolaAmountToUser(); - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Mint PT and YT with maxBorrowAmount (swap recipient is the helper, then distribute PT to ALE and YT to userPk) - bytes - memory pendleMintData = hex"d0f423850000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d29141000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b000000000000000000000000000000000000000000001262826f0b6f86ede6960000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000012f979fb35c93edf349d0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012f979fb35c93edf349d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000010ebfa260c8b84800000000000010234b9b714ebdeffe1e000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f9460000000000000000000000000000000000000000000012f979fb35c93edf349d000000000000000000000000000000000000000000000fa75b424ca0dc1450170000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000012f979fb35c93edf349d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238393639352e3835333636333032323135222c22416d6f756e744f7574555344223a2238393630322e3736333836383236313237222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223736323038393437383431323531343030343832333334222c2254696d657374616d70223a313734383435333136332c22526f7574654944223a2261306432376132302d663431622d343032322d383862622d3861336162386365616332383a36626262373066632d303334662d343464612d616137662d616337346430633038386531222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22524b50635631686f4c766e5172777733342f3572485148787a364d6f6e68316a4b4858586c4f753666723049323168566e56455a3478344e674b4d31564d4875587458423253536c7a34545842534f3575434346724e6f61574464496e54756b457a43657a464b50746d6a7966645368626969316a46696a383576374656716b57764e32784f4c6d782f3730797439734b314235337a78773856596f4b63646b794342557859675251715754754c756358665435342f682f7554334b51644f6466522b79646c65586b3763476e373867793041355959794667423151504f47594d6e6247437532432b597231506f35504d524d343378304f4f67475a4d4e487a43594b5a6f4a35324c4d2f454c6b5545554d51497039415a73576a753053714a4662774d37663732537970445675756a65385056315241654733557144654637524a31735244534f5a66755133443846595138344d513d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleMintData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 0.99888 ether) / 1 ether), //0.99888 ratio minted PT and YT to DOLA - 1e14 // 0.01% Delta - ); - - assertApproxEqRel( - IERC20(pendleYT).balanceOf(userPk), - ((maxBorrowAmount * 0.9988 ether) / 1 ether), // 0.9988 ratio minted PT and YT to DOLA - 1e14 // 0.01% Delta - ); - } - - function test_leveragePosition_Swap_DOLA_for_PT_router() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.01444 ether) / 1 ether), // approx 1.01444 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_leveragePosition_buyDBR_Swap_DOLA_for_PT() public { - _mintInitialDolaAmountToUser(); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 15 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 90) / 100, - 0 - ); - - vm.prank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), 0, pendleSwapData), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + ((maxBorrowAmount * 1.0144 ether) / 1 ether), // approx 1.0144 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - assertEq(DOLA.balanceOf(address(ale)), 0); - assertGt(dbr.balanceOf(userPk), (dbrAmount * 90) / 100); - } - - function test_depositAndLeveragePosition_DOLA() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - // Swap DOLA for PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleInitialDepositData = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000012c0cb6d576cb8794e7f0000000000000000000000000000000000000000000009aaa2ebd355128db45700000000000000000000000000000000000000000000163ba9eb32dd4445eb9400000000000000000000000000000000000000000000135545d7a6aa251b68ae000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000010ff19209971b32000000000000103587d29ad5f2b86551000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000fb90b6d911216f56c840000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238373938382e3139333636313837373139222c22416d6f756e744f7574555344223a2238393736382e3734373436373130323935222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223736353435333238323137303034383530303436323839222c2254696d657374616d70223a313734383435323233342c22526f7574654944223a2238396562656564642d393536662d343938652d383339302d3234663735313035636335663a38626566316330332d653136302d346432362d386666322d363361383933373134313439222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224b7342547659714975636a6735776a584a6b49387049774f7a377157437069496f62515535374b584845523641426e384b3870756c34644a576e464255337a69386f3934366370466f73585952345943376a557a386f707a6b743739623862674a5044716849326e35423633383662774373305265316c6b3874545075436d36456f6878676d32627a38544f2f6f6e435269396f724c3042345939535769476d772f796671446d2b524d3937724248387a39597059416b5331744c547a4a44736d54464144686446526a3850764d6c48466a5655654d354f6a4f555a674c35454472344d664f3572766b4253522f4a4454386f7962326b385347362b6555632f4e61734d48514c4154756b326b4d46474c7649574b30492b37484335644a4b686d4d726c364d366f654d384d534a6d31722b6f756b7a6c4254464f5339707159467345385259716747616643773653324749736e69513d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount - initialDolaDeposit); - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleInitialDepositData) - ); - assertEq(DOLA.balanceOf(address(helper)), 0); - vm.stopPrank(); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - // Swap DOLA for PT (maxBorrowAmount + initialDolaDeposit) (receiver is the helper) - bytes - memory pendleSwapDataDeposit = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000012e30d4759bde13d515c0000000000000000000000000000000000000000000009bc4b7697d35c59ab42000000000000000000000000000000000000000000001664472a5d32ee01704b00000000000000000000000000000000000000000000137896ed2fa6b8b35684000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001331de779a04774d1f030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001331de779a04774d1f0300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000111e45f3365b835000000000000105342eda602de0f038b000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001331de779a04774d1f03000000000000000000000000000000000000000000000fd5e23351abbdcc00e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001331de779a04774d1f0300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239303634322e3339363631313739303638222c22416d6f756e744f7574555344223a2239303336332e3535383535323933383539222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223737303933373636313737333339343435353433383139222c2254696d657374616d70223a313734383435323331322c22526f7574654944223a2264313137623131632d313865372d343763642d626235612d6233376463623764653665633a66656563663635612d326538352d343632612d623934642d653966303838323530656662222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224161646264617a466670655334444e363876344d625953566b4948743569657244554d7844534e4e584a6247424f704250635358366b6e5659674e4c654169714879324e4b343761446167366b466449776c4f316354326753584849426c45786b6876476571522f6a306756442b747133677167632b334572354952794543714848427a72347939414b503057764e63546d426767504865782b7872472b44334161316143564f704e4a365768494c7a73695978546844364c55614c73654746464676704d70794e4a46576239314a5570617744652f39374b742f716f6c444552654a3076324f332b7a474d61645739373168314558582f6a41356b7158554d6479576f727876467a3764465a30307753425567516359442f4c4b7962486937376d6a555a6d6c4b375a30532f584b755a5230495249747a357a6567396b7750546f344b4a666b6b4b70386d5a7662653863737337513d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - DOLA.approve(address(ale), initialDolaDeposit); - ale.depositAndLeveragePosition( - initialDolaDeposit, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapDataDeposit), - dbrData, - false - ); - - assertEq(DOLA.balanceOf(userPk), 0); - - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + - (((maxBorrowAmount + initialDolaDeposit) * 1.01444 ether) / - 1 ether), // approx 1.01444 ratio PT/DOLA - 1e14 // 0.01% Delta - ); - } - - function test_depositAndLeveragePosition_PT() public { - _mintInitialDolaAmountToUser(); - - uint256 initialDolaDeposit = initialDolaAmount / 10; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), initialDolaAmount); - // Swap DOLA for PT (initialDolaDeposit) (receiver is the helper, recipient is the user which will be depositing them) - bytes - memory pendleDataInitialPT = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000002157b5913a8f8fe91d2000000000000000000000000000000000000000000000112fd9a1f3f58c28fc90000000000000000000000000000000000000000000002787a7c14ab4c25e44e000000000000000000000000000000000000000000000225fb343e7eb1851f92000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000001e37a16df6aeae00000000000001cd145af82952f0814e000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000021e19e0c9bab24000000000000000000000000000000000000000000000000001bf3f43c2a2f6d9ee100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000021e19e0c9bab240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2231303031392e303231383839313433323239222c22416d6f756e744f7574555344223a22393938372e313233393031333335383534222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a2238353035343135373735343635313334363533373734222c2254696d657374616d70223a313734383435323436372c22526f7574654944223a2261323231366530352d353630642d346333622d616337632d6539666266366233353433633a32313161653434662d366434632d343139342d623164382d623232356364376438656633222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22417957486d4b6a786b44376f77306a4a66646748386a7a3831697534376f5a576f775951512f534664695a442f57666b43356651394831305734447853455a6c3365465153356c6c55394b2f336470766c38397043624672555167704337454f50703561774b386b314845384535495252687a56535a31724f726e4456763431723864786e7867332b646c54526e69467a4333767a523641464f47716230634747386d6f476766373852672b7973416d4d374651486e77394e6b79305a5056667a4c534f6563366f70317236462b433259524a4d6a35745a6668524651746830335663774c4e647047377258374c316d484d4f4c4f3138586457676b7966636b3667556e7a695a39497548456d5636397130596a7076666e614a6a4a353243646b7869685648652b46476563633864355261394265707969684937324a6a3437634f4b456e78636338756870553046734d4e2b7257773d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 initialLpAmount = helper.convertToCollateral( - initialDolaDeposit, - abi.encode(address(market), 0, pendleDataInitialPT) - ); - - // Swap DOLA to PT (initialDolaAmount - initialDolaDeposit) (receiver is the helper) - bytes - memory pendleDataInitialDeposit = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000012c0cb5af4f69446b1c00000000000000000000000000000000000000000000009aaa2e259523c98906800000000000000000000000000000000000000000000163ba9d56709f1c54c2300000000000000000000000000000000000000000000135545c4b2a4793120d1000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e71790444000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000010ff189be52de05000000000000103587540c5bf447f64f000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000130ee8e7179044400000000000000000000000000000000000000000000000000fb90af2ce8c6545cda30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000130ee8e717904440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2239303137312e3139373030323238393036222c22416d6f756e744f7574555344223a2238393838302e3036343831383238393537222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223736353435323932353934353033313733353335333131222c2254696d657374616d70223a313734383435323530362c22526f7574654944223a2238343538346366332d396564362d343261382d616161662d6135313033336331336331323a65303230316661362d333237342d343231352d383836362d633062376465343236626136222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a22414968464f2f354d6c306b2f4950672b5149797877396676534339466c4d61666f6634486c4374484f4236586130783562424974582f655336427a337533757739516641646f2b773759546b48305435506b6e564c5641726e483442706a7a655a65454e32705a71306e734b4c436b51546a6f384752704142466253437246516e4e58436f56736c337474595a6d776e57357a4e6c42482b6d663163442b56464e5a5963686b484730632f4e6f545a777a36324b356b6133706269366e394c50313546556c30754d6267336f5735486f45524464566c3762724d674e7831754555397552373958424257586e76377378505972626332686c744e2f584743527a78506a6f645670576f642b316e374651696a63446132755744736870484946335457395973596e73786675473366353162565563514d47546833473638684f6562677a386a797a4b372b4e6c4f3065416170345772673d3d227d7d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - helper.convertToCollateralAndDeposit( - initialDolaAmount - initialDolaDeposit, - userPk, - abi.encode(address(market), 0, pendleDataInitialDeposit) - ); - vm.stopPrank(); - - uint256 lpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - // Swap DOLA Max borrow amount to PT (receiver is the helper) - bytes - memory pendleDataLeverage = hex"c81f847a0000000000000000000000004809fe7d314c2ae5b2eb7fa19c1b166434d291410000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000010cde8a8ddbafc2ccc830000000000000000000000000000000000000000000008a8b0770218cc6846590000000000000000000000000000000000000000000013ebed8098b0365c179700000000000000000000000000000000000000000000115160ee043198d08cb3000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000b80000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001113b44baca1ae7597ca0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b50000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e4e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff00000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040d90ce491000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000100000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a349700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001113b44baca1ae7597ca000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000f3ac8083c8bbe90000000000000e862aebfaddb54ee604000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000001113b44baca1ae7597ca000000000000000000000000000000000000000000000e169f64e68a41c9f8b70000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006e4141d33021b52c91c28608403db4a0ffb50ec60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000001113b44baca1ae7597ca00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a2238303934332e3639353831313931313038222c22416d6f756e744f7574555344223a2238303634382e3231343533323637373635222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223638353838303837333037343531333639343438393634222c2254696d657374616d70223a313734383435323539312c22526f7574654944223a2232313937363730322d383131642d343738342d393038622d3637666666393735356233343a36636630396634342d353361342d343830632d623730312d396232613666633835373639222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a224e576b2b553045446133777a347643784b6176764666416b3068534c5a754259546942453564454571435555626852473142515833424a4362426a584c72482b346942434c32436573326567493846394b4f534a6e39714a6b3663425078485753713639355770437744394a684150772f6c30373433554f57655a46646765314567354b786f6a6542524c4e6a75493573324478744a425677514d4136376c7453564456464b63352b766a52496862717736505a75693454646532414f50416a3043314f6c374a72386269723849734775526350624c74646e46735a56576e617075456b75627142455970735331547a545a4c6132503450687a517a785273744c4b526331462b6c597833476338697944417777736e5742494c67306167453345694d44484c5a62555967555937736a6436436449544c51795a7238484a4a45455a545130584f6352456e466176734b443333634f773d3d227d7d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000360000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000019434323570701bea152895885e31962731676e80cc20293f5fb2598c990f59327f3253e63e5641c6000000000000000000000000000000000000000000000000000000006840672b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b0000000000000000000000002082c5a3c08a4d5ade0ce779e24b463daa2416460000000000000000000000002082c5a3c08a4d5ade0ce779e24b463daa241646000000000000000000000000000000000000000000002b083f9649026ba05c7a000000000000000000000000000000000000000000000000013f2da4f04749960000000000000000000000000000000000000000000000000c7d713b49da0000000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041432609fea195c39bd55701172dded30fbe3dacb0e43a80cf784c14fc339832e119c4ce6ddd429b83ff2537a9930031ef010324676c7698bdaa04ba028526bde61c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - IERC20(pendlePT).approve(address(ale), initialLpAmount); - ale.depositAndLeveragePosition( - initialLpAmount, - maxBorrowAmount, - address(market), - address(0), - swapData, - permit, - abi.encode(address(market), uint(100000), pendleDataLeverage), - dbrData, - true - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - lpAmount + - initialLpAmount + - ((maxBorrowAmount * 1.0144 ether) / 1 ether), - 1e14 // 0.01% Delta - ); - } - - function test_deleveragePosition_Redeem_with_PT_and_YT_router() public { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b00000000000000000000000000000000000000000000286ef7733401aae0c9c90000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000273bfee61aa87eedcf2a0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000da4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000231d957fa3a12050a333000000000000000000000000000000000000000000002804e028256e6cfcce30000000000000000000000000000000000000000000002941ba39391031049fc86d3b1d887e484b8e860f8e7718d2deea00000000000000000000000001589603000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000005c0000000000000004c000000000000200816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb79000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001ccb7578062d1560d7be00000000000000000000000000000000000000000000000000000000000000010000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000005c0000000000000004c000000000000070816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000065220079d740aefcb750000000000000000000000000000000000000000000000000000000000000001000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b1000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - assertApproxEqRel( - DOLA.balanceOf(userPk), - 190637534582110192477893 - debt, - 0.01 ether - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - assertEq(market.debts(userPk), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_AFTER_DEADLINE_deleveragePosition_Redeem_with_PT_and_YT_router() - public - { - test_leveragePosition_Mint_PT_and_YT_with_DOLA_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer( - userPk, - amountToWithdraw - IERC20(pendleYT).balanceOf(userPk) - ); - vm.stopPrank(); - // Redeem PT and YT using all collateral (amountToWithdraw) (receiver is the ALE) - bytes - memory pendleRedeemData = hex"47f1de220000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e240000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b00000000000000000000000000000000000000000000286ef7733401aae0c9c90000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000273bfee61aa87eedcf2a0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000da4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000231d957fa3a12050a333000000000000000000000000000000000000000000002804e028256e6cfcce30000000000000000000000000000000000000000000002941ba39391031049fc86d3b1d887e484b8e860f8e7718d2deea00000000000000000000000001589603000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000005c0000000000000004c000000000000200816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb79000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001ccb7578062d1560d7be00000000000000000000000000000000000000000000000000000000000000010000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000005c0000000000000004c000000000000070816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000065220079d740aefcb750000000000000000000000000000000000000000000000000000000000000001000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b1000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(userPk); - vm.startPrank(userPk); - uint256 ytBalBefore = IERC20(pendleYT).balanceOf(userPk); - - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleRedeemData), - dbrData - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), ytBalBefore); - } - - function test_deleveragePosition_Swap_PT_to_DOLA_router() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e2400000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000028ba9e62e99e81ca1c8100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000fa0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000026e5766667a4fd2c5b910000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000da4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000022d0242d5b360fd21c5c0000000000000000000000000000000000000000000027ac9c9ba72849eaaf510000000000000000000000000000000000000000000028e6bbd7e3bff7bfcc7e6cec4b90456446c4a76ea308b9d3da09000000000000000000000000015895aa000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000005c0000000000000004c000000000000200816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb79000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c8bf4b48d5a69220d0400000000000000000000000000000000000000000000000000000000000000010000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000005c0000000000000004c000000000000070816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006442f78cddba6b00f580000000000000000000000000000000000000000000000000000000000000001000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b1000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000028525a3e155cec02f86026709a856fd972db053626c824d8eb56f06d62b59b10c7aadfd65370ec91c932000000000000000000000000000000000000000000000000000000006838822e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b0000000000000000000000000d8f9207ed58fbc8b3e3af2a9f062b7c0419de530000000000000000000000000d8f9207ed58fbc8b3e3af2a9f062b7c0419de53000000000000000000000000000000000000000000005021b4fe1016f7e3f4590000000000000000000000000000000000000000000000000142198cc54f18320000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004129dce8887b455040e3c623e38e023b91ae761ce4f6040134657151e340de14e95e9635b057ce2b3b8dc8265cdfdbeb15cb8a75eea5285c21c8a49124aad92af71b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - vm.startPrank(userPk); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - // Dola balance is equal to collateral sold for DOLA minus debt - assertEq(DOLA.balanceOf(userPk), 189362915206942108645478 - debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_AFTER_DEADLINE_deleveragePosition_Swap_PT_to_DOLA_router() - public - { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 1000000 ether); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e2400000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000028ba9e62e99e81ca1c8100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000fa0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000026e5766667a4fd2c5b910000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000da4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000022d0242d5b360fd21c5c0000000000000000000000000000000000000000000027ac9c9ba72849eaaf510000000000000000000000000000000000000000000028e6bbd7e3bff7bfcc7e6cec4b90456446c4a76ea308b9d3da09000000000000000000000000015895aa000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000005c0000000000000004c000000000000200816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb79000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c8bf4b48d5a69220d0400000000000000000000000000000000000000000000000000000000000000010000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000005c0000000000000004c000000000000070816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006442f78cddba6b00f580000000000000000000000000000000000000000000000000000000000000001000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b1000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000028525a3e155cec02f86026709a856fd972db053626c824d8eb56f06d62b59b10c7aadfd65370ec91c932000000000000000000000000000000000000000000000000000000006838822e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b0000000000000000000000000d8f9207ed58fbc8b3e3af2a9f062b7c0419de530000000000000000000000000d8f9207ed58fbc8b3e3af2a9f062b7c0419de53000000000000000000000000000000000000000000005021b4fe1016f7e3f4590000000000000000000000000000000000000000000000000142198cc54f18320000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004129dce8887b455040e3c623e38e023b91ae761ce4f6040134657151e340de14e95e9635b057ce2b3b8dc8265cdfdbeb15cb8a75eea5285c21c8a49124aad92af71b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - uint256 debt = market.debts(address(userPk)); - - vm.startPrank(userPk); - vm.expectRevert(PendlePTHelper.PendleSwapFailed.selector); - ale.deleveragePosition( - debt, // dola redeemed to be deleveraged (repay debt) - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - } - - function test_deleveragePosition_sellDBR() public { - test_leveragePosition_Swap_DOLA_for_PT_router(); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - uint256 debt = market.debts(address(userPk)); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit( - block.timestamp, - v, - r, - s - ); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 10000, - 0 - ); // sell all DBR - bytes memory swapData; - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the ALE) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000004df2eaa1658a220fdb415b9966a9ae7c3d16e2400000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac0000000000000000000000000000000000000000000028ba9e62e99e81ca1c8100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000fa0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000026e5766667a4fd2c5b910000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000da4e3ead59e00000000000000000000000000c600b30fb0400701010f4b080409018b9006e00000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000022d0242d5b360fd21c5c0000000000000000000000000000000000000000000027ac9c9ba72849eaaf510000000000000000000000000000000000000000000028e6bbd7e3bff7bfcc7e6cec4b90456446c4a76ea308b9d3da09000000000000000000000000015895aa000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000005c0000000000000004c000000000000200816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb79000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c8bf4b48d5a69220d0400000000000000000000000000000000000000000000000000000000000000010000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c5102003040001068000000000000000000000000000005c0000000000000004c000000000000070816c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b10000000000000000000000000655977feb2f289a4ab78af67bab0d17aab84367000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006442f78cddba6b00f580000000000000000000000000000000000000000000000000000000000000001000000000000000000000000d29f8980852c2c76fc3f6e96a7aa06e0bedcc1b1000000000000000000000000ff17dab22f1e61078aba2623c89ce6110e878b3c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000028525a3e155cec02f86026709a856fd972db053626c824d8eb56f06d62b59b10c7aadfd65370ec91c932000000000000000000000000000000000000000000000000000000006838822e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b0000000000000000000000000d8f9207ed58fbc8b3e3af2a9f062b7c0419de530000000000000000000000000d8f9207ed58fbc8b3e3af2a9f062b7c0419de53000000000000000000000000000000000000000000005021b4fe1016f7e3f4590000000000000000000000000000000000000000000000000142198cc54f18320000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004129dce8887b455040e3c623e38e023b91ae761ce4f6040134657151e340de14e95e9635b057ce2b3b8dc8265cdfdbeb15cb8a75eea5285c21c8a49124aad92af71b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - vm.startPrank(userPk); - dbr.approve(address(ale), dbr.balanceOf(userPk)); - ale.deleveragePosition( - debt, - address(market), - address(0), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market), uint(100000), pendleSwapToDolaData), - dbrData - ); - - assertEq(SimpleERC20Escrow(userPkEscrow).balance(), 0); - - assertEq(dbr.balanceOf(userPk), 0); - // Dola balance is greater than collateral sold for DOLA minus debt because we also sold DBR - assertGt(DOLA.balanceOf(userPk), 189384718117561736955569 - debt); - assertEq(market.debts(address(userPk)), 0); - assertEq(DOLA.balanceOf(address(ale)), 0); - } - - function test_fail_InsufficientPT_after_swap() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode( - address(market), - (initialDolaAmount * 1.1 ether) / 1 ether, - pendleDataInitial - ) - ); - } - - function test_fail_InsufficientPT_after_mint() public { - _mintInitialDolaAmountToUser(); - bytes - memory pendleMintData = hex"d0f423850000000000000000000000006218d3d6c01f8077f06519d8f5d31f935f3c3c530000000000000000000000001de6ff19fda7496ddc12f2161f6ad6427c52abbe000000000000000000000000000000000000000000000a960112b065b45d415c0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000313e7ef7d52f5c10ac04ebaa4d33cdc68634c21200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b64e21fd0e900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000066000000000000000000000000000000000000000000000000000000000000008a000000000000000000000000000000000000000000000000000000000000005a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000000000000000000007fffffff0000000000000000000000000000000000000000000000000000000000000540000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002c00000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000038de22a3175708d45e7c7c64cd78479c8b56f76e000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004088e563110000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000c6fdfc08401113d02280e27f53f4f38a19fbccc90000000000000000000000000000000000000000000015280b7c36b1c460d75b00000000000000000000000066a1e37c9b0eaddca17d3662d6c05f4decf3e110000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca30000000000000000000000000000000000000000000000000000000000000040d90ce49100000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000002950460e2b9529d0e00284a5fa2d7bdf3fa4d72000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001744060f9f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000016334a784e6e8ef000000000000152c023123ac67d66fc5000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000004c9edd5852cd905f086c759e8383e09bff1e68b3000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000888888888889758f76e7103c6cbf23abbf58f94600000000000000000000000000000000000000000000152d02c7e14af68000000000000000000000000000000000000000000000000010f001c0e956b978596a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000f4a1d7fdf4890be35e71f3e0bbc4a0ec377eca3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000152d02c7e14af680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002647b22536f75726365223a2250656e646c65222c22416d6f756e74496e555344223a223130303034392e3539363630353032353434222c22416d6f756e744f7574555344223a2239393937322e3732313331353438373937222c22526566657272616c223a22222c22466c616773223a302c22416d6f756e744f7574223a223939393831353130383236313931303837383930333733222c2254696d657374616d70223a313734333730323332352c22526f7574654944223a2266313138373235332d356131632d343865352d393933342d326566393932653734393463222c22496e74656772697479496e666f223a7b224b65794944223a2231222c225369676e6174757265223a2259524b6a7130684373542f625a774b4d72595267694f416d4d7039716e653470306335496776614a4e4b45625259565475574452594c57344d65386274572f58436167584637524249626b7430775868555039386c6c666f32644945714362544868456737656d4336754c7a69587334766b38656450717365586d6533465132696b7471564851536d576e6355564e2f5a56696550652f6230653539695263374a4c55752f6575312f626c71554d53694a684f62526d644b57716f3663362f642f72576e55474b447469595039547938344e4d674c5855554234634f6d324a4a66377759697956396231314c497875644d34367333742f6638776635516c67626c64544d4949755056334954396647626d794c36344d6754415550672b72765374635a49622f7732696a783678724c652f546f79355230795743686c59483068796a6154636a304838794a4175704d5867614d5672673d3d227d7d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - vm.prank(userPk); - vm.expectRevert(PendlePTHelper.InsufficientPT.selector); - helper.convertToCollateral( - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleMintData) - ); - } - - function test_withdrawAndConvertFromCollateral_SWAP() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleSwapToDolaData = hex"594a88cc0000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf0000000000000000000000004339ffe2b7592dc783ed13cce310531ab366deac00000000000000000000000000000000000000000000157b7e3503effccd1e6e00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000009a0000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000001484561f2e48473ed24f0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000007a4e3ead59e000000000000000000000000000010036c0190e009a000d0fc3541100a07380a0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000125caff60701296f6fe00000000000000000000000000000000000000000000014ed6215907cec82a351000000000000000000000000000000000000000000001593134da76e50323986aa2cab75872942be94a0d940b55a3515000000000000000000000000015895c0000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000060016c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb79000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000125caff60701296f6fe000000000000000000000000000000000000000000000000000000000000000010000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9b3e2c3ec88b1b4c0cd853f4321000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000015447f814904a0ac590126709a856fd972db053626c824d8eb56f06d62b59b10c7aadfd65370ec91c932000000000000000000000000000000000000000000000000000000006838822e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b0000000000000000000000000d8f9207ed58fbc8b3e3af2a9f062b7c0419de530000000000000000000000000d8f9207ed58fbc8b3e3af2a9f062b7c0419de53000000000000000000000000000000000000000000005021b4fe1016f7e3f4590000000000000000000000000000000000000000000000000142198cc54f18320000000000000000000000000000000000000000000000000c7d713b49da000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004129dce8887b455040e3c623e38e023b91ae761ce4f6040134657151e340de14e95e9635b057ce2b3b8dc8265cdfdbeb15cb8a75eea5285c21c8a49124aad92af71b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - assertEq(DOLA.balanceOf(userPk), 0); - - vm.startPrank(userPk); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode(address(market), 99000 ether, pendleSwapToDolaData) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - } - - function test_withdrawAndConvertFromCollateral_REDEEM() public { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 20000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b00000000000000000000000000000000000000000000157b7e3503effccd1e6e0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000014d8b2376c0c4f34d08f0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000007a4e3ead59e000000000000000000000000000010036c0190e009a000d0fc3541100a07380a0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000012a82f915616e86042710000000000000000000000000000000000000000000015436e19cf7d321207ee0000000000000000000000000000000000000000000015ebc8994a07aa61c38fff14d7f8cb0343b6805ffaa5c01bc7ae00000000000000000000000001589610000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000060016c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb79000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012a82f915616e860427100000000000000000000000000000000000000000000000000000000000000010000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - uint256 dolaAmount = helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertEq(DOLA.balanceOf(userPk), dolaAmount); - assertEq(IERC20(pendleYT).balanceOf(userPk), 0); - } - - function test_withdrawAndConvertFromCollateral_REDEEM_AFTER_DEADLINE() - public - { - _mintInitialDolaAmountToUser(); - - vm.prank(userPk); - helper.convertToCollateralAndDeposit( - initialDolaAmount, - userPk, - abi.encode(address(market), 0, pendleDataInitial) - ); - - gibDBR(userPk, 1000000 ether); - - uint256 amountToWithdraw = SimpleERC20Escrow(userPkEscrow).balance(); - vm.warp(block.timestamp + 180 days); - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IPendleHelper.Permit memory permit = IPendleHelper.Permit( - block.timestamp, - v, - r, - s - ); - - // Swap all collateral (amountToWithdraw) to DOLA (receiver is the User) - bytes - memory pendleRedeemToDolaData = hex"47f1de220000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf000000000000000000000000b7e51d15161c49c823f3951d579ded61cd27272b00000000000000000000000000000000000000000000157b7e3503effccd1e6e0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000014d8b2376c0c4f34d08f0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000fe6228a3866426e96611ed7a3d0dee918244fcb300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000060000000000000000000000006a000f20005980200259b80c51020030400010680000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000007a4e3ead59e000000000000000000000000000010036c0190e009a000d0fc3541100a07380a0000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a3497000000000000000000000000865377367054516e17014ccded1e7d814edc9ce40000000000000000000000000000000000000000000012a82f915616e86042710000000000000000000000000000000000000000000015436e19cf7d321207ee0000000000000000000000000000000000000000000015ebc8994a07aa61c38fff14d7f8cb0343b6805ffaa5c01bc7ae00000000000000000000000001589610000000000000000000000000888888888889758f76e7103c6cbf23abbf58f946000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000060016c6521dff6bab339122a0fe25a9116693265353000005a0048400000000000300000000000000000000000000000000000000000000000000000000c872a3c50000000000000000000000009d39a5de30e57443bff2a8307a4256c8797a34970000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb79000000000000000000000000a3931d71877c0e7a3148cb7eb4463524fec27fbd0000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f710000000000000000000000000865377367054516e17014ccded1e7d814edc9ce4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012a82f915616e860427100000000000000000000000000000000000000000000000000000000000000010000000000000000000000003cef1afc0e8324b57293a6e7ce663781bbefbb790000000000000000000000008b83c4aa949254895507d09365229bc3a8c7f7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000f20005980200259b80c510200304000106800000000000000000000000000000000000000000000000000000000"; - vm.startPrank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), amountToWithdraw); - - helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode( - address(market), - (initialDolaAmount * 98) / 100, - pendleRedeemToDolaData - ) - ); - assertGt(DOLA.balanceOf(userPk), (initialDolaAmount * 98) / 100); - // Didn't transfer token even if approval granted because after maturity - assertEq(IERC20(pendleYT).balanceOf(userPk), amountToWithdraw); - } - function test_Access_Control_convertToCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertToCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_Access_Control_convertFromCollateral_ALE() public { - vm.expectRevert(PendlePTHelper.NotALE.selector); - helper.convertFromCollateral( - userPk, - initialDolaAmount, - abi.encode(address(market), initialDolaAmount, pendleDataInitial) - ); - } - - function test_updateMarketHelper_if_helper_not_address_zero() public { - vm.prank(gov); - ale.updateMarketHelper(address(market), address(1)); - (, , IPendleHelper helper, ) = ale.markets(address(market)); - assertEq(address(helper), address(1)); - } - - function test_updateMarketHelper_if_market_not_set() public { - vm.expectRevert( - abi.encodeWithSelector(ALEV2.MarketNotSet.selector, address(2)) - ); - vm.prank(gov); - ale.updateMarketHelper(address(2), address(1)); - } - - function test_updateMarketHelper_fails_if_helper_not_set() public { - vm.expectRevert(ALEV2.InvalidHelperAddress.selector); - vm.prank(gov); - ale.updateMarketHelper(address(market), address(0)); - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (amountCollat * - oracle.viewPrice(address(pendlePT), 0) * - market.collateralFactorBps()) / - 10_000 / - 1e18; - } -} diff --git a/test/util/aleTests/ALEPendlePTsUSDeMock.t.sol b/test/util/aleTests/ALEPendlePTsUSDeMock.t.sol deleted file mode 100644 index 43b8c34e..00000000 --- a/test/util/aleTests/ALEPendlePTsUSDeMock.t.sol +++ /dev/null @@ -1,568 +0,0 @@ -pragma solidity ^0.8.13; - -import {PendlePTHelper} from "src/util/PendlePTHelper.sol"; -import "test/marketForkTests/PendlePTsUSDe27Mar25MarketForkTest.t.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {SimpleERC20Escrow} from "src/escrows/SimpleERC20Escrow.sol"; -import {MockPendleRouter} from "test/mocks/MockPendleRouter.sol"; - -interface IFlashMinter { - function setMaxFlashLimit(uint256 _maxFlashLimit) external; -} - -contract ALEV2PTsUSDeMockTest is PendlePTsUSDe27Mar25MarketForkTest { - MockPendleRouter.ApproxParams emptyApprox; - MockPendleRouter.TokenInput emptyTokenInput; - MockPendleRouter.TokenOutput emptyTokenOutput; - MockPendleRouter.LimitOrderData emptyLimit; - ALEV2 ale; - IFlashMinter flash; - address userPk = vm.addr(1); - PendlePTHelper helper; - address userPkEscrow; - MockPendleRouter mockRouter; - - address pendleRouter = address(0x888888888889758F76e7103c6CbF23ABbF58F946); - address pendleYT = address(0x96512230bF0Fa4E20Cf02C3e8A7d983132cd2b9F); - address marketPT = address(0xcDd26Eb5EB2Ce0f203a84553853667aE69Ca29Ce); - - // Pendle Router Mock - address pendleYTHolder = - address(0x9844c3688dAaA98De18fBe52499A6B152236896b); - - function setUp() public override { - super.setUp(); - - mockRouter = new MockPendleRouter( - address(DOLA), - address(pendlePT), - address(pendleYT) - ); - - vm.startPrank(gov); - DOLA.mint(address(this), 100000 ether); - - ale = new ALEV2(triDBRAddr); - - helper = new PendlePTHelper( - gov, - pauseGuardian, - address(DOLA), - address(mockRouter), - address(ale) - ); - ale.setMarket(address(market), address(DOLA), address(helper), false); - helper.setMarket(address(market), address(pendlePT), pendleYT); - - flash = IFlashMinter(address(ale.flash())); - flash.setMaxFlashLimit(10000000 ether); - DOLA.addMinter(address(flash)); - borrowController.allow(address(ale)); - vm.stopPrank(); - userPkEscrow = address(market.predictEscrow(userPk)); - - vm.label(address(DOLA), "DOLA"); - vm.label(address(pendlePT), "pendlePT"); - vm.label(address(pendleYT), "pendleYT"); - // Fill Pendle Router Mock - vm.prank(gov); - DOLA.mint(address(mockRouter), 10000000 ether); - vm.prank(address(pendlePTHolder)); - IERC20(pendlePT).transfer(address(mockRouter), 10000000 ether); - vm.prank(address(pendleYTHolder)); - IERC20(pendleYT).transfer(address(mockRouter), 10000000 ether); - } - function test_leveragePosition_Mint_PT_and_YT_with_DOLA() public { - vm.prank(gov); - DOLA.mint(userPk, 1000000 ether); - - _transformAndDeposit(1000000 ether); - - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.startPrank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - _getPermitForBorrow(maxBorrowAmount), - _encodeMintPt(maxBorrowAmount), - dbrData - ); - vm.stopPrank(); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + maxBorrowAmount, // 1:1 ratio DOLA/PT - 1e14 // 0.01% Delta - ); - assertEq(IERC20(pendleYT).balanceOf(userPk), maxBorrowAmount); - } - - function test_leveragePosition_Swap_DOLA_for_PT() public { - vm.prank(gov); - DOLA.mint(userPk, 1000000 ether); - - _transformAndDeposit(1000000 ether); - - gibDBR(userPk, 20000 ether); - - uint256 ptAmount = SimpleERC20Escrow(userPkEscrow).balance(); - uint maxBorrowAmount = _getMaxBorrowAmount(ptAmount); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.startPrank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - _getPermitForBorrow(maxBorrowAmount), - _encodeSwapForPT(maxBorrowAmount), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertApproxEqRel( - SimpleERC20Escrow(userPkEscrow).balance(), - ptAmount + maxBorrowAmount, // 1:1 ratio DOLA/PT - 1e14 // 0.01% Delta - ); - } - - function test_leveragePosition_buyDBR(uint amount) public { - vm.assume(amount < 5000000 ether); - vm.assume(amount > 0.001 ether); - vm.prank(gov); - DOLA.mint(userPk, amount); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), amount); - helper.convertToCollateralAndDeposit( - amount, - userPk, - abi.encode( - address(market), - 0, - abi.encodeWithSelector( - MockPendleRouter.swapExactTokenForPt.selector, - address(helper), - address(0), - amount, - emptyApprox, - emptyTokenInput, - emptyLimit - ) - ) - ); - vm.stopPrank(); - - uint256 lpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 15 days, 8); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 90) / 100, - 0 - ); - - vm.startPrank(userPk); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(0), - swapData, - _getPermitForBorrow(maxBorrowAmount + dolaForDBR), - _encodeSwapForPT(maxBorrowAmount), - dbrData - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq( - SimpleERC20Escrow(userPkEscrow).balance(), - lpAmount + maxBorrowAmount - ); - assertGt(dbr.balanceOf(userPk), (dbrAmount * 95) / 100); - } - - function test_depositAndLeveragePosition_DOLA(uint amount) public { - vm.assume(amount < 5000000 ether); - vm.assume(amount > 0.001 ether); - vm.prank(gov); - DOLA.mint(userPk, amount); - uint256 initialDolaDeposit = amount / 10; - - _transformAndDeposit(amount - initialDolaDeposit); - - uint256 lpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.startPrank(userPk); - DOLA.approve(address(ale), initialDolaDeposit); - ale.depositAndLeveragePosition( - initialDolaDeposit, - maxBorrowAmount, - address(market), - address(0), - swapData, - _getPermitForBorrow(maxBorrowAmount), - _encodeSwapForPT(maxBorrowAmount + initialDolaDeposit), - dbrData, - false - ); - - assertEq(DOLA.balanceOf(userPk), 0, "DOLA BALANCE"); - assertEq( - SimpleERC20Escrow(userPkEscrow).balance(), - lpAmount + maxBorrowAmount + initialDolaDeposit - ); - } - - function test_depositAndLeveragePosition_PT(uint256 amount) public { - vm.assume(amount < 5000000 ether); - vm.assume(amount > 0.001 ether); - vm.prank(gov); - DOLA.mint(userPk, amount); - uint256 initialDolaDeposit = amount / 10; - - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), amount); - uint256 initialLpAmount = helper.convertToCollateral( - initialDolaDeposit, - _encodeSwapForPT(initialDolaDeposit) - ); - helper.convertToCollateralAndDeposit( - amount - initialDolaDeposit, - userPk, - _encodeSwapForPT(amount - initialDolaDeposit) - ); - vm.stopPrank(); - - uint256 lpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(lpAmount); - - bytes memory swapData; - - ALEV2.DBRHelper memory dbrData; - - vm.startPrank(userPk); - IERC20(address(pendlePT)).approve(address(ale), initialLpAmount); - ale.depositAndLeveragePosition( - initialLpAmount, - maxBorrowAmount, - address(market), - address(0), - swapData, - _getPermitForBorrow(maxBorrowAmount), - _encodeSwapForPT(maxBorrowAmount), - dbrData, - true - ); - - assertEq(DOLA.balanceOf(userPk), 0); - assertEq( - SimpleERC20Escrow(userPkEscrow).balance(), - lpAmount + maxBorrowAmount + initialLpAmount - ); - } - - function test_deleveragePosition_Redeem_with_PT_and_YT( - uint256 lpAmount - ) public { - test_leveragePosition_Mint_PT_and_YT_with_DOLA(); - - uint256 totalLpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - vm.assume(lpAmount > 0.0001 ether); - vm.assume(lpAmount <= totalLpAmount); - uint256 amountToWithdraw = lpAmount / 2; - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - uint256 ytBalBefore = IERC20(pendleYT).balanceOf(userPk); - if (amountToWithdraw > ytBalBefore) { - vm.prank(pendleYTHolder); - IERC20(pendleYT).transfer(userPk, amountToWithdraw - ytBalBefore); - } - - uint256 ytBalAfter = IERC20(pendleYT).balanceOf(userPk); - vm.startPrank(userPk); - IERC20(pendleYT).approve(address(helper), type(uint256).max); - - ale.deleveragePosition( - amountToWithdraw / 2, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - _getPermitWithdraw(amountToWithdraw), - _encodeRedeem(amountToWithdraw), - dbrData - ); - - assertEq( - SimpleERC20Escrow(userPkEscrow).balance(), - totalLpAmount - amountToWithdraw - ); - assertApproxEqAbs(DOLA.balanceOf(userPk), amountToWithdraw / 2, 1); - assertEq( - IERC20(pendleYT).balanceOf(userPk), - ytBalAfter - amountToWithdraw - ); - } - - function test_deleveragePosition_Swap_PT_to_DOLA(uint256 lpAmount) public { - test_leveragePosition_Swap_DOLA_for_PT(); - - uint256 totalLpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - vm.assume(lpAmount > 0.0001 ether); - vm.assume(lpAmount <= totalLpAmount); - uint256 amountToWithdraw = lpAmount / 2; - - ALEV2.DBRHelper memory dbrData; - bytes memory swapData; - - vm.startPrank(userPk); - ale.deleveragePosition( - amountToWithdraw / 2, // dola redeemed to be deleveraged - address(market), - address(0), - amountToWithdraw, - swapData, - _getPermitWithdraw(amountToWithdraw), - _encodeSwapForDola(amountToWithdraw), - dbrData - ); - - assertEq( - SimpleERC20Escrow(userPkEscrow).balance(), - totalLpAmount - amountToWithdraw - ); - assertApproxEqAbs(DOLA.balanceOf(userPk), amountToWithdraw / 2, 1); - } - - function test_deleveragePosition_sellDBR(uint256 lpAmount) public { - test_leveragePosition_Swap_DOLA_for_PT(); - uint256 totalLpAmount = SimpleERC20Escrow(userPkEscrow).balance(); - vm.assume(lpAmount > 0.0001 ether); - vm.assume(lpAmount <= totalLpAmount); - uint256 amountToWithdraw = lpAmount; - - uint256 dolaRedeemed = amountToWithdraw; - - uint256 debt = market.debts(address(userPk)); - uint256 amountToRepay; - if (debt < dolaRedeemed) { - amountToRepay = debt; - } else { - amountToRepay = dolaRedeemed; - } - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 10000, - 0 - ); // sell all DBR - bytes memory swapData; - - vm.startPrank(userPk); - dbr.approve(address(ale), dbr.balanceOf(userPk)); - ale.deleveragePosition( - amountToRepay, - address(market), - address(0), - amountToWithdraw, - swapData, - _getPermitWithdraw(amountToWithdraw), - _encodeSwapForDola(amountToWithdraw), - dbrData - ); - - assertEq( - SimpleERC20Escrow(userPkEscrow).balance(), - totalLpAmount - amountToWithdraw - ); - // Dbrs have also been sold - if (debt < dolaRedeemed) { - // Dola left are more than the debt (plus the DBR sold) - assertGt(DOLA.balanceOf(userPk), dolaRedeemed - amountToRepay); - } else { - // only DBR sold - assertGt(DOLA.balanceOf(userPk), 0); - } - - assertEq(dbr.balanceOf(userPk), 0); - } - - function _transformAndDeposit(uint amount) internal { - vm.startPrank(userPk, userPk); - DOLA.approve(address(helper), amount); - helper.convertToCollateralAndDeposit( - amount, - userPk, - _encodeSwapForPT(amount) - ); - vm.stopPrank(); - } - - function _getPermitForBorrow( - uint amount - ) internal view returns (ALEV2.Permit memory permit) { - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - permit = ALEV2.Permit(block.timestamp, v, r, s); - } - - function _getPermitWithdraw( - uint256 amountToWithdraw - ) internal view returns (ALEV2.Permit memory permit) { - (uint8 v, bytes32 r, bytes32 s) = vm.sign( - 1, - keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ) - ); - return ALEV2.Permit(block.timestamp, v, r, s); - } - - function _encodeSwapForPT( - uint amount - ) internal view returns (bytes memory) { - return - abi.encode( - address(market), - 0, - abi.encodeWithSelector( - MockPendleRouter.swapExactTokenForPt.selector, - address(helper), - address(0), - amount, - emptyApprox, - emptyTokenInput, - emptyLimit - ) - ); - } - - function _encodeSwapForDola( - uint amount - ) internal view returns (bytes memory) { - return - abi.encode( - address(market), - uint(0), - abi.encodeWithSelector( - MockPendleRouter.swapExactPtForToken.selector, - address(ale), - address(0), - amount, - emptyTokenOutput, - emptyLimit - ) - ); - } - - function _encodeMintPt(uint amount) internal view returns (bytes memory) { - return - abi.encode( - address(market), - 0, - abi.encodeWithSelector( - MockPendleRouter.mintPyFromToken.selector, - address(helper), - address(0), - amount, - emptyTokenInput - ) - ); - } - - function _encodeRedeem( - uint256 amount - ) internal view returns (bytes memory) { - return - abi.encode( - address(market), - uint(0), - abi.encodeWithSelector( - MockPendleRouter.redeemPyToToken.selector, - address(ale), - address(0), - amount, - emptyTokenOutput - ) - ); - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (amountCollat * - oracle.viewPrice(address(pendlePT), 0) * - market.collateralFactorBps()) / - 10_000 / - 1e18; - } -} diff --git a/test/util/aleTests/ALESDolascrvUSD.t.sol b/test/util/aleTests/ALESDolascrvUSD.t.sol index b099e555..e6efe88b 100644 --- a/test/util/aleTests/ALESDolascrvUSD.t.sol +++ b/test/util/aleTests/ALESDolascrvUSD.t.sol @@ -23,11 +23,27 @@ contract ALESDolascrvUSDTest is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 1, 2, address(0)); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); vm.stopPrank(); - + _seedTriDbrPool(); userPkEscrow = address(market.predictEscrow(userPk)); } + + function _seedTriDbrPool() internal { + vm.prank(gov); + DOLA.mint(address(this), 600000 ether); + DOLA.approve(address(newTriDBRAddr), 600000 ether); + deal(address(dbrAddr), address(this), 10000000 ether); + deal(address(invAddr), address(this), 20000 ether); + IERC20(dbrAddr).approve(address(newTriDBRAddr), 10000000 ether); + IERC20(invAddr).approve(address(newTriDBRAddr), 20000 ether); + uint[3] memory amounts; + amounts[0] = 600000 ether; + amounts[1] = 10000000 ether; + amounts[2] = 20000 ether; + + ICurvePool(newTriDBRAddr).add_liquidity(amounts, 0); + } } diff --git a/test/util/aleTests/ALESDolascrvUSDYearnV2.t.sol b/test/util/aleTests/ALESDolascrvUSDYearnV2.t.sol index 2a92f446..7b2481f3 100644 --- a/test/util/aleTests/ALESDolascrvUSDYearnV2.t.sol +++ b/test/util/aleTests/ALESDolascrvUSDYearnV2.t.sol @@ -24,11 +24,27 @@ contract ALESDolascrvUSDYearnV2Test is vm.startPrank(gov); DOLA.mint(address(this), 100000 ether); helper.setMarket(address(market), address(curvePool), 1, 2, yearn); - ale = ALEV2(payable(aleV2Addr)); + ale = new ALEV2(newTriDBRAddr, gov); ale.setMarket(address(market), address(DOLA), address(helper), false); borrowController.allow(address(ale)); vm.stopPrank(); - + _seedTriDbrPool(); userPkEscrow = address(market.predictEscrow(userPk)); } + + function _seedTriDbrPool() internal { + vm.prank(gov); + DOLA.mint(address(this), 600000 ether); + DOLA.approve(address(newTriDBRAddr), 600000 ether); + deal(address(dbrAddr), address(this), 10000000 ether); + deal(address(invAddr), address(this), 20000 ether); + IERC20(dbrAddr).approve(address(newTriDBRAddr), 10000000 ether); + IERC20(invAddr).approve(address(newTriDBRAddr), 20000 ether); + uint[3] memory amounts; + amounts[0] = 600000 ether; + amounts[1] = 10000000 ether; + amounts[2] = 20000 ether; + + ICurvePool(newTriDBRAddr).add_liquidity(amounts, 0); + } } diff --git a/test/util/aleTests/ALESdeUSDForkTest.t.sol b/test/util/aleTests/ALESdeUSDForkTest.t.sol deleted file mode 100644 index 86055559..00000000 --- a/test/util/aleTests/ALESdeUSDForkTest.t.sol +++ /dev/null @@ -1,33 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "test/marketForkTests/SdeUSDMarketForkTest.t.sol"; -import "test/util/aleTests/ALEBaseSimpleForkTest.t.sol"; -import "src/DBR.sol"; -import "test/mocks/ERC20.sol"; - -contract ALESdeUSDForkTest is SdeUSDMarketForkTest, ALEBaseSimpleForkTest { - function setUp() public override { - super.setUp(); - - exchangeProxy = new MockExchangeProxy( - address(market.oracle()), - address(DOLA) - ); - - ale = new ALE(address(exchangeProxy), triDBR); - // ALE setup - vm.startPrank(gov); - DOLA.addMinter(address(ale)); - borrowController.allow(address(ale)); - vm.stopPrank(); - - ale.setMarket( - address(market), - address(market.collateral()), - address(0), - true - ); - } -} diff --git a/test/util/aleTests/ALEBaseSimpleForkTest.t.sol b/test/util/aleTests/ALEV2BaseSimpleForkTest.t.sol similarity index 93% rename from test/util/aleTests/ALEBaseSimpleForkTest.t.sol rename to test/util/aleTests/ALEV2BaseSimpleForkTest.t.sol index e3d5d865..2e0fce04 100644 --- a/test/util/aleTests/ALEBaseSimpleForkTest.t.sol +++ b/test/util/aleTests/ALEV2BaseSimpleForkTest.t.sol @@ -2,8 +2,8 @@ pragma solidity ^0.8.13; import "forge-std/Test.sol"; -import "test/marketForkTests/SdeUSDMarketForkTest.t.sol"; -import {ALE} from "src/util/ALE.sol"; +import "test/marketForkTests/MarketForkTest.sol"; +import {ALEV2} from "src/util/ALEV2.sol"; contract MockExchangeProxy { IOracle oracle; @@ -41,15 +41,14 @@ interface IFlashMinter { function setMaxFlashLimit(uint256 limit) external; } -abstract contract ALEBaseSimpleForkTest is MarketForkTest { +abstract contract ALEV2BaseSimpleForkTest is MarketForkTest { bytes exceededLimit = "Exceeded credit limit"; bytes repaymentGtThanDebt = "Repayment greater than debt"; error NothingToDeposit(); MockExchangeProxy exchangeProxy; - ALE ale; - address triDBR = 0xC7DE47b9Ca2Fc753D6a2F167D8b3e19c6D18b19a; + ALEV2 ale; IFlashMinter flash; function getMaxLeverageBorrowAmount( @@ -113,9 +112,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData = ALE.DBRHelper( + ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( dolaForDBR, (dbrAmount * 98) / 100, 0 @@ -198,9 +197,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData = ALE.DBRHelper( + ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( dolaForDBR, (dbrAmount * 98) / 100, // DBR buy, 0 // Dola to borrow and withdraw after leverage @@ -277,9 +276,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData = ALE.DBRHelper( + ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( dolaForDBR, (dbrAmount * 98) / 100, // DBR buy dolaToWithdraw // Dola to borrow and withdraw after leverage @@ -357,9 +356,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData = ALE.DBRHelper( + ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( dolaForDBR, (dbrAmount * 98) / 100, // DBR buy 0 // Dola to borrow and withdraw after leverage @@ -444,11 +443,11 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData = ALE.DBRHelper( + ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( dbr.balanceOf(userPk), - 0, + 1, 0 ); // Sell DBR @@ -463,8 +462,8 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ale.deleveragePosition( convertCollatToDola(amountToWithdraw), address(market), - amountToWithdraw, address(exchangeProxy), + amountToWithdraw, swapData, permit, bytes(""), @@ -534,11 +533,11 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData = ALE.DBRHelper( + ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( dbr.balanceOf(userPk), - 0, + 1, 0 ); // Sell DBR @@ -555,8 +554,8 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ale.deleveragePosition( borrowAmount, address(market), - amountToWithdraw, address(exchangeProxy), + amountToWithdraw, swapData, permit, bytes(""), @@ -570,9 +569,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); // User still has dola and actually he has more bc he sold his DBRs - assertGt(DOLA.balanceOf(userPk), borrowAmount); + assertGt(DOLA.balanceOf(userPk), borrowAmount," Dola balance didn't increase"); - assertEq(dbr.balanceOf(userPk), 0); + assertEq(dbr.balanceOf(userPk), 0, "DBR were not sold"); assertEq(collateral.balanceOf(userPk), amountToWithdraw / 2); } @@ -621,9 +620,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData; // NO DBR + ALEV2.DBRHelper memory dbrData; // NO DBR bytes memory swapData = abi.encodeWithSelector( MockExchangeProxy.swapDolaIn.selector, @@ -703,9 +702,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData; // NO DBR + ALEV2.DBRHelper memory dbrData; // NO DBR bytes memory swapData = abi.encodeWithSelector( MockExchangeProxy.swapDolaOut.selector, @@ -716,8 +715,8 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ale.deleveragePosition( maxBorrowAmount, address(market), - amountToWithdraw, address(exchangeProxy), + amountToWithdraw, swapData, permit, bytes(""), @@ -783,9 +782,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData; // NO DBR + ALEV2.DBRHelper memory dbrData; // NO DBR bytes memory swapData = abi.encodeWithSelector( MockExchangeProxy.swapDolaIn.selector, @@ -828,7 +827,7 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (v, r, s) = vm.sign(1, hash); - permit = ALE.Permit(block.timestamp, v, r, s); + permit = ALEV2.Permit(block.timestamp, v, r, s); swapData = abi.encodeWithSelector( MockExchangeProxy.swapDolaOut.selector, @@ -839,8 +838,8 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ale.deleveragePosition( maxBorrowAmount, address(market), - amountToWithdraw, address(exchangeProxy), + amountToWithdraw, swapData, permit, bytes(""), @@ -895,9 +894,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData; // NO DBR + ALEV2.DBRHelper memory dbrData; // NO DBR bytes memory swapData = abi.encodeWithSelector( MockExchangeProxy.swapDolaOut.selector, @@ -910,8 +909,8 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ale.deleveragePosition( 0, address(market), - amountToWithdraw, address(exchangeProxy), + amountToWithdraw, swapData, permit, bytes(""), @@ -964,9 +963,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData; // NO DBR + ALEV2.DBRHelper memory dbrData; // NO DBR bytes memory swapData = abi.encodeWithSelector( MockExchangeProxy.swapDolaIn.selector, @@ -1021,9 +1020,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData; // NO DBR + ALEV2.DBRHelper memory dbrData; // NO DBR bytes memory swapData = abi.encodeWithSelector( MockExchangeProxy.swapDolaOut.selector, @@ -1036,8 +1035,8 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ale.deleveragePosition( 1 ether, address(market), - amountToWithdraw, address(exchangeProxy), + amountToWithdraw, swapData, permit, bytes(""), @@ -1093,9 +1092,9 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - ALE.Permit memory permit = ALE.Permit(block.timestamp, v, r, s); + ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - ALE.DBRHelper memory dbrData = ALE.DBRHelper( + ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( dolaForDBR, (dbrAmount * 99) / 100, 0 @@ -1124,19 +1123,17 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { address fakeMarket = address(0x69); vm.expectRevert( - abi.encodeWithSelector(ALE.NoMarket.selector, fakeMarket) + abi.encodeWithSelector(ALEV2.NoMarket.selector, fakeMarket) ); ale.setMarket(fakeMarket, address(0), address(0), true); } function test_fail_setMarket_Wrong_BuySellToken_Without_Helper() public { - ale.updateMarketHelper(address(market), address(0)); - address fakeBuySellToken = address(0x69); vm.expectRevert( abi.encodeWithSelector( - ALE.MarketSetupFailed.selector, + ALEV2.MarketSetupFailed.selector, address(market), fakeBuySellToken, address(collateral), @@ -1154,7 +1151,7 @@ abstract contract ALEBaseSimpleForkTest is MarketForkTest { address newHelper = address(0x70); vm.expectRevert( - abi.encodeWithSelector(ALE.MarketNotSet.selector, wrongMarket) + abi.encodeWithSelector(ALEV2.MarketNotSet.selector, wrongMarket) ); ale.updateMarketHelper(wrongMarket, newHelper); } diff --git a/test/util/aleTests/ALEsFrax4626HelperForkTest.t.sol b/test/util/aleTests/ALEsFrax4626HelperForkTest.t.sol deleted file mode 100644 index 79939695..00000000 --- a/test/util/aleTests/ALEsFrax4626HelperForkTest.t.sol +++ /dev/null @@ -1,916 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import {BorrowController} from "src/BorrowController.sol"; -import "src/DBR.sol"; -import {Market, IBorrowController} from "src/Market.sol"; -import {Oracle, IChainlinkFeed} from "src/Oracle.sol"; -import {Fed, IMarket} from "src/Fed.sol"; -import {ALEV2} from "src/util/ALEV2.sol"; -import {ERC4626Helper, IERC4626} from "src/util/ERC4626Helper.sol"; -import {IMultiMarketConvertHelper} from "src/interfaces/IMultiMarketConvertHelper.sol"; -import {console} from "forge-std/console.sol"; -import {BaseHelperForkTest, IERC4626, MockExchangeProxy} from "test/util/aleTests/BaseHelperForkTest.t.sol"; -import {IERC20} from "lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol"; - -interface IMintable is IERC20 { - function mint(address receiver, uint amount) external; - - function addMinter(address minter) external; -} - -interface IBC { - function setMinDebt(address market, uint256 minDebt) external; - - function setStalenessThreshold(address market, uint256 threshold) external; -} - -interface IFlashMinter { - function setMaxFlashLimit(uint256 limit) external; - - function flashFee( - address token, - uint256 amount - ) external view returns (uint256); -} - -contract ALEsFrax4626HelperForkTest is BaseHelperForkTest { - using stdStorage for StdStorage; - - //Market deployment: - Market market; - IChainlinkFeed feed; - BorrowController borrowController; - - address sFraxHolder = 0xBc2F0Ebc412647C7d4EC8FFD88Ca84Bc5b32C8cC; - address fraxHolder = 0x5E583B6a1686f7Bc09A6bBa66E852A7C80d36F00; - - //ERC-20s - IMintable DOLA; - IERC20 collateral; - - //FiRM - Oracle oracle; - DolaBorrowingRights dbr; - Fed fed; - - MockExchangeProxy exchangeProxy; - ALEV2 ale; - IFlashMinter flash; - ERC4626Helper helper; - //Variables - uint collateralFactorBps; - - function getBlockNumber() public view override returns (uint256) { - return 22241605; - } - - function setUp() public override { - super.setUp(); - - DOLA = IMintable(dolaAddr); - market = Market(sFraxMarketAddr); - feed = IChainlinkFeed(sFraxFeedAddr); - borrowController = BorrowController(borrowControllerAddr); - dbr = DolaBorrowingRights(dbrAddr); - helper = ERC4626Helper(erc4626HelperAddr); - initBase(address(helper)); - - exchangeProxy = new MockExchangeProxy( - address(market.oracle()), - address(DOLA) - ); - - vm.startPrank(gov); - helper.setMarket(address(market), fraxAddr, sFraxAddr); - dbr.addMarket(address(market)); - DOLA.mint(address(market), 1000000e18); - - ale = ALEV2(payable(aleV2Addr)); - ale.allowProxy(address(exchangeProxy)); - ale.setMarket(address(market), fraxAddr, address(helper), true); - vm.stopPrank(); - //FiRM - oracle = Oracle(address(market.oracle())); - fed = Fed(market.lender()); - collateral = IERC20(address(market.collateral())); - - vm.startPrank(gov, gov); - market.setBorrowController( - IBorrowController(address(borrowController)) - ); - market.setCollateralFactorBps(8000); - borrowController.setDailyLimit(address(market), 1_000_000 * 1e18); - IBC(address(borrowController)).setStalenessThreshold( - address(market), - 3660 - ); - - market.setLiquidationFactorBps(5000); - market.setLiquidationIncentiveBps(500); - IBC(address(borrowController)).setMinDebt(address(market), 0); - fed.changeMarketCeiling(IMarket(address(market)), type(uint).max); - fed.changeSupplyCeiling(type(uint).max); - oracle.setFeed(address(collateral), feed, 18); - oracle.setFeed(fraxAddr, IChainlinkFeed(fraxUsdFeedAddr), 18); - borrowController.allow(address(ale)); - DOLA.addMinter(address(ale)); - - flash = IFlashMinter(address(ale.flash())); - DOLA.addMinter(address(flash)); - flash.setMaxFlashLimit(1000000e18); - vm.stopPrank(); - - collateralFactorBps = market.collateralFactorBps(); - } - - function checkEq( - uint sFraxDeposit, - uint collateralToSwap, - address userPk - ) internal { - assertApproxEqAbs( - IERC20(sFraxAddr).balanceOf(address(market.predictEscrow(userPk))), - sFraxDeposit + collateralToSwap, - 1 - ); - } - - function test_leveragePosition() public { - // vm.assume(sFraxAmount < 7900 ether); - // vm.assume(sFraxAmount > 0.00000001 ether); - // We are going to deposit some CRV, then leverage the position - uint sFraxAmount = 10000 ether; - address userPk = vm.addr(1); - vm.prank(sFraxHolder); - IERC20(sFraxAddr).transfer(userPk, sFraxAmount); - - gibDBR(userPk, 20000 ether); - - uint maxBorrowAmount = _getMaxBorrowAmount(sFraxAmount); - console.log(sFraxAmount, "sFraxAmount"); - console.log(maxBorrowAmount, "maxBorrowAmount"); - console.log(_convertCollatToDola(sFraxAmount), "maxBorrowAmount"); - console.log(market.collateralFactorBps(), "collateralFactorBps"); - - uint256 fraxAmount = IERC4626(sFraxAddr).convertToAssets( - _convertDolaToCollat(maxBorrowAmount) - ); - // recharge mocked proxy for swap, we need to swap DOLA to unwrapped collateral - vm.prank(fraxHolder); - IERC20(fraxAddr).transfer(address(exchangeProxy), fraxAmount + 2); - - vm.startPrank(userPk, userPk); - // Initial CRV deposit - IERC20(sFraxAddr).approve(address(market), sFraxAmount); - market.deposit(sFraxAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - fraxAddr, - maxBorrowAmount - ); - - ALEV2.DBRHelper memory dbrData; - - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - abi.encode(address(market)), - dbrData - ); - console.log(market.getCollateralValue(userPk)); - console.log(market.getCreditLimit(userPk)); - // market.borrow(10 ether); - // // Balance in escrow is equal to the collateral deposited + the extra collateral swapped from the leverage - assertApproxEqAbs( - IERC20(sFraxAddr).balanceOf(address(market.predictEscrow(userPk))), - sFraxAmount + - IERC4626(sFraxAddr).convertToShares( - _convertDolaToUnderlying(maxBorrowAmount) - ), - 1 - ); - - assertEq(DOLA.balanceOf(userPk), 0); - } - - function test_leveragePosition_buyDBR() public { - // We are going to deposit some st-frax, then leverage the position - uint sFraxAmount = 10000 ether; - address userPk = vm.addr(1); - vm.prank(sFraxHolder); - IERC20(sFraxAddr).transfer(userPk, sFraxAmount); - - uint maxBorrowAmount = _getMaxBorrowAmount(sFraxAmount); - - uint256 fraxAmount = IERC4626(sFraxAddr).convertToAssets( - _convertDolaToCollat(maxBorrowAmount) - ); - - // recharge mocked proxy for swap, we need to swap DOLA to unwrapped collateral - vm.prank(fraxHolder); - IERC20(fraxAddr).transfer(address(exchangeProxy), fraxAmount + 2); - - vm.startPrank(userPk, userPk); - // Initial st-frax deposit - IERC20(sFraxAddr).approve(address(market), sFraxAmount); - market.deposit(sFraxAmount); - - // Calculate the amount of DOLA needed to borrow to buy the DBR needed to cover for the borrowing period - (uint256 dolaForDBR, uint256 dbrAmount) = ale - .approximateDolaAndDbrNeeded(maxBorrowAmount, 365 days, 8); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount + dolaForDBR, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - fraxAddr, - maxBorrowAmount - ); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dolaForDBR, - (dbrAmount * 98) / 100, - 0 - ); - - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - abi.encode(address(market)), - dbrData - ); - - // Balance in escrow is equal to the collateral deposited + the extra collateral swapped from the leverage - checkEq(sFraxAmount, _convertDolaToCollat(maxBorrowAmount), userPk); - - assertEq(DOLA.balanceOf(userPk), 0); - - assertGt(dbr.balanceOf(userPk), (dbrAmount * 98) / 100); - } - - function test_deleveragePosition_sellDBR(uint256 sFraxAmount) public { - vm.assume(sFraxAmount < 15000 ether); - vm.assume(sFraxAmount > 0.0001 ether); - // We are going to deposit some st-frax, then borrow and then deleverage the position - //uint sFraxAmount = 10000 ether; - address userPk = vm.addr(1); - vm.prank(sFraxHolder); - IERC20(sFraxAddr).transfer(userPk, sFraxAmount); - - gibDBR(userPk, sFraxAmount); - - uint borrowAmount = (_getMaxBorrowAmount(sFraxAmount) * 97) / 100; - - vm.startPrank(userPk, userPk); - // Initial sFrax deposit - IERC20(sFraxAddr).approve(address(market), sFraxAmount); - market.deposit(sFraxAmount); - market.borrow(borrowAmount); - vm.stopPrank(); - - assertEq( - IERC20(sFraxAddr).balanceOf(address(market.predictEscrow(userPk))), - sFraxAmount - ); - assertEq(DOLA.balanceOf(userPk), borrowAmount); - - // We are going to withdraw only 1/10 of the collateral to deleverage - uint256 amountToWithdraw = IERC20(sFraxAddr).balanceOf( - address(market.predictEscrow(userPk)) - ) / 10; - - uint256 dolaAmountForSwap = _convertUnderlyingToDola( - IERC4626(sFraxAddr).convertToAssets(amountToWithdraw) - ); - - // recharge mocked proxy for swap, we need to swap DOLA to unwrapped collateral - vm.startPrank(gov); - DOLA.mint(address(exchangeProxy), dolaAmountForSwap); - vm.stopPrank(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 1, - 0 - ); // sell all DBR - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaOut.selector, - fraxAddr, - IERC4626(sFraxAddr).convertToAssets(amountToWithdraw) - ); - - vm.startPrank(userPk, userPk); - dbr.approve(address(ale), type(uint).max); - - ale.deleveragePosition( - _convertCollatToDola(amountToWithdraw), - address(market), - address(exchangeProxy), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market)), - dbrData - ); - - // Some collateral has been withdrawn - assertEq( - IERC20(sFraxAddr).balanceOf(address(market.predictEscrow(userPk))), - sFraxAmount - amountToWithdraw, 'COLLATERAL' - ); - - // User still has dola and actually he has more bc he sold his DBRs - assertGt(DOLA.balanceOf(userPk), borrowAmount, 'DOLA'); - - assertEq(dbr.balanceOf(userPk), 0); - } - - function test_deleveragePosition(uint256 sFraxAmount) public { - vm.assume(sFraxAmount < 10000 ether); - vm.assume(sFraxAmount > 0.00000001 ether); - // We are going to deposit some st-frax, then borrow and then deleverage the position - // uint sFraxAmount = 10000 ether; - address userPk = vm.addr(1); - vm.prank(sFraxHolder); - IERC20(sFraxAddr).transfer(userPk, sFraxAmount); - - gibDBR(userPk, sFraxAmount); - - uint borrowAmount = (_getMaxBorrowAmount(sFraxAmount) * 97) / 100; - - vm.startPrank(userPk, userPk); - // Initial sFrax deposit - IERC20(sFraxAddr).approve(address(market), sFraxAmount); - market.deposit(sFraxAmount); - market.borrow(borrowAmount); - vm.stopPrank(); - - address userEscrow = address(market.predictEscrow(userPk)); - assertEq(IERC20(sFraxAddr).balanceOf(userEscrow), sFraxAmount); - assertEq(DOLA.balanceOf(userPk), borrowAmount); - - // We are going to withdraw only 1/10 of the collateral to deleverage - uint256 amountToWithdraw = IERC20(sFraxAddr).balanceOf(userEscrow) / 10; - uint256 dolaAmountForSwap = _convertUnderlyingToDola( - IERC4626(sFraxAddr).convertToAssets(amountToWithdraw) - ); - - // recharge mocked proxy for swap, we need to swap DOLA to unwrapped collateral - vm.startPrank(gov); - DOLA.mint(address(exchangeProxy), dolaAmountForSwap); - vm.stopPrank(); - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - ALEV2.DBRHelper memory dbrData = ALEV2.DBRHelper(0, 0, borrowAmount / 2); // repay partially debt with DOLA in the wallet - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaOut.selector, - fraxAddr, - IERC4626(sFraxAddr).convertToAssets(amountToWithdraw) - ); - - vm.startPrank(userPk, userPk); - DOLA.approve(address(ale), borrowAmount / 2); - - ale.deleveragePosition( - _convertCollatToDola(amountToWithdraw), - address(market), - address(exchangeProxy), - amountToWithdraw, - swapData, - permit, - abi.encode(address(market)), - dbrData - ); - - // Some collateral has been withdrawn - assertEq( - IERC20(sFraxAddr).balanceOf(userEscrow), - sFraxAmount - amountToWithdraw - ); - // User still has dola but has some debt repaid - assertApproxEqAbs(DOLA.balanceOf(userPk), borrowAmount / 2, 1); - } - - function test_convertToCollateralAndDeposit(uint256 fraxAmount) public { - //vm.assume(fraxAmount < 1 ether); - - uint256 fraxAmount = 1 ether; - address userPk = vm.addr(1); - vm.prank(fraxHolder); - IERC20(fraxAddr).transfer(userPk, fraxAmount); - - vm.startPrank(userPk, userPk); - IERC20(fraxAddr).approve(address(helper), fraxAmount); - helper.convertToCollateralAndDeposit( - fraxAmount, - userPk, - abi.encode(address(market)) - ); - - assertEq(IERC20(fraxAddr).balanceOf(userPk), 0); - - assertEq( - IERC20(sFraxAddr).balanceOf(address(market.predictEscrow(userPk))), - IERC4626(sFraxAddr).convertToShares(fraxAmount) - ); - } - - function test_withdrawAndConvertFromCollateral( - uint256 fraxAmount - ) public { - // vm.assume(fraxAmount < IsFrax(sFrax).availableDepositLimit()); - - uint256 fraxAmount = 1 ether; - address userPk = vm.addr(1); - vm.prank(fraxHolder); - IERC20(fraxAddr).transfer(userPk, fraxAmount); - - vm.startPrank(userPk, userPk); - IERC20(fraxAddr).approve(address(helper), fraxAmount); - helper.convertToCollateralAndDeposit( - fraxAmount, - userPk, - abi.encode(address(market)) - ); - - //Market market = Market(address(helper.market())); // actual Mainnet market for helper contract - uint256 amountToWithdraw = IERC20(sFraxAddr).balanceOf( - address(market.predictEscrow(userPk)) - ) / 10; - - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(helper), - userPk, - amountToWithdraw, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - IMultiMarketConvertHelper.Permit - memory permit = IMultiMarketConvertHelper.Permit( - block.timestamp, - v, - r, - s - ); - - assertEq(IERC20(fraxAddr).balanceOf(userPk), 0); - - helper.withdrawAndConvertFromCollateral( - amountToWithdraw, - userPk, - permit, - abi.encode(address(market)) - ); - - assertApproxEqAbs( - IERC20(fraxAddr).balanceOf(userPk), - IERC4626(sFraxAddr).convertToAssets(amountToWithdraw), - 1 - ); - } - - function test_fail_setMarket_NoMarket() public { - address fakeMarket = address(0x69); - - vm.expectRevert( - abi.encodeWithSelector(ALEV2.NoMarket.selector, fakeMarket) - ); - vm.prank(gov); - ale.setMarket(fakeMarket, address(0), address(0), true); - } - - function test_fail_setMarket_Wrong_BuySellToken_Without_Helper() public { - address fakeBuySellToken = address(0x69); - - vm.expectRevert( - abi.encodeWithSelector( - ALEV2.MarketSetupFailed.selector, - address(market), - fakeBuySellToken, - address(collateral), - address(0) - ) - ); - vm.prank(gov); - ale.setMarket(address(market), fakeBuySellToken, address(0), true); - - vm.expectRevert( - abi.encodeWithSelector( - ALEV2.MarketSetupFailed.selector, - address(market), - address(0), - address(collateral), - address(0) - ) - ); - vm.prank(gov); - ale.setMarket(address(market), address(0), address(0), true); - - vm.expectRevert(); - vm.prank(gov); - ale.setMarket(address(market), fakeBuySellToken, address(0), true); - } - - function test_fail_updateMarketHelper_NoMarket() public { - address wrongMarket = address(0x69); - address newHelper = address(0x70); - - vm.expectRevert( - abi.encodeWithSelector(ALEV2.MarketNotSet.selector, wrongMarket) - ); - vm.prank(gov); - ale.updateMarketHelper(wrongMarket, newHelper); - } - - function test_return_assetAmount_when_TotalSupply_is_Zero() public { - stdstore - .target(sFraxAddr) - .sig(IERC4626(sFraxAddr).totalSupply.selector) - .checked_write(uint256(0)); - - uint256 assetAmount = 1 ether; - assertEq(assetAmount, IERC4626(sFraxAddr).convertToShares(assetAmount)); - } - - function test_fail_collateral_is_zero_leveragePosition() public { - // We are going to deposit some CRV, then leverage the position - uint sFraxAmount = 10000 ether; - address userPk = vm.addr(1); - vm.prank(sFraxHolder); - IERC20(sFraxAddr).transfer(userPk, sFraxAmount); - - gibDBR(userPk, sFraxAmount); - - uint maxBorrowAmount = _getMaxBorrowAmount(sFraxAmount); - - uint256 fraxAmount = IERC4626(sFraxAddr).convertToAssets( - _convertDolaToCollat(maxBorrowAmount) - ); - // recharge mocked proxy for swap, we need to swap DOLA to unwrapped collateral - vm.prank(fraxHolder); - IERC20(fraxAddr).transfer(address(exchangeProxy), fraxAmount + 2); - - vm.startPrank(userPk, userPk); - // Initial CRV deposit - IERC20(sFraxAddr).approve(address(market), sFraxAmount); - market.deposit(sFraxAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - maxBorrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData = abi.encodeWithSelector( - MockExchangeProxy.swapDolaIn.selector, - fraxAddr, - maxBorrowAmount - ); - - ALEV2.DBRHelper memory dbrData; - - // Mock call to return 0 buySellToken balance for the ALE - vm.mockCall( - fraxAddr, - abi.encodeWithSelector(IERC20.balanceOf.selector, address(ale)), - abi.encode(uint256(0)) - ); - - vm.expectRevert(ALEV2.CollateralIsZero.selector); - ale.leveragePosition( - maxBorrowAmount, - address(market), - address(exchangeProxy), - swapData, - permit, - abi.encode(address(market)), - dbrData - ); - } - - function test_Frax_Odos_leverage_and_deleverage() public { - address odos = address(0xCf5540fFFCdC3d510B18bFcA6d2b9987b0772559); - vm.makePersistent(address(helper)); - vm.makePersistent(address(oracle)); - vm.rollFork(22074631); - sFraxHolder = 0x56398b89d53e8731bca8C1B06886CFB14BD6b654; - - vm.startPrank(gov); - ale = new ALEV2(triDBRAddr); - ale.allowProxy(odos); - helper.setMarket(address(market), fraxAddr, sFraxAddr); - ale.setMarket(address(market), fraxAddr, address(helper), true); - borrowController.allow(address(ale)); - vm.stopPrank(); - console.log("ALE address", address(ale)); - uint sFraxAmount = 100000 ether; - address userPk = vm.addr(1); - vm.prank(sFraxHolder); - IERC20(sFraxAddr).transfer(userPk, sFraxAmount); - - gibDBR(userPk, 20000 ether); - - uint borrowAmount = 100000 ether; - vm.startPrank(userPk, userPk); - // Initial sFrax deposit - IERC20(sFraxAddr).approve(address(market), sFraxAmount); - market.deposit(sFraxAmount); - - // Sign Message for borrow on behalf - bytes32 hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "BorrowOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - borrowAmount, - 0, - block.timestamp - ) - ) - ) - ); - (uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash); - - ALEV2.Permit memory permit = ALEV2.Permit(block.timestamp, v, r, s); - - bytes memory swapData = hex"3b635ce4000000000000000000000000865377367054516e17014ccded1e7d814edc9ce400000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef000000000000000000000000853d955acef822db058eb8505911ed77f175b99e00000000000000000000000000000000000000000000152075f09d69360000000000000000000000000000000000000000000000000014ea6047cf09710000000000000000000000000000009123ef9b7db2e3d968b01c6ff99839acb242da130000000000000000000000000000000000000000000000000000000000000140000000000000000000000000d768d1fe6ef1449a54f9409400fe9d0e4954ea3f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013c03030c006701000001020001020e00000304010000c800000400016cd2f9d06701000105040100000d0207070400046700000006080100025600090a0b010000020bff0000000000000000000000000000000000000000000000000000000000744793b5110f6ca9cc7cdfe1ce16677c3eb192ef865377367054516e17014ccded1e7d814edc9ce49d39a5de30e57443bff2a8307a4256c8797a3497dac17f958d2ee523a2206206994597c13d831ec74f493b7de8aac7d55f71853688b1f7c8f0243c855dc1bf6f1e983c0b21efb003c105133736fa0743435664008f38b0650fbc1c9fc971d0a3bc2f1e474c9edd5852cd905f086c759e8383e09bff1e68b3dcef968d416a41cdac0ed8702fac8128a64241a2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48853d955acef822db058eb8505911ed77f175b99e00000000"; - - ALEV2.DBRHelper memory dbrData; - - ale.leveragePosition( - borrowAmount, - address(market), - odos, - swapData, - permit, - abi.encode(address(market)), - dbrData - ); - assertApproxEqAbs( - IERC20(sFraxAddr).balanceOf(address(market.predictEscrow(userPk))), - sFraxAmount + - IERC4626(sFraxAddr).convertToShares( - 99768490416193306361856 // expected FRAX out from ODOS - ), - 1 - ); - - assertEq(DOLA.balanceOf(userPk), 0); - - uint256 amountToWithdraw = IERC20(sFraxAddr).balanceOf( - address(market.predictEscrow(userPk))); - console.log("fraxAmount", IERC4626(sFraxAddr).convertToAssets(amountToWithdraw)); - hash = keccak256( - abi.encodePacked( - "\x19\x01", - market.DOMAIN_SEPARATOR(), - keccak256( - abi.encode( - keccak256( - "WithdrawOnBehalf(address caller,address from,uint256 amount,uint256 nonce,uint256 deadline)" - ), - address(ale), - userPk, - amountToWithdraw, - 1, - block.timestamp - ) - ) - ) - ); - (v, r, s) = vm.sign(1, hash); - - permit = ALEV2.Permit(block.timestamp, v, r, s); - - dbrData = ALEV2.DBRHelper( - dbr.balanceOf(userPk), - 1, - 0 - ); // sell all DBR - - swapData = hex"83bd37f90001853d955acef822db058eb8505911ed77f175b99e0001865377367054516e17014ccded1e7d814edc9ce40a2cd6b5a59e02b5bb76440a2ce28d9c48d54a000000028f5c0001d768d1Fe6Ef1449A54F9409400fe9d0E4954ea3F000000019123ef9b7dB2e3D968B01C6FF99839aCb242dA13000000001d070723010879335a4601020203040101283834826702060106030001012020c36067030001070300010067040001080300010913c3130046000a0a0b0c0109239cc2dd67050e010e0b000108670600010f0b000106560410111200010467031400051401000267020000010401000c000104460616161718010667021a00131a00010067061600090c00010a670000000d1800010856051b12170001004604011c1d1e000467040100191f01000c67040100151801000a4a040120170100026704010121220100ff00000000000000000000000000000000000000000000000073a0cba58c19ed5f27c6590bd792ec38de4815eaa663b02cf0a4b149d2ad41910cb81e23e1c41c32853d955acef822db058eb8505911ed77f175b99ea663b02cf0a4b149d2ad41910cb81e23e1c41c32670a72e6d22b0956c0d2573288f82dcc5d6e3a615dc1bf6f1e983c0b21efb003c105133736fa0743ce6431d21e3fb1036ce9973a3312368ed96f5ce7bbaf8b2837cbbc7146f5bc978d6f84db0be1cacc15e4ff94b70a8f146b4e4afd069014d126035752cf62f905562626cfcdd2261162a51fd02fc9c5b6cacd6fd266af91b8aed52accc382b4e165586e29cf62f905562626cfcdd2261162a51fd02fc9c5b676a962ba6770068bcf454d34dde17175611e66374d968a5db8da0822a2f08840f553de4129aae5f381a2612f6dea269a6dd1f6deab45c5424ee2c4b7425bfb93370f14ff525adb6eaeacfe1f4e3b580283f20f44975d03b1b09e64809b757c47f942beea59d9356e565ab3a36dd77763fc0d87feaf85508c4628f13651ead6793f8d838b34b8f8522fb0cc524c9edd5852cd905f086c759e8383e09bff1e68b3ff17dab22f1e61078aba2623c89ce6110e878b3c0655977feb2f289a4ab78af67bab0d17aab84367f939e0a03fb07f59a73314e73794be0e57ac1b4e0655977feb2f289a4ab78af67bab0d17aab8436738de22a3175708d45e7c7c64cd78479c8b56f76e40d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f30ce6e5a75586f0e83bcac77c9135e980e6bc7a8b45ad160634c528cc3d2926d9807104fa3157305b45ad160634c528cc3d2926d9807104fa3157305865377367054516e17014ccded1e7d814edc9ce466a1e37c9b0eaddca17d3662d6c05f4decf3e1108272e1a3dbef607c04aa6e5bd3a1a134c8ac063b8b83c4aa949254895507d09365229bc3a8c7f710a3931d71877c0e7a3148cb7eb4463524fec27fbd000000000000000000000000000000000000000000000000"; - - console.log("amountToWithdraw", amountToWithdraw); - dbr.approve(address(ale), type(uint).max); - ale.deleveragePosition( - market.debts(userPk), - address(market), - odos, - amountToWithdraw, - swapData, - permit, - abi.encode(address(market)), - dbrData - ); - - // All collateral withdrawn and zero debt - assertEq( - IERC20(sFraxAddr).balanceOf(address(market.predictEscrow(userPk))), - 0, 'COLLATERAL' - ); - assertEq(market.debts(userPk), 0); - // User receives Dola from selling his collateral and actually he has more bc he sold all his DBR - uint256 amountDolaOut = 211963293517859369517056; // Odos output swap - assertGt(DOLA.balanceOf(userPk), 211963293517859369517056 - borrowAmount, 'DOLA'); - assertEq(IERC20(fraxAddr).balanceOf(userPk), 0, 'FRAX'); - assertEq(dbr.balanceOf(userPk), 0); - } - - - function _convertCollatToDola(uint amount) internal view returns (uint) { - uint256 underlying = IERC4626(sFraxAddr).convertToAssets(amount); - return _convertUnderlyingToDola(underlying); - } - - function _convertDolaToCollat(uint amount) internal view returns (uint) { - uint256 underlying = _convertDolaToUnderlying(amount); - console.log(underlying, "underlying"); - return IERC4626(sFraxAddr).convertToShares(underlying); - } - - function _convertDolaToUnderlying( - uint amount - ) internal view returns (uint) { - console.log(amount, "amount"); - return (amount * 1e18) / oracle.viewPrice(fraxAddr, 0); - } - - function _convertUnderlyingToDola( - uint amount - ) internal view returns (uint) { - return (amount * oracle.viewPrice(fraxAddr, 0)) / 1e18; - } - - function _getMaxBorrowAmount( - uint amountCollat - ) internal view returns (uint) { - return - (_convertCollatToDola(amountCollat) * - market.collateralFactorBps()) / 10_000; - } - - function gibDBR(address _address, uint _amount) internal { - vm.startPrank(gov); - dbr.mint(_address, _amount); - vm.stopPrank(); - } - - function gibDOLA(address _address, uint _amount) internal { - vm.startPrank(gov); - DOLA.mint(_address, _amount); - vm.stopPrank(); - } - - function codeAt(address _addr) public view returns (bytes memory o_code) { - assembly { - // retrieve the size of the code, this needs assembly - let size := extcodesize(_addr) - // allocate output byte array - this could also be done without assembly - // by using o_code = new bytes(size) - o_code := mload(0x40) - // new "memory end" including padding - mstore( - 0x40, - add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f))) - ) - // store length in memory - mstore(o_code, size) - // actually retrieve the code, this needs assembly - extcodecopy(_addr, add(o_code, 0x20), 0, size) - } - } -} diff --git a/test/util/aleTests/BaseHelperForkTest.t.sol b/test/util/aleTests/BaseHelperForkTest.t.sol index 9fa03015..0b5e4332 100644 --- a/test/util/aleTests/BaseHelperForkTest.t.sol +++ b/test/util/aleTests/BaseHelperForkTest.t.sol @@ -5,7 +5,6 @@ import "forge-std/Test.sol"; //import "../BorrowController.sol"; import "src/DBR.sol"; import {IOracle} from "src/Market.sol"; -import {ALE} from "src/util/ALE.sol"; import {ITransformHelper} from "src/interfaces/ITransformHelper.sol"; import {console} from "forge-std/console.sol"; import {IERC4626} from "lib/openzeppelin-contracts/contracts/interfaces/IERC4626.sol";