0

I am developing a smart contract which I have two contracts(A and B). I have an array variable in A and fill it with values but when I get that variable from B, the array address is empty.

pragma solidity ^0.4.10;

contract Censo{

struct Persona{
    string name;
    uint age;
    uint weight;
    uint number;
    uint position;
    address[] propuestasVotadas;
}

address personaAutorizada;

mapping(address => Persona) public personas;
address[] public personasList;

function Censo(){
    personaAutorizada = msg.sender;
}

function existePersona(address _address) public constant returns(bool){

    if(personasList.length == 0) return false;
    return (personasList[personas[_address].position] == _address);
}

function addPersona(address _address, string _name, uint _age, uint _weight, uint _number) public{

    require(!existePersona(_address));

    personas[_address].name = _name;
    personas[_address].age = _age;
    personas[_address].weight = _weight;
    personas[_address].number = _number;
    personas[_address].position = personasList.push(_address) - 1;

}

function getNumPersonas() public returns(uint){

    return personasList.length;
}

}

contract MinisterioInterior{

struct Propuesta{
    string nombre;
    string descripcion;
    uint numVotos;
    uint position;
}

event PropuestaAdded(address _address, uint now);
event PersonaVotePropuesta(address _addressPropuesta, address _addressPersona, uint now);

mapping(address => Propuesta) propuestas;
address[] propuestasList;

address personaAutorizadaMinisterio;

function MinisterioInterior(){
    personaAutorizadaMinisterio = msg.sender;
}

function existePropuesta(address _address) returns(bool){

    if(propuestasList.length == 0) return false;
    return (propuestasList[propuestas[_address].position] == _address);
}

function crearPropuesta(address _address, string _nombre, string _descripcion) returns(bool){

    require(!existePersona(_address));
    propuestas[_address].nombre = _nombre;
    propuestas[_address].descripcion = _descripcion;
    propuestas[_address].numVotos = 0;
    propuestas[_address].position = propuestasList.push(_address) - 1;
    return true;
}

}

  • 1
    Is your array in A public? Could you share your source code? – Luiz Soares Dec 1 '17 at 10:52
  • Yes, A is public – aflorom Dec 1 '17 at 10:57
  • Moreover, I'm not sure how to acces to the functions of A from B. – aflorom Dec 1 '17 at 11:03
  • I am looking your code. But here is one tip: if(propuestasList.length == 0) return false; . you should avoid this. You should fail fast and hard. If the array length is zero, it should not be called because your are spending gas here. IMHO, you should use require(propuestasList.length>0) – Luiz Soares Dec 1 '17 at 11:09
  • That code line is necessary because I was testing without that code line an get an opcode error, but with that code line I fix the opcode error. Above all It is necessary because You has an empty array in the first iteration and You are not going to can enter an element in the array. – aflorom Dec 1 '17 at 11:16
0

Here is how you should declare and call a contract inside another contract.

Censo trustedCenso;  //it is a good practice to mark a variable as "trusted" if it was written by yourself, so it is trusted, secure.
trustedCenso = Censo(_censoAddress); 
trustedCenso.existePersona(_personAddress);

And here you can find a good and well written store pattern to help you:

Here are some simple and useful patterns in increasing order of utility. - Are there well-solved and simple storage patterns for Solidity?

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.