Blockchain

在 WMATIC 對上添加流動性時 Quickswap 路由器功能錯誤

  • July 30, 2021

我一直在使用 web3.py 庫嘗試路由器的功能。但是,當使用 WMATIC/Token 流動性對呼叫函式並使用 USDC 作為貨幣購買代幣時,我遇到了麻煩。當使用完全相同的功能將流動性直接添加為 USDC/代幣對時,不會出現問題。

我已經定義了 qs_contract 和 usdc 合約如下:

# 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})

如前所述,此函式在 USDC/Token 對上執行良好,但在 WMATIC/Token 對上失敗,並出現以下錯誤:

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

通過將$WMATIC地址添加到路徑來解決!

# 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})

引用自:https://ethereum.stackexchange.com/questions/104180