uniswap-sdk


Nameuniswap-sdk JSON
Version 0.3.1 PyPI version JSON
download
home_pagehttps://github.com/ApeWorX/uniswap-sdk
Summaryuniswap-sdk: SDK for Uniswap smart contracts
upload_time2025-08-27 19:37:28
maintainerNone
docs_urlNone
authorApeWorX Ltd.
requires_python<4,>=3.10
licenseApache-2.0
keywords ethereum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Uniswap SDK

Ape-based SDK for working with deployments of Uniswap protocol

## Dependencies

- [python3](https://www.python.org/downloads) version 3.10 or greater, python3-dev

## Installation

### via `pip`

You can install the latest release via [`pip`](https://pypi.org/project/pip/):

```bash
pip install uniswap_sdk
```

### via `setuptools`

You can clone the repository and use [`setuptools`](https://github.com/pypa/setuptools) for the most up-to-date version:

```bash
git clone https://github.com/SilverBackLtd/uniswap-sdk.git
cd uniswap-sdk
python3 setup.py install
```

## Quick Usage

### Scripting

The SDK can be used for any scripting task very easily:

```py
>>> from ape_tokens import tokens
>>> from uniswap_sdk import Uniswap
>>> uni = Uniswap(use_v3=False)  # Can skip versions and only index certain tokens
>>> list(uni.index(tokens=tokens))  # Takes time, but makes planning faster (recommended for scripting)
>>> uni.price("UNI", "USDC")  # Get liquidity-weighted prices of entire index in real-time
Decimal("4.75")
>>> usdc = tokens["USDC"]
>>> tx = uni.swap(
...     "UNI",
...     usdc,  # Can use any ContractInstance type
...     amount_in="12 UNI",  # Uses Ape's conversion system
...     slippage=0.3,
...     deadline=timedelta(minutes=2),
...     sender=trader,
... )
```

To swap directly with Ether (native token, **NOT ERC20**):

```py
>>> uni.swap(want="UNI", sender=trader, value="1 ether")
# OR
>>> uni.swap(want="UNI", amount_out="10 UNI", sender=trader, value="1 ether")
# OR
>>> uni.swap(have="UNI", amount_in="10 UNI", native_out=True, ...)
```

If `have=` is not present but `value=` is, then `have=` will be set to WETH (if available on your network) for solving.
If `amount_in=`, `max_amount_in=`, and `amount_out=` are not present (1st example), then `value=` will work like `amount_in=`.
If `amount_out` is present (2nd example), then `value=` will act like setting `max_amount_in=`.
If `native_out=True` is present (3rd example), then the amount received will be native ether and not an ERC20.

### CLI

This SDK installs a special CLI command `uni`.
You can use this command to do common tasks with the SDK such as finding prices or performing swaps.

Try `uni --help` after installing the SDK to learn more about what the CLI can do.

### Silverback

The SDK has special support for use within [Silverback](https://silverback.apeworx.io) bots,
which takes advantage of real-time processing to drastically reduce the overhead of certain search
and solver functions of the SDK:

```py
from ape_tokens import tokens
from silverback import SilverbackBot
from uniswap_sdk import Uniswap

bot = SilverbackBot()
uni = Uniswap()
uni.install(bot)  # This replaces having to do `uni.index()`

# NOTE: The bot will now process all swaps in the background to keep it's indexes up-to-date!

@bot.cron("* * * * *")
async def weth_price(t):
    # So now when you use top-level functions, it takes advantage of cached data in the index
    return uni.price("WETH", "USDC")  # This executes faster w/ Silverback!
```

### Custom Solver

The SDK comes with a default Solver that should be performant enough for most situations.
However, it is likely that you will want to design a custom solver function or class in order
to obtain better results when performing actions like `uni.swap` which leverage the solver.

You can override the default solver by providing a function or object which matches the following interface:

```py
from uniswap_sdk import Order
Route = tuple[PairType, ...]  # 1 (or more) `PairType`s (e.g. `UniswapV2Pair`, etc.)
Solution = dict[Route, Decimal]  # mapping of Route -> amount to swap via Route
SolverType = Callable[[Order, Iterable[Route]], Solution]
# Given `amount` of `token` and `*routes`, find `solution`
```

This can be a class, allowing more flexibility in how you design your solver:

```py
class Solver:
    def __call__(self, order: Order, routes: Iterable[Route]) -> Solution:
        # This function must match `SolverType` to work

my_solver = Solver(...)

uni = Uniswap(use_solver=my_solver)
uni.solve(...)  # Will now use `my_solver` to find solutions (also `uni.swap`)
```

## Development

This project is in development and should be considered a beta.
Things might not be in their final state and breaking changes may occur.
Comments, questions, criticisms and pull requests are welcomed.

### Support

Support for various Uniswap-related protocols:

- [ ] V1
- [x] V2
- [x] V3
- [ ] V4
- [ ] Permit2
- [x] UniversalRouter

## License

This project is licensed under the [Apache 2.0](LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ApeWorX/uniswap-sdk",
    "name": "uniswap-sdk",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.10",
    "maintainer_email": null,
    "keywords": "ethereum",
    "author": "ApeWorX Ltd.",
    "author_email": "admin@apeworx.io",
    "download_url": "https://files.pythonhosted.org/packages/ba/2a/2a4030173e14c14cc70cc8df4aac8014b5dce2de7d1efef0787699c33eed/uniswap_sdk-0.3.1.tar.gz",
    "platform": null,
    "description": "# Uniswap SDK\n\nApe-based SDK for working with deployments of Uniswap protocol\n\n## Dependencies\n\n- [python3](https://www.python.org/downloads) version 3.10 or greater, python3-dev\n\n## Installation\n\n### via `pip`\n\nYou can install the latest release via [`pip`](https://pypi.org/project/pip/):\n\n```bash\npip install uniswap_sdk\n```\n\n### via `setuptools`\n\nYou can clone the repository and use [`setuptools`](https://github.com/pypa/setuptools) for the most up-to-date version:\n\n```bash\ngit clone https://github.com/SilverBackLtd/uniswap-sdk.git\ncd uniswap-sdk\npython3 setup.py install\n```\n\n## Quick Usage\n\n### Scripting\n\nThe SDK can be used for any scripting task very easily:\n\n```py\n>>> from ape_tokens import tokens\n>>> from uniswap_sdk import Uniswap\n>>> uni = Uniswap(use_v3=False)  # Can skip versions and only index certain tokens\n>>> list(uni.index(tokens=tokens))  # Takes time, but makes planning faster (recommended for scripting)\n>>> uni.price(\"UNI\", \"USDC\")  # Get liquidity-weighted prices of entire index in real-time\nDecimal(\"4.75\")\n>>> usdc = tokens[\"USDC\"]\n>>> tx = uni.swap(\n...     \"UNI\",\n...     usdc,  # Can use any ContractInstance type\n...     amount_in=\"12 UNI\",  # Uses Ape's conversion system\n...     slippage=0.3,\n...     deadline=timedelta(minutes=2),\n...     sender=trader,\n... )\n```\n\nTo swap directly with Ether (native token, **NOT ERC20**):\n\n```py\n>>> uni.swap(want=\"UNI\", sender=trader, value=\"1 ether\")\n# OR\n>>> uni.swap(want=\"UNI\", amount_out=\"10 UNI\", sender=trader, value=\"1 ether\")\n# OR\n>>> uni.swap(have=\"UNI\", amount_in=\"10 UNI\", native_out=True, ...)\n```\n\nIf `have=` is not present but `value=` is, then `have=` will be set to WETH (if available on your network) for solving.\nIf `amount_in=`, `max_amount_in=`, and `amount_out=` are not present (1st example), then `value=` will work like `amount_in=`.\nIf `amount_out` is present (2nd example), then `value=` will act like setting `max_amount_in=`.\nIf `native_out=True` is present (3rd example), then the amount received will be native ether and not an ERC20.\n\n### CLI\n\nThis SDK installs a special CLI command `uni`.\nYou can use this command to do common tasks with the SDK such as finding prices or performing swaps.\n\nTry `uni --help` after installing the SDK to learn more about what the CLI can do.\n\n### Silverback\n\nThe SDK has special support for use within [Silverback](https://silverback.apeworx.io) bots,\nwhich takes advantage of real-time processing to drastically reduce the overhead of certain search\nand solver functions of the SDK:\n\n```py\nfrom ape_tokens import tokens\nfrom silverback import SilverbackBot\nfrom uniswap_sdk import Uniswap\n\nbot = SilverbackBot()\nuni = Uniswap()\nuni.install(bot)  # This replaces having to do `uni.index()`\n\n# NOTE: The bot will now process all swaps in the background to keep it's indexes up-to-date!\n\n@bot.cron(\"* * * * *\")\nasync def weth_price(t):\n    # So now when you use top-level functions, it takes advantage of cached data in the index\n    return uni.price(\"WETH\", \"USDC\")  # This executes faster w/ Silverback!\n```\n\n### Custom Solver\n\nThe SDK comes with a default Solver that should be performant enough for most situations.\nHowever, it is likely that you will want to design a custom solver function or class in order\nto obtain better results when performing actions like `uni.swap` which leverage the solver.\n\nYou can override the default solver by providing a function or object which matches the following interface:\n\n```py\nfrom uniswap_sdk import Order\nRoute = tuple[PairType, ...]  # 1 (or more) `PairType`s (e.g. `UniswapV2Pair`, etc.)\nSolution = dict[Route, Decimal]  # mapping of Route -> amount to swap via Route\nSolverType = Callable[[Order, Iterable[Route]], Solution]\n# Given `amount` of `token` and `*routes`, find `solution`\n```\n\nThis can be a class, allowing more flexibility in how you design your solver:\n\n```py\nclass Solver:\n    def __call__(self, order: Order, routes: Iterable[Route]) -> Solution:\n        # This function must match `SolverType` to work\n\nmy_solver = Solver(...)\n\nuni = Uniswap(use_solver=my_solver)\nuni.solve(...)  # Will now use `my_solver` to find solutions (also `uni.swap`)\n```\n\n## Development\n\nThis project is in development and should be considered a beta.\nThings might not be in their final state and breaking changes may occur.\nComments, questions, criticisms and pull requests are welcomed.\n\n### Support\n\nSupport for various Uniswap-related protocols:\n\n- [ ] V1\n- [x] V2\n- [x] V3\n- [ ] V4\n- [ ] Permit2\n- [x] UniversalRouter\n\n## License\n\nThis project is licensed under the [Apache 2.0](LICENSE).\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "uniswap-sdk: SDK for Uniswap smart contracts",
    "version": "0.3.1",
    "project_urls": {
        "Homepage": "https://github.com/ApeWorX/uniswap-sdk"
    },
    "split_keywords": [
        "ethereum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d04164c0c15f1959962210483036dd96aaacb2415ed8354a3c22e8537fc03e4d",
                "md5": "be8cebacb132978d1a1d0f8878cc7c70",
                "sha256": "22671cde735c3007e4c7cf3b63a2888fd39c79a548d930ea85095271896f7400"
            },
            "downloads": -1,
            "filename": "uniswap_sdk-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "be8cebacb132978d1a1d0f8878cc7c70",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.10",
            "size": 44191,
            "upload_time": "2025-08-27T19:37:27",
            "upload_time_iso_8601": "2025-08-27T19:37:27.329048Z",
            "url": "https://files.pythonhosted.org/packages/d0/41/64c0c15f1959962210483036dd96aaacb2415ed8354a3c22e8537fc03e4d/uniswap_sdk-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ba2a2a4030173e14c14cc70cc8df4aac8014b5dce2de7d1efef0787699c33eed",
                "md5": "351a1db829da61f13d90896ee2dccd84",
                "sha256": "aa6a2891735c4cd8065079116af12b1e81153385aef3d5c06c1da0216b9c67c4"
            },
            "downloads": -1,
            "filename": "uniswap_sdk-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "351a1db829da61f13d90896ee2dccd84",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.10",
            "size": 50422,
            "upload_time": "2025-08-27T19:37:28",
            "upload_time_iso_8601": "2025-08-27T19:37:28.746052Z",
            "url": "https://files.pythonhosted.org/packages/ba/2a/2a4030173e14c14cc70cc8df4aac8014b5dce2de7d1efef0787699c33eed/uniswap_sdk-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-27 19:37:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ApeWorX",
    "github_project": "uniswap-sdk",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "uniswap-sdk"
}
        
Elapsed time: 1.89482s