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);
}
}