2

How call this contract functions from python code?

pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;

contract MY_PERSONS {

    struct Person{
        string name;
        uint age;
    }

    Person[] private persons;

    constructor ( Person[] _persons ) public {
    // persons = _persons; // not yet supported
    for(uint i=0; i<_persons.length; i++)
        persons.push(_persons[i]);
    }

    function add_person(Person _person) public  {
        persons.push(_person);
    }

    function get_person(uint index) public view returns(Person) {
        require(index >=0 && index < persons.length);
        return persons[index];
    }

}

I tested Contract Deployment Example and Passing Struct as an argument in call and everything was OK, but I don't know how call the functions.

1 Answer 1

1

Following is a working code on remix. It is recommended to use memory keyword for the struct parameter.

pragma solidity ^0.5.0;  
pragma experimental ABIEncoderV2;

    contract HelloWorld {

        struct User {
            string name;
            uint age;
        }

        User[] users;


        function addUser(User memory user_) public  {
            users.push(user_);
        }

        function getUser(uint index) public view returns(User memory) {
             require(index >=0 && index < users.length);
           return users[index];
        }
    }

user = ["hello", 10];
HelloWorld.transact().addUser(user);
4
  • I use user = ["hello", 10]; also user = ("hello", 10); but this error occurre: ValueError: No matching entries for 'tuple' in encoder registry
    – H.Mohseni
    Commented Apr 27, 2019 at 17:25
  • Did you use on remix?
    – alper
    Commented Apr 27, 2019 at 17:57
  • No, I use web3py and py-solc to compile solidity code like this
    – H.Mohseni
    Commented Apr 28, 2019 at 4:12
  • You might try with the web3 v5 beta. tuples might not be supported in v4. @H.Mohseni
    – alper
    Commented Aug 10, 2019 at 20:30

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.