我收到了一条非常奇怪的错误消息——DeclarationError:未定义的标识符。
它在我所有的函数中都给出了这个,我不知道为什么。有人能告诉我发生了什么事吗?
我正在使用以太坊、Solidity和Openzepellinn来完成这项工作,但为了能够完成下一步,我需要我的界面在没有任何这些错误的情况下工作。
请参阅下面的代码
pragma solidity ^0.8.0;
interface AggregatorV3Interface {
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
// getRoundData and latestRoundData should both raise "No data present"
// if they do not have data to report, instead of returning unset values
// which could be misinterpreted as actual reported values.
function getRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
}
/**
* THIS EXAMPLE USES UN-AUDITED CODE.
* Network: Kovan
* Base: BTC/USD
* Base Address: 0x6135b13325bfC4B00278B4abC5e20bbce2D6580e
* Quote: EUR/USD
* Quote Address: 0x0c15Ab9A0DB086e062194c273CC79f41597Bbf13
* Decimals: 8
*/
contract PriceConverter {
address public priceSource;
address public quote;
uint8 private decimals;
uint256 public fallbackPrice;
event FallbackPrice(
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
constructor(address _priceSource, address _quote, uint8 _decimals) public {
priceSource = _priceSource;
quote = _quote;
decimals = uint8(10 ** uint8(_decimals));
}
function latestRoundData() public view
returns
(uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
require(decimals > uint8(0) && decimals <= uint8(18), "Invalid _decimals");
( , int256 basePrice, , , ) = address(priceSource).latestRoundData();
uint8 baseDecimals = address(priceSource).decimals();
basePrice = scalePrice(basePrice, baseDecimals, decimals);
( , int256 quotePrice, , , ) = address(quote).latestRoundData();
uint8 quoteDecimals = address(quote).decimals();
quotePrice = scalePrice(quotePrice, quoteDecimals, decimals);
return basePrice * decimals / quotePrice;
}
function updateFallbackPrice() public {
(
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) = priceSource.latestRoundData();
if (answer > 0) {
fallbackPrice = uint256(answer);
emit FallbackPrice(roundId,answer,startedAt,updatedAt,answeredInRound);
}
}
function scalePrice(int256 _price, uint8 _priceDecimals, uint8 _decimals)
internal
pure
returns (int256)
{
if (_priceDecimals < _decimals) {
return _price * int256(10 ** uint256(_decimals - _priceDecimals));
} else if (_priceDecimals > _decimals) {
return _price / int256(10 ** uint256(_priceDecimals - _decimals));
}
return _price;
}
} ```
Also i added screenshot of what it looks like on remix IDE
[1]: https://i.stack.imgur.com/e00dC.png
第一个错误,这是您的构造函数
constructor(address _priceSource, address _quote, uint8 _decimals) public {
priceSource = AggregatorV3Interface(_priceSource);
quote = ERC20(_quote);
decimals = uint8 (_decimals);
}
使用类型uint8初始化_decimals,然后再次将其转换为uint8,这不是必需的
在函数中,您使用的是仅在构造内部初始化的_decimals
,但您将其传递给decimals
的值,您必须使用它而不是_decimals