• Stars
    star
    398
  • Rank 108,325 (Top 3 %)
  • Language
    TypeScript
  • License
    GNU General Publi...
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Uniswap Smart Order Router

This repository contains routing logic for the Uniswap V3 protocol.

It searches for the most efficient way to swap token A for token B, considering splitting swaps across multiple routes and gas costs.

Testing

Unit Tests

First make sure you have run npm install and npm run build.

npm run test

Integration Tests

Make sure the .env file is configured to connect to mainnet and other chains. See the CLI section below for more details.

npm run integ-test

Tenderly Simulations

Quotes can be simulated on Tenderly

Ensure you set the following environment variables:

process.env.TENDERLY_BASE_URL!,
process.env.TENDERLY_USER!,
process.env.TENDERLY_PROJECT!,
process.env.TENDERLY_ACCESS_KEY!,

CLI

The package can be run as a CLI for testing purposes.

First create a .env file in the root of the project and configure:

JSON_RPC_PROVIDER = '<JSON_RPC_PROVIDER>'

To run on chains other than mainnet set up a connection by specifying the environment variable

JSON_RPC_PROVIDER_GORLI = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_OPTIMISM = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_OPTIMISM_GOERLI = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_ARBITRUM_ONE = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_ARBITRUM_GOERLI = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_POLYGON = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_POLYGON_MUMBAI = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_CELO = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_CELO_ALFAJORES = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_BNB = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_AVALANCHE = '<JSON_RPC_PROVIDER>'

Then from the root directory you can execute the CLI.

Examples

Some examples to use for manual CLI testing.

Mainnet

./bin/cli quote --tokenIn 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 --tokenOut 0x1f9840a85d5af5bf1d1762f925bdaddc4201f984 --amount 1000 --exactIn --recipient 0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B --protocols v2,v3

Best Route:
100.00% = USDC -- 0.3% --> UNI
	Raw Quote Out:
		35.72
	Gas Adjusted Quote Out:
		34.03

Gas Used Quote Token: 1.691772
Gas Used USD: 47.592951
Calldata: 0x414bf389000000000000...
Value: 0x00

  blockNumber: "13088815"
  estimatedGasUsed: "113000"
  gasPriceWei: "130000000000"


./bin/cli quote-to-ratio --token0 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 --token1 0xdac17f958d2ee523a2206206994597c13d831ec7 --feeAmount 3000 --recipient 0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B  --token0Balance 1000 --token1Balance 2000 --tickLower -120 --tickUpper 120

Best Route:
100.00% = USDT -- 0.05% --> USDC
Raw Quote Exact In:
	392.68
Gas Adjusted Quote In}:
	346.13

Gas Used Quote Token: 46.550010
Gas Used USD: 46.342899
Calldata: 0x414bf389000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000ab5801a7d398351b8be11c439e05c5b3259aec9b000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000176a736c000000000000000000000000000000000000000000000000000000001764f8650000000000000000000000000000000000000000000000000000000000000000
	Value: 0x00

	blockNumber: "13239188"
	estimatedGasUsed: "113000"
	gasPriceWei: "116690684398"

./bin/cli quote --tokenIn 0x0391D2021f89DC339F60Fff84546EA23E337750f --tokenOut 0x4d224452801ACEd8B2F0aebE155379bb5D594381 --amount 10000 --exactIn --recipient 0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B --protocols v2,v3,mixed
Best Route:
[V2 + V3] 100.00% = BOND -- [0x6591c4BcD6D7A1eb4E537DA8B78676C1576Ba244] --> USDC -- 0.3% [0xB07Fe2F407F971125D4EB1977f8aCEe8846C7324] --> APE
	Raw Quote Exact In:
		10437.85
	Gas Adjusted Quote In:
		10433.83

Gas Used Quote Token: 4.018625
Gas Used USD: 29.669402
Calldata: 0x5ae401dc0000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000000e4472b43f300000000000000000000000000000000000000000000021e19e0c9bab240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000391d2021f89dc339f60fff84546ea23e337750f000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000104b858183f00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000080000000000000000000000000ab5801a7d398351b8be11c439e05c5b3259aec9b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002358df5b3b4459a3f5b000000000000000000000000000000000000000000000000000000000000002ba0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000bb84d224452801aced8b2f0aebe155379bb5d59438100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Value: 0x00

  blockNumber: "15303839"
  estimatedGasUsed: "434000"
  gasPriceWei: "38218865879"
Total ticks crossed: 7

Rinkeby

./bin/cli quote --tokenIn 0x5592ec0cfb4dbc12d3ab100b257153436a1f0fea --tokenOut 0x4DBCdF9B62e891a7cec5A2568C3F4FAF9E8Abe2b --amount 200000 --exactIn --minSplits 1 --router alpha --chainId 4

