我试图打印uint timeStamp
通过输入return timeStamp;
就在下面return price;
从这段代码:
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;
return timeStamp;
}
}
当我在 Remix Compiler 上编译上面的代码时,它回复:
类型错误:返回参数类型 uint256 无法隐式转换为预期类型(第一个返回变量的类型)int256。返回时间戳; ^--------^
我倾向于认为我只需要输入int256 return timeStamp
或类似的东西代替return timeStamp;
但我不明白。
感谢您的反馈。
您可以输入uint
to int
使用这个语法:
return int(timeStamp);
注意:这将溢出(在 Solidity 0.7.x 及更早版本中)或抛出异常(在 Solidity 0.8+ 中)2^255
(int
最大值)和2^256-1
(uint
最大值)。但这很可能只是理论上的情况,因为时间戳预计不会有这么大的值。
请注意,此线路无法接通,因为您已经返回price
在上一行。
(
uint80 roundID,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.latestRoundData();
return price; // the `price` is returned, and the function doesn't execute after this line
return timeStamp; // this is ignored because of the early return on previous line
如果要返回多个值,可以使用以下语法:
// note the multiple datatypes in the `returns` block
function getThePriceAndTimestamp() public view returns (int, uint) {
(
uint80 roundID,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.latestRoundData();
return (price, timeStamp); // here returning multiple values
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)