Writing Yearn strategies - Facu Ameal aka @saltyfacu at @ETHDubaiConf

ETHDubai 2022 · 31 Mar 2022 · 17:57

Facu Ameal discusses developing strategies for Yearn yield vaults.

Speakers

Topics

From ETHDubai 2022, hosted by DeFi Dubai.

Transcript

okay hello everyone uh today i want to talk to you about uh how to become a strategist it's actually pretty easy we are here to debunk a myth that you need to be a hardcore developer linux core developer to be able to be a strategist because that's uh that's not true actually the most important thing to become a strategist is to write a strategy and to do that you have to understand how things work that's much more important that writing complicated code or or fancy stuff the important thing is to understand the financial aspects and how the protocols interact between them seems simple it's not that simple but um learning solidity is much more simple so let's let's go what are the votes and what are the

strategies okay the the votes are like a pig bank that's uh they're super simple you just deposit tokens and the strategies are going to grab tokens from the strategies to invest them in the strategies we have something that we call the want token the one token is they want is the token that you want to accumulate more so it's going to be compounding that token that's what the strategies do these are the definitions by andre uh which are quite interesting because uh you can think of us bolts and strategy to be complicated but they are pretty simple okay let let's get into some uh technical details about the votes thank you

uh the votes are written in viper the uh the idea is not to complicate your life with the viper the the thing is that you're not going to be interacting with the vote code a lot most of the interactions between the strategies and the vaults are already coded in the base strategy the the benefits of having the voltas and erc20 is that you will be able to transfer it because it's a share it's just a share you're going to be able to do leverage which is cool and any other thing that you do with your with an erc20 with are more boring actually um they both are going to keep track of your shares the price per share which is the the value that is going to accrue uh when you get more money when you get yield

and the the good thing is in mutual code we're not using any proxy uh weird stuff because we want our users to be sure that they are going to be using exactly the same code every every single time that's important because you can audit the code and you can check what we're doing with the with your money which is super important though with the v2 design the big highlight multiple strategies up to 20 strategies that's super important because uh it's super difficult to deploy um big chunks of money into a single protocol so with multiple strategies what we achieve is to be able to deploy smaller portions into different protocols with different strategies and we use the keeper network it's at

the centralized bot network so we keep everything the centralized and with automation well we wanted to make it more complicated so we wrote the strategy in solidity actually that that makes it more like easier because it's more popular and you have inheritance so what we did is we coded as i told you before everything uh everything to interact with the vault in a in another contract the base uh strategy those sol so you're able to import that contract inherit it and you don't have to code all that boring stuff that's always the same you only have you're going to have to code only the the main part of your strategy the investment part which is the fun part actually

um the strategy is going to manage the accounting report the the profits and the loss to the to the vault it's going to invest and invest collect the rewards and recycle them we're going to see how it works uh later in the presentation and it's also going to do all the heavy lifting like checking the sleepage harvesting returns which is collecting the the rewards and keeping the collateral ratio healthy which is uh for strategies that are debt based like a maker dow cdp you don't have to manually check your cdp every time so you don't get liquidated the strategy is going to do it for you so well this is this is this is really cool because it it

gives you an idea of how a bolt is composed this is the usb the usbc volt this is from a few days ago so you're going to see that uh we have one two three four five six eight eight strategies in my if i'm content right uh that are attached to the vault every single strategy has a different percentage allocation because it doesn't it doesn't matter uh that we have nine or eight or nine strategies we are not going to to assign the same amount of capital to each of them we assign the the capital based on simulations to achieve the best apy and also we cap the percentage we assign by the risk that the strategy has so we want to keep the funds as safe as

possible and also you are going to notice that the last three strategies they have zero capital allocation that's really cool because we can have new strategies ready for the opportunity comes and i'll assign capital when the when this best moment because we want to maximize yield all the time okay here's uh another graph about how the vaults and the strategies interact the vault is the smart contract front-end to call it somehow for the user the user is never going to interact with the strategy directly we don't want that for security reasons we only let the user interact with the vault all or most of the strategy public functions are permissioned nobody

