0

I look for a way to iterate struct values of Array that is returned from calling method of solidity file on eth-brownie python file.
As I do some research on several topics asked on https://ethereum.stackexchange.com/, I just found how to access each element in the array by specifying the index. on solidity file(*.sol)

       contract SimpleStorage {
    struct People {
        uint256 favoriteNumber;
        string name;
    }
    People[] public people;
}

on brownie python file(*.py)

def main():
 xcontract=SimpleStorage[-1]
 for item in  xcontract.people():
    print(item[0]+' , '+item[1])

This is an error. ValueError: people Sequence has incorrect length, expected 1 but got 0 It seems like it can not get the length of the array.

How to resolve this issue?

Running 'scripts\invoke.py::main'...
  File "d:\programdata\python\python39\lib\site-packages\brownie\_cli\run.py", line 51, in main
    return_value, frame = run(
  File "d:\programdata\python\python39\lib\site-packages\brownie\project\scripts.py", line 103, in run
    return_value = f_locals[method_name](*args, **kwargs)
  File ".\scripts\invoke.py", line 38, in main
    for item in  xcontract.people():
  File "d:\programdata\python\python39\lib\site-packages\brownie\network\multicall.py", line 115, in _proxy_call
    result = ContractCall.__call__(*args, **kwargs)  # type: ignore
  File "d:\programdata\python\python39\lib\site-packages\brownie\network\contract.py", line 1751, in __call__
    return self.call(*args, block_identifier=block_identifier, override=override)
  File "d:\programdata\python\python39\lib\site-packages\brownie\network\contract.py", line 1537, in call
    tx.update({"to": self._address, "data": self.encode_input(*args)})
  File "d:\programdata\python\python39\lib\site-packages\brownie\network\contract.py", line 1628, in encode_input
    data = format_input(self.abi, args)
  File "d:\programdata\python\python39\lib\site-packages\brownie\convert\normalize.py", line 20, in format_input
    raise type(e)(f"{abi['name']} {e}") from None
ValueError: people Sequence has incorrect length, expected 1 but got 0

Instead, if I fix the index position, it works well. enter image description here

Ponthorn

1 Answer 1

0

take a look at the doc to see how are the getters for arrays generated

If you have a public state variable of array type, then you can only retrieve single elements of the array via the generated getter function. This mechanism exists to avoid high gas costs when returning an entire array. You can use arguments to specify which individual element to return, for example data(0). If you want to return an entire array in one call, then you need to write a function, for example:

as you can see the getter function doesn't return the entire collection and that is the reason why you can't iterate through the array

the automatically generated getter looks like this

enter image description here

it is waiting for a parameter (the index)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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