chainlink 预言机的使用

本文介绍了如何使用Chainlink预言机获取价格数据。首先,可以从官方水龙头获取LINK代币,然后利用AggregatorV3Interface构造函数初始化并获取价格信息。此外,还阐述了订阅 Chainlink VRF 地址来获取随机数的过程,包括部署合约、设置订阅ID,并在智能合约中实现fulfillRandomWords函数以接收随机数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取 价格

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract PriceConsumerV3 {

    AggregatorV3Interface internal priceFeed;

    /**
     * Network: Goerli
     * Aggregator: ETH/USD
     * Address: 0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
     */
    constructor() {
        priceFeed = AggregatorV3Interface(0x779877A7B0D9E8603169DdbD7836e478b4624789);
    }

    /**
     * Returns the latest price
     */
    function getLatestPrice() public view returns (int) {
        (
            /*uint80 roundID*/,
            int price,
            /*uint startedAt*/,
            /*uint timeStamp*/,
            /*uint80 answeredInRound*/
        ) = priceFeed.latestRoundData();
        return price;
    }
}

1 link -0x326C977E6efc84E512bB9C30f76E30c160eD06FB
水龙头-https://faucets.chain.link/
2 构造函数初始化AggregatorV3Interface

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值