Skip to main content

uniswap-sdk: SDK for Uniswap smart contracts

Project description

Uniswap SDK

Ape-based SDK for working with deployments of Uniswap protocol

Dependencies

  • python3 version 3.10 or greater, python3-dev

Installation

via pip

You can install the latest release via pip:

pip install uniswap_sdk

via setuptools

You can clone the repository and use setuptools for the most up-to-date version:

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:

>>> 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):

>>> 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 bots, which takes advantage of real-time processing to drastically reduce the overhead of certain search and solver functions of the SDK:

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:

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:

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
  • V2
  • V3
  • V4
  • Permit2
  • UniversalRouter

License

This project is licensed under the Apache 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uniswap_sdk-0.3.2.tar.gz (53.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uniswap_sdk-0.3.2-py3-none-any.whl (44.2 kB view details)

Uploaded Python 3

File details

Details for the file uniswap_sdk-0.3.2.tar.gz.

File metadata

  • Download URL: uniswap_sdk-0.3.2.tar.gz
  • Upload date:
  • Size: 53.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.19

File hashes

Hashes for uniswap_sdk-0.3.2.tar.gz
Algorithm Hash digest
SHA256 f8b733680a164add88028658ede45af5f9500185e0d9fae9baf959cf86cd227a
MD5 4eae87f5c2fc152c810a3db49d9ef636
BLAKE2b-256 2d230f37940ec501f83f0f3101d27f6954c7d2589619037d7d5ab262cec9f882

See more details on using hashes here.

File details

Details for the file uniswap_sdk-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: uniswap_sdk-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 44.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.19

File hashes

Hashes for uniswap_sdk-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 405088658152ff65935c0c9bd77ab9daf23029e7cf395e96d6ac81d7ca38bd54
MD5 d57e263072bef14f9238400b762f3a66
BLAKE2b-256 ac427f0d6ddfb25d389e8c86725f7672e2b00cfb14bf5902354c703dc3217266

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page