SuperArbitrage – Leveraging the Superchain for profitable arbitrage.
This repository serves as a proof of concept demonstrating how Superchain Arbitrage can be achieved using Superchain ERC20 interop features.
Cross-Chain Arbitrage: The code identifies and exploits price differences of the same token (e.g., ETH or SuperchainERC20 tokens) between DEX pools on ChainA and ChainB.
Make sure you have foundry and supersim installed in order to run this repository.
To install the necessary dependencies, run:
bun installThe github submodules used have been included in the repo. Cloning the repository will take care of getting the necessary submodules, which are not yet available as published packages.
Since the interop functionality is not live on mainnet yet, we generate a fork of Supersim to help demonstrate the arbitrage.
Run the following command to start Supersim:
bun run src/scripts/startSupersim.ts- In our example, we use Base and Optimism as the two chains.
- The private key used belongs to an account generated from the Supersim deploy.
- Additional chains can be added as needed.
- This command starts an instance of Supersim fork with:
- Forked Chains (Base & Optimism)
- Interop and Autorelay Enabled
- The flags that you desire to start up Supersim with can be modified in the
startSupersim.tsfile.
Run the following forge command to build the contracts:
forge build --via-ir- In case of changes to BridgeSwap.sol, the ABI constructed on running forge build will also change.
- Please remember to update the BrideSwap.ABI accordingly in that case by getting the ABI from the
outfolder generated.
bun run src/scripts/setUpEnvironment.ts- Deployment configurations are stored in
deploy-config.toml. - You can set the following in the
deploy-config.tomlfile:- chains you want to fork on
- unique 'salt' used by CREATE2. We randomly append a number to the salt when running the set up script, to make testing less cumbersome.
- owner_address, name, symbol and decimals of the token to be deployed.
- Uses Foundry to enable the
SuperchainERC20.solscript. - The Create2 mechanism ensures that both the ERC20 and
BridgeSwapcontracts are deployed at the same address on all chains. - The deployed token address and bridge address are recorded in
deployment.jsonand later automatically read intoconfig.ts.
bun run index.ts- Simulation Process:
- Handles creation of
PublicClientandWalletClienton all chains to allow us to pass them in the code later. - Mints tokens and adds a certain amount liquidity to pools on both chains.
- Approves the
BRIDGE_SWAP_CONTRACTfor spending by the ARBITRAGEOUR_ACCOUNT. - Initialises
tokensAndInstancesfor better control of the configuration of the different chains. - Creates and initializes an
executorobject. - The
executor:- Monitors
Syncevents in both pools. - Detects price discrepancies.
- Computes the optimal arbitrage amount.
- Executes the arbitrage transaction, if a profit is viable.
- Monitors
- To simulate a price discrepancy, we manually introduce a large buy on one pool using the
simulateBuyfunction.
- Handles creation of
- The
watchSyncEventsfunction listens for the Sync event emitted on the chain mentioned and has aonPriceUpdatefunction which keeps track of the Pool changes. - For each price update for a pool, we call the
evaluateArbV2Opportunityfunction.
Our goal is to calculate the optimal input amount (r) for arbitrage between two AMM pools.
-
First Swap (Pool A):
When you swapdy₀in Pool A, you get an output, denoted as$$dx$$ , calculated by:$$dx = \frac{dy₀ \cdot (1-f) \cdot x_a}{y_a + dy₀ \cdot (1-f)}$$ - (x_a): Reserve out of AMM A (token reserve)
- (y_a): Reserve in of AMM A (ETH reserve)
- (f): Fee (e.g., 0.3% as 0.003)
-
Second Swap (Pool B):
Then, swapping$$dx$$ in Pool B gives you:$$dy₁ = \frac{dx \cdot (1-f) \cdot y_b}{x_b + dx \cdot (1-f)}$$ - (x_b): Reserve in of AMM B (token reserve)
- (y_b): Reserve out of AMM B (ETH reserve)
-
Finding the Optimal Input:
We define a function for net profit:$$f(dy₀) = dy₁ - dy₀$$ Our goal is to find the value of
$$dy₀$$ that maximizes this profit function. This is done by differentiating$$f(dy₀)$$ with respect to$$dy₀$$ , setting the derivative to zero, and solving the resulting quadratic equation.
By solving the quadratic equation, we derive the optimal input amount
where:
-
$$k = (1-f) \cdot x_b + (1-f)^2 \cdot x_a$$ -
$$a = k^2$$ -
$$b = 2 \cdot k \cdot y_a \cdot x_b$$ -
$$c = (y_a \cdot x_b)^2 - (1-f)^2 \cdot x_a \cdot y_b \cdot y_a \cdot x_b$$
In our evaluateV2ArbOppurtunity function, we calculate:
const c =
(ethReserveANum * tokenReserveBNum) ** 2 -
(1 - f) ** 2 * tokenReserveANum * tokenReserveBNum * ethReserveANum * ethReserveBNum;
const k = (1 - f) * tokenReserveBNum + (1 - f) ** 2 * tokenReserveANum;
const b = 2 * k * ethReserveANum * tokenReserveBNum;
const a = k * k;
const discriminant = b * b - 4 * a * c;
const idealAmountIn = (-b + Math.sqrt(discriminant)) / (2 * a);This is where the magic pertaining to the swap and then bridge across chains takes place.
- Called from
executeArb - Swaps ETH for tokens on the first chain (Chain A), using the Uniswap V2 Router interface.
- Bridges the tokens to the contract on the second chain (Chain B) using the'SuperchainERC20Bridge'.
- Since we have deployed the contract on the same address across chains, we dont need to remember the address separately.
- Sends a cross-chain message via
L2ToL2DomainMessengerfrom Chain A to the contract on Chain B. - The message encodes the calldata for
SwapAndBridgeBack, allowing us to call it on Chain B. INTEROP MAKES THIS POSSIBLE!
- Ensures that the message for the SuperchainERC20 Transfer has been relayed and tokens have been received from Chain A.
- Approves the contract to spend the transferred tokens.
- Swaps the bridged tokens back into ETH on the second chain (chain B).
- Sends the ETH back to the original ARBITRAGEOUR_ACCOUNT using
sendETHfunction in the SuperchainWETH interface.
- Additional Chains:
- The system can be extended to support more chains beyond Base and Optimism.
- More DEX Integrations:
- Currently, we use Uniswap, but other DEX protocols (e.g., Sushiswap, Curve) could be added.
- Support for More Tokens:
- Expanding beyond ETH and SuperchainERC20 stablecoins would increase arbitrage opportunities.
The application:
- Integrates with the Superchain interop predeploys.
- Arbitrage bot that utilizes Superchain interop to profit from price differences across common DEX pools on different chains (e.g., ChainA and ChainB).
Helping OP Labs by collaborating and working on Interop devnets to give feedback and improve the Superchain Interop ecosystem.
This project showcases the feasibility of Superchain Arbitrage by leveraging the Superchain's interop capabilities. By optimizing deployment, monitoring, and execution strategies, we enable profitable and gas-efficient arbitrage transactions across multiple chains. Future improvements can further enhance profitability and scalability.