Chainlink Price Feeds
Chainlink Integrations allows access to price feeds on xDai
Chainlink allows smart contracts to receive aggregated price feeds and access external data using a decentralized network of oracles. The xDai <-> Chainlink integration was completed by Protofire with a Chainlink Community Grant.
Chainlink offers a tutorial on using secure data feeds with xDai. See it in action
πŸƒβ™‚

Addresses

Basic Tutorial: Price Feeds

See the Chainlink documentation for more advanced tutorials and information. The following shows how to use MetaMask/Remix with xDai to deploy a simple price feed contract, then call the function using Blockscout.

1) Switch to xDai RPC in MetaMask

Go to MetaMask, select Custom RPC from the network dropdown, and enter the following details. If you need more info, see MetaMask Setup.

2) Get xDai with Faucet

You can get enough xDai to deploy your contracts and more with the xDai Faucet. Go to https://blockscout.com/poa/xdai/faucet to request .01 xDai. You should see it added to your address in a few seconds.

3) Open Remix and Create File

There are several ways to create a new file. Here we:
  1. 1.
    Create a folder called xDai Price Feed.
  2. 2.
    Create a file in the folder called PriceFeedTest.sol.
  3. 3.
    Paste in the example code below the image.

Example Code

1
/** This example code is designed to quickly deploy an example contract using Remix.
2
* If you have never used Remix, try our example walkthrough: https://docs.chain.link/docs/example-walkthrough
3
* You will need xDai to deploy.
4
* - xDai Faucet: https://blockscout.com/poa/xdai/faucet
5
* - LINK address on xDai: 0xE2e73A1c69ecF83F464EFCE6A5be353a37cA09b2
6
*/
7
​
8
pragma solidity ^0.6.7;
9
​
10
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
11
​
12
contract PriceConsumerV3 {
13
​
14
AggregatorV3Interface internal priceFeed;
15
​
16
/**
17
* Network: xDai
18
* Aggregator: ETH/USD
19
* Address: 0xa767f745331D267c7751297D982b050c93985627
20
*/
21
constructor() public {
22
priceFeed = AggregatorV3Interface(0xa767f745331D267c7751297D982b050c93985627);
23
}
24
​
25
/**
26
* Returns the latest price
27
*/
28
function getLatestPrice() public view returns (int) {
29
(
30
uint80 roundID,
31
int price,
32
uint startedAt,
33
uint timeStamp,
34
uint80 answeredInRound
35
) = priceFeed.latestRoundData();
36
return price;
37
}
38
}
Copied!
The code below uses the Chainlink standard Price Consumer contract along with several modifications:
  • We initialize the ETH/USD xDai Price Feed in the constructor
1
priceFeed = AggregatorV3Interface(0xa767f745331D267c7751297D982b050c93985627);
Copied!
We use the getLatestPrice function to return the current price. It pulls this from the latestRoundData function in the imported AggregatorV3Interface.sol Contract

4) Compile Contract in Remix

Click on the Compiler Icon, adjust items (if necessary, we keep defaults here) and click the Compile button.

5) Deploy Contract

  1. 1.
    Select Deploy Icon.
  2. 2.
    Change Environment to Web3.
  3. 3.
    Click Deploy.
  4. 4.
    Confirm the transaction in MetaMask. You account must be connected to the xDai chain and have a small amount of xDai (see steps 1 and 2).

6) Check Contract

Once deployed, click to expand the contract. Click getLatestPrice to check the ETH/USD price.

Verify Contract in BlockScout Block Explorer

1) Find Deployed Contract

For transparency and interaction purposes, you can verify your contract on BlockScout. Note, if a contract with the same bytecode has already been deployed and verified, your contract code may be viewable. See this example.
  1. 1.
    Copy the deployed contract address in Remix.
  2. 2.
    Go to BlockScout and paste into the search field.
  3. 3.
    Click the Code tab for verification methods.
  4. 4.
    Click the Verify and Publish Button.

2) Add Sources to Verify

Select either a flattened file or Sources. In this example we select the Sources and metadata JSON files from Remix
Remix does not have an auto-export feature. You can use the remixd or copy the contents of each file and save in a text editor computer using the same names and file extensions. Include all imported files called by the contract, in this case the AggregatorV3Interface.sol file.
Drag and drop the files into the interface and click Verify & publish.

3) View your Contract in BlockScout

Once verified, you will see the checkmark next to the code, and you can read (and write) to your contract within the BlockScout environment.
Last modified 6mo ago