2

I have a view function I'm trying to call on the kovan network with the following code. (You'll need an RPC_URL for the kovan network.)

The function is the Chainlink historical price view function. I should be able to pass it a roundId and get back the price data from the round. I am able to do this in solidity, but for some reason I'm having a hard time in 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()

This is not a payable function, so I should not have to use send() as deemed by this similar question.

Error: Node error: {"code":-32015,"data":"Reverted 0x","message":"VM execution error."}

I end up getting this error:

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.'}

Any thoughts?

2

There's nothing wrong with the code you've written, the problem lies in the contract you're calling with those params.

You're getting round data from a proxy contract, which has a different round id than the aggregators themselves (they are phased in order to not cause issues when switching between different aggregator contracts, which could give a lower round id than previously returned).

To solve your issue, try using a different round id. The current round id for this contract is 18446744073709556747, so getting data from this round will work:

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

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.