0

I have contract Users, that contains users list and nftInstance:

contract Users {
    struct UserData{
        User user;
        bool exists;
    }

    mapping (address => UserData) public users;
    NFT public nftInstance;

    constructor() {
        nftInstance = new NFT();
    }

    event UserConnect(User user);

    function connectUser() public  {
        address userAddress = msg.sender;

        if(users[userAddress].exists) emit UserConnect(users[userAddress].user);
        else {
            User user = new User(userAddress, nftInstance);
            users[userAddress].user = user;
            users[userAddress].exists = true;
            emit UserConnect(user);
        }
    }

    function getUser(address _userAddress) public view returns (User){
        return users[_userAddress].user;
    }
}

And i have User contract. In constructor i get nftInstance to be able to create NFT in User . And store NFTs in one storage. This is my User contract:

contract User {
    address public userAddress;
    NFT private nftInstance;

    constructor (address _userAddress, NFT _nftInstance) {
        userAddress = _userAddress;
        nftInstance = _nftInstance;
    }

    event Collect(string tokenURI);

    function collect() public{
        uint256 tokenId = nftInstance.createNFT(userAddress);
        emit Collect(nftInstance.tokenURI(tokenId));
    }

    function getMyNFTs() public {
        mapping(address => uint256) storage tokenIdsCount = nftInstance.userAddressToTokenId; // 1 VARIANT
        uint256[] memory tokenIds = nftInstance.userAddressToTokenId[userAddress]; // 2 VARIANT
    }
}

In NFT contract i have such mapping: mapping(address => uint256[]) public userAddressToTokenId; And i try to get this mapping data in User. But this is wrong

1 - TypeError: Type function (address,uint256) view external returns (uint256) is not implicitly convertible to expected type mapping(address => uint256).

2 - ypeError: Indexed expression has to be a type, mapping or array (is function (address,uint256) view external returns (uint256))

So, how to get tokenIds array from mapping of NFT contract instance?

4
  • I'm amazed that you dared to post this code here ;D
    – Kaneda
    Feb 7, 2022 at 3:03
  • It's okay, just some protest;)
    – Влад
    Feb 7, 2022 at 11:56
  • @Kaneda so, how to interact with mapping from another contract?
    – Влад
    Feb 7, 2022 at 11:58
  • Can you include NFT how tokenIds are defined in the NFT contract? In any case a contract can't access the storage area of another contract directly the only way to do so is for the second contract to have public getter functions.
    – Ismael
    Feb 9, 2022 at 3:25

1 Answer 1

0

I wrote a getter in the NFT contract to get mapping

function getTokenIdsFromAddress(address _userAddress) public view 
 returns(uint256[] memory) {
    return userAddressToTokenId[_userAddress];
}

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.