Chainlink

如何從 chainlink 價格資訊中獲取所有歷史數據?

  • February 15, 2022

我想通過chainlink oracles提取參考數據合約儲存的歷史數據。既然所有數據都在鏈上,我應該可以全部查看嗎?但目前,我的契約只能看到最新的答案。有什麼想法嗎?

pragma solidity ^0.6.0;

import "@chainlink/contracts/src/v0.6/dev/AggregatorInterface.sol";

contract ReferenceConsumer {
 AggregatorInterface internal ref;

 constructor(address _aggregator) public {
   ref = AggregatorInterface(_aggregator);
 }

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

您可以查看參考契約的整個歷史記錄,您需要使用getPreviousAnswer下面的方法,然後輸入您想要返回多少個答案。

pragma solidity ^0.6.0;

import "@chainlink/contracts/src/v0.6/dev/AggregatorInterface.sol";

contract ReferenceConsumer {
 AggregatorInterface internal ref;

 constructor(address _aggregator) public {
   ref = AggregatorInterface(_aggregator);
 }

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

 function getLatestTimestamp() public returns (uint256) {
   return ref.latestTimestamp();
 }

 function getPreviousAnswer(uint256 _back) public returns (int256) {
   uint256 latest = ref.latestRound();
   require(_back <= latest, "Not enough history");
   return ref.getAnswer(latest - _back);
 }

 function getPreviousTimestamp(uint256 _back) public returns (uint256) {
   uint256 latest = ref.latestRound();
   require(_back <= latest, "Not enough history");
   return ref.getTimestamp(latest - _back);
 }
}

引用自:https://ethereum.stackexchange.com/questions/84804