Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forked Deployments and V1 Vault Fixture Logic #69

Merged
merged 78 commits into from
Jul 20, 2023
Merged
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
7397838
abstract deposit and harvest
0xrin1 Apr 27, 2023
c637dad
simulate profit
0xrin1 Apr 28, 2023
eca18b2
forge fmt
0xrin1 Apr 28, 2023
de04764
add deposits that have not been redeemed to fixtures
0xrin1 Apr 28, 2023
eae9c3a
merge main
0xrin1 May 4, 2023
2c2f255
just repay half to simulate profit so that LTV diverges by 50%
0xrin1 May 4, 2023
3ab4b62
console.log scWETH and scUSDC addresses
0xrin1 May 4, 2023
a4427ca
add newline for clarity
0xrin1 May 4, 2023
f0c203a
prevent newline from using variable spacing
0xrin1 May 4, 2023
7a6c726
merge main
0xrin1 May 25, 2023
445c721
remove GoerliConstants.sol
0xrin1 May 25, 2023
d169a50
[WIP] run testnet deployment and fixtures against forked node
0xrin1 May 26, 2023
fb30eb7
merge mainnet and testnet deployment
0xrin1 May 31, 2023
b322e78
working testnet deployment profit function
0xrin1 May 31, 2023
5ead1aa
address linting complaints
0xrin1 Jun 2, 2023
996c437
rename usdcContract to _scUSDC
0xrin1 Jun 2, 2023
fcd3f55
deposit rebalance and redeem in testnet fixture
0xrin1 Jun 4, 2023
ef5319a
remove comment
0xrin1 Jun 4, 2023
0db6fe0
merge main
0xrin1 Jun 4, 2023
04347da
0.8.20
0xrin1 Jun 4, 2023
17a08cb
scUSDC profit and rebalance
0xrin1 Jun 6, 2023
c57ea5a
forge fmt
0xrin1 Jun 6, 2023
e5d3658
remove rebalances that the backend should be doing
0xrin1 Jun 6, 2023
debcb6d
[WIP] set token balance with stdStore
0xrin1 Jun 6, 2023
1bc5c4d
_divergeLTV
0xrin1 Jun 6, 2023
d7861c7
setTokenBalance not erroring but not actually changing the balance value
0xrin1 Jun 6, 2023
27770fa
divergeLTV code is there in two forms but both dont work
0xrin1 Jun 7, 2023
56f8743
merge main
0xrin1 Jun 20, 2023
1adfdf7
fix merge
0xrin1 Jun 20, 2023
07672e9
add newline to constants file for clarity
0xrin1 Jun 20, 2023
70ce1aa
staking contract "notifyRewardAmount" fix (#85)
brkomir Jun 21, 2023
08c2218
refactor to improve code quality of the scip staking contracts (#86)
brkomir Jun 21, 2023
f433919
add fv report
0xBRM Jun 21, 2023
b34b0a0
rename
0xBRM Jun 21, 2023
7e7e3df
add visual assets
0xBRM Jun 21, 2023
4290a25
small edit
0xBRM Jun 21, 2023
5c18828
md doesnt render svg
0xBRM Jun 21, 2023
ac335be
fix path
0xBRM Jun 21, 2023
1e1bfc9
fix size
0xBRM Jun 21, 2023
67d0887
size
0xBRM Jun 21, 2023
8206812
text fixes
0xBRM Jun 21, 2023
0f430b9
consistency
0xBRM Jun 21, 2023
ee55000
Merge pull request #90 from lindy-labs/fv_report
0xBRM Jun 21, 2023
0e81ef4
moving all constant values to Constants.sol and removing duplicates (…
brkomir Jun 22, 2023
3f33104
merge main and resolve conflicts
0xrin1 Jun 22, 2023
c071cd8
remove swapWethForUsdc duplication
0xrin1 Jun 22, 2023
903e99b
renaming adapters to differentiate usage between scUSDCv2 & scWETHv2 …
brkomir Jul 4, 2023
7241406
document the decision of not including LQTY into total assets (#94)
brkomir Jul 5, 2023
b55a3dc
fix for the logic for maintaining the float balance on withdrawals (#92)
brkomir Jul 5, 2023
cf7903e
merge main
0xrin1 Jul 6, 2023
9a37e1e
newline console log
0xrin1 Jul 6, 2023
c745522
DeployLeveragedEth review style adjustments
0xrin1 Jul 6, 2023
d85cd4a
more code style review comment implementation
0xrin1 Jul 6, 2023
55b28d5
rename vaultToken to vault
0xrin1 Jul 6, 2023
f56671d
remove depositForUsers function
0xrin1 Jul 6, 2023
b06085d
use test constants for fixture deposit and funding amounts
0xrin1 Jul 6, 2023
368f736
INITAL_WETH_DEPOSIT
0xrin1 Jul 11, 2023
62f7a17
merge main
0xrin1 Jul 13, 2023
4a1e97b
INITIAL USDC and WETH WITHDRAW constant
0xrin1 Jul 13, 2023
f8ad6e4
move INITIAL test constants into test deployment script
0xrin1 Jul 19, 2023
b5a8018
review codestyle
0xrin1 Jul 19, 2023
b1c0b8d
remove LTV diverge logic
0xrin1 Jul 19, 2023
e17328d
pull main
0xrin1 Jul 19, 2023
8e10822
spacing
0xrin1 Jul 19, 2023
39f8d9a
move testing addresses to the bottom
0xrin1 Jul 19, 2023
f8184ec
forge fmt
0xrin1 Jul 19, 2023
bff2bc5
rename classes to match file name
0xrin1 Jul 19, 2023
f20d7e5
deployleveragedethtestnet comment
0xrin1 Jul 19, 2023
3ea8cf4
more comments
0xrin1 Jul 19, 2023
1c7821e
update scWeth targetLTV
0xrin1 Jul 19, 2023
1d59e5c
IPool instead of mock pool and remove unused imports
0xrin1 Jul 19, 2023
1c5f21f
comment clarification
0xrin1 Jul 19, 2023
b58a585
remove redundant deposits
0xrin1 Jul 19, 2023
b6d5563
rename testnet deploy script to forked deploy script
0xrin1 Jul 19, 2023
bd79656
rename sepolia staking script to just staking
0xrin1 Jul 19, 2023
82386d3
DeployStaking
0xrin1 Jul 19, 2023
69a37b7
adjust approve method
0xrin1 Jul 20, 2023
fd37bda
DeploymentConstants.sol
0xrin1 Jul 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix for the logic for maintaining the float balance on withdrawals (#92)
* fixed the logic for maintaining float balance on withdrawals

* fix min float test assertation
brkomir authored Jul 5, 2023
commit b55a3dc9601fb84f5fb9604cd6bfa88fc0bec5cd
51 changes: 23 additions & 28 deletions src/steth/scWETHv2.sol
Original file line number Diff line number Diff line change
@@ -343,23 +343,35 @@ contract scWETHv2 is BaseV2Vault {
uint256 n = protocolAdapters.length();
uint256 flashLoanAmount;
uint256 totalInvested_ = _totalCollateralInWeth() - totalDebt();
bytes[] memory callData = new bytes[](n + 1);
bytes[] memory callData = new bytes[](n + 1); // +1 for the last call to swap wstEth to weth

uint256 flashLoanAmount_;
uint256 amount_;
uint256 adapterId;
// limit the amount to withdraw to the total invested amount
if (_amount > totalInvested_) _amount = totalInvested_;

uint256 id;
address adapter;
uint256 repayPerProtocol;
uint256 withdrawPerProtocol;
for (uint256 i; i < n; i++) {
(adapterId, adapter) = protocolAdapters.at(i);
(flashLoanAmount_, amount_) = _calcFlashLoanAmountWithdrawing(adapter, _amount, totalInvested_);
(id, adapter) = protocolAdapters.at(i);
uint256 collateral = IAdapter(adapter).getCollateral(address(this));

// skip if there is no position on this protocol
if (collateral == 0) continue;

flashLoanAmount += flashLoanAmount_;
uint256 debt = IAdapter(adapter).getDebt(address(this));
uint256 assets = priceConverter.wstEthToEth(collateral) - debt;

// withdraw from each protocol in equal weight (based on the relative allocation)
withdrawPerProtocol = _amount.mulDivDown(assets, totalInvested_);
repayPerProtocol = withdrawPerProtocol.mulDivDown(debt, assets);
flashLoanAmount += repayPerProtocol;

callData[i] = abi.encodeWithSelector(
this.repayAndWithdraw.selector,
adapterId,
flashLoanAmount_,
priceConverter.ethToWstEth(flashLoanAmount_ + amount_)
id,
repayPerProtocol,
priceConverter.ethToWstEth(repayPerProtocol + withdrawPerProtocol)
);
}

@@ -388,9 +400,7 @@ contract scWETHv2 is BaseV2Vault {

if (assets <= float) return;

uint256 minimumFloat = minimumFloatAmount;
uint256 floatRequired = float < minimumFloat ? minimumFloat - float : 0;
uint256 missing = assets + floatRequired - float;
uint256 missing = assets + minimumFloatAmount - float;

_withdrawToVault(missing);
}
@@ -406,21 +416,6 @@ contract scWETHv2 is BaseV2Vault {
_finalizeFlashLoan();
}

function _calcFlashLoanAmountWithdrawing(address _adapter, uint256 _totalAmount, uint256 _totalInvested)
internal
view
returns (uint256 flashLoanAmount, uint256 amount)
{
uint256 debt = IAdapter(_adapter).getDebt(address(this));
uint256 assets = priceConverter.wstEthToEth(IAdapter(_adapter).getCollateral(address(this))) - debt;

// withdraw from each protocol based on the allocation percent
amount = _totalAmount.mulDivDown(assets, _totalInvested);

// calculate the flashloan amount needed
flashLoanAmount = amount.mulDivDown(debt, assets);
}

function _harvest() internal {
// store the old total
uint256 oldTotalInvested = totalInvested;
32 changes: 32 additions & 0 deletions test/scWETHv2.t.sol
Original file line number Diff line number Diff line change
@@ -512,6 +512,38 @@ contract scWETHv2Test is Test {
vault.redeem(giftAmount, alice, address(this)); // no shares anymore
}

function test_withdraw_maintainsFloat(uint256 amount) public {
_setUp(BLOCK_BEFORE_EULER_EXPLOIT);

amount = bound(amount, vault.minimumFloatAmount() * 3, 15000 ether);
_depositToVault(address(this), amount);

// invest & create position on aave v3
uint256 investAmount = amount - vault.minimumFloatAmount();
uint256 flashLoanAmount = _calcSupplyBorrowFlashLoanAmount(aaveV3Adapter, investAmount);
uint256 aaveV3SupplyAmount = priceConverter.ethToWstEth(investAmount + flashLoanAmount).mulWadDown(0.99e18);

bytes[] memory callData = new bytes[](2);

callData[0] = abi.encodeWithSelector(scWETHv2.swapWethToWstEth.selector, investAmount + flashLoanAmount);
callData[1] = abi.encodeWithSelector(
scWETHv2.supplyAndBorrow.selector, aaveV3AdapterId, aaveV3SupplyAmount, flashLoanAmount
);

vm.prank(keeper);
vault.rebalance(investAmount, flashLoanAmount, callData);

// withdraw more than what is already in the vault as float
vault.withdraw(weth.balanceOf(address(vault)) + 1, address(this), address(this));

_floatCheck();

// withdraw everything to ensure minimum float logic doesn't apply for the vault's only/last user
vault.redeem(vault.balanceOf(address(this)), address(this), address(this));

assertApproxEqRel(weth.balanceOf(address(this)), amount, 0.015e18, "user's end balance");
}

function test_redeem_zero() public {
_setUp(BLOCK_AFTER_EULER_EXPLOIT);