2

To quote an example from a book:

contract StockPriceOracle {
  uint quoteFee = 500;
  mapping (string => uint) private stockPrices;

  //...

  function getStockPrice(string _stockTicker) payable returns (uint _stockPrice) {
    if (msg.value == quoteFee) {
      //...
      _stockPrice = stockPrices[_stockTicker];
    } else {
      revert();
    }
  }
}

There is a payable function that returns a value. Correct me if I'm wrong, but as far as I understand, this example does not make any practical sense. For the caller to be able to retrieve the desired information (the stock price), the function would have to emit an event. Or is there a situation where the code above is actually practical?

1

For the caller to be able to retrieve the desired information (the stock price), the function would have to emit an event.

That's true for an off-chain caller, for example:

  • Web3.js script
  • Web3.py script
  • MyEtherWallet
  • Remix

Or is there a situation where the code above is actually practical?

Yes, it (the returned value) is practical for an on-chain caller, for example:

  • A function in the same contract
  • A function in some other contract

Note that the function's access-level (public/private/external/internal) also plays a role in restricting which caller types are permitted here.

| improve this answer | |
  • Ah, this clarifies the matter. Many thanks! – Taziano Fiorentini Jan 20 at 9:07
  • @TazianoFiorentini: You're welcome. Note that the function access-level (public/private/external/internal) also plays a role here. – goodvibration Jan 20 at 9:08
  • In how far? And do you maybe know any place on the web where there's a concise explanation of this for people who are starting out coding on Ethereum? I think it's a very good book I've started working through, but it doesn't seem to make these specifics of contract function invocation clear to me (yet?). – Taziano Fiorentini Jan 20 at 9:21

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.