API Reference#
Uniswap class#
- class uniswap.Uniswap(address: Optional[Union[Address, ChecksumAddress, str]], private_key: Optional[str], provider: Optional[str] = None, web3: Optional[Web3] = None, version: int = 1, default_slippage: float = 0.01, use_estimate_gas: bool = True, factory_contract_addr: Optional[str] = None, router_contract_addr: Optional[str] = None, enable_caching: bool = False)#
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).
enable_caching – Optionally enables middleware caching RPC method calls.
- 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: Optional[Union[Address, ChecksumAddress]] = None, fee: Optional[int] = None, slippage: Optional[float] = None, fee_on_transfer: bool = False) 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: Optional[Union[Address, ChecksumAddress]] = None, fee: Optional[int] = None, slippage: Optional[float] = None) 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 #
Add liquidity to the pool.
Supports Uniswap v1
- remove_liquidity(token: str, max_token: int) HexBytes #
Remove liquidity from the pool.
Supports Uniswap v1
- mint_liquidity(pool: Contract, amount_0: int, amount_1: int, tick_lower: int, tick_upper: int, deadline: int = 18446744073709551616) TxReceipt #
add liquidity to pool and mint position nft
Supports Uniswap v3
- close_position(tokenId: int, amount0Min: int = 0, amount1Min: int = 0, deadline: Optional[int] = None) TxReceipt #
remove all liquidity from the position associated w/ tokenId, collect fees, and burn token.
Supports Uniswap v3
- get_tvl_in_pool(pool: Contract) Tuple[float, float] #
Iterate through each tick in a pool and calculate the TVL on-chain
Note: the output of this function may differ from what is returned by the UniswapV3 subgraph api (Uniswap/v3-subgraph#74)
Params#
- pool: Contract
pool contract instance to find TVL
- approve(token: Union[Address, ChecksumAddress], max_approval: Optional[int] = None) None #
Give an exchange/router max approval of a token.
- multicall(encoded_functions: Sequence[Tuple[ChecksumAddress, bytes]], output_types: Sequence[str]) List[Any] #
Calls aggregate() on Uniswap Multicall2 contract
Params#
- encoded_functionsSequence[Tuple[ChecksumAddress, bytes]]
array of tuples containing address of contract and byte-encoded transaction data
- output_types: Sequence[str]
array of solidity output types for decoding (e.g. uint256, bool, etc.)
returns decoded results
- get_token(address: Union[Address, ChecksumAddress], abi_name: str = 'erc20') 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_pool_instance(token_0: Union[Address, ChecksumAddress], token_1: Union[Address, ChecksumAddress], fee: int = 3000) Contract #
Returns an instance of a pool contract for a given token pair and fee. Requires pair [token_in, token_out, fee] has a direct pool. Will return 0x0 address if pool does not exist.
Supports Uniswap v3
- create_pool_instance(token_0: Union[Address, ChecksumAddress], token_1: Union[Address, ChecksumAddress], fee: int = 3000) Contract #
Creates and returns UniswapV3 Pool instance. Requires that fee is valid and no similar pool already exists.
Supports Uniswap v3
- get_pool_immutables(pool: Contract) Dict #
Fetch on-chain pool data.
Supports Uniswap v3
- get_pool_state(pool: Contract) Dict #
Fetch on-chain pool state.
Supports Uniswap v3
- get_liquidity_positions() List[int] #
Enumerates liquidity position tokens owned by address. Returns array of token IDs.
Supports Uniswap v3
- mint_position(pool: Contract, amount0: int, amount1: int) HexBytes #
Supports Uniswap v3
- get_raw_price(token_in: Union[Address, ChecksumAddress], token_out: Union[Address, ChecksumAddress], fee: Optional[int] = None) float #
Returns current price for pair of tokens [token_in, token_out] regrading liquidity that is being locked in the pool Parameter fee is required for V3 only, can be omitted for V2 Requires pair [token_in, token_out] having direct pool
Supports Uniswap v2, v3
- estimate_price_impact(token_in: Union[Address, ChecksumAddress], token_out: Union[Address, ChecksumAddress], amount_in: int, fee: int, 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.