12

I want to copy a struct from Contract A into a struct in Contract B using Contract C. Is it possible in solidity ??

We can do it for single variables.

3 Answers 3

15

You can unpack a struct and return multiple return variables from a call:

contract Foo {

    /**
    * Define pricing schedule using milestones.
    */
    struct Milestone {

        // UNIX timestamp when this milestone kicks in
        uint time;

        // How many tokens per satoshi you will get after this milestone has been passed
        uint price;
    }

    Milestone[] milestones;

    function getMilestone(uint n) public constant returns (uint, uint ) {
        return (milestones[n].time, milestones[n].price);
    }    
}

For the underlying issue, as far as I know struct data storage is internal to a contract and not portable between contracts.

1
  • 3
    Worth mentioning that due to the (current) stack size limitation you cannot pass back arbitrarily big structs in this way.
    – SCBuergel
    Commented Dec 31, 2016 at 20:50
10

Mikko is correct about struct data not being portable between contracts.

A struct is an example of a dynamically sized type that can only be passed around internally. I've seen some discussion among the devs about possibly removing this limitation. For now, the interface can only consist only of arguments with fixed size.

1

As long as the struct is public you can call it from another contract. You just import the original contract and then define its address and make calls to it to fetch struct data.

import "./example1.sol";

contract example2 is example1 {
    address public example1Address = 0xc3c695f67520a07b7745a7e6fb7b560f77d6154b;

    function setOriginContract(address _example1Address) public onlyOwner returns(bool) {
    example1Address = _example1Address;
    return true;
}

//to use the remote contract

function exampleFunction()public returns(uint){
    example1Contract example1 = example1Contract[example1Address];
    return example1.Example1Function();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.