Ethereum 有没有比通过节点进行单独的多个API调用更简单的方法从Chainlink获取分散的数据?

Ethereum 有没有比通过节点进行单独的多个API调用更简单的方法从Chainlink获取分散的数据?,ethereum,solidity,remix,Ethereum,Solidity,Remix,我想在我的solidity项目中获得ETH的价格反馈数据,我正在用Remix进行测试。我使用请求数据作为指导,这样我的数据就可以分散 现在,我向具有不同URL的不同节点发出3个链接请求,然后计算三个响应的中间值,以获得一个分散的价格。看起来这是一个相当乏味的方法,有没有更简单的方法 function requestEthereumPrice(address _address, bytes32 job_id, string url) public onlyOwner {

我想在我的solidity项目中获得ETH的价格反馈数据,我正在用Remix进行测试。我使用请求数据作为指导,这样我的数据就可以分散

现在,我向具有不同URL的不同节点发出3个链接请求,然后计算三个响应的中间值,以获得一个分散的价格。看起来这是一个相当乏味的方法,有没有更简单的方法

 function requestEthereumPrice(address _address, bytes32 job_id, string url) 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(JOB_ID), address(this), this.fulfill.selector);
    req.add("get", url);
    req.add("path", "USD");
    req.addInt("times", 100);
    sendChainlinkRequestTo(_address, req, ORACLE_PAYMENT);
  }

  function multipleData() public{
      requestEthereumPrice(ORACLE_ADDRESS, JOB_ID);
      requestEthereumPrice(ORACLE2_ADDRESS, JOB2_ID);
      requestEthereumPrice(ORACLE3_ADDRESS, JOB3_ID);
  }

 function fulfill(bytes32 _requestId, uint256 _price)
    public
    // Use recordChainlinkFulfillment to ensure only the requesting oracle can fulfill
    recordChainlinkFulfillment(_requestId)
  {
    currentPriceList[index] = _price;
    index = (index + 1) & 3
    currentPrice = median();
  }
欢迎来到SO

你的方法是最“暴力”的方式来做这件事,这是有效的。但是,目前有两种方法可以改进这一点

1.使用 参考数据契约是一种契约,它具有多个可信的分散链接节点,从不同的数据提供商向区块链上的参考点发送价格更新,供任何人查询。您的特定货币对
ETH/USD
是当前支持的货币对之一。获取该数据的代码如下所示:

pragma solidity ^0.6.0;

import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/dev/AggregatorInterface.sol";

contract ReferenceConsumer {
  AggregatorInterface internal ref;

// Use 0xF79D6aFBb6dA890132F9D7c355e3015f15F3406F as the address of the ETH/USD pair
  constructor(address _aggregator) public {
    ref = AggregatorInterface(_aggregator);
  }

  function getLatestAnswer() public view returns (int256) {
    return ref.latestAnswer();
  }

  function getLatestTimestamp() public view returns (uint256) {
    return ref.latestTimestamp();
  }
您可以在那里找到当前受支持的提要列表,或者在其页面上找到外观稍好的提要

2.使用预协调员合同 如果您想要一个盒装解决方案,那么第1条是很好的,但是,可能您不同意Chainlink的节点选择,并且希望您自己的实现(这增加了分散性!)。您的解决方案是选择自己的节点网络的一种方法

预协调器契约允许您像上面所做的那样创建一个正常的
buildChainlinkRequest
,但可以为您将其分散到多个节点。要创建此合同,请首先部署合同:

pragma solidity ^0.5.0;

import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.5/PreCoordinator.sol";
然后,您可以从合同中调用
createServiceAgreement
,其中包含参数
uint256\u minResponses、address[]\u oracles、address[]\u jobIds、uint256[]\u payments
。您可以使用混音来部署它们

一旦您从precoordinator合同中部署了服务协议,它将为您提供一个服务协议ID。您可以通过检查日志或查看类似etherscan的内容,然后转到
主题1
查看ID来找到此ID。下面是一个示例

获得预协调员的地址和服务协议后,可以将预协调员合同的地址用作oracle地址,并将服务协议用作jobId。这意味着您可以通过以下方式调用节点主机:

function requestEthereumPrice(address precoordinator_address, bytes32 service_agreement, string url) 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(service_agreement, address(this), this.fulfill.selector);
    req.add("get", url);
    req.add("path", "USD");
    req.addInt("times", 100);
    sendChainlinkRequestTo(precoordinator_address, req, ORACLE_PAYMENT);
  }