目前,我打电话的时候
oraclize_getPrice("random", 400000);我回来了
8000000000000000在rinkeby上。如果在主干网上,那将超过2美元,而且看起来也很奇怪。为什么这么高?
发布于 2018-08-24 07:16:16
您的oraclize_getPrice("random", 400000)返回8000000000000000,因为它使用给定的gasLimit 400000和默认的gasPrice 20 Gwei计算gasLimit * gasPrice。The总成本是由 $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的“实际”价格的因素有三:
__callback()函数的成本。此函数由Oraclize调用,必须由您的合同支付。__callback()时使用200 K的气体限制。这20万元是由你方合同支付的。但是,unused gas将返回到Oraclize,而不是返回到您的合同!尝试并错误地找到一个封闭值,并在oraclize_newRandomDSQuery(delay, N, CUSTOM_GASLIMIT)和oraclize_getPrice("random", CUSTOM_GASLIMIT)中使用它。This值必须是>=,而不是(1)中确定的值。oraclize_setCustomGasPrice(customGasPrice)在构造函数或任何其他函数中设置它。我建议不要对这个值进行硬编码,因为在高网络负载的时候,需求可能会有所不同。If您的gasPrice太低,对您的合同的回复可能需要很长时间,甚至可能永远不会被挖掘。下面是一个如何处理这些值的示例:
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。
注:您应该使您的合同拥有和限制访问上述一些功能的合同所有者。
https://ethereum.stackexchange.com/questions/57327
复制相似问题