I have many addresses that map to values in an external program. I want to feed this into a smart contract's state, but I'm wondering what is the most cost-effective way to do this.

Right now I have the simple solution:

 function storeValues(address[] addresses, uint[] values) public {
        for (uint i = 0; i < addresses.length; i++) {
            balances[addresses[i]] = values[i];

The problem is that balances is a state variable and for each iteration, it saves to this state, which is expensive. I tried to create a temporary mapping inside the function and save to that instead, but apparently, it wasn't possible to create a dynamic mapping like that


Ironically speaking, if you want to update the all values, there is no more cost effective that what you wrote.

However, you can have workarounds. Usually, if have large data saved at external program, you can serialize the data and then get the hash and save it into BlockChain (by updating a state variable inside a Smart Contract).

So, what you can do for example is for each transaction that contains couple of values' updates, you can put serialize all the operations (JSON for example). And then take the Hash and save only the Hash at your Smart Contract state variable. In this case, when you want verify the operations, you need to have the data saved at your external system. But you can verify that the data is correct by comparing the Hash. If you also need to have the whole data immutable and always available, you may consider using IPFS, StorJ and similar solutions. Where for example you can save your operations, serialized in JSON for example, into IPFS and save the Hash at some Smart Contract state variable.

Another workaround, if this is possible in your case, is to accumulate the operations happen at your external system. And once a day, or at specific event, you can save the final balances (slightly similar to how Raidian Network do).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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