This library attempts to present a clean interface to Uniswap, but in order to use it to its full potential, you must familiarize yourself with the official Uniswap documentation:
Table of contents
You can install the latest release from PyPI, or install the latest commit directly from git:
# Install the latest release from PyPI: pip install uniswap-python # or install from git: pip install git+git://github.com/uniswap-python/uniswap-python.git # or clone and install with poetry: git clone https://github.com/uniswap-python/uniswap-python.git cd uniswap-python poetry install
If you want to trade you need to provide your address and private key. If not, you can set them to
In addition, the
Uniswap class takes several optional parameters, as documented in the API Reference.
from uniswap import Uniswap address = "YOUR ADDRESS" # or None if you're not going to make transactions private_key = "YOUR PRIVATE KEY" # or None if you're not going to make transactions version = 2 # specify which version of Uniswap to use provider = "WEB3 PROVIDER URL" # can also be set through the environment variable `PROVIDER` uniswap = Uniswap(address=address, private_key=private_key, version=version, provider=provider) # Some token addresses we'll be using later in this guide eth = "0x0000000000000000000000000000000000000000" bat = "0x0D8775F648430679A709E98d2b0Cb6250d2887EF" dai = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
The program expects an environment variables to be set in order to run the program. You can use an Infura node, since the transactions are being signed locally and broadcast as a raw transaction. The environment variable is:
PROVIDER # HTTP Provider for web3
These methods assume a certain route for the swap to take, which may not be the optimal route. See issue #69 for details.
There are two functions to retrieve the price for a given pair, one for specifying how much you get given a certain amount of the input token, and another for specifying how much you need to pay to receive a certain amount of the output token.
Returns the cost of the given number of input tokens, priced in the output token.
# Returns the amount of DAI you get for 1 ETH (10^18 wei) uniswap.get_price_input(eth, dai, 10**18)
Returns the amount of input token you need for the given amount of output tokens.
# Returns the amount of ETH you need to pay (in wei) to get 1000 DAI uniswap.get_price_output(eth, dai, 1_000 * 10**18)
These methods return the price as an integer in the smallest unit of the token. You need to ensure that you know how many decimals the token you’re trying to trade uses to get prices in the common decimal format. See issue #12 for details.
Decimals for common tokens:
ETH, DAI, and BAT uses 18 decimals (as you can see in code below)
WBTC uses 8 decimals
USDC and USDT uses 6 decimals
You can look up the number of decimals used by a particular token by looking up the contract on Etherscan.
The same route assumptions and need for handling decimals apply here as those mentioned in the previous section.
# Make a trade where the input qty being known parameters uniswap.make_trade(eth, bat, 1*10**18) # sell 1 ETH for however many BAT uniswap.make_trade(bat, eth, 1*10**18) # sell 1 BAT for however many ETH uniswap.make_trade(bat, dai, 1*10**18) # sell 1 BAT for however many DAI uniswap.make_trade(eth, bat, 1*10**18, "0x123...") # sell 1 ETH for however many BAT, and send the BAT to the provided address
# Make a trade where the output qty is known, based on the input parameters uniswap.make_trade_output(eth, bat, 1*10**18) # buy however many ETH for 1 BAT uniswap.make_trade_output(bat, eth, 1*10**18) # buy however many BAT for 1 ETH uniswap.make_trade_output(bat, dai, 1*10**18, "0x123...") # buy however many BAT for 1 DAI, and send the BAT to the provided address
# Get the balance of ETH in an exchange contract. uniswap.get_ex_eth_balance(bat) # Get the balance of a token in an exchange contract. uniswap.get_ex_token_balance(bat) # Get the exchange rate of token/ETH uniswap.get_exchange_rate(bat)