Effectively storing arrays in Solidity.

I’ve been doing some work with Ethereum blockchain recently, and the idea I worked on requires that we write a relatively large array of integer values in smart contract’s storage. Now, most tutorials warn you not to store a lot of data on blockchain, because it’s very costly. But how much is “a lot”, and at which point the price becomes too high for practical purposes? I needed to find that out, since we really needed this data on-chain.

For a quick approximation, let’s have a look at a wide-spread type of contract, ERC20 token. At the very least, it stores a mapping of addresses to token holders’ balances. The address is a 20-byte value, and the balance is usually 32 bytes, so we have at least 52 bytes per token holder, which in reality translate to 64 bytes, since the storage in EVM is parceled in 32-byte blocks. A somewhat popular token can easily have 10000 or more holders, so our estimate that it stores about 625Kb of information. That’s quite a lot, actually – more than enough for our purposes, since by our estimates, we only needed to store maybe tens of kilobytes!

Naive approach

So, let’s go ahead and pass that data to EVM and write it down to the storage:
Continue reading