0

i've been trying the functions from the router using the web3.py library. However am getting into troubles when calling the function with a WMATIC/Token liquidity pair and using USDC as currency to buy the token. The problem doesn't arise when the liquidity is added directly as a USDC/Token pair, using the exact same functions.

I've defined the qs_contract and the usdc contract as follows:

# public sender address
sender_address = ""

# This is the router plus abi definition
qs_router = "0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff"
with open("qs_router_abi.json") as qsJsonAbi:
    qs_abi = json.load(qsJsonAbi)
    qsJsonAbi.close()

qs_contract = w3.eth.contract(address=qs_router, abi=qs_abi)

# This is the USDC contract definition
usdc_contract_id = w3.toChecksumAddress("0x2791bca1f2de4661ed88a30c99a7a9449aa84174")

# Here goes the token created
token_to_buy = ""

# lets say budget to spend is $1
budget = 1000000
estimated_amounts = qs_contract.functions.getAmountsOut(
        budget, [usdc_contract_id, tokenToBuy]
    ).call({"from": sender_address})

As mentioned early, this functions goes well on USDC/Token pairs, but fails with WMATIC/Token pairs with the following error:

File "/home/basti/.local/lib/python3.8/site-packages/web3/contract.py", line 957, in call
    return call_contract_function(
  File "/home/basti/.local/lib/python3.8/site-packages/web3/contract.py", line 1501, in call_contract_function
    return_data = web3.eth.call(
  File "/home/basti/.local/lib/python3.8/site-packages/web3/module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str,
  File "/home/basti/.local/lib/python3.8/site-packages/web3/manager.py", line 181, in request_blocking
    return self.formatted_response(response,
  File "/home/basti/.local/lib/python3.8/site-packages/web3/manager.py", line 161, in formatted_response
    apply_error_formatters(error_formatters, response)
  File "/home/basti/.local/lib/python3.8/site-packages/web3/manager.py", line 61, in apply_error_formatters
    formatted_resp = pipe(response, error_formatters)
  File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
  File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
  File "/home/basti/.local/lib/python3.8/site-packages/web3/_utils/method_formatters.py", line 554, in raise_solidity_error_on_revert
    raise ContractLogicError('execution reverted')
web3.exceptions.ContractLogicError: execution reverted
0

Solved by adding the $WMATIC address to the path!

# lets say budget to spend is $1
budget = 1000000
estimated_amounts = qs_contract.functions.getAmountsOut(
        budget, [usdc_contract_id, wmatic_address, tokenToBuy]
    ).call({"from": sender_address})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.