首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机价格过高(rinkeby)?

随机价格过高(rinkeby)?
EN

Ethereum用户
提问于 2018-08-24 06:04:15
回答 1查看 567关注 0票数 3

目前,我打电话的时候

代码语言:javascript
复制
oraclize_getPrice("random", 400000);

我回来了

代码语言:javascript
复制
8000000000000000

在rinkeby上。如果在主干网上,那将超过2美元,而且看起来也很奇怪。为什么这么高?

EN

回答 1

Ethereum用户

发布于 2018-08-24 07:16:16

您的oraclize_getPrice("random", 400000)返回8000000000000000,因为它使用给定的gasLimit 400000和默认的gasPrice 20 Gwei计算gasLimit * gasPriceThe总成本是由 $0.05 +( call of your __callback() + execution gas of the __callback()) * gasPrice计算的,但是实际上它是由$0.05 + CUSTOM_GAS_LIMIT * CUSTOM_GAS_PRICE计算的,因此CUSTOM_GAS_LIMIT应该足够高地执行您的__callback()和CUSTOM_GAS_PRICE,以便及时挖掘事务。

请参见以下如何影响这些值:

影响使用oraclize的“实际”价格的因素有三:

  1. gas __callback()函数成本。此函数由Oraclize调用,必须由您的合同支付。
  2. (与(1)密切相关) Oraclize使用的<#>gas限制()向您的合同发送答案。默认情况下,oraclize在调用__callback()时使用200 K的气体限制。这20万元是由你方合同支付的。但是,unused gas将返回到Oraclize,而不是返回到您的合同!尝试并错误地找到一个封闭值,并在oraclize_newRandomDSQuery(delay, N, CUSTOM_GASLIMIT)oraclize_getPrice("random", CUSTOM_GASLIMIT)中使用它。This值必须是>=,而不是(1)中确定的值。
  3. 设定的gas价格。默认情况下,oraclize的价格相对较高,为20 uses。目前,根据Ethgasstation.info的说法,3-4格威就足够了(光是这一项就能降低你80%的成本!)您可以通过调用oraclize_setCustomGasPrice(customGasPrice)在构造函数或任何其他函数中设置它。我建议不要对这个值进行硬编码,因为在高网络负载的时候,需求可能会有所不同。If您的gasPrice太低,对您的合同的回复可能需要很长时间,甚至可能永远不会被挖掘。

下面是一个如何处理这些值的示例:

代码语言:javascript
复制
pragma solidity ^0.4.24;
import "oraclize-api/contracts/usingOraclize.sol";

contract MyContract is usingOraclize {

  // USD Cent value of 1 ether. Init with non-zero value.
  uint256 public ethusd = 1;

  // Sold out / sale active?
  bool public soldOut = false;  

  // Oracle active?
  bool public oracleActive = false;

  // Delay between autonomous oraclize requests
  uint256 public oracleInterval;

  // Gas price for oraclize callback transaction. 5 Gwei
  uint256 public oracleGasPrice = 5000000000;

  // Gas limit for oraclize callback transaction
  // Unused gas is returned to oraclize.
  uint256 public oracleGasLimit = 100000;


  constructor(uint _oracleInterval, uint _oracleGasPrice) public {
    require(_oracleInterval > 0);
    require(_oracleGasPrice > 0);

    oracleInterval = _oracleInterval;    
    oracleGasPrice = _oracleGasPrice;

    oraclize_setCustomGasPrice(_oracleGasPrice);
  }

  // Activate ethusd oracle and load contract with ETH
  function activateOracle() external payable {
    oracleActive = true;
    requestEthUsd(0);
  }

 /**
   * @dev Request ETHUSD rate from oraclize
   * @param _delay in seconds when the request should be scheduled from now on
   */
  function requestEthUsd(uint _delay) internal {
    if (oracleActive && !soldOut) {
      if (oraclize_getPrice("URL") > address(this).balance) {
        oracleActive = false;
      } else {
        if (_delay == 0) {
          oraclize_query("URL", "json(https://api.gdax.com/products/ETH-USD/ticker).price", oracleGasLimit);
        } else {
          oraclize_query(_delay, "URL", "json(https://api.gdax.com/products/ETH-USD/ticker).price", oracleGasLimit);
        }
      }
    }
  }

  /**
   * @dev Called by oraclize.
   */
  function __callback(bytes32 myid, string result) public {
    if (msg.sender != oraclize_cbAddress()) revert();
    ethusd = parseInt(result, 2);
    requestEthUsd(oracleInterval);
  }

  /**
   * @dev Public function to set the oracle gas price.
   */
  function setOracleGasPrice(uint256 _gasPrice) external {
    require(_gasPrice > 0, "Gas price must be a positive number.");
    oraclize_setCustomGasPrice(_gasPrice);
    oracleGasPrice = _gasPrice;
  }


  /**
   * @dev Public function to set the oracle gas limit.
   */
  function setOracleGasLimit(uint256 _gasLimit) external {
    require(_gasLimit > 0, "Gas limit must be a positive number.");
    oracleGasLimit = _gasLimit;
  }


  /**
   * @dev Public function to set the oracle query interval.
   */
  function setOracleInterval(uint256 _interval) external {
    require(_interval > 0, "Interval must be > 0");
    oracleInterval = _interval;
  }
}

这样,即使在合同部署之后,也可以应用新的值。我的"URL“查询的<#>The成本已经从$1.20降到$0.13

注:您应该使您的合同拥有和限制访问上述一些功能的合同所有者。

票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/57327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档