Skip to content

Latest commit

 

History

History
98 lines (78 loc) · 3.95 KB

File metadata and controls

98 lines (78 loc) · 3.95 KB

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:
cd aggregatorv3
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 directory
    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, 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419 if you want the ETH/USD price feed on the Ethereum mainnet.

Usage

After the installation step, run the example

go run main.go

Example:

go run main.go

2024/03/06 16:52:38 ETH / USD Price feed address is  0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
2024/03/06 16:52:38 Round id is 110680464442257322577
2024/03/06 16:52:38 Answer is 379607523254
2024/03/06 16:52:38 Formatted answer is 3796.07523254
2024/03/06 16:52:38 Started at 2024-03-06 16:31:11 +0100 CET
2024/03/06 16:52:38 Updated at 2024-03-06 16:31:11 +0100 CET
2024/03/06 16:52:38 Answered in round 110680464442257322577

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

go run main.go 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419

Example:

go run main.go 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419

2024/03/06 16:53:07 ETH / USD Price feed address is  0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
2024/03/06 16:53:07 Round id is 110680464442257322577
2024/03/06 16:53:07 Answer is 379607523254
2024/03/06 16:53:07 Formatted answer is 3796.07523254
2024/03/06 16:53:07 Started at 2024-03-06 16:31:11 +0100 CET
2024/03/06 16:53:07 Updated at 2024-03-06 16:31:11 +0100 CET
2024/03/06 16:53:07 Answered in round 110680464442257322577