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
bindingsaggregator_v3_interface.go
Follow these steps to learn how to generate this file:
- 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 calledAggregatorV3Interface.abi
:wget https://raw.githubusercontent.com/smartcontractkit/chainlink/develop/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol solc --abi AggregatorV3Interface.sol -o .
- From
AggregatorV3Interface.abi
, generate theGo
bindingsaggregator_v3_interface.go
required to programmatically interact with a smart contract > using generated, typesafeGo
code. Ensure that Geth is installed so you can use > > >abigen
.abigen --abi=AggregatorV3Interface.abi --pkg=aggregator_v3_interface --out=aggregator_v3_interface.go
- Go
- Be familiar with Chainlink Data Feeds and Chainlink Data Feeds Contract Addresses.
- Get a RPC API Key from a node provider such as Alchemy, Infura, Moralis, or QuickNode. This example uses the Rinkeby testnet.
- Clone the repo
git clone https://github.com/smartcontractkit/smart-contract-examples.git
- Enter the direcory
cd pricefeed-golang
- Copy the example environment file
.env.example
to create.env
filecp .env.example .env
- In
.env
file , replaceREPLACE_BY_YOUR_RPC_URL
by the RPC_URL of the EVM chain you want to connect to. - In
.env
file , replaceREPLACE_BY_PRICE_FEED_PROXY_ADDR
by the price feed address. For instance,0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
if you want theETH/USD
price feed on the Rinkeby testnet.
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