Skip to content

Commit

Permalink
External Liquidity Providers (#13)
Browse files Browse the repository at this point in the history
* WIP MultiLP

* forge install: openzeppelin-contracts

v5.0.2

* WIP

* forge install: openzeppelin-contracts-upgradeable

v5.0.2

* WIP redeem queue

* PeggedARM now support one sided and double sided trades
Made the AbstractARM more generic
Improved OETH ARM tests

* Added Lido ARM

* Added Lido ARM fork tests

* Added a Multi LP version of the Lido ARM

* Updated diagrams

* Added convertToShares and convertToAssets to MultiLP
Added donation attack protection to MultiLP

* Added performance fee to MultiLP

* Changed implementation of the performance fee

* Add support for multiple tranches

* Added admin functions to update tranche prices and allocations

* Generated latest diagrams

* Add _claimHook to add liquidity back

* Added AccessControlLP
Made it clear the deposit and withdraw hooks are post function

* Added AccessControlShares

* Split performance fee logic into PerformanceFee contract
Moved calc of perf fee to LP deposit and withdraw instead of swaps

* Fixed saving of new total assets to after asset transfers

* Natspec updates

* Generated new contract diagrams

* Added _gap in inherited contracts

* Changed timestamp that was storing the request time in WithdrawalRequest to claimTimestamp
Fixed compilation of contracts using PerformanceFee

* Refactor PerformanceFee
Renamed liquidityToken to liquidityAsset
Renamed _assetsInWithdrawalQueue to _externalWithdrawQueue

* Generated latest contract diagrams

* Fixed OETH ARM fork tests

* Refactor MultiPriceARM
Started LidoMultiPriceMultiLpARM fork test

* Updated contract diagrams

* Fixes to MultiPriceARM

* More LidoMultiPriceMultiLpARM fork tests

* Added whitelist controls to LidoFixedPriceMultiLpARM
Started LidoFixedPriceMultiLpARM fork tests

* Updated Natspec

* Generated latest contract code

* Removed minimumFunds logic for LP version

* Fix Napspec

* changed setLiquidityProviderCaps to bulk set lp caps
Removed increasing the LP's cap on withdrawals

* Added LiquidityProviderCap event when decrementing the lp cap

* Moved the Liquidity Provider Controller out of the ARM

* Refactor test structure (#16)

* refactor test structur.

* feat: adjust tests for liquidityProviderController.

* forge fmt

* feat: add asOwner modifier

* bump forge version.

* feat: use random address.

* Switch from gitmodules to Soldeer. (#17)

* chore: switch from gitmodules to soldeer.

* chore: update Makefile.

* try to fix CI

* chore: use rpc_endpoints for forking.

* Added Lido ARM deploy script

* Changed the Lido ARM owner to be the mainnet 5/8 multisig

* Anyone can now call collectFees

* Updated README

* Added operator to LiquidityProviderController which will be a Defender Relayer

* Add test for Swaps in LidoFixedPriceMultiLpARM.sol. (#18)

* fix: rename `lidoARM` in `lidoFixedPriceMulltiLpARM` for tests.

* feat: add badToken.

* [WIP] test: add tests for `SwapExactTokensForTokens`.

* test: finalize `swapExactTokensForTokens()`.

* chore: set fuzz runs in config.

* fix: transfer to address dead steth when dealing 0.

* test: add tests for `swapTokensForExactTokens()`.

* fix: adjust `swapExactTokensForTokens()` tests.

* Refactor setPrices for FixedPriceARM

* Renamed MAX_FEE to FEE_SCALE

* Removed old Make upgrade actions

* Changed the _gap's so each contract uses 50 slots

* Updated Lido ARM deploy script

* Updated deploy script

* Added postDeploy Hardhat task

* Merge latest tests (#19)

* [WIP] test: add reverting test for `deposit()`.

* fix: update `lastTotalAssets` before checking caps.

* [WIP] test: add more test for deposit.

* fix: cleanup tests.

* test: add failing test.

* test: add failling test.

---------

Co-authored-by: Clément <[email protected]>

* Added address of ARMBuyback

* Moved initializing lastTotalAssets to _initPerformanceFee

* Moved burn of shares down in requestRedeem

* Fixed applying the performance fee twice on deposits

* fix: increase global queued amount when requestRedeem.

* [WIP] test: add tests for `requestRedeem()`.

* Fixed applying the performance fee twice on deposits

* test: add tests for `requestRedeem()` when ARM profit/loss.

* Updated deposit assertions

* Fix requestRedeem which now stored the updated queued amount

* Updated deploy script

* Added Lido smoke test that uses the deploy script

* Removed experimentation code

* generated contract docs

* Add more tests. (#21)

* chore: add coverage files to gitignore.

* test: add tests for claimRedeem and TotalAssets.

* test: add more tests for setters.

* forge fmt

* Deposit now adds to the withdrawal queue
Added test of deposit with outstanding withdrawal request

* Changed requestRedeem to use convertToAssets instead of previewRedeem

* Add more tests. (#22)

* test: add more test for `totalAssets()`.

* feat: add modifiers to simulate gain or loss assets in ARM.

* fix: use boolean instead of int256.

* fix: use Foundry stdError lib for expectRevert.

* test: add setter test for LCP.

* chore: fix lcov name in .gitignore

* test: add tests for collectFees().

* Fix performance fee calc not removing new perf fee from lastTotalAssets

* Write to storage once in requestStETHWithdrawalForETH

* Changed the preview functions to be external now that are no longer being used internally

* Generated latest contract diagram

* Added more detail to the invalid token error

* Fix compile warning in test

* Ensure swaps don't use withdrawal queue liquidity (#23)

* Ensure swaps don't use WETH reserved for the withdrawal queue

* Fund the withdrawal queue when WETH is swapped into the ARM

* More refactoring of MultiLP
deposit and swap not longer add to the withdrawal queue. Its only need on claim

* Refactor withdrawal queue storage to optimize swap gas usage

* Generated latest contract diagrams

* Removed _postClaimHook as it's no longer needed

* Added more Natspec

* Add more Natspec

* Changed the _postWithdrawHook to _postRequestRedeemHook

* Generated latest contract diagrams

* Minor change to constructor param

* Don't allow prices to cross 1

* Bring in the latest tests (#24)

* test: add test for requestStETHWithdrawamForETH.

* chore: use last version of forge for `mockFunction()`.

* feat: create a fake Lido Withdraw to mock it when testing.

* [WIP] test: add test for `claimStETHWithdrawalForWETH()`.

* fix: tests for claimWithdrawals.

* fix: typo on variable name.

---------

Co-authored-by: Clément <[email protected]>

* Flatten the LidoARM inheritance (#25)

* Flatten LidoARM's inheritance

* Renamed LidoARM contract

* Changed to LidoARM name in tests

* Generated new contract diagrams

* Minor changes to the LidoARM

* Renamed lidoARM variable in tests

* Updated deposit test comments

* Updated setPrice tests

* Small change to setPrices test

* test: add extra tests for deposit while lido withdraw.

* test: add extra test for swap with withdrawQueue full.

* Added switch to LiquidityProviderController so the account level caps can be disabled

* Changed setAccountCapEnabled to only be called by Owner

* fix: use approx eq for steth balance.

* Added accountCapEnabled tests

* fix: use approx eq for steth balance (again).

* More setter tests

* Added setTotalAssetsCap tests

* Added setLiquidityProviderCaps tests

* Updated assertions of initial deploy

* Generated latest contract diagrams

* feat: return true when running test against tenderly testnet.

* Added parseDeployedAddress

* Updated postDeploy Hardhat task

* WIP Broadcast fork section of deploy script

* Added liquidity provider Hardhat tasks

* Fixed setting of token0 and token1 in LidoARM (#27)

* Restore deploy script so fork section uses prank

* Fix hardhat postDeploy task

* Added HH task to snap Lido ARM

* Add Hardhat tasks snapLido, swapLido, submitLido
Renamed the depositLido task

* Added Deposit event
LidoARM was setting token0 and token1 the wrong way around
Used PRICE_SCALE in setPrices
Improved smoke tests

* Generated latest LidoARM contract diagram

* Handler Tenderly testnet (#26)

* fix: handler better tenderly testnet

* fix: use -unlocked keywork to broadcast & impersonate

* feat: add initial deployer test address.

* fix: give enough gas to deployer for Tenderly test.

---------

Co-authored-by: Nicholas Addison <[email protected]>

* Fixed ARM acronym in Natspec

* Removed postDeploy Hardhat task now the forge script is working

* Simplified deposit to WETH

* Fixed deploy script when run as a fork

* Removed approveStETH as its in the initializer

* changes to setPrices to make it easier to understand

* Fix bug in _rawTotalAssets not checking feesAccrued before subtraction

* Renamed total assets to available assets (#28)

* Renamed _rawTotalAssets to _availableAssets
Renamed lastTotalAssets to lastAvailableAssets

* Generated new LidoARM diagrams

* Update Natspec

* Added Lido contract dependency diagram

* Added Hardhat tasks requestRedeemLido and claimRedeemLido

* Adjust lastAvailableAssets post deposit and redeem so lastAvailableAssets is up only

* Reordered the storage slots for fee variables

* Save gas on WETH balanceOf call on swaps of WETH out if no outstanding withdrawals

* Only accrued fees when they are collected (#30)

* Only accrue fees when the fees are collected

* packed fee and lastAvailableAssets back into a single slot

* Generated latest LidoARM contract diagram

* Simplification of claimRedeem

* Generated latest LidoARM contract diagram

* Aligned the storage slot gap

* Changed isClaimable to claimable

* Limit performance fee to 50%

* Fixed visibility of fee functions

* Natspec update

* Return 0 on early exit

* emit at end of the function

* Collapsed LidoLiquidityManager into LidoARM

* Fixed claimRedeem tests with new event order

* Removed LidoLiquidityManager as it's now in LidoARM

* Generated latest contract diagrams

* Clean up _availableAssets

* cleanup claimRedeem

* Renamed LiquidityProviderController to CapManager

* Renamed outstandingEther to lidoWithdrawalQueueAmount

* Updated contract diagram

* Updated _transferAsset to use seth

* simplified _transferAsset

* Fixed collectFees so it doesn't use WETH reserved for the withdrawal queue

* changed _liquidityAvailable to _requireLiquidityAvailable

* Limited prices to crossing 2 basis point past 1.0

* Changed so buy price has to be below 1

* Increases initial total cap

* Made claim delay immutable and only 1 minute for Testnet

* Generate latest LidoARM diagram

* Add Zapper for LidoARM (#31)

* feat: create Zapper contract.

* test: add tests for Zapper.

* test: add extra test for rescue tokens.

* Added assets (ETH) to Zapper's Zap event

* Added ZapperLidoARM to deploy script

* Updated deploy script for Testnet

---------

Co-authored-by: Nicholas Addison <[email protected]>

* updated snapLido Hardhat task

* Added collectFees Hardhat task

* Update Natspec

* Updated contract diagrams

* Added crossPrice to check when Operator sets prices

* Set cross price in fork section fo deploy script

* Safety check in setCrossPrice so no stETH in ARM when cross price reduced

* Allow the cross price to be 20 basis points below 1

* totalAssets returns MIN_TOTAL_SUPPLY if fees > available assets
setPrice enforcement of crossPrice also applies to Owner to ensure no asset losses
remove old Price cross check as that's replaced by the crossPrice check

* Generated latest LidoARM contract diagram

* Updated Natspec

* Now pricing the base assets, stETH, using the crossPrice

* fix: make proxy falback payable.

* Price stETH in the withdrawal queue at 1.0 as it can't be sold for any less

* Fixed when fees = available assets in totalAssets

* Natspec updates

* Add Zap compliant with Liquidity Provider Cap (#32)

* feat: add zap to deposit.

* Updated Natspec

---------

Co-authored-by: Nicholas Addison <[email protected]>

* Generated latest contract diagrams

* standardizes deal in smoke tests

* Renamed request and claim Lido withdrawals

* Natspec update

* More info to snapLido Hardhat task

* Added requestLidoWithdrawals, lidoClaimWithdraw and lidoWithdrawStatus Hardhat tasks

* Added setZap to deploy script

* depositLido now supports depositing ETH using the Zapper
Added setZapper Hardhat task

* fix deploy script

* Added more logs to the deploy script

* Made claimable external as it's not used internally

* Renamed withdrawalQueue to lidoWithdrawalQueue so its not confused with the ARM withdrawal queue

* Updated contract dependency diagram

* Removed te extra logic that transfers and extra 2 wei for stETH

* snapLido now compares against 1Inch, Curve and Uniswap

* Change Zap integration in ARM. (#33)

* fix: remove zap restricted method..

* style: change liquidity provider param name.

* fix: change state then write call.

* fix: state change then transfer token.

* fix: use `fees` instead of `fee` when collecting fees.

* fix: move up `nextWithdrawalIndex` increase.

* fix: avoid code duplication.

* fix: pack variable action (read & write).

* Added gas costs to lidoSnap

* Added RequestLidoWithdrawals and ClaimLidoWithdrawals events

* put nextWithdrawalIndex into its own slot

* Updated comments for setCrossPrice

* Clearer setPrices comments
Fixed comment typos

* Generated latest LidoARM contract diagrams

* Pre launch changes (#34)

* Set buy and sell prices in initialize function
setCrossPrice also validates buy and sell prices against the new cross price

* fix: use `newCrossPrice` instead of `crossPrice`.

* fix: use `<=` instead of `<` for `setCrossPrice`.

* test: add reverting test for `setCrossPrice`.

* fix: revert commit `a7947f3`.

* fix: move `setCrossPrice` test to new file.

* test: add more test for `setCrossPrice`.

* feat: allow anyone to call claimLidoWithdrawals.

* fix: adjust failing test

* fix: remove testing stuff.

---------

Co-authored-by: Clément <[email protected]>

---------

Co-authored-by: Clément <[email protected]>
Co-authored-by: Clément <[email protected]>
Co-authored-by: Daniel Von Fange <[email protected]>
  • Loading branch information
4 people authored Oct 17, 2024
1 parent 83a0e4c commit f506939
Show file tree
Hide file tree
Showing 97 changed files with 9,869 additions and 449 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Install Dependencies
run: forge soldeer install

- name: Run fork tests
run: forge test -vvv --summary --detailed
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ artifacts

# Forge
.gas-snapshot
dependencies/
soldeer.lock

# Coverage
lcov.info*

# Defender Actions
dist
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"solidity.packageDefaultDependenciesContractsDirectory": "src",
"solidity.packageDefaultDependenciesDirectory": "lib",
"solidity.compileUsingRemoteVersion": "v0.8.23+commit.f704f362",
"solidity.formatter": "forge"
"solidity.formatter": "forge",
"cSpell.words": ["traderate"]
}
35 changes: 19 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ default:
# Always keep Forge up to date
install:
foundryup
forge install
forge soldeer install
yarn install

clean:
@rm -rf broadcast cache out

clean-all:
@rm -rf broadcast cache out dependencies node_modules soldeer.lock

gas:
@forge test --gas-report
Expand All @@ -19,14 +26,20 @@ snapshot:
@forge snapshot

# Tests
test-std:
forge test --summary --fail-fast --show-progress

test:
@forge test --summary
@FOUNDRY_NO_MATCH_CONTRACT=Invariant make test-std

test-f-%:
@FOUNDRY_MATCH_TEST=$* make test
@FOUNDRY_MATCH_TEST=$* make test-std

test-c-%:
@FOUNDRY_MATCH_CONTRACT=$* make test
@FOUNDRY_MATCH_CONTRACT=$* make test-std

test-all:
@make test-std

# Coverage
coverage:
Expand All @@ -49,20 +62,10 @@ deploy:
@forge script script/deploy/DeployManager.sol --rpc-url $(PROVIDER_URL) --private-key ${DEPLOYER_PRIVATE_KEY} --broadcast --slow --verify -vvvv

deploy-testnet:
@forge script script/deploy/DeployManager.sol --rpc-url $(TESTNET_URL) --private-key ${DEPLOYER_PRIVATE_KEY} --broadcast --slow -vvvv
@forge script script/deploy/DeployManager.sol --rpc-url $(TESTNET_URL) --broadcast --slow --unlocked -vvvv

deploy-holesky:
@forge script script/deploy/DeployManager.sol --rpc-url $(HOLESKY_URL) --private-key ${DEPLOYER_PRIVATE_KEY} --broadcast --slow --verify -vvvv

# Upgrade scripts
upgrade:
@forge script script/002_Upgrade.s.sol --rpc-url $(PROVIDER_URL) --private-key ${DEPLOYER_PRIVATE_KEY} --broadcast --slow --verify -vvvv

upgrade-testnet:
@forge script script/002_Upgrade.s.sol --rpc-url $(TESTNET_URL) --private-key ${DEPLOYER_PRIVATE_KEY} --broadcast --slow -vvvv

upgrade-holesky:
@forge script script/002_Upgrade.s.sol --rpc-url $(HOLESKY_URL) --private-key ${DEPLOYER_PRIVATE_KEY} --broadcast --slow --verify -vvvv
@forge script script/deploy/DeployManager.sol --rpc-url $(HOLESKY_URL) --private-key ${DEPLOYER_PRIVATE_KEY} --broadcast --slow --verify -vvv

# Override default `test` and `coverage` targets
.PHONY: test coverage
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,18 @@ function swapTokensForExactTokens(
### Install

```
foundryup
forge install
forge compile
make install
cp .env.example .env
```

In the `.env` file, set the environment variables as needed. eg `PROVIDER_URL` for the RPC endpoint.

### Format and Compile

```
make
```

### Running tests

Fork and Unit tests run with the same command, as the fork is initiated on the test file itself if needed.
Expand Down
13 changes: 11 additions & 2 deletions build/deployments-1.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
{
"executions": { "001_CoreMainnet": 1723685111 },
"executions": {
"001_CoreMainnet": 1723685111,
"002_UpgradeMainnet": 1726812322,
"003_UpgradeLidoARMScript": 1729073099
},
"contracts": {
"LIDO_ARM": "0x85B78AcA6Deae198fBF201c82DAF6Ca21942acc6",
"LIDO_ARM_CAP_IMPL": "0x8506486813d025C5935dF481E450e27D2e483dc9",
"LIDO_ARM_CAP_MAN": "0xf54ebff575f699d281645c6F14Fe427dFFE629CF",
"LIDO_ARM_IMPL": "0x3d724176c8f1F965eF4020CB5DA5ad1a891BEEf1",
"LIDO_ARM_ZAPPER": "0x01F30B7358Ba51f637d1aa05D9b4A60f76DAD680",
"OETH_ARM": "0x6bac785889A4127dB0e0CeFEE88E0a9F1Aaf3cC7",
"OETH_ARM_IMPL": "0xd8fF298eAed581f74ab845Af62C48aCF85B2f05e"
"OETH_ARM_IMPL": "0x187FfF686a5f42ACaaF56469FcCF8e6Feca18248"
}
}
46 changes: 46 additions & 0 deletions docs/CapManagerHierarchy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions docs/CapManagerSquashed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions docs/LidoARMHierarchy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f506939

Please sign in to comment.