Skip to main content

DEXsim

An API to create and interact with a local version of Uniswap v3. This library use a pre-deployed version of Uniswap v3 along with a set of pre-deployed pools. It's intended for use in agent-based modeling or other simulation environments. It's also a useful tool in learning about how Uniswap works.

Under the covers, dexsim is built on Simular that uses a production-grade EVM. Uniswap v3 is deployed from a snapshot from the main Ethereun chain. However, it does not include any state other than currently supported pools.

The pools are only deployed, not initialized with an starting price. You can set the starting price via the configuration file described below.

Install

> pip install dexsim

Getting started

The following pools can be configured and used:

pool fee pool name
usdc/dai 100 (0.01) usdc_dai_100
usdc/dai 500 (0.05) usdc_dai_500
usdc/weth 500 (0.05) usdc_weth_500
dai/weth 500 (0.05) dai_weth_500
wbtc/weth 500 (0.05) wbtc_weth_500
wbtc/dai 500 (0.05) wbtc_dai_500
wbtc/usdc 500 (0.05) wbtc_usdc_500

The pools must be initialized with a starting price via the YAML configuration file. You only need to configure the pools you intend to use. Here's an example of the file format:

Example config file: example.yaml

simulator:
  pools:
    usdc_dai_100: [1, 1]
    usdc_weth_500: [5000, 1]

This configures 2 pools with there exchange prices:

  • usdc_dai_100 = 1 usdc for 1 dai
  • usdc_weth_500 = 5000 usdc for 1 weth.

Here's an example of creating and using the pools:

from dexsim import DEX

# Create the Unswap DEX based on the configuration file
dex = DEX('./example.yaml')
assert 2 == dex.total_number_of_pools()
assert (5000, 0.0002) == dex.pools.usdc_weth_500.exchange_rates()

# create a wallet for Bob
bob = dex.create_wallet()

# mint some ERC20 tokens for bob
dex.pools.usdc_weth_500.mint_tokens(10000, 2, bob)

# mint a liquidity position for bob in the price range of $4900 - $5100 by 
# providing 10_000 usdc and 2 weth.  
# Note the prices are specified in terms of the y token (weth)
_, _, _, nft_id = dex.pools.usdc_weth_500.mint_liquidity_position(
        10000, 2, 1 / 4900, 1 / 5100, bob
    )

Lending

Each pool can also provide a lending pool for models. Lending pools can be configured along with the uniswap pools. For example:

simulator:
 pools:
   usdc_dai_100: [1, 1]
   usdc_weth_500: [5000, 1]
 lending:
   usdc_weth_500: weth

adds a lending pool for the USDC/WETH pair. It specifies that weth is the collateral token.

The format for configuring a lending pool is a key/value pair, where the key is the uniswap pool name and the value is the name of the collateral token. For example: uniswap pool name : name of collateral token.

When the dex is started it will automtically configure and create any specified uniswap and lending pools. Interacting with the lending API is very similar to working with pools.

Here's an example using the configuration above where lending is available for the usdc_weth_500 pool and the collateral token is weth

from dexsim import DEX

# borrow USDC with WETH collateral 
# note: under the covers, the values are converted to 1e18 decimals
BORROW_AMOUNT = 10_000
WETH_REQUIRED = 2.5

dex = DEX('...configuration file...')
agent = dex.create_wallet()

assert 0 == dex.lending.usdc_weth_500.collateral_token_balance(agent)

# calculate how much WETH is required to borrow USDC
weth_needed = dex.lending.usdc_weth_500.collateral_required(BORROW_AMOUNT)
assert WETH_REQUIRED == weth_needed

# mint weth for the user (this is for convenience) mint erc20 collateral token
dex.lending.usdc_weth_500.mint_collateral_token(weth_needed, agent)
assert WETH_REQUIRED == dex.lending.usdc_weth_500.collateral_token_balance(agent)

# supply the weth collateral for the loan
dex.lending.usdc_weth_500.provide_collateral(weth_needed, agent)

# take loan
dex.lending.usdc_weth_500.borrow(BORROW_AMOUNT, agent)

# check loan information (value are returned in 1e18 format)
assert [
    2500000000000000000,
    10000000000000000000000,
    True,
] == dex.lending.usdc_weth_500.loan_information(agent)

# check we got the loan
assert 10_000 == dex.lending.usdc_weth_500.lending_token_balance(agent)

See dexsim/lender.py for all the functionality.

See tests/ for several examples of using both pool and lending APIs.

Release files for dexsim 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dexsim 0.2.3
File Size Uploaded
dexsim-0.2.3.tar.gz 309.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dexsim 0.2.3
File Interpreter ABI Platform
dexsim-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size:629.1 kB

Release files / dexsim-0.2.3.tar.gz

Download URL dexsim-0.2.3.tar.gz
Size 309.4 kB
Tags Source
SHA-256 checksum
How to use checksums
884aec5d133ea5f10de64e01d139c465634706ca0b9027fbf96ad178b9fc47fe
BLAKE2b-256 checksum
How to use checksums
8d8f0558ab131cd0f335ec0ac525b5f54e7c4eee0c029bb4ce8f95b4a94f712b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 3, 2025.

Transparency log

Release files / dexsim-0.2.3-py3-none-any.whl

Download URL dexsim-0.2.3-py3-none-any.whl
Size 319.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c0d9264bbd759ddcf526f3b6712a6d818c2a6be510f28f91aef43f9298e9778
BLAKE2b-256 checksum
How to use checksums
44ac6f46991c8007b1ae6c3f1973c3c366c75baa0e2a9e23df8f86553095221b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 3, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page