0

I'm going to be using a smart contract to add data to an array, so I'll be using array.push (fixed gas). The data added is an address and the purpose of the array is just to keep a list of all addresses (an index). The array will never be accessed from within the smart contract and will only ever be retrieved using the call method (zero cost).

I want to use a dynamic array as I don't know how large the index will grow but I'm worried I could run into using an unchecked array size. Would it be better to have multiple indexes instead, say maxed at 50k?

1 Answer 1

1

If what you wrote is the only way that the array is being used, then there is nothing wrong with your implementation. Arrays can be very large. There is, of course, a maximum, but I doubt that you'll reach it.
If you plan on traversing your array within a smart contract, that is a different story, since you will likely run out of gas once you get to a high enough size.

You could also use a mapping from a uint to an address & keep a separate mapping length uint. This costs essentially the same amount of gas.

    uint mapLen;
    mapping(uint => address) public map;

    function addToMap(address a) external {
        map[mapLen] = a;
        mapLen++;
    }
2
  • Would the idea behind this be that it is easy to access? I could make a helper view function to get any value by "index" from that map thus making retrieval easier vs trying to traverse the array using getStorage? Feb 19, 2022 at 13:31
  • Getting from an array and getting from a map should be essentially the same complexity. I only included the map example as an option because many implementations prefer maps over arrays. How you choose to implement it is up to you. Feb 19, 2022 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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