Amplifying @Travis's answer.
This line
Original original;
adds considerable size to SecondContract
because it contains Original
.
That's a separate issue that can be solved with an interface. SecondContract
doesn't require the complete bytecode for Original
. That is repetitive and unnecessary. It only needs the function signature and address of the instance to talk to.
There are some tricky inheritance and type-casting concerns to untangle so that SecondContract
can inherit the smallest possible representation of Original
's surface area (but not the working code) and both contracts can be in perfect sync about the layout of the Person
. I decided to take a stab at it for you. You can extrapolate from that to a more complex situation.
SecondContract
will be much smaller because the interfaces
it inherits will never grow very large.
pragma solidity 0.8.1;
interface Types {
struct Person {
uint age;
string name;
}
}
interface IOriginal is Types {
function persons(bytes32) external view returns(uint, string memory);
}
contract Original is IOriginal {
mapping(bytes32 => Person) public override persons;
}
contract SecondContract is Types {
IOriginal original;
constructor(address ofOriginalContract) {
original = IOriginal(ofOriginalContract);
}
function processData(bytes32 someBytes) public {
(uint age, string memory name) = original.persons(someBytes);
// do something with entry
}
}
It would be more satisfying if function persons()
returned a Person
instead of the members in the struct
but I didn't get it to work. Perhaps a kind soul will chime in with a solution for that.
Hope it helps.