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