0

Is it possible to get a variable state from existing contract which was not verified but I know what variables, functions etc. the contract contains?

Assume, we have the following script (using Brownie, the name of variable is variable_to_obtain):

from brownie import Contract
from scripts.helpers import get_account


def get_variable_of_instance(instance_address):
    account = get_account()
    instance_contract = Contract.from_explorer(instance_address, {"from": account})
    variable_to_return = instance_contract.variable_to_obtain()
    return variable_to_return


def main():
    var = get_variable_of_instance("address_of_instance")
    print(var)

Any idea? Thank you so much.

1 Answer 1

3

The way how to do this depends on the contract that you want to extract the variable from.

If the contract normally provides a function to query this variable you just need to know the correct function id to query it.

If this is not the case then you can use eth_getStorageAt to query the storage at a specific slot for that contract, but for this you will require some knowledge about the internals of that contract.

2
  • Thank you very much @Richard for providing me with hints. Could you please guide me how to query to the function of the smart contract (if I know its id)?
    – haraslub
    Jan 21, 2022 at 9:34
  • Something like ethereum.stackexchange.com/questions/91510/… should be the way to go
    – Richard
    Jan 21, 2022 at 20:33

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.