Skip to content

Latest commit

 

History

History

14-chainlink-price-feed

English / 中文

Feed price and random number predictor

Blockchain is a very secure and reliable network for the exchange of value, but there is no way to secure and tamper-proof access to off-chain data or send data to off-chain systems. Use Chainlink predictor to feed prices and obtain real-time financial market price data directly on the chain through the predictor network

Test Process

Configure the private key

The private key put in .env in the format "PRIVATE_KEY= XXXX ", from which the code automatically reads.

// .env
PRIVATE_KEY = xxxxxxxxxxxxxxxx;
INFURA_ID = yyyyyyyy;

Install Dependencies

yarn install

Executing the test script

npx hardhat run scripts/01-PriceConsumerV3Deploy.js --network goerli

Off-Chain Call the price feeder

// ./UsingDataFeedsByEthers.js
require('dotenv').config();

const { ethers } = require('ethers'); // for nodejs only
const provider = new ethers.providers.JsonRpcProvider(`https://goerli.infura.io/v3/${process.env.INFURA_ID}`);
const aggregatorV3InterfaceABI = require('@chainlink/contracts/abi/v0.8/AggregatorV3Interface.json');

const addr = '0x9326BFA02ADD2366b30bacB125260Af641031331';
const priceFeed = new ethers.Contract(addr, aggregatorV3InterfaceABI, provider);

async function test() {
  const roundData = await priceFeed.latestRoundData();
  console.log("Latest Round Data", roundData);
  const price = roundData[1];
  const decimal = await priceFeed.decimals();
  console.log("decimal = ", decimal);

  console.log("eth's price = ", price.toNumber() / 10 ** decimal + "USD");
}

test();

The returned data format is as follows:

Latest Round Data [
  BigNumber { _hex: '0x0200000000000029f5', _isBigNumber: true },
  BigNumber { _hex: '0x5b755c30c0', _isBigNumber: true },
  BigNumber { _hex: '0x61c3c368', _isBigNumber: true },
  BigNumber { _hex: '0x61c3c368', _isBigNumber: true },
  BigNumber { _hex: '0x0200000000000029f5', _isBigNumber: true },
  roundId: BigNumber { _hex: '0x0200000000000029f5', _isBigNumber: true },
  answer: BigNumber { _hex: '0x5b755c30c0', _isBigNumber: true },
  startedAt: BigNumber { _hex: '0x61c3c368', _isBigNumber: true },
  updatedAt: BigNumber { _hex: '0x61c3c368', _isBigNumber: true },
  answeredInRound: BigNumber { _hex: '0x0200000000000029f5', _isBigNumber: true }
]

Chainlink VRF

Chainlink VRF verifiable random function is a provably fair and verifiable source of randomness. As a tamper-proof random number generator, build smart contracts for any application builds that rely on unpredictable results.

  • Blockchain game and NFT
  • Randomly assigned responsibilities and resources (e.g. randomly assigned judges to hear cases)
  • Selection of representative samples for consensus mechanisms

Operation Process

  1. Create ChainLink SubscriptionID
    Login ChainLink VRF Test network , Click on" Create Subscription" to Create a SubscriptionID and you can see the created SubscriptionID under "My Subscriptions"

  1. Save SubscriptionID
    Save the SubscriptionID created in the previous step to .env

## .env
SubscriptionId=ddddd
  1. Run the deployment script to deploy the contract

    npx hardhat run scripts/02-RandomNumberConsumerDeploy.js --network rinkeby
  2. Access to ChainLink coins
    Login ChainLink Faucet , Get ChainLink coins for subsequent RandomNumberConsume, where Network selects Rinkeby and "Testnet Account Address "enters the account address of the contract owner

  1. Empower contracts to consume ChainLink coins for random number capture
    Login ChainLink VRF test network , and Click SubscriptionID

Then on the new page, "Add Funds" and "Add Consumer ". Where "Add Funds" is the number of ChainLink coins deposited, and "Add Consumer "needs to fill in the successfully deployed RandomNumberConsumer contract address, which is the contract address printed in Step 3

  1. Run the test script

    npx hardhat run  scripts/03-RandomNumberConsumer --network rinkeby

The result may take 2 to 3 minutes, and you can see two random values returned by ChainLink

❯ npx hardhat run scripts/03-RandomNumberConsumer.js --network rinkeby
Listen on random number call...
Listen on random number result...
first transaction hash: 0xb822b742836e3e028102b938ff9b52f5c31ecbf00a663b4865c50f83d141c441
event RequestId(address,uint256)
random0 requestID:  BigNumber { value: "68813323376039607636454911576409413136200025762802867082556497319163019860937" }
event FulfillRandomness(uint256,uint256[])
args[0] : BigNumber { value: "68813323376039607636454911576409413136200025762802867082556497319163019860937" }
random0Res:  21345191237588857524675400331731955708910062406377169110385405370996391926856,49611358654743768743671276783545638722996121599596073254340228099561828202433

todo

Will add aggregate obtain.

Reference Documentation

Reference documents are linked below: