Hao Ye Soon: Practical DeFi |15th Oct 2023 | Sunway University | ETHKL 2023
Ethereum Malaysia·Wed, Oct 9, 2024, 12:00 AM
The ETHKL 2023 conference is a two-day event that covers a wide range of topics, including Zero Knowledge Proofs, Layer 2, DeFi, Data infrastructure, security, and more. This gathering brings together industry experts and pioneers to explore the latest advancements in Ethereum. Attendees will have access to insightful discussions and presentations on various subjects related to Ethereum technology.
Transcript
next speaker is a software engineer he's a Malaysian and he's working at TOA Labs his name is Sun how yeah let's put our hands together and welcome him to the stage all right how yeah the stage is yours take it away thank you all right uh thank you so much uh thanks everyone for taking your time to attend this talk um hope everyone is still awake uh this is certainly not the best time slot for a talk maybe because I didn't pay for my tickets okay so uh earlier today we've heard uh many people talk about very exciting stuff on the future of defi right what the future holds for us but today or right now I'm going to take a step back and talk about what's happening in the present okay some of the nuances and you will basically hear me rent for the next 30 minutes wait okay so a quick intro about myself uh my name is Haya I am a senior software engineer at tokal laabs so what tokal laabs does uh we are primarily focused on Market making on me searching uh path finding for intent based swaps as well as block building we also have many more exciting projects down the pipeline that's proving okay yeah okay so uh I want to start with a hypothetical case right um so imagine you have a Trader friend who comes to you and say that hey I have found a strategy that can give you consistently uh 10,000% in returns and I've back tested it can you help me to write a about to implement it and you as a Dev your answers to the call and so what is the strategy simple instead of being the 100x trader we have found the 100x trader and we decide to copy all this trades so what kind of Trades is this person making specifically it involves mem coins we're not even talking about paypay here we're talking about Harry Potter Obama Sonic enu here this is the real deal your 100x coin and so in order to monitor uh the trades being made from that address you need a way to monitor transactions in the blockchain right and how do you go about it what do you look out for first of all very basic you need to look at uh who where is it sending from who is it sending to you do filtering over there and by looking at the recipient address you can probably guess that uh which contract is it being sent to if it's using some generic uh Unis swap router contract to ex a swap then you can pretty much decode what are parameters being sent to and you can know what's being traded a better approach would be to look at the logs so um if it's a Unis swap based pool and all of its Forks uh it's going to emit this uh event this swap event here and uh there are some pretty useful info that you can get from there like what's the amount what's the pool address and sorts uh but at times um these sophisticated Traders they don't use the like Unis swap uh router contract directly they write their own smart contracts to do the routing for them and uh as such it's hard to decode and figure out what's going on in a transaction and so uh in this case you have to rely on internal transactions as your best buddy so uh essentially these are uh this happens when a smart contract calls another smart contracts function and then it involves some funds transfer so these things are captured in internal transactions so on ether scan it's on this tab here and so um where do you monitor so there's only two places right one is the public M Pool and one are confirmed transactions so if you look at transactions that are mined or confirmed this takes the least amount of effort this the least risk uh but if you're trying to be the fastest uh you're trying to set your order as fast as possible uh this might not be ideal for you because all your actions are at least one block slower than the previous trade you only know uh after the matter of fact and uh a better approach would be to look at pending transactions or some people call it mempool transactions so as such if you see the transaction in mle you can immediately send your order and expect that your transaction happens in the same block as that's the transaction that you are spying on uh but the problem is the person sending the transaction that you want to follow might not end up in the public men pool because they can send what's called a private transaction that does not go through this path and if you were to just look at pending transactions you wouldn't see anything there and also uh because these transactions are pending they're not confirmed they may actually revert when it's actually being executed so it carries a bit of risk there if you try to follow that and so how do you monitor these transactions there are a couple of ways you can start with using uh web3 apis so your regular note providers has a web sockets and point for you to subscribe to where you can subscribe to all transactions that's going on um but they only give you the transaction hash and then for each of them you have to fetch the data and then do your own filtering your own decoding so it's a lot of work there and the problem is you cannot see internal transactions from there it's not part of the standard uh we3 API it's not exposed there it's actually one layer down at the ethereum node client and if you want to get that information you're going to have to pay a bit of money so um there are these companies like just to name a few Blox rout Blox native alchem as them nanson uh you can have to pay money to them to subscribe to also websocket endpoint and you get a lot more information and it's much easier to work with so you just tell them uh tell the the the endpoint or the service provider what address you want to monitor what address you want to spy on and then as soon as it detects anything related to that address it's going to send you all the information you need including internal transactions it also res results in higher performance as a result um but if this doesn't satisfy you or maybe the service provider has uh terrible service like sometimes it doesn't work or it just misses out something or the latency is too high for you you might want to try Okay uh you might want to try to run your own ethereum node uh with some custom Logic on top of it uh as such you have more control you can do pretty much everything there everything you need to see is already there um if done correctly it can give you high performance but at the same time it results in uh more effort and cost from your side because running a note uh requires a pretty high performance uh machine to do it and also you need someone to maintain it so purely due to economies of scale might be better off using paid services for most people okay so uh we go one layer up to our strategy some people might say that it's not even a strategy right you're just copy trading but um there are things to consider so first thing to consider is are you even able to execute a trade so there are many avenues that the trader can send the trade to like um different dexes and if it happens that that Dex is not supported by you you didn't uh integrate it so you cannot follow that trade how much to trade if the trader is trading like 10 eth you only have five e in your Capital then you got to decide on a number um and another concern is low liquidity pools so uh these pools are considered a very high risk because any kind of trade with a small amount results in a very large price movement so this gives a lot of problems when you're trying to calculate your unrealized profit and loss as you cannot get an accurate picture of what will be the actual price that you can execute if you decide to close the trade also uh you need to do some uh risk management um so in the context of meme coins there are quite a lot of risks involved uh I will bring up some uh very common and funny examples first one is token tax so um if anyone has traded mem coins uh you probably know that these tokens carry a tax and uh basically when you buy buy and sell they will take a cut from you but how much do they take you have to find that out and it can be different a lot of times there 0% buy tax but a 30% sell tax so they want to get you in okay uh this is even even more nuanced because some of these contracts have the ability to update the tax at any time so yeah if you're trying to calculate again you're trying to calculate your unrealized profit and loss right you don't know the tax then how are you going to calculate also um we've seen certain tokens that carry taxes that change according to time like for example the longer you huddle the lower the tax but the longer you hold the more exposure you get yeah so yeah uh there are a lot more gimmicks out there so the other one is bot detection and also some white listing measures so uh basically what they're trying to do is they're trying to filter out bots so if they detect like you're sending the transaction from this address but the recipient of the token goes to another place yeah sometimes they consider that as a b or they might have a list of addresses that they maintain somewhere and before they actually transfer the token they will check if that address is in their white list so they just want to uh let their close group of people trade that token and they only do it when you try to sell they don't check when you're trying to buy um the third one is outright scam tokens um you can say that Meme tokens are a scam altogether but these are the scams of the scams okay so uh it's also called a Honeypot token uh essentially you can only buy you cannot sell when you try to sell it will throw some weird error that that doesn't make sense yeah and uh more interesting one is a sell tax of 100% yeah so when you try to sell you end up with nothing some have 99% % yes a little better okay thanks um fourth one uh very common Rock pools so um yeah the first method is a token dump essentially the token creator has a large stash of the token and when the time is ripe he just dumps all that token in the pool and extracts out the liquidity of the other side of the pair the second one is called a liquidity rug whereby the owner supplies almost all of the liquidity in the pool and when a time is ripe he just removes all of them and then no one can trade from there anymore unless you want to supply liquidity there okay so uh We've Come to order execution and uh there are couple of things that you need to take care of when you're sending your transactions um the first one uh is transaction simulation so it might be a good idea to simulate your transactions before sending them on chain and how do you do it um it's not it's not part of the web fre apis it's actually a private service that's provided by these different providers here um tenderly Alchemy block native RPC Fest just to name a few and what these Services provide you with is that um you you can send an unsigned transaction to an endpoint that they provide and then they will help you to simulate your transaction against not only the latest block but also against all the transactions that's pending in the manool and as a result you get much more accurate gas estimation you can also have a very high uh high probability of guess of whether your transaction meets your outcomes you also if the transaction happens to fail in simulation then you can avoid sending it maybe change some parameters for example setting a higher slippage before sending it so as to avoid wasting gas on reverting uh but the issue is not all chains are supported so if you're only on ethereum Main net it's uh generally okay but uh I think for tenderly they support a lot more chains but the rest it's pretty much very limited and uh also there's this issue of unpredictable latency so sometimes it takes like uh below 100 milliseconds sometimes it takes like a second for the results to come back so uh that's going to affect your over overall system performance and then after you have simulated the transaction you have a very high confidence of sending it onchain uh what you do basically you call the send trans transaction function right you send it to a mempo and then one of the block Builders or if not all the block Builders pick your transaction the block with the highest value gets picked up by a validator and it goes on chain right but um that's not the case because if you don't set your slippage correctly you're going to encounter me Searchers and what they're going to do is that they're going to apply what's called a sandwich attack on your transaction where by they take your transaction from the men pool slot in one transaction before and right after the transaction and send what's called a bundle transaction to the block Builder directly with a very high priority gas fee which you can think of as bribe to the block Builder and as such the block Builder is incentivized to take that bundle instead of take your transaction directly from the men poool to be included in the block and yep that results in the person sending the order uh executing at the worst price and then the profits go to the mbv Searcher and the block Builders and validators yeah so how do you prevent that you can send a private transaction or a bundle transaction by yourself as well so you're going to have to send it to the block Builders directly and uh hope they include it but uh usually it's okay if you're on ethereum main net because um probably some of you know that on ethereum Main net right now five block Builders dominate like 80 or 90% uh they create 80 or 90% of the blocks so you just have to send your transaction to five of these block Builders it's very likely that your your transaction can go through but from experience on other chains like if you're on BSC it's not that simple like the success rate of private transactions are much lower there and uh for bundles it's even worse yep so um just going to give a quick run through of how this sandwich attack looks like um if you go to The Ether scan you see something like that right it's not immediately obvious like you see this person Jared from Subway is a very famous mvv Searcher very profitable guy or team so you see like two transactions and then in the middle can be one or more transactions and if you go in and decode what's going on and it involves the same pair of tokens it's very likely it's a sandwich attack and there's a very useful website called igoni uh you just paste the first transactions hash there it can actually show you it can link up the other transactions and show you what's going on so you can see that um this is the original transaction so victim victim is the one in the middle and then what Jarett has done is front run and back run your transaction which just means putting a transaction before and after so as to affect the swap price and uh lastly going to just go through quickly on the blockchain itself how do you talk to it um it's very simple right you just use a web 3 API and job's done right but if if performance is an issue to you um this is the first place that you to try to optimize which is your networking and from from experience um if you use alchemy's uh RPC node right and your server is in Southeast Asia you're going to have horrible performance and by just spinning up your server in US instead of Southeast Asia your latency can improv like 10 times yeah and if you happen to be using quick node um AIA is okay yeah so these are some of the useful tools that uh I use and also shown in the previous slides for analytics you can use Dune defy Lama and uh to analyze tokens whether it's a scam or not uh what's the taex like um there's Dex tools Dex analyzer ether scan definitely uh tenderly very good for simulation as well as for debugging they have a very rich debug info provided there in a very nice UI and it's free Dex screener um to basically look at what's the most trending meme coins and look at charts ieni very good for um Arbitrage and me related stuff um they have a very comprehensive uh analysis there and very useful statistics there as well so um some key takeaways that I hope you guys uh get from this talk is that um you understand some of the basic components when it comes to defi trading not trading General but things that are specific in defi that goes on chain and some of the considerations when building them uh challenges at different stages some popular tools that you can use and uh lastly um it's an exciting field there's always something to learn and always new problems to solve and uh with that I'd like to conclude the presentation H yeah thank you all for your attention um if you like to discuss more please hit us up in our website email LinkedIn and uh we are still hiring thank you awesome thank you very much let's give him another round of applause thank you how from topse uh they are still hiring but all positions are in Singapore right yeah okay so anybody want to earn Singapore dollar well you know where to go all right anybody want to raise the hand for a quick question you can ask him any questions you have about the defi trading strategies that he's mentioned uh just raise your hand we'll pass a microphone to you I want to ask something have you ever done a sandwich attack to other people oh no no very good boy e person sure sure you tell me later how you did it anyway um is the only way to prevent a sandwich attack by doing you mentioned two things private or bundle are those the only two ways uh no actually you can also set your slippage requirements to be really tight such that um there's really not much that you can move so a sandwich attack can only happen if your slipage is B enough usually usually yeah but by setting your slippage to a very tight value you're trying transaction is uh very likely to revert due to Pure trading activity I see okay okay or just use a centralized exchange anyway uh don't listen to him uh yeah feel free to raise your hand anybody got a question about defi trading strategies we got one in the front here we'll pass a microphone to you just introduce your name please sir and the question you have hi my name is Steve um earlier you showed about the M me searches right the Bots how do they make money okay um I think uh this itself is a big topic but I'll just go specific to uh the sandwich itself yep so uh if youall don't know the amm model how they price a token is based on the liquidity that's available in uh both pairs so if they want to sandwich your order what they can do is that they let's say you're buying a token and then they submit an a buy order before you with a very high large amount such that it moves the price up and as such if your slippage allowance uh is okay it passes through your trade will get executed at that very high price and then how they earn money is that by inserting a transaction right after your order because the price is already jacked up and you have bought at a high price and then they will sell the token at a high price so they earn like a Delta from there so yeah that's a general idea to think about it okay you can probably have a master class on meev uh bot execution strategies next time uh I'm actually quite curious to learn I know it's quite an interesting topic as well anyone else want to ask another question about defi trading strategies challenges implementations going once going twice gone all right let's give another big round Applause thank you very much really appreciate your time you can just leave your mic on the table here thank you so much
Automatic transcript — names and jargon may be misspelled.