npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@moraswap/universal-router

v1.3.0

Published

Smart contracts for Universal Router

Downloads

15

Readme

Universal Router

Please read the Contributions section before submitting a Pull Request.

To see the commit of the smart contracts that was used in the latest deployment, see branch deployed-commit. To see the addresses of this latest deployment on each network, see folder deploy-addresses.

High-Level Overview

The Universal Router is a ERC20 swap router that allows users greater flexibility when performing trades across multiple token types.

Our flexible command style allows us to provide users with:

  • Splitting and interleaving of Moraswap trades
  • Partial fills of trades
  • Wrapping and Unwrapping of ETH
  • Time-bound, signature controlled token approvals using Permit2

Transactions are encoded using a string of commands, allowing users to have maximum flexibility over what they want to perform. With all of these features available in a single transaction, the possibilities available to users are endless

Contract Overview

The Universal Router codebase consists of the UniversalRouter contract, and all of its dependencies. The purpose of the UniversalRouter is to allow users to unify Moraswap ERC20 swaps (on V2 and V3), in a single transaction.

UniversalRouter integrates with Permit2, to enable users to have more safety, flexibility, and control over their ERC20 token approvals.

UniversalRouter command encoding

Calls to UniversalRouter.execute, the entrypoint to the contracts, provide 2 main parameters:

  • bytes commands: A bytes string. Each individual byte represents 1 command that the transaction will execute.
  • bytes[] inputs: An array of bytes strings. Each element in the array is the encoded parameters for a command.

commands[i] is the command that will use inputs[i] as its encoded input parameters.

Through function overloading there is also an optional third parameter for the execute function:

  • uint256 deadline: The timestamp deadline by which this transaction must be executed. Transactions executed after this specified deadline will revert.

How the command byte is structured

Each command is a bytes1 containing the following 8 bits:

 0 1 2 3 4 5 6 7
┌─┬─┬───────────┐
│f│r|  command  │
└─┴─┴───────────┘
  • f is a single bit flag, that signals whether or not the command should be allowed to revert. If f is false, and the command reverts, then the entire transaction will revert. If f is true and the command reverts then the transaction will continue, allowing us to achieve partial fills. If using this flag, be careful to include further commands that will remove any funds that could be left unused in the UniversalRouter contract.

  • r is one bit of reserved space. This will allow us to increase the space used for commands, or add new flags in future.

  • command is a 6 bit unique identifier for the command that should be carried out. The values of these commands can be found within Commands.sol, or can be viewed in the table below.

   ┌──────┬───────────────────────────────┐
   │ 0x00 │  V3_SWAP_EXACT_IN             │
   ├──────┼───────────────────────────────┤
   │ 0x01 │  V3_SWAP_EXACT_OUT            │
   ├──────┼───────────────────────────────┤
   │ 0x02 │  PERMIT2_TRANSFER_FROM        │
   ├──────┼───────────────────────────────┤
   │ 0x03 │  PERMIT2_PERMIT_BATCH         │
   ├──────┼───────────────────────────────┤
   │ 0x04 │  SWEEP                        │
   ├──────┼───────────────────────────────┤
   │ 0x05 │  TRANSFER                     │
   ├──────┼───────────────────────────────┤
   │ 0x06 │  PAY_PORTION                  │
   ├──────┼───────────────────────────────┤
   │ 0x07 │  -------                      │
   ├──────┼───────────────────────────────┤
   │ 0x08 │  V2_SWAP_EXACT_IN             │
   ├──────┼───────────────────────────────┤
   │ 0x09 │  V2_SWAP_EXACT_OUT            │
   ├──────┼───────────────────────────────┤
   │ 0x0a │  PERMIT2_PERMIT               │
   ├──────┼───────────────────────────────┤
   │ 0x0b │  WRAP_ETH                     │
   ├──────┼───────────────────────────────┤
   │ 0x0c │  UNWRAP_WETH                  │
   ├──────┼───────────────────────────────┤
   │ 0x0d │  PERMIT2_TRANSFER_FROM_BATCH  │
   ├──────┼───────────────────────────────┤
   │ 0x0e │  -------                      │
   └──────┴───────────────────────────────┘

Note that some of the commands in the middle of the series are unused. These gaps allowed us to create gas-efficiencies when selecting which command to execute.

How the input bytes are structures

Each input bytes string is merely the abi encoding of a set of parameters. Depending on the command chosen, the input bytes string will be different. For example:

The inputs for V3_SWAP_EXACT_IN is the encoding of 5 parameters:

  • address The recipient of the output of the trade
  • uint256 The amount of input tokens for the trade
  • uint256 The minimum amount of output tokens the user wants
  • bytes The MoraswapV3 path you want to trade along
  • bool A flag for whether the input funds should come from the caller (through Permit2) or whether the funds are already in the UniversalRouter

Encoding parameters in a bytes string in this way gives us maximum flexiblity to be able to support many commands which require different datatypes in a gas-efficient way.

For a more detailed breakdown of which parameters you should provide for each command take a look at the Dispatcher.dispatch function, or alternatively at the ABI_DEFINITION mapping in planner.ts.

Developer documentation to give a detailed explanation of the inputs for every command will be coming soon!

Usage

To Compile and Run Tests

  1. Create .env file with api key
INFURA_API_KEY='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  1. Run yarn commands to compile and test

To Run Hardhat Tests

yarn install
yarn symlink
yarn compile
yarn test

To Update Hardhat Gas Snapshots

yarn test:gas

To Run Forge Tests

forge install
forge build
forge test

Integrating

  1. Install the latest version of @moraswap/universal-router package.
  2. Add git submodules for contracts that aren't a node package. Make sure there's an empty .gitmodules file. Then run:
      git submodule add https://github.com/transmissions11/solmate
      git submodule add https://github.com/moraswap/permit2
  3. You should now be able to import contracts from universal-router and compile.

Contributions

Before you submit your PR, run all of the following and commit the changes:

# make sure all tests pass this will also update gas snapshots
yarn test:all

# lint code
yarn prettier:fix

If you are only concerned with investigating gas diffs, you can run this command to only run gas tests

yarn test:gas