0

I am writing tests with Foundry and trying to populate a dynamic array.

When I try:

        IAsset[] memory assets;
        uint256[] memory maxAmountsIn;
        assets.push(OmniUSD);

I get an error saying

Member "push" is not available in contract IAsset[] memory outside of storage.

When try

assets[0] = IAsset(OmniUSD)

I get an invalid opcode.

I would appreciate some clarity on what i am doing wrong

The Renaissance
  • 2,749
  • 1
  • 17
  • 45
0xsegfault
  • 1,260
  • 9
  • 24
  • You can't change the length of a memory array. See https://ethereum.stackexchange.com/questions/46761/how-to-fill-dynamic-in-memory-array – 0xSanson Sep 29 '22 at 16:00

1 Answers1

1

According to the Solidity docs, as opposed to storage arrays, it is not possible to resize memory arrays (e.g. the .push member functions are not available). You either have to calculate the required size in advance or create a new memory array and copy every element.

See: https://docs.soliditylang.org/en/v0.8.12/types.html#allocating-memory-arrays

Yongjian P.
  • 4,170
  • 1
  • 3
  • 10