这是我第一次在Remix上部署合同,并学习如何在Solidity上编码。
我已经阅读了本指南,并成功部署了提供的智能合约模板:
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;
}
}
然而,我认为在部署了上面的模板后,每当我点击getLatestPrice按钮时,这对鞋的价格就会立即更新,我错了,价格实际上变成了"冷冻的";第一次点击后。
因此,我想知道在上面的模板中键入什么是强制性的,以实现的目标
此外,我试图通过在return price;
的正下方键入return timeStamp;
来打印timeStamp
,但在编译时,Remix编译器回答:
TypeError:返回参数类型uint256不能隐式转换为预期类型(第一个返回变量的类型(int256。返回时间戳;^-------^
那么,出于好奇,我如何将uint256变量转换为int256变量,以获得每个更新价格的时间戳(每次单击getLatestPrice button
(?
感谢阅读
价格提要合同实际上是由一组链链接节点独立更新的,而您的合同只是从该合同中读取的。
当你打电话给getLatestPrice
时,它实际上只是在阅读合同。
此外,价格馈送合同会根据某些阈值和偏差进行更新。特别是在testnet上,它们的更新非常零散。
如果你能为你的TypeError
单独提出一个问题,那将是理想的,谢谢!
测试网价格源仅偶尔更新(因此它们不会每秒甚至每分钟更新价格或时间(
如果你想看到这个函数被称为,这里有一个例子
// 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,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.latestRoundData();
return (price, timeStamp, roundID);
}
function counterKovanEthPrice() public {
countButtonClicks = countButtonClicks+1;
(kovanEthPrice, kovanTimestamp, kovanRoundID) = getKovanEthPrice();
}
}