0

This code returns the address perfectly when i run it on ganache local chain but when i run this on ropsten it just returns a "-"

Any idea on what the problem is?

function newMatch(uint _matchId,uint _noOfGames,uint _oddsOfA, uint _oddsOfB) public returns(address newContract)
      {
        Match c = new Match(_matchId,_noOfGames,_oddsOfA,_oddsOfB);
        matchTracker[_matchId] = addr;
        count++;
        return address(c);
      }
  • 1
    Yes, the problem is that the function is not constant (pure or view). So it returns to any off-chain application a transaction hash. If you want the returned value, then you can either emit it in an event and obtain it from the transaction receipt (which you can retrieve via the transaction-hash that this function has returned), or you can read the last element from the matchTracker array, by calling the public getter function matchTracker(index), where index is the length of the matchTracker array minus 1 (you'll also need to add a view function to return the length of this array). – goodvibration Nov 22 '19 at 12:56
  • And BTW, it is customary here to accept the answer to your question by clicking on the V next to it, when this is indeed the correct answer. – goodvibration Nov 22 '19 at 12:57
0

Your function most probably does return value, you just cannot capture it. It is not easy to capture value returned by a functions, when the function is called on-chain from externally owned address. Try calling the function from other smart contract.

Another way is to log result value in even along with returning it, like this:

<!-- language: lang-solidity -->

event NewMatch (address indexed match);

function newMatch(uint _matchId,uint _noOfGames,uint _oddsOfA, uint _oddsOfB)
public returns(address newContract)
{
  Match c = new Match(_matchId,_noOfGames,_oddsOfA,_oddsOfB);
  matchTracker[_matchId] = addr;
  count++;
  emit NewMatch (address (c));
  return address(c);
}
|improve this answer|||||
0

Should the return statement not look like: returns(address) instead of returns(address newContract)

But I wonder why you sometimes get a valid address back. Maybe when returning the Match object, sometimes its still in the heap with valid values, and sometimes it's already been deleted

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.