0

I'm trying to understand inheritance, interfaces and the call of functions from other smart contracts in Solidity. So I did this example. It has two interfaces, one for struct type definition and other for functions definition. It also has two contracts for implementing logic of functions defined in the interface. Finally, there is a main contract that interacts with functions contracts via the interface.

My question is how to share data written and read through the contract, since when interacting with functions contracts on the main smart contract, the struct data type that is written in registerPerson function is not read on getPerson function. It returns me a blank struct data type.

I think maybe is something that's missing in the code, but I don't know what it is.

Thanks in advance.

StructInterface interface Smart Contract

interface StructInterface {

    struct Person {
        uint personId;
        string name;
        string surname;
        uint age;
    }
}

FunctionsInterface interface Smart Contract

interface FunctionsInterface {

    function addPerson (uint, string memory, string memory,uint ) external;

    function askPerson(uint) external view returns (uint, string memory, string memory, uint);
}

RegisterPerson Smart Contract

import "./StructInterface.sol";

contract RegisterPerson is StructInterface {

    mapping (uint => Person) persons;

    function addPerson (
        uint _personId,
        string memory _name,
        string memory _surname,
        uint _age
    ) external {

        persons[_personId].personId = _personId;
        persons[_personId].name = _name;
        persons[_personId].surname =_surname;
        persons[_personId].age = _age;
    }
}

GetPerson Smart Contract

import "./StructInterface.sol";

contract GetPerson is StructInterface {
    mapping (uint => Person) persons;

    function askPerson(uint _personId) external view
    returns (uint personId, string memory name, string memory surname, uint age) {

        personId = persons[_personId].personId;
        name = persons[_personId].name;
        surname = persons[_personId].surname;
        age = persons[_personId].age;

        return (personId, name, surname, age);
    }
}

Main Smart Contract

import "./StructInterface.sol";
import "./RegisterPerson.sol";
import "./GetPerson.sol";
import "./FunctionsInterface.sol";

contract Main is StructInterface{

    address regPersonA;
    address getPersonA;

    Person myPersons;

    constructor (address _regPersonA, address _getPersonA) {
        regPersonA = _regPersonA;
        getPersonA = _getPersonA;
    }

    function registerPerson(
        uint _personId,
        string memory _name,
        string memory _surname,
        uint _age
    ) external {

        FunctionsInterface interf = FunctionsInterface(regPersonA);
        interf.addPerson(_personId, _name, _surname, _age);
    }


    function getPerson(
        uint _personId
    ) external view
    returns (uint personId, string memory name, string memory surname, uint age) 
    {

        FunctionsInterface interf = FunctionsInterface(getPersonA);
        return interf.askPerson(_personId);
    }
}
Nisase
  • 1
  • 1
  • interface usage: https://stackoverflow.com/questions/71072475/does-a-contract-instance-created-via-interfaces-have-the-same-address-as-the-ori/71075753#71075753 – Yilmaz Feb 13 '22 at 13:01
  • with inheritance you can also reach public variables: https://stackoverflow.com/questions/70643149/call-a-function-in-another-contract-solidity/70643414#70643414 – Yilmaz Feb 13 '22 at 13:05
  • @Yilmaz thanks, I have posted a question on https://stackoverflow.com/questions/71072475/does-a-contract-instance-created-via-interfaces-have-the-same-address-as-the-ori/71104713#71104713 , if you can review it and answer me. – Nisase Feb 13 '22 at 20:51
  • On the left you see `CONTRACTA 0x1d1....` that is the address. at the end of that line u will see a a button to copy yhe address – Yilmaz Feb 13 '22 at 21:30
  • @Yilmaz, I have edited my question regarding info you gave me. But the struct written in one function is not able to be read on the other function, maybe you could help me, I think something is missing on code but I can't get it right. – Nisase Feb 16 '22 at 23:34

0 Answers0