Sepolia

./bin/cli quote --tokenIn 0x7AF17A48a6336F7dc1beF9D485139f7B6f4FB5C8 --tokenOut 0x6f14C02Fc1F78322cFd7d707aB90f18baD3B54f5 --amount 10 --exactIn --minSplits 1 --router alpha --chainId 11155111

Kovan

./bin/cli quote --tokenIn 0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa --tokenOut 0xd0a1e359811322d97991e03f863a0c30c2cf029c --amount 10 --exactIn --minSplits 1 --router alpha --chainId 42

Ropsten

./bin/cli quote --tokenIn 0x07865c6e87b9f70255377e024ace6630c1eaa37f --tokenOut 0xc778417e063141139fce010982780140aa0cd5ab --amount 200000 --exactIn --minSplits 1 --router alpha --chainId 3

Optimism

./bin/cli quote --tokenIn 0x7F5c764cBc14f9669B88837ca1490cCa17c31607 --tokenOut 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1 --amount 200000 --exactIn --minSplits 1 --router alpha --chainId 10

Optimism-Goerli

./bin/cli quote --tokenIn 0x7E07E15D2a87A24492740D16f5bdF58c16db0c4E --tokenOut 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1 --amount 200000 --exactIn --minSplits 1 --router alpha --chainId 420

Optimistic-Kovan

./bin/cli quote --tokenIn 0x7F5c764cBc14f9669B88837ca1490cCa17c31607 --tokenOut 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1 --amount 200 --exactIn --minSplits 1 --router alpha --chainId 69

Arbitrum

./bin/cli quote --tokenIn 0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9 --tokenOut 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 --amount 20000 --exactIn --minSplits 1 --router alpha --chainId 42161 --debug

Arbitrum-Rinkeby

./bin/cli quote --tokenIn 0x09b98f8b2395d076514037ff7d39a091a536206c --tokenOut 0xb47e6a5f8b33b3f17603c83a0535a9dcd7e32681 --amount 200 --exactIn --minSplits 1 --router alpha --chainId 421611

Arbitrum-Goerli

./bin/cli quote --tokenIn 0xe39ab88f8a4777030a534146a9ca3b52bd5d43a3 --tokenOut 0x8FB1E3fC51F3b789dED7557E680551d93Ea9d892 --amount 200 --exactIn --minSplits 1 --router alpha --chainId 421613

Polygon Mumbai

./bin/cli quote --tokenIn 0x001b3b4d0f3714ca98ba10f6042daebf0b1b7b6f --tokenOut 0x9c3c9283d3e44854697cd22d3faa240cfb032889 --amount 1 --exactIn --protocols v3 --recipient 0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B --minSplits 1 --router alpha --chainId 80001

Polygon Mainnet

./bin/cli quote --tokenIn 0x2791bca1f2de4661ed88a30c99a7a9449aa84174 --tokenOut 0x7ceb23fd6bc0add59e62ac25578270cff1b9f619 --amount 5 --exactIn --minSplits 1 --protocols v3 --router alpha --chainId 137

Celo Mainnet

./bin/cli quote --tokenIn CELO --tokenOut 0x765DE816845861e75A25fCA122bb6898B8B1282a --amount 5 --exactIn --minSplits 1 --protocols v3 --router alpha --chainId 42220

BNB Mainnet

./bin/cli quote --tokenIn 0x55d398326f99059fF775485246999027B3197955 --tokenOut 0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d --amount 1 --exactIn --minSplits 1 --protocols v3 --router alpha --chainId 56

AVAX Mainnet

./bin/cli quote --tokenIn 0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E --tokenOut 0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7 --amount 1000 --exactIn --minSplits 1 --protocols v3 --router alpha --chainId 43114

Adding a new Chain

