SharkTeam UniswapX Source Code Analysis

Design Principles

UniswapX aims to solve the complexity of routing by outsourcing it to third-party fillers on an open network, where these fillers compete to fill the swaps using on-chain liquidity such as AMM pools or their own private inventory.

With UniswapX, traders will be able to use the Uniswap interface without worrying about whether they are getting the best price, and the trades will always be recorded and settled transparently on-chain. All orders are supported by the Uniswap smart order router, forcing fillers to compete with Uniswap v1, v2, v3, and eventually v4 once it is launched.


How It Works

First, let’s assume Alice (the trader) wants to swap 1 ETH for USDC. Alice requests quotes from potential fillers Bob, Charlie, and Danielle:

  • Bob offers to buy Alice’s ETH for 1,000 USDC

  • Charlie has 999 USDC

  • Danielle has 998 USDC

  • Alice can also directly swap her 1 ETH for 997 USDC through Uniswap v3

Alice accepts Bob’s offer of 1,000 USDC and signs the order.

The order includes a maximum value (set by Bob’s offer of 1,000 USDC) and a minimum value of 997 USDC (set by the Uniswap smart order router API).

Bob can use his own USDC or route Alice’s 1 ETH to various on-chain liquidity venues (Uniswap protocol, Sushiswap, etc.) to fill Alice’s order.

Bob decides to use his own USDC to fulfill Alice’s order and sends 1,000 USDC to Alice in exchange for her 1 ETH.

If Bob decides to retract his proposal, Alice does not need to submit a new order and signature.

Instead, her existing order is automatically updated to offer 1 ETH to anyone who can provide her with 999 USDC in return.

A block has passed, and now Charlie and Danielle (as well as any other fillers participating in the UniswapX system) are not willing to fill Alice’s order at a price of 999 USDC. After another Ethereum block (12 seconds) expires, Alice’s 1 ETH can be exchanged for 998 USDC.

Suddenly, Danielle realizes that by routing Alice’s trade through a combination of Uniswap v3 and Sushiswap, she can fill Alice’s sell order for 1 ETH at a price of 998 USDC while still earning a profit of 1 USDC for herself.

Danielle, on behalf of Alice, sends Alice’s 1 ETH to Uniswap v3 and Sushiswap, returns 998 USDC to Alice, and keeps the remaining 1 USDC as output for herself.

Transaction Process

UniswapX is a decentralized trading protocol that introduces signature-based authorization and transfer functions through the Permit2 token authorization contract, applicable to any ERC20 token. In addition, UniswapX also uses the Reactor contract for on-chain settlement. The Reactor contract is responsible for verifying whether the transaction meets the user’s specified parameters and can revoke transactions that do not meet the conditions. To participate in UniswapX transactions, the swapper must first authorize the Permit2 contract.

The swapper does not need to manually create and submit transactions, but instead signs the transaction order, specifying the following parameters:

  1. Input token (payment token)

  2. Output token (acquired token)

  3. Input (output) amount

  4. Initial output (input) amount

  5. Minimum output (input) amount

  6. Decay function

  7. Exchange deadline

  8. Authorize UniswapX Reactor contract to use the token on behalf of the swapper

These orders are received by MEV searchers, liquidity providers, and/or other on-chain agents (referred to as fillers) and sent to the Reactor contract. By submitting the swapper’s order on-chain, the filler pays the gas fee on behalf of the swapper. These fees are reflected in the execution price to compensate for gas costs.

The Reactor contract calls the filler’s Executor contract, which contains specific order execution logic. Once the asset source is determined, the Executor contract sends the assets to the swapper’s address and extracts funds from the swapper’s address. Finally, the Reactor contract verifies whether the order meets the conditions.

UniswapX does not specify how fillers fill swapper’s orders. Liquidity can come from on-chain liquidity pools of Uniswap or other decentralized exchanges, off-chain liquidity sources, or other UniswapX orders. Multiple orders can be bundled into the same transaction, and other operations can be atomically executed on-chain.

Key Source Code Analysis

Since the filler needs to submit gas on behalf of the swapper, the transaction fees can be reduced by executing orders in batches.

The _fill function handles the specific order execution logic. There are two cases here: if the filler uses their own holdings to complete the swapper’s exchange, no callback contract is needed and the asset swap is done directly. Otherwise, the specific logic needs to be handled in the callback contract, such as exchanging in other pools.

The contract uses the validate function to verify if the filler is the designated filler for the order.

The contract uses the permit2 library to verify the signature and transfer the token, ensuring that the swapper’s money is not transferred arbitrarily.

If the filler chooses to complete the order using their own holdings, the tokens will be directly transferred from the filler’s address to the swapper’s address.

After the callback function of the callback contract is executed, the check function is called to verify whether the user has received enough tokens. If there are not enough tokens, the entire transaction is rolled back.

In summary, the contract only involves on-chain logic. Since users do not need to pay gas fees to complete this transaction, most of the initial operations are chosen to be performed off-chain, including sending user exchange requests and signing transactions. UniswapX chooses to send user exchange requests to the filler off-chain, and once the filler accepts the filling request, the filler sends the transaction on-chain and earns the difference as profit.

Like what you're reading? Subscribe to our top stories.

We will continue to update Gambling Chain; if you have any questions or suggestions, please contact us!

Follow us on Twitter, Facebook, YouTube, and TikTok.


Was this article helpful?

93 out of 132 found this helpful

Gambling Chain Logo
Digital Asset Investment
Real world, Metaverse and Network.
Build Daos that bring Decentralized finance to more and more persons Who love Web3.
Website and other Media Daos

Products used

GC Wallet

Send targeted currencies to the right people at the right time.