Here’s a simple working Solidity contract demonstrating your mapping example with NetworkConfig
#4738
mirmohmmadluqman
started this conversation in
Ideas
Replies: 3 comments 2 replies
-
|
Excellent, Thanks for this detailed explanation. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Cool, but I don’t get how I could actually use it — not how the mappings work, but like, in what situation I’d even use this NetworkManager contract. (Sorry if that’s a basic question) |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
Interesting |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Here’s a simple working Solidity contract demonstrating your
mappingexample withNetworkConfig:1 How it works
networkConfigstores configs per chain ID.localNetworkConfigstores config for your local/test network.setNetworkConfig()→ saves data into mapping.getEntranceFee()→ reads data from mapping.public mappingautomatically creates a getter too, e.g.:networkConfig[31337].entranceFeeBrief Explaination:
let’s break mapping in Solidity clearly
2 What a mapping is
mappingis like a key-value store (like a dictionary in Python).uint256(maybe a chain ID like 1 for Ethereum mainnet, 31337 for local).NetworkConfigstruct.2 How data is stored
Solidity stores mapping in a hash table in storage, not in a sequential array.
You cannot iterate over mappings directly — you only access values by key.
Example storing a value:
NetworkConfigfor chain ID 31337.4 How to call/use it
public, Solidity auto-creates a getter:4️⃣ Accessing
localNetworkConfiglocalNetworkConfig.entranceFee; // gets the fee for local networkTL;DR
networkConfig[chainId] = ...networkConfig[chainId].fieldBeta Was this translation helpful? Give feedback.
All reactions