DeFi Quant Engineers - Patrick Collins
web3con 2022 · 5 Mar 2022 · 41:13
Patrick Collins, developer advocate at Chainlink, shows how to work as a DeFi quant engineer using Python. After a demo of depositing and borrowing on Aave on a test network, he builds Brownie scripts that wrap ETH, approve tokens, deposit collateral, read the health factor and borrow DAI, all without writing new smart contracts. The session covers ABIs, interfaces, dependency remappings and programmatic lending.
Speakers
Topics
From web3con 2022, hosted by Developer DAO.
Transcript
this talk is sponsored by polygon polygon is a leading platform for ethereum scaling and infrastructure development its growing suite of products enables developers to build scalable user-friendly dapps with low transaction fees without ever sacrificing on security learn more at polygon.technology hi all my name is patrick collins and i am a developer advocate on the chain link protocol today we're going to be talking about how to make you a d5 quant engineer using python and using a ton of amazing scripts let's just jump right into it shall we so what i'm going to be showing you is a little bit of this ave protocol
here so this is avi we're at staging.avi.com it's on the coven test network you don't have to go ahead and play around with this you can just watch this for what we're going to be doing so ave is a borrowing and lending protocol which allows us to deposit assets and then borrow assets as well you might want to do this so that you could have a leveraged position you can do some really fancy financial financial tricks build some financial instruments get some apy earn some yield obviously it's not going to be you know quite this lucrative uh because this is just a test net but these are the types of things that you're going to be able to do so i'm at staging.ava.com like i said on the coventest net i'm going to go ahead
and practice depositing something let's go ahead and pause 0.5 eth and this is something that you might want to do so that you can gain some interest on this asset right so i'm going ahead and deposit that transaction is pending if i go back to my dashboard it'll still be 1.7 as the transaction is going through right we can see it here and then once it actually goes through we'll see this number go up to uh two 2.2.2 which we do right there awesome now that we have some collateral we can go ahead and actually borrow different assets right so you see this number up here this is our health factor this tells us how healthy we are we always need to have more
collateral than we do assets borrowed so right now we have you know five thousand eth five thousand five thousand eight hundred dollars of eth borrowed and we have maybe a hundred and hundred one dollars of different tokens borrowed and we can go ahead and see what that borrowing looks like so i can go ahead and borrow some dye you can even borrow with this sliding factor maybe i'm gonna borrow you know just tendai stable apy 3.99 it'll uh means that our apy how much interest we pay during the borrowing of this will always be 3.99 variable means it'll change based off of how well the protocol does um one is this obviously
means it'll always be 3.999 but this means that it could spike up to like 11 or something so we're going to go ahead and borrow 10 die here we'll go ahead and confirm and once this goes through on our dashboard we'll be able to see that borrowed here and we'll see this die number go up and the reason you see two different dies here is because on test nets um sometimes they change token addresses because it's a test net and they want to like fiddle with stuff so it doesn't really matter but see that transaction just went through now we're at 1 10 die bro and then what you could do is you could take this die you go sell it for more eth or some other you know or ave or maker or compound or whatever
right you could use this uh borrowed money to go leverage up on some other asset cool and that's what we're gonna do but we're gonna do it programmatically so we're not going to do it through through this ui here now i have a video on this um on my youtube as well one goes a little bit more in depth one goes into kind of like the leveraging and then one is a little bit more higher level gonna be similar to what we're doing here but these are two other resources you can definitely check out uh additionally i have everything that we're gonna go through on my github here so if you ever get lost or you're a little bit confused definitely be feel free to check out here
and then one more thing um as we're going through this so we're going to be working with the brownie protocol here however a lot of my future videos might be with this ape works protocol as it matures aperx is another pythonic framework that's really cool and i'm really excited to play around with more in the future so cool let's jump in so i'm in my code editor now right it can be any code editor you want and the first thing you'll need to do is you'll need to make sure brownie dash version and ganache cli are installed so we can make sure we do that with brownie dash version and we can do ganache cli
version i have instructions for setting these both of these up in my ave brownie pie github repo but cool so now that we have that let's start a new brownie project with brownie init this will create a new brownie project with a whole bunch of stuff in here now what we're not going to be doing is we're actually not going to be building any smart contracts we're just going to be interacting with smart contracts so the way that you interact with smart contracts typically you need to have you always always always need to have two things what are those two things well at least two things you need to have the api and the address those are always always always the two things you need to do to interact with them i mean obviously you're going to need an eth connection
which we're going to be using alchemy and we'll get to that in a minute um but you always need an abi in the address and for us the way we get an abi the way we get the abi of these smart contracts that are on avalanche is we can just use an interface and compile it down now if you go to the ave docs which again you know all these links are in that github repo it links right to those apis so if you come to the docs you might get put on v3 first uh we're gonna still be interacting with v2 however all the uh everything's basically the same right all the interac all the fundamentals are going to be basically the same
now the main interface that we're going to be using is this i lending pool interface this has all the different functions that we need and all the different functions that we just demoed it does borrow it is withdraw or excuse me deposit borrow withdraw repay you know everything you would need you'd think about for borrowing and lending but notice this right here lending pool methods deposit borrow withdraw and repay are only for erc 20. if you want to do this with native ethomatic or you know whatever use west gateway instead now we're actually just going to make it easier we're going to uh convert ethereum to west so that we can just use
these you know we can pretend like this ethereum's uh your typical erc20 but you could also use this weft gateway if you'd prefer right so you'd have deposit eth withdraw eath repay you know f etc but for us we're just going to be wrapping our ethereum cool so we need this interface let's grab it we'll copy it new file i lending pool dot soul basing it does spell that right i lending pool yes it did okay cool and then if you're using like a vs code linter you can ignore this for now this is something that the link linters
are working on brownie is able to compile with different versions of solidity so you can ignore that additionally this eye lending pool addresses provider so this is an interface that will give us the different addresses for different pools and different things that we need to work with so back in here we're going to say new file i lending pool address says provider dot sol paste that in boom okay cool two interfaces down what else do we need well we're probably going to need an erc20 interface right because we're working with your c20 tokens now i already have these uh um so i'm not going to go ahead and find some random place you can just go
ahead copy paste these off my github all right go to i or you see 20 soul copy this whole thing and then paste it in or create iris 220. so boom we have something like this additionally we're going to need a weft token or a wrapped ethereum which again i have on my github already so you can just copy paste it from there and the reason we're going to need a web token is because this has a withdraw deposit and a withdrawal function we need in order to transform your ethereum to an erc20 version of ethereum or wrapped ethereum you need to deposit in a weft token contract so that's what we're going to do cool all right so we have the main interfaces that we
need there's going to be one more interface that we're going to need but we'll save that for a little bit so let's go ahead and start creating our script create a create new file called ave borrow dot pi and we're also going to create one called get west dot pi and then we're gonna do knit dot pi cool so the first thing we're going to do is since we're going to be working with wrapped ethereum we're going to need to learn how to get rapid ethereum from ethereum so we're going to do that in this get weth script so in here we're going to create at the bottom we're gonna do death main and this is gonna call
get weth great at the top we're gonna do def get west and this is where we're gonna do all of our stuff right so maybe we'll do print hi just to make sure it's working run brownie run scripts get wet dot pi oh and i think it actually will fail so it actually fails i actually didn't copy paste for my github you should you'll get a different error than me um so this is copying right from the ave repository what we want to do is we want to import from the obvi repository right now this says hey import from our local place which obviously doesn't work so i'm just going to go ahead and copy my eye lending pool
for my github which has kind of this already transformed for us i'll then pull address providers yep okay i did do that one correctly okay cool now the only thing that we need to do here is our compiler doesn't know what ave contracts is so we need to tell it where to import this from so the way we do that is we'll create brownie config.yaml and in here what we're going to do is we're going to create some dependencies dependencies and our dependencies are going to be on the ave protocol b2 at 1.0.0 so we're going to import
from github if i just go to github and i even just paste that right there we're telling brownie hey grab from this repo at tag 1.0.0 and once we have that we can do something called remappings so we'll say compiler sulk remap mappings and we'll say anytime you see at ave in a contract you're really mean ave protocol call pro to call v2 at 1.0.0 cool
now we can even try with brownie compile first make sure everything compiles yay stuffs compile and then we'll try running the script which should just say hello or something or hi launch guess there's gonna be it's gonna launch a fake blockchain that does nothing and it says hi okay perfect let's get some wet now now to get an account that is actually part of your your wallet that's actually associated with your metamask you do something like this with your private key feel free to see the repo for that for us we're just going to use kind of a built-in fake account that brownie gives us so at the top we're just going to say from brownie import accounts and this is going to be
our account that we use we're going to say account equals accounts of zero so brownie just gives us kind of this fake account for us to use now in our get west then what we're going to want to do is we're going to want to get the wet token so we're going to say weth equals interface we're going to import interface from brownie as well interface dot west interface and we're going to say config networks network does show active left token now to do this we're going to need to import network from here as well and what we're saying here is we're
saying go to our config which we're going to have to grab as well and grab the weft token address from our config and use this from our web interface use this abr we'll now have like a wef api that we can send transactions from we're gonna have to create a new networks mainnet fork with token then we have to put an address in here oops you can just go ahead and copy this from my github repo again instead of actually uh looking up where the web token address is now what's interesting here is we're using this mainnet fork network so we want to simulate
everything that's happening on the blockchain we want to simulate this with this mainnet fork network instead of having to run everything on a fake network or a test network where things are going to cost a lot of money so we're going to be using this maintenance fork so if you run browning networks list you'll see you should have a maintenance fork in your development networks if you don't have it just go ahead and upgrade to a new um to the newest version of brownie but once you have that in we're actually gonna be using alchemy as our provider here and so you can just go on to alchemy you can sign up for a new account and you can go ahead and hit view key and it
will give you an http endpoint and with that you can do brownie networks modify we're going to modify the main net fork and we're going to say the forking url is going to be and then you know you'll paste in your forking url and hit enter i've already done it so i'm not going to do that here but now you'll have a way to connect to our maintenance fork with this in fact we can even do a little we can't even check it we can say print weath dot balance of account see how much wealth we have we should
have zero right so if we're in brownie networks excuse me brownie run scripts get uh dash dash network main net fork that's what we got to do now you see it's forking here i'm gonna cover this up and we see our balance here is zero perfect now in our config we're going to give us a default network of mainnet fork so that now we just always go to main network and we don't have to pass that network flag anymore okay great so now we have a way to connect we have
a way to get print our balance let's even do tx equals west.deposit we'll say from count value oh this should be in parentheses value's gonna be zero it's actually going to be we're going to say we're going to do from web3 import web3 we're say web3.2way 0.1 ether we're saying we're going to deposit 0.1
ether um with this transaction here we're going to call the deposit function we're going to send it from our fake account that brownie gives us oh and and brownie gives us this fake account and has like a thousand ethernet already uh we're gonna deposit one 0.1 ether and we do it like this because uh ethereum can't understand decimals so we have to wrap it down like this which that's equal to doing like you know one one two three five six seven eight nine ten you know 17 zeros with that cool once we call this deposit we can even do we'll do tx.weight so we'll wait for the transaction to be mined with one block and now we'll print the balance and we should get 0.1 so let's run this again
cool so we do indeed see that right here and if we count those zeros it's two three four five six seven eight nine ten one two three four five six seven it's exactly what we wanted right cool so we're getting wet awesome so now we have a way to get some wealth now that we get some wealth let's actually deposit it into ave so we can borrow some stuff let's do it okay so let's create a main function def main and this main function we'll call borrow excuse me deposit actually yeah we'll have a call deposit and borrow cool now do def
pause or excuse me yeah now we'll create our def deposit and borrow great so now we have a function def department boss uh deposit and borrow and that's what we're gonna do so the first thing we're gonna need to do is get an account so we'll say accounts equals excuse me account equals accounts zero same thing that we did in the beginning we'll do from brownie import accounts and again in my github repo i do it a little bit different um but that's because i do it a little bit different in here that's because i i give i wrap it in this get account thing i give it the ability to mess around and use um not just
maintenance forks or test nets so account equals account zero and now we're going to need to get our our web token address so we'll say west token address equals again config we already did this network network.showactive web token right same thing we did before and we'll say if network dot show active in mainnet fork we'll go ahead and run get weth which we can go ahead and import from our scripts so say from scripps dot get west
imports get left so what we're doing here is we're we're getting that web token oh we're not using it yet but now we're saying you know hey if we're on this maintenance fork just go ahead and get the wet token right now that way we don't have to run get west if we don't need to cool once we have the web token that's what we're going to use to deposit into ave so in order to in order to deposit into ave we need to get the lending pool address and the lending pool contract so we'll say lending pool equals well how do we get the lending pool oh we're going to create a function called get lending pool and you'll see why in a second
so down here we're gonna say def get lending pool and what this is gonna do is we're first gonna get the lending tool addresses provider from that i lending pool addresses provider interface so we're gonna say interface to i landing pool address says provider it's gonna be from config networks oops network dot show active landing pool address provider right like that so this means again we're gonna have to go back in our config
create a new address called lending pool address provider and we can find this config or excuse me we can find this address in the ave docs but for speed here you can just find it uh right in my browning config.yml right so if you go to main to fork let me pull address provider boom made a forks right there boom same one right here okay cool the reason that we need this and we wrap it like this so now we have this landing pool address provider contract is because we need to call a function on it we need to call get lending pool so we can say landing polar provider dot get lending pool this will tell us the address of the
lending pool so we'll say lending poll address equals lendingpolaris.getliningpool and then we're going to wrap this address in that interface so we're going to say a lending pool equals interface dot lending pool of the lending pool address and then we're going to return the lending pool return lending poll and boom now we have a lending pool this is going to be the contract we're going to borrow stuff from deposit to et cetera et cetera great so in order to deposit anything we need to approve our erc20 first right
so we need to approve our web token um we need to approve our web token so we're going to create a function called approve your uh approve with and we're gonna give it an amount to approve give it the lending pool dot address the address of who to approve it um the we give it the weap token address and then we'll also uh pass the account so we have lending pool we have what token address we have account we just need a mount we'll make this at the top probably should be all caps but for
purpose of time we're not going to do that we're going to say amount equals web 3 2 way 0.1 ether or um yeah we'll do zero point one either so again we gotta do from web3 import web three cool so we're gonna deposit all of our weft we're gonna pause it all over with and we gotta prove it first so we say def approve west so this is gonna take all these variables so i'm actually just gonna copy paste it down here so we're gonna say uh amount lending pool address what token address account
oh actually we don't even need to pass them out now let's pass them out now let's not pass it out because amount's kind of global and that's going to be really confusing so we're just going to not we're going to keep amount as global which is a little confusing by the time so a landing pool address web token account and we'll even do a quick print we'll say approving west three we'll say erc 20 or west equals interface dot irc20 uh we can also just do um i wes but uh if you want to make it kind of with any erc20 you could just do interface.irc20
we'll pass in that weft token address and now we'll say tx equals west.approve lendingpool address amount from account so we're going to approve the lending pool address to use our wealth and then we'll do tx.weight 1 print approved boom and now we have a function to approve our wealth what do we do after we prove it well we're probably going to want to deposit it so now we'll do a quick print we'll do print deposit and we'll do lending pool dot
deposit oops we're going to just call that deposit function right here we'll say tx equals lending pool that deposit we do the web token address amount account dot address at zero so if we look at the deposit documentation right if we go back to excuse me the lending pool here we go deposit we see it takes an address of the asset to deposit the amount of the asset deposit on behalf of and then a referral code which is just here right now so address amount on behalf of which is us and then zero um
cool and then we're gonna do from of course it's gonna be our account i can actually close that for now cool i'm going to do tx.wait for one block confirmation and then it's going to be done now i'm going to create a function called get user data and what it's going to do is going to return all the users data from ave so we'll create this function def get user data and we're going to call it right after this we're going to do gate user data and there's a function on this lending pool called
get user account data which returns a whole bunch of stuff you know what's your health factor how much money you have borrowed how much you know money eth you have deposited all this stuff so in this get user data function [Music] we're going to say lending pool dot get user account data we'll say account dot address and so this will pass this uh an account and we'll pass it lending pool and then from this we're going to get a whole bunch of stuff i'm just going to copy paste it because it's quicker but you can actually see what it returns total collateral total debt build the
borrow you know feel free to pause this of course liquidation threshold tlv and health factor you can read up on what those are in the docs but once we have those we'll do a couple print statements so we'll just do print do printf you have say web3.from way total oh look at that total collateral ether total collateral do it again we'll do total depth eath ether eats debt one more and this will be available borrow
eth to borrow so it tells us how much collateral we have how much debt we have and how much eath we can borrow and if we run this script right now with get user data and then what are we passing lending pool and account now if we run this with brownie run scripts avibar.pi and we don't have to do the network flag right because oops because we have the default network defined in our configure we just run this like this it'll spin up config not defined i should probably
define config network maybe some other stuff but we'll go ahead and run this and it should tell us what's going on here we also got to grab interface hopefully nothing else this should be eye lending pool not landing pool root okay here we go approved now we're depositing great you have zero one eighth collateral you have no debt and you can borrow 0.825 you'll notice you can't borrow exactly
how much collateral you have right you can check the ave docs you can check on chain to see kind of what that conversion looks like because not all assets are one-to-one right you can only borrow up to 82.5 percent uh if you have eth uh as collateral right so that that is how that works great all right cool so we have some stuff deposited yay and actually this get user data function we're gonna need to know how much we can borrow because we're going to return a float of available borrow eath
and float of oh tall that eath so we're going to return both of these oops so back in here in our function we're going to say available borrow eth and then total dead ease equals get user so uh first thing we're going to need to do let's say we want to borrow some die right now we're going to borrow die so we're going to say we're going to say die price equals get die price right or i have like get asset
price i might get a repo but we need a way to actually get this diet price and the reason we need to get the diet price is because we need to convert from die to eath so we know how much um we can actually borrow so we're going to say def get diet price we're going to say die f price feed equals interface dot aggregate or v3 interface config and don't worry if you don't know what's going on for a second networks network show active die price feed boom so we're going to get a chain-link price feed that gets the conversion of
die and eth and this comes from an aggregator of e3 interface that we don't have yet so you can either grab this from the from docs.chain.link or you can just go right to my github repo uh which has it and that's exactly what i'm going to do and then i'm just going to paste it right here boom aggregator via interface these are a decentralized price feed for us to get our pricing information from cool so now that we have the die eat the price feed oh and we also need the diet price feed address on mainnet so let's get that again you can go look it up on the chainlink docs or for now uh just copy it from my repo boom and that's going to be the price
feed there but if you want to find the docs you'd go you know ebm chains contract addresses ethereum data feeds and we're looking for the price of die eath and boom that's it right there where you can see all these oh that's on coven well it's a good thing i checked huh we're looking for the main one by eth boom which is this address right here so we'll paste that in here cool once we have that we can now call and get the latest price let's say latest price equals die eat price feed
dot latest round data uh latest round data returns a list of things and we're just gonna the first one is actually the latest price and then we're just going to return that say return latest price boom so now that we have that we can even do print die price let's see if we're getting that correctly from the chain link price feeds and boom we're saying it's 0.0003 die per one each which is probably right
right die is going to be a lot cheaper than what eath is so now that we have the the die price and we have the amount available to borrow we can actually go ahead and do a borrow so let's say let's get the amount years amount of die to borrow it's going to be equal to and this is where we've got to do a little bit of math so we're going to say 1 divided by the web 3 dot from whey diabetes ether times borrowable eth that we have and then just for a safety
measure we're going to times it by 0.95 so that we have a little bit less than the max number right because again if the price changes a little bit and we borrowed the max we could get liquidated so we're just going to want to uh be a little bit a little bit cautious here excuse me available borrow eth and then we're actually gonna web 3. uh from way that as well just so that all the math makes a little bit more sense here so basically we're converting um the eth to amount of die we can borrow so we can even do like print amount out of borrow and just has a sanity check see if this number looks about right so this is going to be the amount of die
we want to borrow here with 0.1 each is collateral and we need to wrap this as a float and we need to wrap this as a float because the numbers are too big and cool so it's saying around 206 die which makes sense like one tenth of the current price of eth which is like 2 700 is gonna be 206 right so we can even do a quick
or actually this is going to be 0.82 or 8 of you get the picture this this is uh this is about correct so let's go ahead and borrow this right so that's how much we want to borrow now we're going to want to borrow that erc20 so we'll do borrow your c20 we'll say put in the landing pool for some function we'll say amount die to borrow and then we'll pass the account and we'll create a new function called def our ears 320 and then we'll pass those exact parameters here and great and we'll do
the x equals lending pool dot borrow and again you can see the abi in the in the avid docs but we're going to say uh we need actually we need the address too so we'll say excuse me die address and then borrow your c20 do die address we'll say die address equals this we put this in our config 2 address you could look up the mainnet address if you want or just copyright from copyright from my um github
the address equals config networks network does show active die token okay perfect now uh tx equals lendingpool.borrow we'll say the die address i'll do the amount to borrow and since right now the amount to borrow is in not web3 way or excuse me in the web 3 format we'll do web3.2a comma ether one zero so the one again if we go back to the
lending pool here we go to borrow we have the address of the assets the amount the interest rate mode uh which i believe one is variable refroca which could just be zero and then address behalf of is going to be account and of course we're going to say from account and then we just do dx.weight one print yay borrowed i need a parenthesis there great and then after this we're just going to
run this get user data function again and it'll tell us we've got a whole bunch of stuff borrowed so and this is going to be the whole thing we're going to run through we're going to approve we're going to deposit oh and i ran into an issue here die token this is going to be config oops diet token let's run this one more time so it's going to tell us
that we've got stuff deposited that we can borrow stuff now and cool and now at the end it says okay we have 0.1 ethers collateral and we've actually gained a little bit of money right because as you deposit you gain a little bit of interest and we have almost zero point you know that 82 in debt but it's 0.78 this is the die that we have in debt in terms of eth and this is how much more eth we could borrow right and if we waited longer right if we waited some time that interest would have been even more right because the interest increases over time so this is how you can actually kind of run these python functions and
you can do d5 right in python without actually having to deploy any smart contracts or anything now if you go to my github we do a little bit more things and we including we actually repay it back um but this is basically how you can get started doing um all these fantastic things and actually working with chain link working with avi working with these d5 protocols for your quantitative engineering journey thank you see you next time
