2
contract Published {

    struct Test {
        uint testData_1;
        uint testData_2;
    }
    
    // address => tokenId => struct
    mapping(address => mapping(uint256 => Test)) public tests;
}

The new contract reads from the already published one.

// NEW CONTRACT THAT READS FROM THE PUBLISHED

// Add the getter function to the contract Interface

interface IPublished {
    XXXXX
}

contract New {
    IPublished publishedContract;

    constructor(
        address _publishedContract
    ) {
        publishedContract = IPublished(_publishedContract);
    }

    function getFromMapping(
        address caller,
        uint256 tokenId
    ) external returns (IPublished.Test memory test) {

        return publishedContract.tests(caller, tokenId); 

    }
}
0

1 Answer 1

2

Like this works for me :

return publishedContract.tests(caller, tokenId);

Basically, for a mapping the getter will accept one parameter, if the value type of the mapping is also a mapping then the getter accepts a second parameter and so on.

You can read more from the documentation (0.8.10).

4
  • Thanks, I was not finding the documentation around this topic.
    – Gordo
    Nov 30, 2021 at 16:30
  • How to do set up the interface of the New in order to access that getter?
    – Gordo
    Nov 30, 2021 at 18:28
  • What do you mean exactly ? You don't have the interface ? Just knowing the name of the public mapping is enough though
    – hroussille
    Nov 30, 2021 at 22:34
  • But you cannot call it from another contract without its interface type
    – Gordo
    Dec 1, 2021 at 0:07

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.