[Ethereum] Oraclize out of gas on Ropsten Testnet

contract-designcontract-developmentoraclessolidity

Every time I try to run the getPairPrice function in the contract bellow I get an out of gas error. My contract always has a balance before I call the function, 2 ethers to be exact, but I still get the out of gas error. Why is that?

pragma solidity ^0.4.3;
import "github.com/oraclize/ethereum-api/oraclizeAPI_0.4.sol";

contract BinaryTrading is usingOraclize {
  // ********* //
  // VARIABLES //
  // ********* //
  address minter;

  uint balance;

  string public ethusd;

  function BinaryTrading() {
    minter = msg.sender;
    oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
  }

  // This function is the fallback function
  function () payable {
    balance += msg.value;
  }

  function updatePrice(string newPrice) {
      ethusd = newPrice;
  }

  // This function is called when the oraclized request results are ready
  function __callback(bytes32 myid, string result, bytes proof) {
    if (msg.sender != oraclize_cbAddress()) throw;
    ethusd = result;
  }

  // IMPORTANT: Add private when in production, so that I can only call what will not give an error
  function getPairPrice() {
    bytes32 myid = oraclize_query("URL", "json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD");
  }

}

I have also tried running the code changing the gas value to 2000000.

By changing this line:

bytes32 myid = oraclize_query("URL", "json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD");

to

bytes32 myid = oraclize_query("URL", "json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD", 2000000);

Is 2000000 gas not enough to cover updating a variable?

Please let me know what might be causing this. I am testing it using the online solidity compiler. When I create the contract using MetaMask it says "Transaction Error. Exception thrown in contract code." That same message appears when I call the getPairPrice function but not the others.

Here is one of those test contracts I created using the code above:
https://testnet.etherscan.io/address/0x2831899baa68f655764d17e7a65c8e526e4ac53c

Best Answer

Is this the failing transaction? https://testnet.etherscan.io/tx/0x369c48423586f5cdffefb2d202d6588e3874b6f1075b3feda205c7718f13dff3

This shows you sending only 78525 gas, which is probably not enough to call your getPairPrice() function, which in turn needs to pay for some initial oraclize functions - I think it'll be a price lookup and an event write, and possibly some storage.

The gas parameter you're adding to oraclize_query() isn't for your call to getPairPrice(), it's for the call that Oraclize will make later to your __callback() function. That will be done in a separate transaction, and that part will need to come out of the value that gets transferred from your contract balance when you send oraclize_query(), not the gas that you send to getPairPrice().

Related Topic