Build a Searcher Bot to Capture MEV Using the OEV Network | Billy Campana - API3 DAO
Ethereum Denver·Wed, Oct 9, 2024, 12:00 AM
Speaker
Learn about Oracle Extractable Value and how to build a bot to recapture lost MEV using the OEV network. Demo will consist of the steps to build a searcher bot using Javascript/Ethers.js and how to get started build a bot to auction bid on price feeds. Bot will bid for a price update, execute price update and liquidate. APPLICATIONS FOR ETHDenver 2024: #YearOfTheSpork ARE OPEN APPLY NOW https://ethdenver.com/apply
Transcript
[Music] hello everybody I am Billy otherwise known as Billy Jitsu from API 3 uh API 3 is a first Oracle solution for your needs stepping through we offer four Services right now we have a chain API which allows you to bring your ofan cha ofan data uh on chain uh we have qrng which allows you to bring random numbers to your smart contracts we have dpis which are price feeds that are pushed on chain and our newest product which is OE Network which is stands for Oracle extractable value which allows users uh to bid for the ability to update a price feed and we're going to dig into that today who am I I am Billy also known as Billy Jitsu quick intro QR code if you want to follow the socials and the nonsense that I do online but uh we'll go in that just a second so what we're learning today is we're going to show you how to build your own search robot for the oev network it can be used outside of the O Network but we're going to really focus on what it can do on the oev network so the workshop breakdown goes as this is where is the current value going right now what's the meta right how is value being lost to daps and onchain uh the flow of the current meta and then how to build a search robot uh introduction to the oev network and then uh samples of the contract and the scripts that you can run a bot with so right now like the number one question I get all the time is like what is or or extractable value so let's take a step back of like how oracles work so price is changing on assets all the time right so like if you look at coin gecko or if you're looking on a an exchange you'll see that the minute chart is constantly moving up and down but that doesn't happen on chain because the limitation for us on chain is that every time you make that adjustment it costs a transaction fee so during a bull run you know when when guay is like up to like 200 way it costs a lot of money to update a value on chain right so consensus has us do this at a certain threshold so when there's a difference in price like 1% half a percent 0. 25% then we can then update that price on on chain and then you can use it right so Oracle extractable value allows anyone to place a bid to update the price feed at any moment when they need it so what is the extractable value part of this right so we're going to do a quick example of like there's a liquidable position on a at eth valued at $2,022 right and so for Simplicity we're just going to keep eth at $2,000 and it has a 1% price deviation so it updates at $20 so it either update at 2020 or at 1980 right so the price of eth is uh changing on coin gecko as you're watching it and you're seeing it climb from 2,000 to 2005 to 2012 which theoretically you should be able to liquidate that position and it goes all the way to 2019 and you can't do anything about it because the Oracle has not changed right so until it gets to 220 it will not change right and so once we hit that eth at 220 now the price updates and it becomes a battle between who's going to bribe the validator to run your transaction first so you have to bid up you know massive amounts of eth and then pay that and then cause that liquidation you know going into your profits and then basically all that profit goes to the validator right so all all this is is just happening at the current meta right now on mayet and you know a few other side chains so what is Obie trying to solve so how we seem to help with this issue is Searchers can bid to update the price feed through the oev network right so we wanted to have that price update for that example for at 2012 right but we know it wasn't going to upd to 2020 so we bid like we would like to update this price feed at 2012 so if we win the bid we get a signature that allows us to update the price feed and then we can use that signature and bulk it in a liquidation transaction using something we call a multi- call to then let us update the price feed and then run the Liquidation on the same call right so now the value is now split back between the DAP and the Searcher instead of the validator so instead of just waiting for it we can now force that update at 2012 All right so the focus is the search robot so what's really needed to build a search robot uh you can use almost any script here that has like e Library either's uh JS VM python rust um web 3js our examples that we have in the repo uses ether's JS and JavaScript because that seems to be the most popular uh you will need to deploy your own multi- call contract and if you choose to do so your own liquidation contract to call um it's requ because it has an only owner function you don't have to modify anything in the contract portion we've already written it for you so all you have to do to deploy it under your wallet so here's the quick liquidation flow using the oev network to see where it works in the place this is a very highle L bird's eye view of how it works and then we'll get down a little bit deeper But first you find an opportunity right and then once you find that opportunity you got to calculate that price of liquidation now we know that that price is now we want to place a bid on the oev network saying okay that 2012 example I know that's the liquidation price I want a bid to update that Oracle at 2012 and we bid that in the auction house and the oov network we listen and then we get uh we we won right so we said okay you're allowed to update that price and so we get this signature we bring it back to our our local machine and we combine that update a price with our liquidation transaction we call our multi call contract profit right price updates we get the liquidation so that's a very very high level flow of it we're going to dig down a little bit deeper and so what's happening on like the ethereum network which our network is sepolia testet eth and what's happening on a local computer a little bit more detailed on what's happening there so we're Gathering all the user addresses that have a borrowed position and we're saving that locally on our machine we have a database of information that have all the wallets that have all these positions right and then we got to go and use that information and then monitor the health positions of all the positions on the lending dap so then we have to call the lending dap and get all the health factors of all the users that are you know have any positions now we have to store that information locally on our machine for all the health factors that are about 1 1.1 right each one of those factors now we're going to calculate the liquidation price and then we're going to place a bid also happening on here on this network is we definitely want to deploy our multicol contract it doesn't have to happen first it doesn't have to happen second but it has to happen before you get your your bid in right so then it magically does something on the oov network which we're going to cut in a little bit and we magically win and then again going back we get that signature we combine our liquidation transactions that we did mathematically on our local machine and then we combine that and put that into our multi- call contract that happens on the test net the proxy price change on the price feed happens on that test net and then we get our liquidation we win so going in a little bit more deeper this is pretty much the entire flow right so we're again we're we're basically doing the same thing of getting the addresses you know checking the health uh diagrams make sure our deployed uh multi-contract is is ready to use and so now we've had our calculation liquidation price right so one thing that we have to do right now for the hackathon is you have to request OE eth uh from the API 3 team so that way you can place bids on the OE Network and you will have to deposit eth into the auction contract in order to play the game so you have to basically stake your eth on the auction contract to play the game once we have our liquidation price we set up our bid conditions which is what price are we wanting to update at what price feed are we wanting to update what chain do we want to update and a few other factors that don't really matter for the test net right then we place our bid and we have to wait and listen to see if we won that bid a bid happens every time there's a change in PR price so that could be 1 second 2 seconds whenever there's a fluctuation in the price the auctioneer is listening for any changes from the outside Oracle so if it goes from 2012 to 2012 and 15 cents there's an auction happening to it's going to check if there's an auction happening and if there is and that's the only winner then you win the bid once you win the bid you receive the auction signature and then we go back to the original flow where then we gather that data we've done our calculations for the liquidation we call update the Oracle and then get the liquidation so what are the details on that what are the the fine details on that so on the OE Network you only really need to deposit 10% of what you're actually bidding on the auction so if you're willing to bid up to 1 E for that auction you only have to place in 0. one e so you don't have to like pull your whole treasury on the OE Network right you don't pay the fee on the oev network but you pay it on the chain that you're pay you're updating the price feed on so if you were doing on Main net you would pay the fee in eth on Main net if you were doing on the test net you're doing on sapoia test net right so the fee value gets passed from the API 3 server to the price feed um update transaction so when you're updating the price feed that's when you're paying the fee and it goes into the auction house server then the DAP then can withdraw the value that you did the update fee into their contract and then they can use the value how they see fit so this is like a hackathon bounty that we have going on right now once the DAP has received the bidding fee what are they going to do with it are they going to soften that liquidation for the user are they going to reward the stakers for the app uh are they going to donate to public goods uh those are the things that we're looking to see what people get creative with to see what that new found value is back to the DAP a little bit more details um in your B in B ah sorry bid details on the O Network the address of the transaction is part of the winning signature so the way you can't get front run is because you're you're Whit listed if you win the bid for that price feed your address is now Whit listed in the transaction so it's in the public Mol Po and you send that transaction to update the bid it's the only address that can update it so if they try to front run you it will revert right and then so you do have to use the multi- call contract so then you can combine the the price feed update and the liquidation function on the single call if you do the call and update the price feed in a single transaction you will get front run and then someone El say thanks for the price update and then you know they get the liquidation so I am not a big fan of PowerPoint Pres presentations I'm more about digging into the code so a quick QR code for everybody that wants to follow along and grab the repo this is also part of the EO uh e Denver repo so all the examples are there I'm just giving it enough time to for anybody that wants to pull out their phones and get the QR code takes you right to our our repo well let's get into the code and make that a little bit bigger and hide that so this is an example of our search bot uh it's basically three functions but they're three long functions but basically going based on our flow it's going to semi make sense so there's don't get too nervous I'm going to guide you through the whole thing we're going to break down the code piece by piece so we have our first function where we need to fetch the loans right so we got to get all the users that have deposited on the uh lending d right so I know it looks a little scary and uh we we'll get you through it so in order to store the addresses in in our local machine you get to choose whatever you want we chose SQ light database just to store data to access it differently right and so we build a uh a function that's going to store all that data and the only way to get the data from who's deposited is to go to all the past events now there's different ways to go around that if there is a subgraph deployed for that dap you can then refer to the subgraph to get past history events uh unfortunately we don't have it on uh testet sapoia so we have to manually do it right so starting from when the uh D was deployed at that's this block right here specifically we're going to start from there and go to the current block so we have to go block by block to listen to all the events and filter out all the depositors right so we don't want you to kill your RPC calls and then you know do a thousand thousand calls in a second so we did it on blocks of 500 and so every block of 500 it would gather the data of all the events and then start get in another 500 but just to save time and that's not the focus of the hackathon we've already compiled a list of all the wallet that have already deposited for you so you can access that at any time going down a little bit deeper once we've collected all those filters we basically just filter out to see what you know what uh the uh user deposited the amount they deposited and the asset that they deposit right now we're keeping it really simple and very simplistic we have a fake token which is called oev not speculative we're not dropping a token it's just a token so people don't get confused and then the asset being borrowed is in usdc so it's a stable value all right so a very easy single asset one to look at so now that we have you know all the user data now we have to check against the DAP to see all the health positions right and what did they borrow so now we go through another function and basically looping through of getting the user account data which is the function you would call on the lending dap that gives you your health position uh the collateral that was given and the deposit right and it has all the details of what you get in return the LTV the health Factor it gets very complicated when you start doing mult assets and they have different like loans to values so we're keeping it really simple with a single asset and single borrow and then basically in order to get your liquidation price you get your total debt divided by the total collateral uh times the the loan to value and you'll get your liquidation price of what it whatever it is at that point so here we're just doing some calculations to say okay this position has you know 10,000 borrowed I'm sorry 10,000 deposited 75,000 borrowed the liquidation price if the OE OE token goes down like 5% this will be the liquidation price so we're doing all the math here and and running it through so now once we have it we're going to save it to our SQ light database with all the information the users's address the health Factor all the collaterals and then the liquidation price and that just gets stored on our database so we can access it quickly and then uh this is just more like um cleaning up we're going to sort it in ascending order based on health Factor so like our highest uh opportunity is going to go first and then it's going to go lower and lower and then we're just making a table again for the hackathon those that are doing their own search orot are optimizing it don't have to do it this way if you have a backend user on your team or database Pro on your team you can go totally a different direction on that and so we basically just build this table up and then we write all the loans to the database this is all just databasing and then cleaning it up to a readable format and then here we just you know um log out the the top 10 positions that have the most opportunity right so that pretty much we've have this log a table log of all our positions all our liquidation positions we have that all available to us on our database so we got all the user data and all the opportunities right so now we want to go ahead and start placing our bids so we do a little bit of checks and balances first to make sure that we have you know those positions haven't changed from the time our bot has got the information to when we want to place our bid so the first thing we do is we're setting up our auction house uh contract address on uh on the OE Network and then we're looking through all our loans we're finding all our loans what the bid status is and we have it labeled has the bid status been awarded or has it been lost because we don't want to place bids on stuff what we lost on or awarded right and so it'll look up through the database and then basically Loop through each one of those positions we have and we reset the liquidation price every time just to make sure we don't get anything you know skewed in our in our Loop and re re uh reset the percent every time so every time it goes through the loop it goes to the user and it resets to 99999 and then we look at a liquidation price for each one of our users so we have 10 users so I go Loop through 10 times and we just look at all the liquidation price to store them as variables and we just uh check the bids right so here we're looking at our listener we're checking the Oracle price to see did the Oracle price change at all is there any changes or adjustments in our flow um if there is anything that that has been outdated or anything or if there's any labels on there or if the price articles has changed basically expire the bid cancel the bid you know get it's not not worth our time for the RPC calls right and so it's just checking making sure all those balances are going through um once we get closer to you know filtering out stuff we're making sure is the Oracle up to date right coin gecko is not the same thing as our Oracle price feed right so if we have 2012 for our liquidation price and coin gecko saying 2013 and our Oracle saying 2011 we got to adjust for that so we have small adjustment factors here in the code in order to make sure that the you know the percentages are within range and so once we've done all that math and that you know balances here we basically check to see okay is it still in a liquidable position is it like between 1.1 and 1.
0 and is in is the oracles within range if it meets all that criteria now we can actually finally place a bid right be a good bot and place a bid right where we need it and so we are now ready to place our bid and then uh set up a liquidation price and then we have two ways to place a bid one with expiration time and then one without expiration time all our demos are with an expiration time because we have a fake mock Oracle that we know is going to happen so you can like make it fake you know after 2 hours 12 hours 24 hours your choice of however you want to do that and then uh once we place our bid we just update our loan database to make sure hey we've now placed a bid so that status has changed so the next time the bot goes through again we know that that user's position has had a bid placed and now we set up some listeners right did we win the bid uh do we have any bids that we win so we're just looping through all our positions and listening say hey did did we get anything and let's say we get the bid that's awarded so we're listening to see if the auction house said we've been awarded and so if we do get awarded then we save the award details this is the signature that we need that's going to allow us to update the price feed on the chain that we're trying to update so once we have that detailed we're going to go ahead and update our table we have all that we need so now we know we got to trigger the liquidation right I can update the price I know the positions now let's trigger it so that's what we call this trigger liquidation that leads us to our last function trigger liquidation right and here we're just going to go through the loans one more time make sure that we haven't lost any any bids because we had multiple bids going out so maybe we lost some so let's filter out all those lost bids that we did and only the ones that we've got awarded and then we're going to go ahead and um you know get the loans from the health factors that are less than one you know we want to make sure that they're still up toate you know sometimes they repay their loan like oh I'm getting close somebody repays their Loan in the last second they have a bot that repays the loan or they uh you know add more collateral or anything like that so we want to make sure their health factor is still one so we're not wasting money right and then uh we connect it to the uh our Liquidator contract so you can deploy your own Liquidator contract or you can call the contract directly and so we're going to build up our um our call right and so we know for Simplicity that we only have one asset for for the reserve and one asset for the collateral right so we know that the collateral is the oev token so we don't have to like fumble or anything it's it's straightforward it's only one asset to to play with and then we know that the reserve is usdc that's what we got to pay back right and then we have the database of our user so we encode all that up into a single function which is my liquidation function and then we have that all ready to encode so you have to encode your function and all the details in a multi call so you can fit it all in the array and then we go ahead and connect our our our contracts to all the different loans that we have in position so if we had 10 successful bids we're going to do 10 liquidations and then we just go through the whole mapping and get that all set up and then now we're finally going to do our multi call right so we're calling our Liquidator that's connected to the multi call the first thing we're going to do is we're going to update the price right so that's the API 3 server on the sapoia Chain address uh you know if you were doing on eth mainnet it would be the address for the updator on eth mainnet or whatever other chain you use and then this is the price update that we wanted to update it to and then the bidding amount that we bid on there so this is what we promised to pay if we won the bid so if we bid one eth we have to pay one eth if we bid 01 e like we had here we're paying 01 eth and don't confuse that with the value that has to pass through the value that you're paying then goes to that smart contract also bulked into this transaction is our liquidations right so we have all our liquidations that we have on file so we bulk all those liquidations with all the addresses and then we have all the multi call data all the functions that are encoded for each position all our strategies that have been set are going into this data feed and then we're not passing any value we're not paying any eth to that contract what we are doing is making making sure we pay back the usdc to claim the collateral right and so you have to make sure that your Liquidator has the usdc in the contract to be able to pay back the loan and then once we get that all bulked up we call the the the multi call and profit right so this is a very very basic bot to get you up and running so like for those that are looking to do you know any kind of botting or maybe this is not out of your league we have something that's already working for you that you can modify right and so just a quick example like in the readme docs um you know we have tons of examples that oops tons of examples that kind of give you like what the data is going to look like take you step by step on here let me just zoom that out just a little bit uh and it's let me Zoom that out and that didn't work out too well because of the screen has a different uh thing but it's got a lot of good documentation in there for you to go through I was able to go through it with a few non-technical guys and they were pretty happy with it so I'm happy with it okay so let me go on to this next session here so we have uh Bounty prizes for those interested in learning how to do liquidations so we have a $5,000 prize pool for those that build a modification on a borrow lending dob to reallocate that new found value that liquidation profit um so you know that value that we sent to the the Oracle that goes to the DAP what are they going to do with it right like like I said you know are you going to fund public goods are you going to do soft liquidations to uh entice people to borrow more go more djen are you going to reward stakers what would you do with that new found value right we also have a uh ongoing maximum profit $6,000 to for the hackers that really love to do bots is uh we have an ongoing uh liquidation going on in a uh iv2 fork and the goal is to liquidate as much as possible so those that get as many oev tokens at the end of the hackathon is first place second place third place we also have a special $1,000 uh price uh pool for those that keep trying right there could be some killer like M guys that are excellent abots and we don't want you to lose hope in the very beginning because they liquidated like 10,000 or $10 million in the first like 20 minutes right but if you keep showing us and you know keep talking to the team that hey we're trying this strategy we're we're adjusting this strategy and you keep adjusting your code to be a little bit more optimistic we have that special price pool just for you too and I was a little afraid of the internet so I took a a screenshot of the fork so we have a current market happening right now and you'll see the ongoing position so we have a100 million uh uh supplied on there so there's $100 million of usdc on that token that specific token that is available to be liquidated as we have a ongoing price feed it will through the whole hackathon it's going to be lowering in price more of those positions that were in that wallet will be liquidable so there's lots of opportunities so if you didn't get the first one you didn't get the second one there's going to be thousand wallets that are available to be liquidated so give it a shot use the bot maybe you're the first one to use it maybe you know somebody optimize it right off the top but there's a lot of opportunities go through all the wallets there's a script so you can go through it so you don't have to use whole bunch of RPC calls access that database we're very curious to see how you you know perform if you have any questions you know definitely join our Discord Community because in order to get the oev tokens we're going to have to like faucet it to you so join the the Discord I'm going to leave this up right here this is the QR code for we're going to be on uh during the event we'll have our booth so you can definitely talk to the text uh you could follow me ask me questions I'll be here in the corner I've been told don't stand in front of the stairs stand next to the stairs so if you have any questions about the Bots or anything you should be good to go um and yeah I mean have fun experiment this is like meev on training wheels right you don't have to worry about front running it's about experimenting and seeing what you can do with it and then getting started with a good framework and that that's it guys good luck
Automatic transcript — names and jargon may be misspelled.