Solidity

視圖函式呼叫錯誤 {‘code’: -32015, ‘data’: ‘Reverted 0x’, ‘message’: ‘VM execution error.’}

  • September 14, 2020

我有一個視圖函式,我試圖使用以下程式碼在 kovan 網路上呼叫。(你需要一個用於 kovan 網路的 RPC_URL。)

該功能是Chainlink歷史價格查看功能。我應該能夠通過它roundId並從這一輪中取回價格數據。我能夠做到這一點,但由於某種原因,我在 web3.py 中遇到了困難

import os
from web3 import Web3

web3 = Web3(Web3.HTTPProvider(os.getenv('RPC_URL')))
address = '0x396c5E36DD0a0F5a5D33dae44368D4193f69a1F0'
abi = '[{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"description","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint80","name":"_roundId","type":"uint80"}],"name":"getRoundData","outputs":[{"internalType":"uint80","name":"roundId","type":"uint80"},{"internalType":"int256","name":"answer","type":"int256"},{"internalType":"uint256","name":"startedAt","type":"uint256"},{"internalType":"uint256","name":"updatedAt","type":"uint256"},{"internalType":"uint80","name":"answeredInRound","type":"uint80"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"latestRoundData","outputs":[{"internalType":"uint80","name":"roundId","type":"uint80"},{"internalType":"int256","name":"answer","type":"int256"},{"internalType":"uint256","name":"startedAt","type":"uint256"},{"internalType":"uint256","name":"updatedAt","type":"uint256"},{"internalType":"uint80","name":"answeredInRound","type":"uint80"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"version","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]'
price_feed_contract = web3.eth.contract(address=address, abi=abi)
price_feed_contract.functions.getRoundData(1).call()

不是一個應付函式,所以我不應該send()像這個類似問題所認為的那樣使用。

錯誤:節點錯誤:{“code”:-32015,“data”:“Reverted 0x”,“message”:“VM 執行錯誤。”}

我最終得到這個錯誤:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web3/contract.py", line 964, in call
   **self.kwargs
 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web3/contract.py", line 1499, in call_contract_function
   return_data = web3.eth.call(call_transaction, block_identifier=block_id)
 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_utils/functional.py", line 45, in inner
   return callback(fn(*args, **kwargs))
 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web3/eth.py", line 434, in call
   [transaction, block_identifier],
 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web3/manager.py", line 153, in request_blocking
   raise ValueError(response["error"])
ValueError: {'code': -32015, 'data': 'Reverted 0x', 'message': 'VM execution error.'}

有什麼想法嗎?

您編寫的程式碼沒有任何問題,問題在於您使用這些參數呼叫的契約。

您從代理合約中獲取輪次數據,該輪次 id 與聚合器本身不同(它們是分階段的,以便在不同的聚合器合約之間切換時不會引起問題,這可能會提供比以前返回的輪次 ID 更低的輪次 id)。

要解決您的問題,請嘗試使用不同的回合 ID。此合約的目前輪 id 是18446744073709556747,因此從這一輪獲取數據將起作用:

>>> price_feed_contract.functions.getRoundData(18446744073709556747).call()
[18446744073709556747, 1209967561, 1600117216, 1600117216, 18446744073709556747]

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