Basic Solidity Smart Contract for creating your own Yearn Strategy (Strategy.sol
)
Configured github template with Foundry framework for starting your yearn strategy project.
Sample test suite. (tests
)
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()
).
To install with Foundry.
Fork this repository (easier) or create a new repository using it as template. Create from template
Clone your newly created repository recursively to include modules.
git clone --recursive https://github.com/myuser/foundry-yearn-strategy
cd foundry-yearn-strategy
NOTE: if you create from template you may need to run the following command to fetch the git submodules (.gitmodules for exact releases) git submodule init && git submodule update
make build
Sign up for Infura and generate an API key and copy your RPC url. Store it in the ETH_RPC_URL
environment variable.
NOTE: you can use other services.
Use .env file
.env.example
ETH_RPC_URL
, ETHERSCAN_API_KEY
and other example vars
NOTE: If you set up a global environment variable, that will take precedence.Run tests
make test
To deploy the demo Yearn Strategy in a development environment:
TODO
contracts/Strategy.sol
is where you implement your own logic for your strategy. In particular:
Strategy.name()
.Strategy.adjustPosition()
.Strategy.prepareReturn()
.Strategy.liquidatePosition()
.Strategy.exitPosition()
.want
tokens managed by the strategy via Strategy.estimatedTotalAssets()
.Strategy.prepareMigration()
.Strategy.protectedTokens()
.Tests run in fork environment, you need to complete Installation and Setup step 6 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
You can Deploy and verify your strategies if PRIV_KEY and ETHERSCAN_API_KEY are both set in the .env by using the command
make deploy
Before deploying, update the constructor-args variable within the Makefile to include any parameters applicable. Make sure to seperate each argument only by a space, no commas.
The deploy script is coded to deploy the "Strategy" contract within the Strategy.sol file. This can be updated by simply updating to src/YourContract.sol:YourContract.