can touch them and it's the strategy the one that is going to interact with other protocols or the taxes so i want to have multiple strategies but the strategy can only have one bolt that is because we only have one token per volt we want to keep it simple for the users so that's that's also it's going to be easier when you build your first strategy because you're only going to have to manage one token okay let's go to this uh complicated diagram it's not that complicated actually uh these are all the parts that interact with the strategy usually so you have the keeper network which are which are bots basically the bolt the strategy a different protocol it can be for example compound that were like

the positioning co to compound um borrowing money to get uh comp rewards and then attacks uni swap sushi swap whatever you want to call it why why we use a text always because we want to exchange the rewards in the case of compound the comp token for more die for example that's the one token the one token is die we want to get more more and more and more die so how does it work the numbers that you see there is the order in which the uh the transactions the actions are executed so the first action is the keeper monitoring something that is called the harvest trigger that's um that's only a boolean but there's a logic behind telling the keeper now it's now it's a good moment to call harvest

and to execute the next the next steps why because sometimes you want to accrue more tokens because of gas cost imagine having to recycle i don't know like every hour having to sell tokens on ethereum that's crazy the gas the gas cost with the eat us alive uh so when the harvest trigger is true the keeper bud is going to run what we call harvest the harvest function it has simple tasks the first one is that um harvest is going to in the strategy the strategy is going to claim the rewards from the d5 protocol is going to get the rewards and then just swap them in at x for more tokens that we want the tokens that the vault is managing the last thing the last

two things two three things it's reporting to devote the accounting because with the vault shares what it's going to increment is the price per share and just that you're not going to it's not reversing you're not going more you're not getting more tokens into your wallet the price per series is the thing changing uh it's going to execute at just position at just position we're going to see it a bit more later but what it does is um adjust the percentage of that allocation because if we assign to a strategy for example one percent and because of the rewards it ends up having five percent or whatever we don't want that to happen because uh remember that the percentages that we allocate are uh risk based also so we want to

keep always the same data location when all that is done send success and that's it all these complex diagrams it's pretty much abstracted from you anyways well this has been the like the boring part theory theory theory let's talk about uh what you need to write a strategy first this is what i got when i wrote my first strategy the the software is like git is brownie which is a development framework ganache which is going to help you get a main network so you can test your strategy before deploying it uh the brownie mix is our repo it's a it's actually a template that you can you can use and it has already made the tests and all the code

replays with nodes that where you have to complete with the logic of your strategy some programming experience so i used to code in php and that's pretty much it and i learned solidity using cryptozombas which is super fun i did till level four and for for me that was enough to be able to call the strategy uh then the the two most important stuff understanding how things work not only how the bolt and the strategies interact but also how the the e5 protocols interact between themselves and how the slippage works and all that stuff that's the important stuff and a ton of good power you gotta sit down and start understanding how things work so that requires hours sitting down from the computer and trying to figure things out

so you want to write a strategy you have a great idea but we have a process um this process is because we are focused on security for us security is super important we never risk the the principle uh so that's why we have uh due diligence that's the first step utilization is a little investigation of the protocol uh then you code the strategy then two peers have to review it because we all know how audits are these days they are super expensive and they take a lot of time if we have to audit every strategy we're dead we are we're going to lose every yield opportunity let us go to experimental core dev review which is an internal auditors inside of yearn

the state farming committee is going to re uh really review it and then it goes through production let's go more in depth into these uh steps well what do we check uh in the due diligence part well actually it's something the strategy has to do for each new protocol we do an in-depth investigation not only of the code like auditing the the code but also how it works and who is owning that contract and that's super important for example you're going to see a lot of aspects here but um i i like to highlight the breakability part uh that is are the owners of the contracts or the protocol able to rockford the users to like take the use of funds for example

