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);
}
}
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.
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.
Can anyone see what I'm doing wrong here?
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