Skip to content

nikola43/chainlinkpricefeed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting an asset price from EVM chain

Table of Contents

About The Project

The project shows how to retrieve asset price from any EVM chain. The price data feed contract addresses can be found here.

📘 Note on Go bindings aggregator_v3_interface.go

Follow these steps to learn how to generate this file:

  1. Generate the Contract ABI from a solidity source file using solc. In this case, we are going to download the AggregatorV3Interface and generate an ABI called AggregatorV3Interface.abi:
wget https://raw.githubusercontent.com/smartcontractkit/chainlink/develop/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol
solc --abi AggregatorV3Interface.sol -o .
  1. From AggregatorV3Interface.abi, generate the Go bindings aggregator_v3_interface.go required to programmatically interact with a smart contract > using generated, typesafe Gocode. Ensure that Geth is installed so you can use > > > abigen.
abigen --abi=AggregatorV3Interface.abi --pkg=aggregator_v3_interface --out=aggregator_v3_interface.go

Prerequisites

Installation

  1. Get a RPC API Key from a node provider such as Alchemy, Infura, Moralis, or QuickNode. This example uses the Rinkeby testnet.
  2. Clone the repo
    git clone https://github.com/smartcontractkit/smart-contract-examples.git
  3. Enter the direcory
    cd pricefeed-golang
  4. Copy the example environment file .env.example to create .env file
    cp .env.example .env
  5. In .env file , replace REPLACE_BY_YOUR_RPC_URL by the RPC_URL of the EVM chain you want to connect to.
  6. In .env file , replace REPLACE_BY_PRICE_FEED_PROXY_ADDR by the price feed address. For instance, 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e if you want the ETH/USD price feed on the Rinkeby testnet.

Usage

After the installation step, run the example

go run main.go

2022/05/18 18:48:26 ETH / USD Price feed address is 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
2022/05/18 18:48:26 Round id is 36893488147419115714
2022/05/18 18:48:26 Answer is 195086228530
2022/05/18 18:48:26 Formatted answer is 1950.8622853
2022/05/18 18:48:26 Started at 2022-05-18 18:10:36 +0200 CEST
2022/05/18 18:48:26 Updated at 2022-05-18 18:10:36 +0200 CEST
2022/05/18 18:48:26 Answered in round 36893488147419115714

Note that you can also override the price feed address on the command line. For instance:

go run main.go 0x78F9e60608bF48a1155b4B2A5e31F32318a1d85F

2022/05/18 18:48:46 EUR / USD Price feed address is 0x78F9e60608bF48a1155b4B2A5e31F32318a1d85F
2022/05/18 18:48:46 Round id is 73786976294838233703
2022/05/18 18:48:46 Answer is 104976000
2022/05/18 18:48:46 Formatted answer is 1.04976
2022/05/18 18:48:46 Started at 2022-05-18 18:45:36 +0200 CEST
2022/05/18 18:48:46 Updated at 2022-05-18 18:45:36 +0200 CEST
2022/05/18 18:48:46 Answered in round 73786976294838233703

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published