SharkTeam: UniswapX Source Code Analysis
SharkTeam
2023-08-08 03:58
本文约1763字,阅读全文需要约7分钟
UniswapX aims to address the complexity of routing by outsourcing it to third-party fillers on an open network. These third-party fillers compete to provide liquidity for swaps using AMM pools or their own on-chain inventory.

Design Principles

UniswapX aims to solve the complexity of routing by outsourcing it to third-party fillers on an open network, who then compete to fill the futures by using on-chain liquidity from AMM pools or their own private inventory, etc.

With UniswapX, traders will be able to use the Uniswap interface without worrying about getting the best price themselves, and trades will always be recorded and settled transparently on-chain. All orders are supported by the Uniswap smart order router, which forces fillers to compete with Uniswap v 1, v 2, v 3, and eventually v 4 once it's launched.

Advantages

  • Get better prices by aggregating liquidity sources and have gasless swaps


  • Prevent MEV (Maximal Extractable Value)


  • No fees for failed transactions


  • In the coming months, UniswapX will expand to gasless cross-chain swaps.


Working Principles

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 using Uniswap v 3

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

This order includes a maximum value (set by Bob's bid at 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 abandon his proposal, Alice does not need to submit a new order or signature.

Instead, her existing order will automatically update, offering 1 ETH to anyone who can provide her with 999 USDC in return.

One block has passed, and now Charlie and Danielle (along with any other fillers participating in the UniswapX system) are unwilling to fill Alice's order at the 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 sending Alice's transaction to 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 functionality based on the Permit 2 token authorization contract, applicable to any ERC 20 token. In addition, UniswapX also uses the Reactor contract for on-chain settlement. The Reactor contract is responsible for verifying if a transaction meets the user-specified parameters and can revoke transactions that do not meet the conditions. To participate in UniswapX transactions, the swapper must first authorize the Permit 2 contract.

Swappers do not need to manually create and submit transactions, but instead sign the transaction order, specifying the following parameters:

  1. Input token (pay token)

  2. Output token (receive token)

  3. Input (Output) Quantity

  4. Initial Output (Input) Amount

  5. Minimum Output (Input) Quantity

  6. Decay Function

  7. Exchange Deadline

  8. Authorized UniswapX Reactor Contract Represents the Token Used

These orders are received by MEV searchers, liquidity providers, and/or other on-chain agents (collectively referred to as fillers) and sent to the Reactor contract. By submitting the exchanger's order on-chain, fillers pay the gas fees on behalf of the exchanger. 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 exchanger's address and extracts funds from the exchanger's address. Finally, the Reactor contract verifies if the order meets the conditions.

UniswapX does not specify how fillers fill exchangers' 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 fillers need to submit gas on behalf of exchangers, the gas fee loss caused by each transaction can be reduced by batch execution of orders.

The _fill function handles the specific order execution logic. There are two scenarios: if the filler uses their own holdings to complete the user's exchange, no callback contract is needed, and the asset swap is performed directly; otherwise, the specific logic needs to be handled in the callback contract, such as exchanging in other trading pools.

image.png

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

The contract uses the permit 2 library to verify the signature and transfer tokens, ensuring that the user's money cannot be transferred arbitrarily.

If the filler chooses to complete the order using personal holdings, the tokens will be transferred directly from the filler's address to the user'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 not, the entire transaction will be 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 performed off-chain, including sending user exchange requests and signing transactions. UniswapX chooses to send user exchange requests to fillers off-chain, and once the filler accepts the fill request, the transaction is sent to the chain by the filler, who earns the difference as profit.

About Us

The vision of SharkTeam is to comprehensively protect the security of the Web 3 world. The team is composed of experienced security professionals and senior researchers from around the world, who are proficient in the underlying theories of blockchain and smart contracts. They provide services including smart contract auditing, on-chain analysis, emergency response, etc. SharkTeam has established long-term cooperative relationships with key participants in various fields of the blockchain ecosystem, such as Polkadot, Moonbeam, Polygon, OKC, Huobi Global, imToken, ChainIDE, etc.

Official website: https://www.sharkteam.org

SharkTeam
作者文库