[Ethereum] How to you share a struct definition between contracts in separate files

contract-developmentcontract-invocationgo-ethereumsolidity

I can't declare a stand-alone struct in its own file, what gives?

Best Answer

You can with a library! Here's an example:

pragma solidity ^0.4.17;

library SharedStructs {
    struct Thing {
        address[] people;
    }    
}

contract A {
    SharedStructs.Thing thing;
}

contract B {
    SharedStructs.Thing thing;
}

Two important things to keep in mind: 1) the library gets deployed to the chain and then is referenced by its address, and 2) the library acts as a true pass-through, meaning msg.sender (and related values) refer to the original caller.

More info and details here: http://solidity.readthedocs.io/en/develop/contracts.html#libraries

Related Topic