1

I have a simple solidity script where I tested in remix (injected Web3 using geth local environment) and worked fine returning an array of data from a constant method.

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);
  }
}

enter image description here

When I do this in angular 5 with Geth local environment, first I need to submit transaction through placeBet method.

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);
      }); 
  }

It will open up metamask and it looks like it went through. enter image description here

Now, I want to call getBettingInfo to see the data I submitted into network.

  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);
      }); 
    }

But this returns an empty array in the console. enter image description here

Can anyone see what I'm doing wrong here?

  • Are you sure the transaction was mined correctly? Perhaps you are calling getBettingInfo before the transaction is mined. Is betterAddress the same address that placed the bet? – Ismael Feb 23 '18 at 18:11
  • Yeah I was mining with geth and I changed to Ganache and it works now. 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 This was my geth command in which mine was enabled. Still don't know why it didn't work with that – bbusdriver Feb 23 '18 at 22:52
-1

This is becausethis.TestBetting.deployed().then((instance) is creating the new instance every time which doesn't possess the previous state changes. So you need to create one instance and use it for all functions access.

  • Contract.deployed() does not create a new instance, it only returns a reference to the instance create with the migration scripts. – Ismael Feb 23 '18 at 18:09

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.