除了可证明或预言外,是否还有其他神谕可以通过智能协议与外部api进行通信。
发布于 2020-06-23 09:44:02
请注意,您可以开始使用一种集中的方式从API中提取数据,但是为了分散您的契约,您可能希望通过多个节点从多个源中提取数据。
例如,您可以从单个Chainlink节点中提取数据以开始开发,但是对于生产级别系统,您可能希望从节点网络中提取数据。
以下是从单个链接节点中提取数据的完整示例。此作业返回一个Uint256。要返回不同的数据类型,请查看链连接适配器。
要调用requestEthereumPrice
函数,您必须选择一个甲骨文和一个工作。您可以使用以下链接池 oracle/作业。
address ORACLE = 0x83F00b902cbf06E316C95F51cbEeD9D2572a349a;
string constant JOB = "c179a8180e034cf5a341488406c32827";
pragma solidity ^0.4.24;
import "@chainlink/contracts/src/v0.4/ChainlinkClient.sol";
// MyContract inherits the ChainlinkClient contract to gain the
// functionality of creating Chainlink requests
contract ChainlinkExample is ChainlinkClient {
// Stores the answer from the Chainlink oracle
uint256 public currentPrice;
address public owner;
constructor() public {
// Set the address for the LINK token for the network
setPublicChainlinkToken();
owner = msg.sender;
}
// Creates a Chainlink request with the uint256 multiplier job
function requestEthereumPrice(address _oracle, bytes32 _jobId, uint256 _payment)
public
onlyOwner
{
// newRequest takes a JobID, a callback address, and callback function as input
Chainlink.Request memory req = buildChainlinkRequest(_jobId, address(this), this.fulfill.selector);
// Adds a URL with the key "get" to the request parameters
req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD");
// Uses input param (dot-delimited string) as the "path" in the request parameters
req.add("path", "USD");
// Adds an integer with the key "times" to the request parameters
req.addInt("times", 100);
// Sends the request with the amount of payment specified to the oracle
sendChainlinkRequestTo(_oracle, req, _payment);
}
// fulfill receives a uint256 data type
function fulfill(bytes32 _requestId, uint256 _price)
public
// Use recordChainlinkFulfillment to ensure only the requesting oracle can fulfill
recordChainlinkFulfillment(_requestId)
{
currentPrice = _price;
}
// cancelRequest allows the owner to cancel an unfulfilled request
function cancelRequest(
bytes32 _requestId,
uint256 _payment,
bytes4 _callbackFunctionId,
uint256 _expiration
)
public
onlyOwner
{
cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
}
// withdrawLink allows the owner to withdraw any extra LINK on the contract
function withdrawLink()
public
onlyOwner
{
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}
https://ethereum.stackexchange.com/questions/84443
复制