I want to get an estimation of how much it would cost to store arbitrarily many elements in a mapping of the form BYTES32 -> BYTES32. I see here that a STORAGEADD operation costs 20000 gas, which I believe is the instruction to add an element to a mapping (is it correct?).

The gas price today is 20 gwei, so 20 * 10^9 wei which gives me a total of 20000 * 20 * 10^9 * 10^-18 = 0.0004 ether ~ 0.0048 USD (with 1 ETH = 12 USD).

Is it correct and is there any limit to how many elements I can put into the mapping (disregarding gas price limitations)?

The total storage is made of 32-bytes slots addressed with 256 bits. This gives us 2^256 * 32 bytes to use. When you add an item to a mapping, it is sent to a random location in the storage calculated by sha3, see this answer.

Adding an item to a mapping will never fail because there always will be a sha3-calculated location to put the info into. Of course, the closer you get to 2^256 insertions, the higher the likelihood that you will eventually overwrite something else.

See this response to a similar question. In short: you can store any number of elements in a mapping.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.