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?
Best Answer
I wrote a getter in the
NFT
contract to get mapping