0

Basically, I have my smart contract create a structure array, and would like to be able to read the whole array with python in order to access its contents and perform some tasks with it. The struct and array look something like this.

struct object{
    string name;
    int256 number;
}

object[] public myarray.

What I would like to do is create a python array which mirrors the array created in this way (assume that in the array the contract stores an arbitrary number of object N) Thank you very much!

1
  • Add a get function in solidity and call it? I’m not sure if this the best way, you can also add events to ur contract when something is added or removed and then subscribe for events
    – Majd TL
    Nov 1, 2021 at 19:15

1 Answer 1

0

You basically need to access the array using Web3 library. let's say the array is inside a contract called "contract" which is a already initialized in python.

First, Add to the contract a function that provides myarray's length called "get_myarray_length"

function get_myarray_length() public view returns(uint){
    return myarray.length;
}

Than using python:

tuppled_object_array = []
for i in range(contract.functions.get_myarray_length.call()):
    tuppled_object = contract.functions.myarray(i).call() # (name, number)
    tuppled_object_array += [tuppled_object]

Your Answer

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

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