Search code examples
mappingethereumsoliditymodifier

Mapping Not being updated in contract while sending data from another contract


// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract t1{
    mapping(address => uint256[]) AllSpecialNFT;
    function addNewVal( uint _tokenId) public {
        AllSpecialNFT[msg.sender].push(_tokenId);
    }
    function findSize() public view returns(uint){
        return AllSpecialNFT[msg.sender].length;
    }
    
}
pragma solidity >=0.4.22 <0.9.0;
import './t1.sol';
contract t2 {
    t1 _t1;
    constructor(t1 t1_){
        _t1 = t1_;
    }
    
    function callandAdd(uint _tokenId) public{
        _t1.addNewVal(_tokenId);
    }
    
    
}

This code runs successfully, and is able to add the data in the mapping. But, this does not change the size in the T1 contract. Is there any way i can update add new elements in the mapping and update the size of the contract?

I was expecting that the size of array in mapping to be increased after calling the function callandVal().


Solution

  • So, i was able to update the mapping from another contract by simply adding a new parameter in the function.

    // SPDX-License-Identifier: MIT
    pragma solidity >=0.4.22 <0.9.0;
    contract t1{
        mapping(address => uint256[]) AllSpecialNFT;
        function addNewVal(address _off, uint _tokenId) public {
            AllSpecialNFT[_off].push(_tokenId);
        }
        function findSize(address _off) public view returns(uint){
            return AllSpecialNFT[_off].length;
        }
        
    }
    pragma solidity >=0.4.22 <0.9.0;
    import './t1.sol';
    contract t2 {
        t1 _t1;
        constructor(t1 t1_){
            _t1 = t1_;
        }
        
        function callandAdd(uint _tokenId) public{
            _t1.addNewVal(msg.sender,_tokenId);
        }
        
        
    }