如何解决每次单击“getLatestPrice”按钮时,如何更新某些 ChainLink 聚合器提供的数据?
这是我第一次在 Remix 上部署合约以及学习如何在 solidity 上编码。
我已阅读此 guide 并成功部署了提供的智能合约模板:
QObject::movetoThread: Current thread (0x125b2f0) is not the object's thread (0x189e780).
Cannot move to target thread (0x125b2f0)
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/deepak/Desktop/SampleApp/lib/python3.9/site-packages/cv2/qt/plugins" even though it was found.
This application Failed to start because no Qt platform plugin Could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb,eglfs,linuxfb,minimal,minimalegl,offscreen,vnc,wayland-egl,wayland,wayland-xcomposite-egl,wayland-xcomposite-glx,webgl.
Aborted
然而,我以为在部署了上面的模板后,每当我点击getLatestPrice button时,该对的价格会立即更新,我错了,价格实际上在之后“冻结”了第一次点击。
所以,我想知道在上面的模板中输入什么来实现这个目标是必须的
此外,我尝试通过在 myClass myclass = myClass();
myclass.method1();
myclass.method2();
正下方键入 pragma solidity ^0.6.7;
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract PriceConsumerV3 {
AggregatorV3Interface internal priceFeed;
/**
* Network: Kovan
* Aggregator: BTC/USD
* Address: 0x6135b13325bfC4B00278B4abC5e20bbce2D6580e
*/
constructor() public {
priceFeed = AggregatorV3Interface(0x6135b13325bfC4B00278B4abC5e20bbce2D6580e);
}
/**
* Returns the latest price
*/
function getThePrice() public view returns (int) {
(
uint80 roundID,int price,uint startedAt,uint timeStamp,uint80 answeredInRound
) = priceFeed.latestRoundData();
return price;
}
}
来打印 timeStamp
,但是在编译时,Remix 编译器回复:
TypeError:返回参数类型 uint256 不能隐式转换为预期类型(第一个返回变量的类型)int256。返回时间戳; ^-------^
所以,出于好奇,我如何将 uint256 变量转换为 int256 变量以获取每个更新价格的时间戳(每次点击 return timeStamp;
)?
感谢阅读
解决方法
Testnet pricefeeds 只会偶尔更新(因此它们不会每秒甚至每分钟更新价格或时间)
这是一个例子,如果你想看到函数被调用
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.6;
pragma experimental ABIEncoderV2;
//To run on remix use Injected Web3 with Metamask on Rinkeby network activated
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract ChainlinkPriceFeed {
int public countButtonClicks;
int public kovanEthPrice;
uint public kovanTimestamp;
uint80 public kovanRoundID;
/**
* Network: Kovan
* Aggregator: ETH/USD
*/
constructor() public {
countButtonClicks = 0;
kovanEthPrice = -1;
kovanTimestamp = 0;
kovanRoundID = 0;
// Examples -> Rinkeby network
// See https://docs.chain.link/docs/reference-contracts/ for available feeds and blockchains
// priceData["ETHUSD"] = 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e;
// priceData["BTCUSD"] = 0xECe365B379E1dD183B20fc5f022230C044d51404;
// priceData["LINKUSD"] = 0xd8bD0a1cB028a31AA859A21A3758685a95dE4623;
// priceData["AUDUSD"]= 0x21c095d2aDa464A294956eA058077F14F66535af;
//Examples -> Kovan Netowrk see https://docs.chain.link/docs/ethereum-addresses/
}
/**
* Returns the latest price information from the asset address
*/
function getLatestPrice(address assetAddress) public view returns
(int price,uint80 roundID,int decimals,string memory description,uint timestamp)
{
AggregatorV3Interface priceFeed = AggregatorV3Interface(assetAddress);
(
roundID,price,uint startedAt,timeStamp,uint80 answeredInRound
) = priceFeed.latestRoundData();
description = priceFeed.description();
decimals = priceFeed.decimals();
return (price,roundID,decimals,description,timestamp);
}
function getKovanEthPrice() public view returns (int price,uint timeStamp,uint80 roundID) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
(
uint80 roundID,int price,uint80 answeredInRound
) = priceFeed.latestRoundData();
return (price,roundID);
}
function counterKovanEthPrice() public {
countButtonClicks = countButtonClicks+1;
(kovanEthPrice,kovanTimestamp,kovanRoundID) = getKovanEthPrice();
}
}
,
价格馈送合约实际上是由一组 chainlink 节点独立更新的,而您的合约只是从该合约中读取数据。
当您调用 getLatestPrice
时,它实际上只是在读取该合同。
此外,价格馈送合同会根据特定阈值和偏差进行更新。尤其是在测试网上,它们会不时更新。
如果您能为您的 TypeError
提出一个单独的问题,那就太理想了,谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。