for multi-six there seems to be super safe it's like someone tell you hey yeah we have a multisee what's the threshold one of ten now you have ten people that are going to be able to execute transactions from that multisig that's not good that's not really good so you have to also ask about the the threshold and verify it on chain so you need to have like something like three or five or six of nine people so they cannot collude and steal your user's funds and then we check the audit reports how the protocol works etc etc and we have a path to production which is the process that the strategy is going to take to go to production so well now how to code and this is this is only one slide because it's so simple

we did it in a workshop yesterday and in three hours we had something simple working so you cloned the brownie mix strategy mix um and to code the strategy you only need to code three basic functions all the rest is going to be like dependencies from the code that you put in these three functions the first one is adjust position the only thing it does is invest the funds it's going to for example deposit into a masterchef contract and that's it the second one is the the other way around is liquidated position so what it's going to do is going to withdraw money from the in this case in our example from a master chef contract we are we want to assure to all our users that they can withdraw at any

point in time so there's no way for us to block that withdrawals in the bots and the the final one is prepare return this is the one that has a little bit more complexity because you have to build the logic behind your strategy pretty much there this is the one that is going to claim the rewards is going to sell the rewards using a dex and it's going to send a report to the vault saying hey this is how it went this is how much we won or we didn't so this is where the business logic goes well and then write tests that's also kind of boring it's not nice they take time they take time to run they're not fun they're not glamorous but it's the only way to assure that your strategy is not going to have problems in production um

let's see how this is this is the brownie mix these are the tests that you're going to have by default when you when you use the repo those are like four tests because the conf test it's um it's a configuration one this is not the ideal scenario but this they say we test improv this is a production strategy it has like i don't know 15 tests this is the way to do it you have to test everything possible like every edge case what happens if i airdrop some tokens to a strategy is it going to work well is it going to lock the rewards uh all the weird stuff we have to test it because when we send it to production it's too late so let's say you go to your strategy you have uh amazing tests uh two peers reviewed your

strategy and you are able to pro to publish it into app tags eight tags is not yarn it's a platform that the strategy is to maintain we try to make it funny but with the right disclaimer that warning there says that if you deposit there you're pretty much going to lose your money this is the testing product we do which is two emojis a funny name and we try to promote it for users to deposit and to get like early yields for more risky things but we're just testing it this is testing next step this is uh part of our process the safe farming committee so you have to convince a 12 people committee that your strategy is

safe and it's worth going to production so they're going to review the whole due diligence they're going to ask a lot of questions uh but it's pretty fast if you're convinced that the strategy is good and it's performing well it's going to be fast then a core team member is going to review it because as i told you before getting an audit here it's impossible so what we do is we get a review from a core team reviewer we have two security auditors internally and if the strategy is getting bigger and bigger we get a committee and then we get then we get another but after strategies of reproduction and reviewed by at least three or four people and with some time in testing so it's like it's the banking also a bit that means the myth that we

test in prague there are stuff that you need to test in product but you need to make sure that all these steps are there if not maybe it's not going to work well well this is the last step this is like super verbose i don't know why because it's super simple it's just sending it to governance so when all this is done uh governance is going to either endorse the vault it's what we call it the technical term if you want the function we call in the in the register we have or add the strategy to the to a pre-existing vault so if you are making a strategy for a new token anyways it's super easy it's just pasting the address in in one of the configuration parts of the deployment and that's it but it's going

to deploy a new vault with a new strategy that you created and endorse it by governance if not it's going to attach it to a previous generative vault well that's it your strategy is in production you are great applause you are making a lot of money yeah but strategies are not something that you set and forget uh adjourn with monitor everything because uh i will get again the example of compound when the compound rewards got stuck got a problem we were monitoring the time lock so we knew ahead of time that those rewards were not going to be there and the strategy might get locked so we took preventive action and nobody even

noticed that we might have had an issue because we were monitoring in d5 things change so monitoring is essential so you don't have to leave it's not set it don't forget it and you get reached from one day to other no you have to keep monitoring and you are the one who made the strategy so stay around you're the one who knows so when something happens you you can help fix it and that's it guys help us build the future you're welcome to join yarn and start to build the strategy together all right