La función constante devuelve una matriz vacía + web3js

Tengo un script de solidez simple donde probé en remix (inyecté Web3 usando el entorno local geth) y funcionó bien devolviendo una matriz de datos de un método constante.

pragma solidity ^0.4.11;

contract TestBetting {

  struct BettingInfo {    
      uint256 matchId;
      uint homeTeamScore;
      uint awayTeamScore;     
      uint bettingPrice;  
  }

  address public owner;
  mapping(address => BettingInfo[]) public bettingInfo;

 // constructor
  function MyBetting() {
    owner = msg.sender;
  }

  // Fall back 
  function () payable {}

  event LogDeposit(address sender, uint amount);

  // Place a bet  
  function placeBet(uint256 _matchId, 
                    uint _homeTeamScore, 
                    uint _awayTeamScore, 
                    uint _bettingPrice) payable returns (bool) {

    bettingInfo[msg.sender].push(
      BettingInfo(_matchId, _homeTeamScore, _awayTeamScore, _bettingPrice)); 

    require(_bettingPrice == msg.value); 
    this.transfer(msg.value); 

    LogDeposit(msg.sender, msg.value);

    return true;
  }

  function getBettingInfo(address _better) public constant returns (uint256[], uint[], uint[], uint[]) {
    uint length = bettingInfo[_better].length;
    uint256[] memory matchId = new uint256[](length);
    uint[] memory homeTeamScore = new uint[](length);
    uint[] memory awayTeamScore = new uint[](length);
    uint[] memory bettingPrice = new uint[](length);   

    for (uint i = 0; i < length; i++) {
      matchId[i] = bettingInfo[_better][i].matchId;
      homeTeamScore[i] = bettingInfo[_better][i].homeTeamScore;
      awayTeamScore[i] = bettingInfo[_better][i].awayTeamScore;
      bettingPrice[i] = bettingInfo[_better][i].bettingPrice;   
    }

    return (matchId, homeTeamScore, awayTeamScore, bettingPrice);
  }
}

ingrese la descripción de la imagen aquí

Cuando hago esto en angular 5 con el entorno local de Geth, primero debo enviar la transacción a través del método placeBet.

submit(form: NgForm): void {  
    this.bettingPrice = 20;
    this.bettingPrice = this.web3.toWei(this.bettingPrice, "ether");

    this.TestBetting.deployed().then((instance) => {
        return instance.placeBet(this.matchId, 
                                 this.homeTeamScore, 
                                 this.awayTeamScore, 
                                 this.bettingPrice, {
            from: this.account,
            value: this.bettingPrice,
            gas: 50000
        });
      })
      .then((value) => {
        console.log(value);
      })
      .catch((e) => {
        console.log(e);
      }); 
  }

Se abrirá la metamáscara y parece que pasó.ingrese la descripción de la imagen aquí

Ahora, quiero llamar a getBettingInfo para ver los datos que envié a la red.

  loadGetBettingInfo() {   
    this.TestBetting.deployed().then((instance) => {
        return instance.getBettingInfo.call(this.betterAddress, {
            from: this.betterAddress,
            gas: 50000
        });
      })
      .then((value) => {
        console.log(value);
      })
      .catch((e) => {
        console.log(e);
      }); 
    }

Pero esto devuelve una matriz vacía en la consola.ingrese la descripción de la imagen aquí

¿Alguien puede ver lo que estoy haciendo mal aquí?

¿Estás seguro de que la transacción se extrajo correctamente? Quizás esté llamando a getBettingInfo antes de que se extraiga la transacción. ¿Es betterAddress la misma dirección que hizo la apuesta?
Sí, estaba minando con geth y cambié a Ganache y ahora funciona. geth --networkid 4224 --mine --datadir "..path" --nodiscover --rpc --rpcport "8545" --port "...port #" --rpccorsdomain "*" --nat "any" --rpcapi eth,web3,personal,miner,net --unlock 0 --password password.sec Este fue mi comando geth en el que el mío estaba habilitado. Todavía no sé por qué no funcionó con eso

Respuestas (1)

Esto se debe a que this.TestBetting.deployed().then((instance)está creando la nueva instancia cada vez que no posee los cambios de estado anteriores. Por lo tanto, debe crear una instancia y usarla para acceder a todas las funciones.

Contract.deployed()no crea una nueva instancia, solo devuelve una referencia a la instancia creada con los scripts de migración.