Due Dilligence: https://hackmd.io/@-c0MLh5CTZOQ3HyKamAsvQ/SkFqB2OG5
This strategy deposits want
into Stargate LP tokens, farms them, sells STG for want.
Update: This strategy also works on Optimism and allows to sell the OP rewards.
After installing the prerequisites (below), run the following:
brownie test tests/Optimism/ -v --network optimism-main-fork --interactive
brownie networks add Optimism optimism-main chainid=10 explorer=https://api-optimistic.etherscan.io/api host=https://optimism-mainnet.infura.io/v3/$WEB3_INFURA_PROJECT_ID multicall2=0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696
brownie networks add optimism-main-fork host=http://127.0.0.1 timeout=120 accounts=10 evm_version=istanbul fork=optimism-main gas_limit=12000000 mnemonic=brownie port=8545
(brownie networks modify optimism-main-fork host=http://127.0.0.1 timeout=120 accounts=10 evm_version=istanbul fork=optimism-main gas_limit=12000000 mnemonic=brownie port=8545)
-
Basic Solidity Smart Contract for creating your own Yearn Strategy (
contracts/Strategy.sol
) -
Interfaces for some of the most used DeFi protocols on ethereum mainnet. (
interfaces/
) -
Sample test suite that runs on mainnet fork. (
tests/
)
This mix is configured for use with Ganache on a forked mainnet.
Let's say Alice holds 100 DAI and wants to start earning yield % on them.
For this Alice needs to DAI.approve(vault.address, 100)
.
Then Alice will call Vault.deposit(100)
.
Vault will then transfer 100 DAI from Alice to itself, and mint Alice the corresponding shares.
Alice can then redeem those shares using Vault.withdrawAll()
for the corresponding DAI balance (exchanged at Vault.pricePerShare()
).
-
Install Brownie & Ganache, if you haven't already. Make sure that the version of Ganache that you install is compatible with Brownie. You can check Brownie's Ganache dependency here.
-
Sign up for Infura and generate an API key. Store it in the
WEB3_INFURA_PROJECT_ID
environment variable.
export WEB3_INFURA_PROJECT_ID=YourProjectID
- Sign up for Etherscan and generate an API key. This is required for fetching source codes of the mainnet contracts we will be interacting with. Store the API key in the
ETHERSCAN_TOKEN
environment variable.
export ETHERSCAN_TOKEN=YourApiToken
- Optional Use .env file
- Make a copy of
.env.example
- Add the values for
ETHERSCAN_TOKEN
andWEB3_INFURA_PROJECT_ID
NOTE: If you set up a global environment variable, that will take precedence
- Make a copy of
- Download the mix.
brownie bake yearn-strategy
To deploy the demo Yearn Strategy in a development environment:
- Open the Brownie console. This automatically launches Ganache on a forked mainnet.
$ brownie console
- Create variables for the Yearn Vault and Want Token addresses. These were obtained from the Yearn Registry. We load them from a different repository found in the brownie-config.yml under dependencies (yearn/[email protected]):
from pathlib import Path
yearnvaults = project.load(Path.home() / ".brownie" / "packages" / config["dependencies"][0]) #load the base vaults project to access the original Vault contract
Vault = yearnvaults.Vault
Token = yearnvaults.Token
vault = Vault.at("0xdA816459F1AB5631232FE5e97a05BBBb94970c95")
token = Token.at("0x6b175474e89094c44da98b954eedeac495271d0f")
gov = "ychad.eth" # ENS for Yearn Governance Multisig
or you can get the contracts ABI from etherscan API, make sure you have exported your etherscan token.
from brownie import Contract
vault = Contract("0xdA816459F1AB5631232FE5e97a05BBBb94970c95")
token = Contract("0x6b175474e89094c44da98b954eedeac495271d0f")
gov = "ychad.eth" # ENS for Yearn Governance Multisig
- Deploy the
Strategy.sol
contract.
>>> strategy = Strategy.deploy(vault, {"from": accounts[0]})
Transaction sent: 0xc8a35b3ecbbed196a344ed6b5c7ee6f50faf9b7eee836044d1c7ffe10093ef45
Gas price: 0.0 gwei Gas limit: 6721975
Flashloan.constructor confirmed - Block: 9995378 Gas used: 796934 (11.86%)
Flashloan deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87
- Approve the strategy for the Vault. We must do this because we only approved Strategies can pull funding from the Vault.
# 10% of the vault tokens will be allocated to the strategy
>>> vault.addStrategy(strategy, 1000, 0, 2 ** 256 - 1, 1_000, {"from": gov})
Transaction sent: 0xa70b90eb9a9899e8f6e709c53a436976315b4279c4b6797d0a293e169f94d5b4
Gas price: 0.0 gwei Gas limit: 6721975
Transaction confirmed - Block: 9995379 Gas used: 21055 (0.31%)
If you are getting a revert error, it's most likley because the vault can't add more strategies, you can check the vault.debtRatio()
the value should be under 10,000. You can try to lower one of the existing strategy debt ratio vault.updateStrategyDebtRatio("0x1676055fE954EE6fc388F9096210E5EbE0A9070c", 0, {"from": gov})
- Now we are ready to put our strategy into action!
>>> harvest_tx = strategy.harvest({"from": accounts[0]}) # perform as many time as desired...
contracts/Strategy.sol
is where you implement your own logic for your strategy. In particular:
- Create a descriptive name for your strategy via
Strategy.name()
. - Invest your want tokens via
Strategy.adjustPosition()
. - Take profits and report losses via
Strategy.prepareReturn()
. - Unwind enough of your position to payback withdrawals via
Strategy.liquidatePosition()
. - Unwind all of your positions via
Strategy.exitPosition()
. - Fill in a way to estimate the total
want
tokens managed by the strategy viaStrategy.estimatedTotalAssets()
. - Migrate all the positions managed by your strategy via
Strategy.prepareMigration()
. - Make a list of all position tokens that should be protected against movements via
Strategy.protectedTokens()
.
To run the tests:
brownie test
The example tests provided in this mix start by deploying and approving your Strategy.sol
contract. This ensures that the loan executes succesfully without any custom logic. Once you have built your own logic, you should edit tests/test_flashloan.py
and remove this initial funding logic.
See the Brownie documentation for more detailed information on testing your project.
Use the --interactive
flag to open a console immediatly after each failing test:
brownie test --interactive
Within the console, transaction data is available in the history
container:
>>> history
[<Transaction '0x50f41e2a3c3f44e5d57ae294a8f872f7b97de0cb79b2a4f43cf9f2b6bac61fb4'>,
<Transaction '0xb05a87885790b579982983e7079d811c1e269b2c678d99ecb0a3a5104a666138'>]
Examine the TransactionReceipt
for the failed test to determine what went wrong. For example, to view a traceback:
>>> tx = history[-1]
>>> tx.traceback()
To view a tree map of how the transaction executed:
>>> tx.call_trace()
See the Brownie documentation for more detailed information on debugging failed transactions.
If you are using Ganache to fork a network, then you may have issues with the blockchain archive state every 30 minutes. This is due to your node provider (i.e. Infura) only allowing free users access to 30 minutes of archive state. To solve this, upgrade to a paid plan, or simply restart your ganache instance and redploy your contracts.
- Yearn Discord channel
- Brownie Gitter channel