The main components to complete are:

  • Deploy contracts on chain, add the pools to subgraph
  • Populate v3 providers in src/providers/v3/subgraph-provider and src/providers/v3/static-subgraph-provider
  • Populate chainId and addresses in src/util/chains.ts and src/util/addresses.ts
  • Populate token providers in src/providers/caching-token-provider and src/providers/token-provider.ts
  • Populate gas constants in src/routers/alpha-router/gas-models/*
  • Populate bases in src/routers/legacy-router/bases.ts
  • Populate test/integ/routers/alpha-router/alpha-router.integration.test.ts and src/providers/v2/static-subgraph-provider.ts
  • Populate src/routers/alpha-router/*
  • Add a log to /CHANGELOG.md
  • Run npm run integ-test successfully

Troubleshooting

ProviderGasLimit errors

The package sends many large multicall requests to nodes. You must ensure that your node provider's eth_call gas limit is high enough to successfully process the RPC calls.

By default each eth_call will consume up to:

  • 132,000,000 gas on Optimism
  • 120,000,000 gas on Arbitrum
  • 50,000,000 gas on Celo
  • 150,000,000 gas on every other network (Mainnet, Goerli, etc.)

These parameters should work on Infura and Alchemy by default.

This total amount of gas each eth_call can consume is equal to the multicallChunk config value multiplied by the gasLimitPerCall config value. If you are using a node provider with a lower gas limit per eth_call you will need to override the default V3QuoteProvider with an instance that lowers the multicallChunk and gasLimitPerCall parameters such that the multiplication is below your node providers limit. Lowering these values will cause each multicall to consume less gas. See here for examples of how to set these values. Note some providers have different limits per chain.

If you are running your own node, we recommend you configure start your node with a higher gas limit per call. For example, on Geth you can use the command line argument --rpc.gascap 150000000 to raise the limit to 150m, which is enough to run the default configuration of this package.

If you are using Hardhat mainnet forking, you should add blockGasLimit: 150_000_000 to your Hardhat config to use the default package configuration.

More Repositories

1

web3-react

A simple, maximally extensible, dependency minimized framework for building modern Ethereum dApps
TypeScript
5,373
star
2

interface

🦄 Open source interfaces for the Uniswap protocol
TypeScript
4,923
star
3

v3-core

🦄 🦄 🦄 Core smart contracts of Uniswap v3
TypeScript
4,386
star
4

v2-core

🦄 🦄 Core smart contracts of Uniswap V2
TypeScript
2,964
star
5

v4-core

🦄 🦄 🦄 🦄 Core smart contracts of Uniswap v4
Solidity
1,810
star
6

v3-periphery

🦄 🦄 🦄 Peripheral smart contracts for interacting with Uniswap v3
TypeScript
1,194
star
7

token-lists

📚 The Token Lists specification
TypeScript
926
star
8

v2-periphery

🎚 Peripheral smart contracts for interacting with Uniswap V2
Solidity
916
star
9

permit2

🔑🔑🔑 next generation token approvals mechanism
Solidity
745
star
10

v4-periphery

🦄 🦄 🦄 🦄 Peripheral smart contracts for interacting with Uniswap v4
Solidity
640
star
11

wallet

Uniswap Wallet is the simplest, safest, and most powerful self-custodial crypto wallet. It is developed by the Uniswap Labs team, inventors of the Uniswap Protocol.
TypeScript
530
star
12

merkle-distributor

📦 A smart contract that distributes a balance of tokens according to a merkle root
TypeScript
497
star
13

v3-sdk

🛠 An SDK for building applications on top of Uniswap V3
TypeScript
453
star
14

v1-contracts

🐍Uniswap V1 smart contracts
Python
438
star
15

v2-sdk

🛠 An SDK for building applications on top of Uniswap V2
TypeScript
427
star
16

universal-router

Uniswap's Universal Router for NFT and ERC20 swapping
TypeScript
408
star
17

v2-subgraph

Uniswap V2 subgraph 📊
TypeScript
399
star
18

UniswapX

🦄 Gasless ERC20 swap settlement protocol 🦄
Solidity
380
star
19

v3-info

Interface for Uniswap V3 analytics. 📊
TypeScript
378
star
20

v3-subgraph

Subgraph for Uniswap V3
TypeScript
341
star
21

v3-staker

Canonical liquidity mining contract for Uniswap V3
TypeScript
330
star
22

docs

📓 Uniswap V3 docs website
TypeScript
310
star
23

default-token-list

◦ The Uniswap default token list
JavaScript
298
star
24

info

ℹ️ Uniswap v1+v2 analytics
JavaScript
283
star
25

tokenlists-org

A site for browsing featured token lists
JavaScript
175
star
26

sybil-list

List of verified mappings from Ethereum addresses to social profiles
162
star
27

routing-api

TypeScript
147
star
28

org-v1

🌐 Uniswap protocol homepage
JavaScript
144
star
29

token-list-bridge-utils

TypeScript
138
star
30

solidity-lib

📖 Solidity libraries that are shared across Uniswap contracts
TypeScript
131
star
31

widgets

TypeScript
124
star
32

examples

TypeScript
124
star
33

governance-seatbelt

Make governance safer
TypeScript
116
star
34

retroactive-query

👀⬅️ Queries for surfacing information about past users of Uniswap
109
star
35

v3-market-depth-study

Market depth calculation for Uniswap v3
Python
107
star
36

swap-router-contracts

Smart contracts for swapping on Uniswap V2 and V3. Superseded by https://github.com/Uniswap/universal-router
TypeScript
102
star
37

unisocks

🧦
JavaScript
101
star
38

v3-polars

Jupyter Notebook
96
star
39

redux-multicall

A React + Redux library for fetching, batching, and caching chain state via the MultiCall contract.
TypeScript
93
star
40

api-deprecated

🔮 Uniswap market data
TypeScript
86
star
41

governance

🏛 Governance contracts for the Uniswap protocol
Solidity
78
star
42

sdk-core

⚙️ Code shared across TypeScript Uniswap SDK versions
TypeScript
77
star
43

universal-router-sdk

TypeScript
77
star
44

hardhat-v3-deploy

Hardhat plugin for Uniswap V3 deployment
TypeScript
75
star
45

sybil-interface

Interface that incorporates Sybil, a governance tool for discovering delegates.
TypeScript
69
star
46

deploy-v3

CLI to deploy Uniswap V3 to any Ethereum compatible network
TypeScript
62
star
47

liquidity-staker

Initial UNI liquidity staking contracts
TypeScript
60
star
48

unisocks-erc721

🧦
Python
52
star
49

sdks

prototype SDK monorepo
TypeScript
44
star
50

v1-docs

42
star
51

uniswapx-sdk

SDK bindings for the UniswapX protocol
TypeScript
40
star
52

uniswapx-parameterization-api

TypeScript
40
star
53

widgets-demo

@uniswap/widgets demo
40
star
54

uniswapx-service

UniswapX Order Service
TypeScript
39
star
55

marketdepth-v3

Uniswap v3 onchain market depth calculator
Solidity
39
star
56

unipig

🦄🐷
JavaScript
38
star
57

v3-new-chain-deployments

Instructions for deploying the Uniswap V3 protocol to new chains after receiving approval from governance.
34
star
58

unified-routing-api

TypeScript
34
star
59

uniswap-first-contract-example

Example code for the smart contract getting started example.
JavaScript
29
star
60

permit2-sdk

TypeScript
29
star
61

old-solidity-contracts

⚠️ DEPRECATED
Python
29
star
62

unisocks-erc20

🧦
Python
26
star
63

v3-core-optimism

Optimism fork of the V3 core contracts
TypeScript
25
star
64

lp-action-contracts

Solidity
25
star
65

sybil-verifier-worker

Cloudlfare worker to verify mappings for Sybil, a governance tool for delegates.
JavaScript
25
star
66

router-sdk

TypeScript
23
star
67

advanced-weth

A smart contract that wraps WETH that adds functionality for transparently dealing in WETH
JavaScript
20
star
68

ethers-rs-mobile

Rust
17
star
69

v3-periphery-optimism

Optimism fork of the V3 periphery contracts
TypeScript
17
star
70

ds-tools

Python
15
star
71

jest-environment-hardhat

TypeScript
15
star
72

conedison

Utilities for Uniswap repositories
TypeScript
13
star
73

extended-token-list

JavaScript
12
star
74

v1-interface

💀 Legacy interface for the Uniswap V1 protocol
JavaScript
12
star
75

returns-comparison-study

Python
11
star
76

mask-app

TypeScript
10
star
77

convert-cidv0-cidv1

GitHub action that converts CIDv0 for IPFS to CIDv1
JavaScript
9
star
78

polygon-bridge

Bridge contract for Polygon
TypeScript
9
star
79

mrkl-drop-data-chunks

The Merkle drop data broken up into chunks of 101 addresses and including a mapping file
TypeScript
8
star
80

fx-comparison-study

R
7
star
81

snapshot-gas-cost

Tiny utility package for snapshotting gas costs
TypeScript
7
star
82

eslint-config

Uniswap ESLint config
JavaScript
7
star
83

analytics

Uniswap analytics
TypeScript
7
star
84

interface-redirects

↪️ Redirects for alternative app.uniswap.org domains
6
star
85

analytics-events

Uniswap analytics events
TypeScript
6
star
86

permit2-dashboard

a dashboard for managing permit2 permissions
TypeScript
6
star
87

replace-vercel-dns-records

GitHub action for replacing Vercel DNS records, particularly useful for IPFS DNSLink TXT records
JavaScript
5
star
88

view-quoter-v3

Solidity
5
star
89

ticketblaster

a whimsical ticketing thing
TypeScript
4
star
90

relayer

Contracts supporting relayed UniversalRouter swaps
Solidity
4
star
91

v1-sdk

🛠 An SDK for building applications on top of Uniswap V1
TypeScript
3
star
92

signer

TypeScript
3
star
93

contracts

Collection of all Uniswap smart contracts
Solidity
2
star
94

interfaces

Solidity
2
star
95

info-redirects

↪️ Redirects for the uniswap.info domain
1
star