Knowing the problems with the arrays in solidity and its high cost of gas, I thought of creating an array dynamically in a function that will look for the values in a mapping indexed through a counter. I consider that this option is cheaper than the direct storage in an array.
However, it gives me error. It doesn’t specify which one because it would compiles me perfectly. I am using a structure as type of the array and I don’t know if this is a problem, although I think it shouldn’t be.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Estructuras {
/// /// STRUCT ///
struct Estructura {
string cadena;
uint id;
address direccion;
}
Estructura datosUsuario;
mapping(uint => Estructura) public users;
mapping(address => bool) public permisos;
uint public contador;
function crearUsuario(uint _idNFT, string memory _nombre) public {
require(!permisos[msg.sender], "Usted ya esta registrado");
contador++;
Estructura memory newEstructura = Estructura(_nombre, _idNFT, msg.sender);
users[contador] = newEstructura;
permisos[msg.sender] = true;
}
function createArray() public view returns(Estructura[] memory){
Estructura[] memory array;
for(uint i = 0; i < contador; i++){
array[i] = users[i + 1];
}
return array;
}
}
Where is my mistake?