How to pass struct from contract A to contract B? Best practice

Viewed 3021

I found such way, when one general interface with structure is created and then contract A and B inherit the interface with structure.

But I'm wondering if there are other ways?

And could there be a case where a contract with a structure can be updated?

pragma experimental ABIEncoderV2;
pragma solidity ^0.6.0;
 
interface params {
     struct  structTest {
        uint256 data;
    }
}

contract contractA is params{
    function testCall(structTest calldata _structParams) public pure returns (uint256){
        return _structParams.data;
    }
}

contract contractB is params{
    contractA aContractInstance;
    
    constructor (address _a) public {
        aContractInstance = contractA(_a);
    }
    
    function test(structTest calldata _structParams) public view returns(uint256){
        // call contract A from B and pass structure
        return aContractInstance.testCall(_structParams);
    }
}
1 Answers
interface IContractA {

    struct User {
        address addr;
    }
    function getUser(aaddress addr) external view returns (User memory user);
}

contract contractB{

    function getUserFromContractA(address addr) public view
        returns (IContractA.User memory user)
    {
      ContractA = IContractA(addrContractA);
      user = ContractA.getUser(addr);
    }
}
Related