Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

How to access Uniswap on Polygon network? #246

Unanswered
roya12 asked this question in Q&A
Mar 28, 2022 · 2 comments · 4 replies
Discussion options

Hello, newbie here. I'm trying to use this package to access Uniswap on Polygon network. I changed the provider to a free matic RPC (provided by Maticvigil) and provided the relevant token addresses on the Matic network, but the script returns an error. How do I access the swaps information on Uniswap's Polygon chain (Or other chains like BNB / Fantom / Xdai / etc.)? Thanks!

from uniswap import Uniswap

address = ""          # or None if you're not going to make transactions
private_key = ""  # or None if you're not going to make transactions
version = 3                       # specify which version of Uniswap to use
provider = "https://rpc-mainnet.maticvigil.com/v1/<maticvigil-key>"    # can also be set through the environment variable `PROVIDER`

uniswap = Uniswap(address=address, private_key=private_key, version=version, provider=provider)

# MATIC Some token addresses we'll be using later in this guide
matic = "0x0000000000000000000000000000000000001010"
dai = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"

print (uniswap.get_price_input(matic, dai, 10**18))
You must be logged in to vote

Replies: 2 comments · 4 replies

Comment options

Please, post the error message. From what I see, this snippet should work correctly.

You must be logged in to vote
1 reply
@roya12
Comment options

Hello, I have attached the error message below (I'm running on Windows with Python 3.9.10). Thank you.

D:\py-projects\uniswap>py uniswap-2.py
No fee set, assuming 0.3%
Traceback (most recent call last):
  File "D:\py-projects\uniswap\uniswap-2.py", line 14, in <module>
    uniswap.get_price_input(matic, dai, 10**18)
  File "C:\Python39\lib\site-packages\uniswap\uniswap.py", line 198, in get_price_input
    return self._get_token_token_input_price(token0, token1, qty, fee, route)
  File "C:\Python39\lib\site-packages\uniswap\uniswap.py", line 296, in _get_token_token_input_price
    price = self.quoter.functions.quoteExactInputSingle(
  File "C:\Python39\lib\site-packages\web3\contract.py", line 957, in call
    return call_contract_function(
  File "C:\Python39\lib\site-packages\web3\contract.py", line 1501, in call_contract_function
    return_data = web3.eth.call(
  File "C:\Python39\lib\site-packages\web3\module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str,
  File "C:\Python39\lib\site-packages\web3\manager.py", line 198, in request_blocking
    return self.formatted_response(response,
  File "C:\Python39\lib\site-packages\web3\manager.py", line 170, in formatted_response
    apply_error_formatters(error_formatters, response)
  File "C:\Python39\lib\site-packages\web3\manager.py", line 70, in apply_error_formatters
    formatted_resp = pipe(response, error_formatters)
  File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
    return c_pipe(data, funcs)
  File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
    data = func(data)
  File "C:\Python39\lib\site-packages\web3\_utils\method_formatters.py", line 580, in raise_solidity_error_on_revert
    raise ContractLogicError('execution reverted')
web3.exceptions.ContractLogicError: execution reverted
Comment options

I'm not sure if they have a 0.3% pool there, try fee=500 in constructor params like
uniswap = Uniswap(address=address, private_key=private_key, version=version, provider=provider, fee=500)

You must be logged in to vote
3 replies
@roya12
Comment options

Tried, however, it seems that 'fee' is not part of the params in the Uniswap class

Traceback (most recent call last):
  File "d:\py-projects\uniswap\uniswap-2-matic.py", line 9, in <module>
    uniswap = Uniswap(address=address, private_key=private_key, version=version, provider=provider, fee=500) 
TypeError: __init__() got an unexpected keyword argument 'fee'

classuniswap.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, use_estimate_gas: bool = True, factory_contract_addr: Optional[str] = None, router_contract_addr: Optional[str] = None)

@ErikBjare
Comment options

I think @liquid-8 was a bit too quick there.

The fee argument is on the get_price_input/output methods: http://uniswap-python.com/api.html#uniswap.Uniswap.get_price_input

If you configure logging correctly, it should emit a warning that fee was implicitly set to 3000 (0.3%).

@roya12
Comment options

Sure, I have tried changing to the following:

print (uniswap.get_price_input(matic, dai, 10**18, fee=500))

However, the script indicates an error in that line:

Traceback (most recent call last):
  File "d:\py-projects\uniswap\uniswap-2-matic.py", line 14, in <module>
    print (uniswap.get_price_input(matic, dai, 10**18, fee=500))
  File "C:\Python39\lib\site-packages\uniswap\uniswap.py", line 198, in get_price_input
    return self._get_token_token_input_price(token0, token1, qty, fee, route)
  File "C:\Python39\lib\site-packages\uniswap\uniswap.py", line 296, in _get_token_token_input_price
    price = self.quoter.functions.quoteExactInputSingle(
  File "C:\Python39\lib\site-packages\web3\contract.py", line 957, in call
    return call_contract_function(
  File "C:\Python39\lib\site-packages\web3\contract.py", line 1501, in call_contract_function
    return_data = web3.eth.call(
  File "C:\Python39\lib\site-packages\web3\module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str,
  File "C:\Python39\lib\site-packages\web3\manager.py", line 198, in request_blocking
    return self.formatted_response(response,
  File "C:\Python39\lib\site-packages\web3\manager.py", line 170, in formatted_response
    apply_error_formatters(error_formatters, response)
  File "C:\Python39\lib\site-packages\web3\manager.py", line 70, in apply_error_formatters
    formatted_resp = pipe(response, error_formatters)
  File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
    return c_pipe(data, funcs)
  File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
    data = func(data)
  File "C:\Python39\lib\site-packages\web3\_utils\method_formatters.py", line 580, in raise_solidity_error_on_revert
    raise ContractLogicError('execution reverted')
web3.exceptions.ContractLogicError: execution reverted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.