Skip to content

Create Yearn V3 "Tokenized Strategies" using Foundry

Notifications You must be signed in to change notification settings

yearn/tokenized-strategy-foundry-mix

Repository files navigation

Tokenized Strategy Mix for Yearn V3 strategies

This repo will allow you to write, test and deploy V3 "Tokenized Strategies" using Foundry.

You will only need to override the three functions in Strategy.sol of _deployFunds, _freeFunds and _harvestAndReport. With the option to also override _tend, _tendTrigger, availableDepositLimit, availableWithdrawLimit and _emergencyWithdraw if desired.

For a more complete overview of how the Tokenized Strategies work please visit the TokenizedStrategy Repo.

How to start

Requirements

  • First you will need to install Foundry. NOTE: If you are on a windows machine it is recommended to use WSL
  • Install Node.js

Clone this repository

git clone --recursive https://github.com/yearn/tokenized-strategy-foundry-mix

cd tokenized-strategy-foundry-mix

yarn

Set your environment Variables

Use the .env.example template to create a .env file and store the environement variables. You will need to populate the RPC_URL for the desired network(s). RPC url can be obtained from various providers, including Ankr (no sign-up required) and Infura.

Use .env file

  1. Make a copy of .env.example
  2. Add the value for ETH_RPC_URL and other example vars NOTE: If you set up a global environment variable, that will take precedence.

Build the project

make build

Run tests

make test

Strategy Writing

For a complete guide to creating a Tokenized Strategy please visit: https://docs.yearn.fi/developers/v3/strategy_writing_guide

NOTE: Compiler defaults to 8.23 but it can be adjusted in the foundry toml.

Testing

Due to the nature of the BaseStrategy utilizing an external contract for the majority of its logic, the default interface for any tokenized strategy will not allow proper testing of all functions. Testing of your Strategy should utilize the pre-built IStrategyInterface to cast any deployed strategy through for testing, as seen in the Setup example. You can add any external functions that you add for your specific strategy to this interface to be able to test all functions with one variable.

Example:

Strategy _strategy = new Strategy(asset, name);
IStrategyInterface strategy =  IStrategyInterface(address(_strategy));

Due to the permissionless nature of the tokenized Strategies, all tests are written without integration with any meta vault funding it. While those tests can be added, all V3 vaults utilize the ERC-4626 standard for deposit/withdraw and accounting, so they can be plugged in easily to any number of different vaults with the same asset.

Tests run in fork environment, you need to complete the full installation and setup to be able to run these commands.

make test

Run tests with traces (very useful)

make trace

Run specific test contract (e.g. test/StrategyOperation.t.sol)

make test-contract contract=StrategyOperationsTest

Run specific test contract with traces (e.g. test/StrategyOperation.t.sol)

make trace-contract contract=StrategyOperationsTest

See here for some tips on testing Testing Tips

When testing on chains other than mainnet you will need to make sure a valid CHAIN_RPC_URL for that chain is set in your .env. You will then need to simply adjust the variable that RPC_URL is set to in the Makefile to match your chain.

To update to a new API version of the TokenizeStrategy you will need to simply remove and reinstall the dependency.

Test Coverage

Run the following command to generate a test coverage:

make coverage

To generate test coverage report in HTML, you need to have installed lcov and run:

make coverage-html

The generated report will be in coverage-report/index.html.

Deployment

Contract Verification

Once the Strategy is fully deployed and verified, you will need to verify the TokenizedStrategy functions. To do this, navigate to the /#code page on Etherscan.

  1. Click on the More Options drop-down menu
  2. Click "is this a proxy?"
  3. Click the "Verify" button
  4. Click "Save"

This should add all of the external TokenizedStrategy functions to the contract interface on Etherscan.

CI

This repo uses GitHub Actions for CI. There are three workflows: lint, test and slither for static analysis.

To enable test workflow you need to add the ETH_RPC_URL secret to your repo. For more info see GitHub Actions docs.

If the slither finds some issues that you want to suppress, before the issue add comment: //slither-disable-next-line DETECTOR_NAME. For more info about detectors see Slither docs.

Coverage

If you want to use coverage.yml workflow on other chains than mainnet, you need to add the additional CHAIN_RPC_URL secret.

Coverage workflow will generate coverage summary and attach it to PR as a comment. To enable this feature you need to add the GH_TOKEN secret to your Github repo. Token must have permission to "Read and Write access to pull requests". To generate token go to Github settings page. For more info see GitHub Access Tokens.

About

Create Yearn V3 "Tokenized Strategies" using Foundry

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published