Part1
Part2
我们以Chainlink提供的TestnetConsumer合约中的一个requestEthereumPrice 方法为例来简单讲一下请求响应的流程。这个函数定义如下:
function requestEthereumPrice(address _oracle, string _jobId)
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), this, this.fulfillEthereumPrice.selector);
req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD");
req.add("path", "USD");
req.addInt("times", 100);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
它所实现的功能就是从指定的API(cryptocompare)获取ETH/USD的交易价格。函数传入的参数是指定的oracle地址和jobId。将一些列的请求参数组好后,调用sendChainlinkRequestTo 方法将请求发出。sendChainlinkRequestTo是定义在Chainlink提供的库中

本文通过Chainlink的TestnetConsumer合约中requestEthereumPrice方法,详细解析了请求响应模式下预言机的工作流程。从调用API获取ETH/USD价格,到请求的发送、接收、处理及结果反馈,全面展示了预言机如何连接链上链下数据。
最低0.47元/天 解锁文章
6625

被折叠的 条评论
为什么被折叠?



