API Reference

Uniswap class

class uniswap.Uniswap(address: Optional[Union[Address, ChecksumAddress, str]], private_key: Optional[str], provider: Optional[str] = None, web3: Optional[web3.main.Web3] = None, version: int = 1, default_slippage: float = 0.01, factory_contract_addr: Optional[str] = None, router_contract_addr: Optional[str] = None)

Wrapper around Uniswap contracts.

Parameters
  • address – The public address of the ETH wallet to use.

  • private_key – The private key of the ETH wallet to use.

  • provider – Can be optionally set to a Web3 provider URI. If none set, will fall back to the PROVIDER environment variable, or web3 if set.

  • web3 – Can be optionally set to a custom Web3 instance.

  • version – Which version of the Uniswap contracts to use.

  • default_slippage – Default slippage for a trade, as a float (0.01 is 1%). WARNING: slippage is untested.

  • factory_contract_addr – Can be optionally set to override the address of the factory contract.

  • router_contract_addr – Can be optionally set to override the address of the router contract (v2 only).

get_price_input(token0: Union[Address, ChecksumAddress], token1: Union[Address, ChecksumAddress], qty: int, fee: Optional[int] = None, route: Optional[List[Union[Address, ChecksumAddress]]] = None)int

Given qty amount of the input token0, returns the maximum output amount of output token1.

get_price_output(token0: Union[Address, ChecksumAddress], token1: Union[Address, ChecksumAddress], qty: int, fee: Optional[int] = None, route: Optional[List[Union[Address, ChecksumAddress]]] = None)int

Returns the minimum amount of token0 required to buy qty amount of token1.

make_trade(input_token: Union[Address, ChecksumAddress], output_token: Union[Address, ChecksumAddress], qty: Union[int, Wei], recipient: Union[Address, ChecksumAddress] = None, fee: int = None, slippage: float = None, fee_on_transfer: bool = False)hexbytes.main.HexBytes

Make a trade by defining the qty of the input token.

make_trade_output(input_token: Union[Address, ChecksumAddress], output_token: Union[Address, ChecksumAddress], qty: Union[int, Wei], recipient: Union[Address, ChecksumAddress] = None, fee: int = None, slippage: float = None)hexbytes.main.HexBytes

Make a trade by defining the qty of the output token.

get_eth_balance()Wei

Get the balance of ETH for your address.

get_token_balance(token: Union[Address, ChecksumAddress])int

Get the balance of a token for your address.

get_ex_eth_balance(token: Union[Address, ChecksumAddress])int

Get the balance of ETH in an exchange contract.

Supports Uniswap v1

get_ex_token_balance(token: Union[Address, ChecksumAddress])int

Get the balance of a token in an exchange contract.

Supports Uniswap v1

get_exchange_rate(token: Union[Address, ChecksumAddress])float

Get the current ETH/token exchange rate of the token.

Supports Uniswap v1

add_liquidity(token: Union[Address, ChecksumAddress], max_eth: Wei, min_liquidity: int = 1)hexbytes.main.HexBytes

Add liquidity to the pool.

Supports Uniswap v1

remove_liquidity(token: str, max_token: int)hexbytes.main.HexBytes

Remove liquidity from the pool.

Supports Uniswap v1

approve(token: Union[Address, ChecksumAddress], max_approval: Optional[int] = None)None

Give an exchange/router max approval of a token.

get_token(address: Union[Address, ChecksumAddress], abi_name: str = 'erc20')uniswap.token.ERC20Token

Retrieves metadata from the ERC20 contract of a given token, like its name, symbol, and decimals.

get_weth_address()ChecksumAddress

Retrieves the WETH address from the contracts (which may vary between chains).

Supports Uniswap v2, v3

get_raw_price(token_in: Union[Address, ChecksumAddress], token_out: Union[Address, ChecksumAddress], fee: int = 3000)float

Returns current price for pair of tokens [token_in, token_out] regrading liquidity that is being locked in the pool

Supports Uniswap v2, v3

estimate_price_impact(token_in: Union[Address, ChecksumAddress], token_out: Union[Address, ChecksumAddress], amount_in: int, fee: Optional[int] = None, route: Optional[List[Union[Address, ChecksumAddress]]] = None)float

Returns the estimated price impact as a positive float (0.01 = 1%).

NOTE: Work-in-progress.

See examples/price_impact.py for an example which uses this.

get_fee_maker()float

Get the maker fee.

Supports Uniswap v1, v2

get_fee_taker()float

Get the taker fee.

Supports Uniswap v1, v2

Token class

class uniswap.token.BaseToken(symbol: str, address: Union[Address, ChecksumAddress])

Base for tokens of all kinds

symbol: str

Symbol such as ETH, DAI, etc.

address: Union[Address, ChecksumAddress]

Address of the token contract.

class uniswap.token.ERC20Token(symbol: str, address: Union[Address, ChecksumAddress], name: str, decimals: int)

Represents an ERC20 token

name: str

Name of the token, as specified in the contract.

symbol: str

Symbol such as ETH, DAI, etc.

address: Union[Address, ChecksumAddress]

Address of the token contract.

decimals: int

Decimals used to denominate the token.

Exceptions

exception uniswap.exceptions.InvalidToken(address: Any)

Raised when an invalid token address is used.

exception uniswap.exceptions.InsufficientBalance(had: int, needed: int)

Raised when the account has insufficient balance for a transaction.