October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Read a Mapping in Another Solidity Contract

Read another contract’s mapping by calling its public getter through a typed reference. In factory designs, choose the child contract and mapping key separately.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To read a mapping owned by another deployed Solidity contract, call its public getter through a typed contract reference and pass the mapping key. You do not index the other contract’s storage directly. If a factory tracks multiple child contracts, first select the child, then pass that child’s mapping key.

Call the getter through a contract reference

Solidity generates a getter function for a state variable declared public. A different contract can call that function using the deployed contract’s address and a contract type or interface that declares the getter. For example, if the target declares mapping(address => uint256) public balances, a caller with a typed reference named a reads an entry with a.balances(account).

The expression is a function call on the target contract, not direct access to its mapping storage. Solidity documents the generated getter behavior in its Contracts documentation.

Declare only the interface you need

If the caller does not import the full implementation, it can use an interface containing the getter signature:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
interface IBalanceBook {
    function balances(address account) external view returns (uint256);
}

contract Reader {
    function readBalance(address book, address account)
        external
        view
        returns (uint256)
    {
        return IBalanceBook(book).balances(account);
    }
}

Here, book must be the address of a deployed contract whose callable getter matches the interface. The interface describes how to call it; it does not create access to the target’s storage or guarantee that an arbitrary address implements the expected behavior.

Choose the correct contract and mapping key

When a factory creates and tracks multiple child contracts, a read can involve two independent lookups: select the child instance from the factory, then select an entry in that child’s mapping. A practical example uses this getter:

function SfGet(uint256 _SimpleStorageDataID, uint256 _ID)
    public view returns (string memory, address)
{
    return ListOfSimpleStorageContracts[_SimpleStorageDataID]
        .DataIdToData(_ID);
}

_SimpleStorageDataID selects a deployed SimpleStorage instance in the factory’s list. _ID is passed to that instance’s public DataIdToData mapping getter and selects an entry within that child. Although both parameters are uint256, they mean different things. Using a valid entry ID with the wrong child can return a plausible but unintended result. The example and its factory context appear in Reading a Mapping That Lives on a Different Contract.

Return every field later logic needs

The example returns both a string and an owner address. If downstream logic uses ownership to decide whether an action is allowed, the getter must preserve and return the address as well as the data. A mapping read should not silently drop a field that later authorization depends on.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reading mappings whose values are structs

A generated getter for a public mapping whose values are structs returns the accessible members as function outputs. The caller should declare and decode the getter’s actual output types and ordering; it does not receive a storage reference to a struct held by another contract. See the discussion of calling a mapping of structs from another contract.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What a mapping getter does—and does not—allow

It reads; it does not authorize writes

A public mapping exposes a getter, not a general-purpose way to change the mapping. To modify the target contract’s state, that contract must implement a state-changing function, and the function must enforce the authorization rules the application requires. A caller cannot gain write access merely by declaring an interface or calling a getter. The distinction is also discussed in this OpenZeppelin Forum example.

It cannot enumerate keys by itself

Mappings have no built-in length and do not expose their keys for iteration. If an application needs to list entries, the contract must maintain a separate key list or another indexing scheme. The Solidity Types documentation describes mapping behavior and its constraints.

A default value does not prove an entry was written

Mappings behave as though every possible key has a value initialized to the default for the mapped value type. A getter returning a default value therefore does not establish that the application explicitly stored that value for the key. If the distinction matters, track existence separately—for example, with an additional mapping or an indexed record.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right read design

Need Design
The mapping key is known and the mapping is public Call the generated getter through a reference or interface matching its signature.
The mapping is not public or the caller needs a tailored result Implement an explicit view function on the target contract and expose the required output.
The caller needs to discover or list keys Maintain a separate key list or other index; a mapping alone cannot enumerate entries.
The caller must change target state Implement a state-changing function on the target and apply the intended access control there.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.