This video explains how to implement a flash swap arbitrage strategy between two Uniswap V3 pools using Solidity. The mechanism works by first borrowing tokens from Pool Zero through a flash swap without depositing any funds, then swapping those borrowed tokens on Pool One to purchase the same tokens at a lower price, and finally repaying Pool Zero with the purchased tokens plus fees. The profit is derived from the price difference between the two pools minus the swap fees. The implementation requires declaring interfaces for Uniswap V3 pool and router contracts, encoding callback data with ABI.encode, and implementing the uniswapV3SwapCallback function to execute the arbitrage logic.
Building a Uniswap V3 Arbitrage Smart Contract in Solidity
Added:both in unisa B2 and in unisa B3 during a swap you can actually receive a token before you put the token in we'll use this mechanism to build a smart contract that will do an arbit charge between two Unis swap B3 pools for example let's say that we have two un swap B3 pools die we with 0.3% swap fee and another Pool die we with 0.05% swap fee and let's also say that in the first pull D we with 0.3% swap fee we is selling for 2,000 die and then the second pool with 0.05% swap fee let's say that we is selling for 2,100 die here we can execute an Arbitrage to make some profit to initiate an Arbitrage to buy the we from the first pool we won't need to deposit any di this is because we're going to do a flash swap on pool zero here we'll say that the first pool is pool zero and the second pool is pool one so we do a flash flop on pool Z without putting any die in we'll be able to request for we on the Second Step we'll swap this we that we got for D this is executing a regular swap on pool one and once we have die on the last step we'll Reay pool zero with the die the profit from this Arbitrage will be the die that we receive from pool one this is from swapping from step two and then the die that we repaid in step three the difference will be the profit and die okay let's write some code to begin with I'll first declare some interfaces that we're going to need for this contract we'll need the interface I swap br02 to execute a regular swap with pool one to initiate a flash swap we will directly call the pool so we're also going to need the interface for UNIS B3 pool and also declare two interfaces ic20 and I okay next I'm going to declare some constants first I'll declare the contract address of swap router Z2 and then inside our contract we'll initiate the I swap router 02 interface passing in the address of the swap router 02 also declare some constants Min square root ratio and Max square root ratio these constants will be handy when we directly call our unisoft B3 pool and then call the function swap okay next I'll declare three functions function flash Swap this will be our entry point this will be the function that we'll call to initiate the Arbitrage next I'll also create a internal function and this will be a simple wrapper on calling the function exact input single on Swap router 02 now when we initiate the flash swap we will directly call the Unis swap B3 pool contract and then call the function swap then we'll ask this pool contract to send us some tokens when we do this the pool contract will call back into our function the specific function that we'll need to implement it's called function Unis swap B3 swap call back this is the function that unisoft B3 pool will call back into and then inside here we'll execute our Arbitrage then send the amount of die was requested back over to the pool contract okay so let's begin by writing the function flash F this is the function that will initiate the Arbitrage for the input I'll pass in the address of the first pool pool zero and to identify the second pool I pass in three parameters U 24 P1 and address token in and address token out with these three parameters we can identify a pool and Unis B3 okay and the last parameter that we'll pass into this function is uint 256 amount in this will be the amount that we'll have to pay to pull zero okay next we will directly call the function swap on the pool contract so to do this I'll first use the interface I Unis swap B3 pool pool zero and then we'll call the function swap to call the function swap let's look at the parameters that we need to pass so these are the parameters that we need to pass for the recipient this will be the receiver of the token let's set this to address this 041 is this trade going to be 041 if it is true it means we're sending token zero and then receiving token one now how can we figure out whether this trade will be a 041 or a 140 so say Boolean 04 1 is equal to well way that the token are identified as token zero and token 1 is by ordering the tokens by the address so for example when we compare token in and token out if token in is less than token out then we know that token in is token zero and token out is token one this will be a trade for 041 if token in is less than token out otherwise token in will be an address that has a greater value than token out so we know that token in will be token one and token out will be token zero okay so that's 041 041 the next input amount specified and notice that this was a int 256 we're going to put token in so this will be int 2556 amount in okay next un 160 square root price liit x96 this will be the limit on the price that we're willing to accept for this example we'll keep it simple and then either set it to Max or Min depending on the direction of the trade so this is where the constant that we declared over here becomes useful here we have main square root ratio and Max square root ratio when we execute the function Swap and then pass in the square root price limit over here the value that we pass over here must be greater than this number and less than this number so let's say over here un 160 Square Ro TK price limit x96 is equal to if this flap is at 041 then the square root price will decrease otherwise if the trade is a 140 then the square root price will increase so using this fact we can figure out what we should put for square root price limit x96 so say 041 if it is a 041 then the price will decrease and the minimum that it can decrease to will be Min square root ratio + 1 otherwise square root price will increase and the maximum it can reach to will be Max Square Ro T ratio minus one okay so now we figured out square root price limit x96 square root price limit x96 the last parameter that we'll prepare is data this will be the data that will be sent over to unisoft B3 pool contract and then this data will be forwarded to the Callback unisa B3 SW callback we'll encode the data that we're going to need inside the Callback here so say byes memory data is equal to ABI do encode we'll encode message sender the message sender of to the function flash flop and this is because when this function is called it's going to be called by the pool contract so message sender will be the pool contract and we want to know who the message sender is when this function flash swop is called so we'll encode message sender message. sender next we'll encode all of these data so this will be cool 0er V1 token in token out amount in and we'll also encode this 041 041 and then over here I'll put data okay so that completes the function flash swap again this function is going to initiate a swap to Kickstart the AR charge when we call this function it's going to call swap on pool contract and the pool contract is going to call back into the function uni swap B3 swap callback for the inputs we'll receive three inputs amount zero amount 1 and data notice here that amount zero and amount 1 are int 256 and int 256 when these numbers are negative it means that the tokens came out from the pool for example if amount Z is negative then this means that amount zero of token zero came out out of the pool and if amount one is negative this means that amount one of token one came out out of the pool here are the steps that we'll Implement inside this function let's first start by decoding this data so I'm going to do is going to copy this and then paste it here so that we know what we encoded okay and then say the first data that is encoded will be address message senders so I'll call this caller the second one is the pool so say address pool zero next one is V1 okay I'm going to scroll back up fe1 is encoded as U24 so going back down we'll decode this as U24 V1 next is token in and token out address token in address token out okay next is amount in un 256 amount in and that's the 041 this was a Boolean variable 041 and we'll decode this by using the function ABI do decode decode the data and the types to decode it as are I'll copy this paste it here and then remove the names we're just keeping the types here okay once we decode this data next we'll call the internal function swap to do a swap on P one we have not implemented this internal function called swap what do we need to pass in for the inputs here well if I scroll down to call the function exact input single on Swap router 02 these are the inputs that we'll need to prepare so here say token in I'll leave it as a question mark for now token help we also need to pass in the fee fee these three parameters will identify the pool to swap on further we recipient that you see over here we'll set it to this contract so we won't need to pass this as an input to our internal function well okay amount in amount in and amount out minimum we'll call this amount out Min and for the square root price limit x96 we'll set it as Zero by setting this as zero the swap router will automatically set the square root price limit x96 when it calls the pro contract to these values that you see over here so we'll call this internal function Swap and we'll ask this function to return the final amount of token that came out so U to 56 I'll call this buy back amount is equal to and then to calculate the profit what we'll do is say U 256 profit will be equal to the amount that came out from swapping on pool one buy back a ount minus the amount that we have to pay back to poio this is encoded inside amount in say amount in so this is how you will calculate the profit next let's go back to this swap function and figure out what parameters that we'll need to pass into this function so to begin with um pool zero what we did was we first requested token out and we promised P zero to pay back Rift token in so when this call back is called the token that we have is token out and on pool one what we want to do is Swap this token out that we have so far token out with token in so over here token in will be token out from pool zero and token out will be token in from po zero fee is easy this is the fee that is decoded over here this will be V1 amount in how much amount do we need to pass in here well when this call back is called we got back out token out and what is amount out over here to figure this out we'll need to look at these two parameters so remember I said that if these numbers are negative then this means that this much amount came out from the pool so to figure out what amount out is we can look at this 041 and since the amount that came out will be negative we'll need to turn it into a positive number so I'll declare a variable U amount out is equal to this is the amount out that came from pool 0o so is this trade at 041 041 on pool 0 is this as SW for 041 if it is then we know that the amount L is inside amount one that we get from the input over here and this will be a negative number and we want to to turn this into a positive number so you'll put a minus and it is int 256 so we'll cast it into uint 256 otherwise if the trade was not a 041 then it would be a 1 140 which means that the token now was token zero so again you'll say un 256 minus amount Z okay so we now figured out what amount out is let's put this back over here when we do a swap with pool one we'll take this amount out that we figured out over here and then use it as amount in amount in first offing with pool one amount out okay how about amount out minimum what is the minimum amount that we want to receive from pool one well when we do a swap on pool one we're going to put in token out relative to pool zero and then get back token in the minimum amount of token in that we'll need to repay pool zero is this amount in so when we're stopping with pool one the amount out minimum should be amount in otherwise there's no arbit charge profit okay next let's complete the internal function Swap and then we'll come back to this function un swap B3 swap callback to finish repaying P zero and then paying the profit over to caller okay so first I'm going to copy this and then I'll paste it here forur the input to this internal function swap you need to pass in token in address token in address token out fee will be U24 U 256 amount in and un 26 amount out Min and when we call this function we want to return the amount that came out from the spot returns U 256 amount out inside here the first thing that we'll do is approve the router to spend so say ic20 token in do approve the address of the router for the amount amount in next we'll call the router and call this function so I'll copy the struct and the function and then paste it here let's start by preparing the parameter so the parameter is I swap router 02 dot exact input single params memory of qu params is equal to I'll copy this again then paste it here and this is the parameter that we'll need to prepare token in will come from the input token in token out will also come from the input token out fee will also come from the input fee recipient we want to send the token out to this contract so this will be address this amount in will also come from the input put amount in amount out minimum this will also come from the input amount out Min and square root price limit x96 we'll set it to zero okay once we have the parameters prepared next we'll call this function exact input single on the router contract router. exact input single pass in the parameters and then it's going to return a single input called amount out we declare this amount out as an output so we just assign it okay so that completes the internal function swap let's go back to finish off Unis swap B3 swap callback so we're not going to need this and then the last part is to repay pool zero and pay the profit to color so here I'll say ic20 token in dot transfer to pool zero for the amount amount this is the amount that we need to repay for zero and the profit will be ic20 token name. transfer to caller this will be the caller to the function flash flop to the caller pay profit and lastly there's a chance that profit is zero so we can require that this profit is greater than zero profit equals Zer okay that completes our Arbitrage contract so again what this contract is going to do is it's going to initiate a overcharge by first requesting for token out from pool zero pool zero will call back into this function un B3 SW call back inside here we'll execute a trade with pool one get the token out and then from the token that came out we repay pool zero and then whatever that is left over we send to the caller this will be the profit so I wrote a test and before we execute it I want to quickly show you what's going on inside the test will simulate a over charge between DW pool with 0.3% swap fee and DW pool with 0.05% swap fee The Profit that we can make is really small so the amount of die that we will send will be 10 die and before we execute the Arbitrage inside the setup I'll first create an Arbitrage opportunity what it's going to do here is first it's going to make we cheaper on PO zero and this is done by selling 500 W on PO zero once we is cheaper on pool zero we execute our Arbitrage and print out how much profit we made by sending 10 die inside my terminal I'll execute the test and the test passed so for swapping 10 die worth of We The Profit that we got is roughly 24 C
Up Next

Arbitrage Algorithm for Uniswap V2 and V3 Pools
@smartcontractprogrammer
166 views•2026-02-18

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Understanding Flashbots and Preventing Sandwich Attacks in DeFi
@smartcontractprogrammer
16.4K views•2021-12-25

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto















































