学习 Solidity——智能合约开发Solidity 是一种受 C++、JavaScript 和 Python 影响的面向对象的编程语言。区分变量作用域:状态变量通常位于智能合约内部,但位于函数外部。局部变量位于函数内部,不能从该函数之外访问。全局变量不是由你声明的,当时它们“神奇地”可供你使用。// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
contract PriceConsumerV3 {
AggregatorV3Interface internal priceFeed; // priceFeed is a public variable of type AggregatorV3Interface
constructor() { // _priceFeed is the address of the contract that implements the AggregatorV3Interface
priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e); // priceFeed is set to the address of the contract that implements the AggregatorV3Interface
}
function getLatestPrice() public view returns(int) {
(
/*uint80 roundID*/,
int price, // price is the price of the asset in the chainlink oracle
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData(); // Call the latestRoundData() function on the priceFeed contract
return price; // Return the price of the asset in the chainlink oracle
}
}参考cid:link_0
yd_262567365
发表于2024-05-09 14:17:23
2024-05-09 14:17:23
最后回复
74