Write investment DeFi strategies in solidity with 0xSmit for devs
ETHDubai 2024 · 20 Apr 2024 · 42:15
Smit walks through a Solidity yield strategy using Beefy vaults and Silo Finance. The workshop explains deposit receipts, share accounting, reward harvesting, swaps, reinvestment, fees, and caller incentives, then demonstrates a harvest on a fork and discusses adapting strategies for other protocols.
Speakers
Topics
From ETHDubai 2024, hosted by DeFi Dubai.
Transcript
so I'll start the session um am I audible to everybody okay uh so little bit of introduction on my side uh I'm Smith uh I got started with crypto around a decade ago uh in school uh I have a background in cyber security have helped some companies like telegram dap node Uber Ghana with cyber security um got into smart contracts around 2016 team work with some large exchanges when it comes to wallets infrastructure security uh recently I led Investments and Technical diligence for a web3 venture fund uh right now I'm a security researcher and I'll be your instructor for this session so I'll be covering def5 strategies uh tell me how many of
you have coded in solid before okay quite nice and how many of you know what a defi strategy is okay so I I'll take a brief uh example of how uh strategy Works what a strategy is and you know why are their strategies why don't why do people pull money for a strategy so uh for strategy we can take a simple example as a bank so let's say if you have $100 if you have $100 if you have $100 you all deposit the money into a bank and then Bank lends out that money to uh you know different borrowers makes the interests and gives you your money back so
similarly there are certain pools certain Farms so today we'll be taking an example of Silo Finance so on Silo uh Silo is a uh defi protocol so when you deposit some eth on Silo uh you get yield so let's say just like you put money into the bank um um you you put let's say $100 into the bank and you get $3 as yield per year similarly you get yield but what silot does is in order to incentivize people to deposit money into Silo Silo gives you some governance tokens as free money as as yield or as rewards uh so let's assume Silo gives you when you put in $100 Silo gives you
$3 as $3 as yield and it gives you like $7 of its own tokens so you you end up making like $10 in yield at the end of the year so you make 10% deal at the end of the year but now the thing is silo is giving you its own tokens and its value may be going up maybe going down what if uh every single day you can go to Silo and say that okay you have my $100 you are giving me 7% yield on Silo tokens how about you give me the yield today I sell those tokens I buy dollars and deposit again so that's what we call autoc compounding uh is is it clear or am I going too
fast yeah so Silo gives you its native token uh and it gives you every minute like every with every block so whatever interest is accured so here in this strategy we are dealing with with strategy uh this is the strategy we would be dealing with so we would be depositing eth into the strategy uh now if you look if you look at the uh strategy uh you you receive 3% uh and 10% as APR and there is a boost APR so Silo is giving you 4.5% as extra APR and that's what we going to do like we will be building a strategy who sole a and and when you deposit money into
Silo and when you autoc compound it there is some fees Associated so smart contracts are not self-executing like it cannot execute on its own somebody has to call the smart contract to execute it but let's assume you have put in $10 I have put in $10 somebody has put in $10 somebody has put in $100 but in order to autoc compound average cost you pay per transaction is like $20 now it wouldn't make sense if you are paying $20 every day because you have only deposited like $100 so what what we building is we're building a strategy wherein we can pull everybody's money uh like if you look at this strategy here there's 1.88 million deposited by several users this strategy
then takes that money puts it into Silo and earns interest on top of it and whatever Silo tokens that we receive for free uh if you look there's something called as last Harvest so there's a function in the smart contract which we call to harvest our tokens we ask Silo that okay whatever free tokens that you are going to give us give us right now we'll sell it we'll get eth and again deposited again so we are technically autoc compounded it so that's that's our goal so for 1 million paying $10 or $15 in fees once is not a big deal right but if you have deposited $100 you paying $10 every day you'll exhaust your $100
in 10 days but somebody paying $10 for 1.88 million is is still uh a very small amount right so like 45 minutes is quite less to cover entire code of the strategy so what I'll do is I'll I'll break the strategy down into two parts one is what we call a vault and one is what we call a strategy uh I'll run by the code and I'll give you the code in a zip file because if we go and start you know deploying right now one R may not be enough what's say like do you guys want to deploy or should we go over the code sorry we go with the code yeah
so we deploy so there are two parts to this strategy one is the strategy and one is the con one is the w so what I'll do is I I'll put this into a zip file and give you guys a link uh so you can download it on your computers and browse it just give me a minute so you guys can to open this link tinyurl.com Dubai defi U if you want a QR code maybe to scan here's a QR as
well sorry uh it's a zip file yeah lower the so if if you're not able to scan it just type in tiny url.com Dubai def uh were you all able to open it okay so in that uh uh so if you go to Strat then there's a
folder called contracts then Biffy then strategies and then Silo and then strategy Silo uh so you guys got it okay so there are two main smart
contracts that we use here one is called as strategy and one is called as uh Vault so what we do is basically we take user deposits into vault uh this is the W contract so this is this is the W so this is the Vault contract uh so there are some helper variables and functions that we uh have here uh so Bey we are using be's VA so Bey is a very famous autoc compounding uh dap that a lot of people use to deposit their money and they automate
autoc compounding with that so what with B what we are doing is we building a smart contract called as Walt uh you can ignore the first variable right now strategic and so we declare a variable called strategy which will hold the address of our smart contract which is called a strategy which contains the actual code which will which will be a Swiss army knife which will take our money deposit into Silo autoc compound all that stuff uh so this contains some functions so let's say if a developer makes any changes to this there should be some delay uh before the changes go live so we have something called as approval delay but we won't go get into that uh so there's this function called as
initialize which we initially call to uh deploy our uh Vault contract so in that so our our wall contract is essentially an erc20 token so when you when you deposit money into the bank okay you you get some receipt like you you have it on your pass book you have it uh somewhere so when you when you let's say buy government bonds you you deposit money with the government and you get a bond so here in in our wal contract we'll be issuing erc20 tokens for all the people who have deposited money into our WS so initially when let's say somebody deposits $100 we give them 100 tokens and um now nobody else has deposited money into our wall 100 token
tokens are out there in the market but with the yield our tokens are now 107 so now the 100 tokens that we issued are now equivalent to the actual $17 so that's that's our strategy here and then so we initialize it uh we give a name to this uh erc20 token we can give it maybe name as B strategy Silo strategy something like that we give it a symbol uh we need to own that contract so we we using open Zeppelin open Zeppelin is are some open source smart contracts that we use uh so that we don't have to write a lot of code lot of redundant code open Zeppelin um are some open source smart contracts that
you can use to deploy ERC 20s ERC 721s um so we pass these four things while creating a new Vault uh one is the name of the erc20 want the symbol uh the strategy smart contract so before the we need to deploy a strategy we I I'll go to that in a moment uh and there is an approval delay so let's say um let's say if there's a if a developer makes any changes to the smart contracts people need to know that okay somebody has made a changes maybe I want to withdraw my money something like that let's say so in in this smart contract because we are managing people's money we we have a fee of 5% that we are charging to the users uh for managing their money but let's say if
tomorrow a developer increases it to like 50% people may not want to to uh have their money into the VA so there's something called as approval delay uh so let's say I if I deploy new strategy today maybe it goes live 7 Days Later not before that um so there are some helper functions that we deploy deploy which is uh want so basically what is want want function returns as the token that we are taking so in in this strategy uh in this strategy are taking weet as the token so when we call this it will give us the
address of the token so uh I have these tokens deployed here you can call it right now so when we click on want it gives us an address so this is the token token that the strategy or the Walt wants we cannot deposit any other token into this apart from the want token so what is what is the want token if you go to Ether scan check this out it is actually the wet token just like we went in the last time so we Define balance so because we
are issuing new shares new new arc2 tokens to map it with the money that's in the strategy uh we have a a balance function we also have an available function that like it is uh an extra function we not using it right now but it is added just in case we add any new uh Ines to it so as I said like let's say if you deposit $100 I issue so strategy Issues new 100 tokens uh which guarantees that you own uh 100 tokens in through the wall but over time we are auring interest so the $100 you put in is now worth
$107 so what we do is we we see that how much outstanding amount of tokens do we have in the market and how much have do we have in hand so let's say if we have 100 tokens in outstanding in the market and in hand we have 107 so now we proportionally divide it so each one token is now equal to 1.07 so for that we have a function to calculate that uh we have a function called deposit all which basically does the same thing as deposit so this is the function which our users will be calling to put in the money so what it does is it takes the money from the user and deposit it into strategy so it calls the strategy and
calls a hook called as before deposit so let's go into the strategy function yeah so basically before deposit and after deposit are hooks let's say if we
want to do certain calculations let's say if we want to charge let's say 0.1% on every deposit maybe we can add a function here but right now we are not charging anything on deposits so the functions are empty then we we calculate our own balance what are the what is the amount of ERC 20s that we have issued in the market uh then we save transfer from the user so let's say if you have called the smart contract whatever amount you have asked so when you call the function you pass an amount let's say you want to deposit to eth so we call the want function we do a safe transfer from and
the amount gets transferred into the Vault smart contract then once that is done WT smart contract calls something called as earn function now what the earn function does is this here's our ear function so our earn function takes whatever balance we have so we have a VA contract wherein we deposit eth and then the wall contract takes it to the strategy contract so for that amount of time we whatever eth the VA contract has we check what balance we have so we check whatever balance we have then we
trans it to strategy we we use the safe transfer from function and transfer it to strategy and then we call the function strategy. deposit now we go to strategy when the W contract calls strategy on the deposit function like it takes all the money and deposits into Silo and it emits a fun uh it emits an event called you know whatever amount we have deposited so our strategy function has uh so I added some comments here uh yeah so this is our strategy contract so uh this is the native address that we use for weet second is the address for
Silo token because the rewards that we receive right Silo is giving us some free B tokens free rewards but we we need to let the smart contract know what rewards we are receiving so we write it down uh as Silo token want is basically the token that we want to deposit Silo is again the address of the tokens we receive collateral uh is not useful here reward claim is basically the addresses we pass to Silo to tell tell The Silo contract that okay uh I I I have some rewards and I want those rewards so we initialize Silo reward addresses we initialize uh an helper
called as Silo lens so we can ask ask Silo it tell me how much money do you have of mine so because let's say when you deposited into a bank a bank also goes and deposits into some other bank so basically how the bank will ask the other bank okay tell me how much money you have of mine so this is what Silo lens does here we we use this contract to ask it that tell me how much money you have how much you owe me how much rewards you owe me um this uh so have you guys used balancer or Unis swap yeah so whatever tokens we'll be receiving whatever the free tokens that we'll be receiving in Silo uh we want to convert it instantly because we don't
know the price would go up or down so what we'll be doing is balancer is similar to unisoft so we'll be converting those tokens into eth instantly and this is the route we would be using for sapping so we have some functions so when we when we initialize the strategy forun function we call initialize uh nothing much happening here like you just put in all the values that you pass um Silo address the contract address wherein you put in money uh collateral token the token uh that you that you want to deposit so in in our case it will be we so the most important function or the
interesting function is deposit so we see when you call deposit here when you when you call deposit here it calls the earn function on strategy it calls deposit so when it calls deposit we come here so then it first checks that okay how much e do we have and once you know how much e do is it if the balance is more than zero then we go to Silo uh Silo is basically our other bank where we deposit in money and tell them okay here's my balance uh take this deposited and we emit a function uh that okay we have uh
emit an event that okay we have deposited this much amount uh into Silo then we again come back here our earn function has completed executing so we again go back here now we check that okay how much balance do we have now and let's say if so earlier uh before this user put in $5 we checked what was the balance before and what is the balance after so let's say if you come in you put in $100 I issue you 100 infl 100 erc20 tokens so there's a record that you have money into the protocol now if
somebody else comes in first we check that before this guy put in money how much balance did we have okay we had $100 now he came in he puts in $5 so we need to issue five new tokens and we issue five new tokens to this um user so that's how we come up with the calculation of shares that what share a particular user holds into uh the protocol and then we send them uh the ERC 20s and we mint some new tokens and send it to the user so you might say that like the money is into Silo how how will it autoc compound so for that we have something
called as the Harvest function here so what the Harvest function does it it it it calls Silo tells it okay whatever free rewards you have acrew till now give me the free rewards convert it into e that's we that's what we do here so yeah so in Harvest function we claim all
the erc20 uh free tokens that we have received uh so we are also charging some fees to the user uh so for all all of this we are charging them 5% so we do all of that calculation here and fees so right now we charging them 5% so let's say in future we want to charge them 10% so we have something called as fee category which is uh changeable we can change add fees so every time somebody calls the harest function we get the fees uh based on that we swap amounts we we we swap these amounts we put it into uh the pool whatever extra tokens
we receive we convert it into WTH and we put it into the pool we charge them fees uh we emit a function called fees charged and then we come back to the Harvest function we note we note down whatever the last Harvest time is because we don't want users to be spending gas fees uselessly calling the Harvest function multiple times so we note down whatever the last Harvest Time was uh then user can users can check it um what what the last Harvest Time was and call the Harvest function only if there's a good amount of time so here if we so here if we see it it says last
Harvest was 8 days ago so that's quite some time so maybe a user can just go and call the Harvest function uh so but why will somebody call the Harvest function right so in order to incentivize users to call the Harvest function we we give them some incentive so we can we can try it out right now so uh I I'll I'll deploy uh I I'll deploy a fork uh chain so we
can uh call the Harvest function and you know see how how free tokens we receive e so if you see I have some e but I have
zero width so my balance is zero um I have literally no width uh now we'll call the Harvest function on the strategy has
okay so here's are strategy contract so right now I have zero withth balance uh now when I call the Harvest function on this you can see I have some extra weth tokens now this is the incentive that we give users on calling the Harvest
function uh we can uh go over the execution as well in uh tenderly so just like how we went over the go so we first called The Harvest function then we charge them fees uh then we lock some profit we deposit the money again into Silo uh and we send some tokens to the user uh in this case I was the user who called The Harvest function I know this would have been too
much uh like 30 minutes like do you have any questions that I can answer in a simple way yeah sorry in case Silo doesn't give us any free tokens uh so so she's asking that what yeah so she's asking in case what if Silo doesn't give us any free tokens so Silo has an incentive right now but it won't last forever it will only be for let's
say next 2 3 4 months so in that case we we have a function to see okay if we don't re receive any Silo token so one again so here we have uh balance function so in case if we receive more than zero if the free tokens that we receive is more than zero only then we we take the hard step of charging them fees converting all of that into uh vet depositing it again into Silo um and then issuing new tokens and all that stuff um after some time Silo might stop giving us incentives so in that case the balance will not be more than zero so this
entire function of doing the hard step of charging fees harvesting would would not work what is what is the next um block for Harvest can we check it uh sorry the next Harvest time can we check it how so you can Harvest it you can Harvest it anytime like you can so the thing is we received some free tokens for harvesting right um this amount of tokens for harvesting now right now if I call the Harvest again it we call the Harvest like 2 minutes back so in 2 minutes the rewards acur may be very very less we may end up paying like $10 in gas fees but the incentive we receive may be just
$2 so this is why uh before calling the Harvest function we can check how much of Harve how much of free rewards have been acur and if it is more than the gas fees only then we uh call it you can so uh 2 years back on polygon when the polygon's gas fees is anyways very low so 2 years back when the gas fees was was very very low people were calling Harvest functions on multiple strategies like every minute and they were receiving some free tokens um but the gas fees was so low so it didn't matter if even if they were paying gas fees every single uh block the rewards they received were always higher than the gas fees uh any anybody else uh like wants
to you know ask anything so so the question would be it's not aut compounded like in the back end there's actually a user that has to be incentivized to to run it sorry like when you do the compounding Auto compounding yeah there actually has to be a user to actually execute it it's not done within the team's back end to do so uh yeah so we don't actually ask anybody to compound it uh so there are me Bots um so who we pay them some incentive to a compound it so uh right now now we received this amount right so maybe you can write a bot so Bey uh you can go to B uh.com there are multiple
strategies here you can go to all these strategies take the strategy address and you can calculate that okay in case uh you can try with cheaper chains like they are also on polygon so you can go to uh any of their so this is a pool on polygon what you can do is you can check the strategy contract and see that okay it has 238k in rewards what so it was last harvested like 11 hours ago you can you can write a simple bot um and check that okay if I harvest right now what's the free amount of tokens what's the incentive that I am
getting so you might get like $2 in incentive maybe the gas fees you pay would be half a dollar or something so two years back I I wrote one simple bot which was which was doing the same thing uh so it it made like 10,000 transactions in a month or so and at the month end it it got like free tokens were $2,000 so anybody of you can just go to bf.com take up the strategy contract there's a harvest function see that if you call the Harvest function right now how much are you paying in gas fees and how much free tokens are you receiving if you receive more tokens um just just do it like on on most strategies if you look at most strategies uh like this strategy so see just just two days just
some time back somebody called an Harvest function so this was the address who called it 8557 and you see this address called 8 uh 8557 received some free tokens so they received they received like Z 0.075 Matic in free tokens they paid 0.2 uh Matic in fees so the free tokens that they received was less than the fees uh they paid so they are in loss so what you can do is B has like hundreds of strategies here just note down the strategy contracts of all these uh Auto
compounding contracts write a simple bot it it it wouldn't be very long write a simple bot monitor that okay if you and what you can do is you can you can sort by tvl so this pool has 3.7 million somebody called an Harvest 5 hours ago same guy 557 uh so in this case he received 0.12 Matic and he paid 0 16 Matic so it is very close to the amount of fees that he paid so sometimes what will happen is you may end up paying less gas fees and make more in free rewards uh and not
only with B there are so you can go through be's documentation you can go through year documentation all these uh comp contracts you can call Harvest functions and make free money if you are faster than other Bots yeah thank you uh does anybody have any questions maybe about the bots on the harvesting part on the smart contract on the W on the strategy can you change the token rewarded token
sorry I mean like instead of can you change token a token okay so uh depends uh if we are in ethereum okay so Silo is a market which is giving some boost same we can go to with uh this pool here there is some com so this is a different
strategy when you deposit into this you receive some Vector tokens so just like how we saw the entire strategy for Silo same way the strategy would be different for Wester tokens so same way um if you come across any Strate so this this strategy is very different than what we saw technically it does the same thing it takes everybody's money pulls it together just like a bank does and deposits deposits it into uh a strategy or a lending pool or a decks uh but the calculations how it issues tokens is it's it's mostly all same what it does in so this functions change then so the Harvest function that
we made here that will be different for Wester but majority of the other functions would stay same so you can use the same contract you can just copy paste the same contract and for different for Wester you can uh write the code again for Wester you can write the code again for um curve you can write the code again for a so there's a similar so how many of you have heard about yearn Finance yeah so H is the one who were the first ones to build such thing so if you if you go to V3 vaults uh this is a vault on polygon right now it is giving you uh like 28% in
a so if you go to their walls uh this is the a ender wal you can go go to the vaal contract and you can read the entire code here like how they manage the money for a so technically it would all be same issuing uh receip tokens for for the money people have deposited autoc
compounding it um swapping it again and deposited again but every protocol is different uh just like how every bank is different so the deposit function and the Harvest functions so this is our Harvest function for Silo and this is our deposit function for silo so deposit and harvest functions would change but other than that you can reuse like 60 to 70% of the code for any strategy you want and if you want to code your own
strategy uh you can try out y Finance you can go to the doc docs so you can go to docs. yarn. fi check out their WS their strategy and you can deploy your own strategy and maybe charge 5% to everybody who deposits money into U your token and you can also take B.A apppp here you can go through the strategies write some Bots uh which Harvest uh the
money and you know get some free tokens so right now U the total tvl is like 265 million if let's say you are making uh historical APR of 10% if you get like 5% of the 10% you still make up a good amount of money just by harvesting strategies uh on Bey on year so on year year Finance there are Keepers who do the same thing and who take away the money on B people like you and me can call the Harvest function and get some free tokens so with that I'll I'll uh if you guys don't have any questions then I'll I'll end my talk uh in case you want the code link again I can Flash
it tinyurl.com Dubai defi in case anybody wants to download it okay uh so then I'll end my talk thanks for coming to the session