Using Data Feeds on Tron
This tutorial demonstrates how to read Chainlink Data Feeds on the TRON blockchain. You will learn how to deploy a smart contract on TRON Nile Testnet using TronBox and interact with it offchain using TronWeb.
Chainlink Data Feeds on TRON provide reliable, decentralized price data for your applications. This tutorial covers:
- Smart Contract Development: Creating and deploying a contract that reads price feeds
- Offchain Interaction: Using TronWeb to interact with your deployed contract
By the end of this tutorial, you will have a working smart contract that reads real-time price data and a JavaScript application that displays the results.
Prerequisites
Before you begin, ensure you have:
-
Knowledge of JavaScript and Solidity
-
Node.js 20 or higher - Install the latest release. Optionally, use nvm to switch between Node.js versions with
nvm use 20$ node -v v20.11.0 -
TronBox (version 3.3 or higher) - Install globally with
npm install -g tronboxVerify your installation using
tronbox version:$ tronbox version Tronbox v4.2.0 Solidity v0.8.23 (tron-solc) -
npm package manager
-
A TRON compatible Web3 wallet (e.g., TronLink)
Setting Up Your TRON Wallet
- Install the TronLink browser extension
- Create a new wallet or import an existing one
- Export your private key (you will need this for deploying your smart contract):
- Open TronLink wallet extension
- Open the Wallet Management menu
- Select Export Account
- Select Private Key
- Enter your password
- Copy your private key
Getting Test Tokens
Fund your wallet with test TRX from the TRON Nile Testnet faucet:
- Visit https://nileex.io/join/getJoinPage
- Enter your TRON wallet address
- Complete the verification and receive 2000 test TRX
Setup
Clone the Example Repository
git clone https://github.com/smartcontractkit/smart-contract-examples.git && cd smart-contract-examples/data-feeds/tron/getting-started
Install Dependencies
npm install
Configure Environment
Create a .env file by copying the sample:
cp .env.example .env
Edit the .env file and add your private key:
export PRIVATE_KEY_NILE=your_nile_testnet_private_key_here
Part 1: Smart Contract Development and Deployment
Understanding the Smart Contract
The DataFeedReader.sol contract provides four key functions to interact with Chainlink Data Feeds on TRON:
getChainlinkDataFeedLatestAnswer(): Returns complete round data including price, timestamps, and round IDsgetLatestPrice(): Returns only the current price for simplified usagegetDecimals(): Returns the number of decimal places for proper price formattinggetDescription(): Returns human-readable feed description (e.g., "BTC / USD")
The contract uses the AggregatorV3Interface to interact with Chainlink data feeds.
View the complete contract code: DataFeedReader.sol
Price Feeds Used in This Tutorial
This tutorial uses the following price feeds on TRON Nile Testnet:
| Asset Pair | Contract Address |
|---|---|
| BTC/USD | TD3hrfAtPcnkLSsRh4UTgjXBo6KyRfT1AR |
| ETH/USD | TYaLVmqGzz33ghKEMTdC64dUnde5LZc6Y3 |
Compile the Contract
tronbox compile
Deploy to TRON Nile Testnet
Load your environment variables and deploy:
source .env && tronbox migrate --network nile
After successful deployment, you will see output similar to:
Deploying 'DataFeedReader'
...
DataFeedReader: TTZEzaRUfrSm2ENfkhrPzk5mMEkZVwS3eD
Part 2: Offchain Interaction with TronWeb
Understanding the TronWeb Script
The reader.js script demonstrates how to interact with your deployed contract using TronWeb. The script provides several key features:
- TronWeb Configuration: Connects to TRON Nile Testnet with proper endpoints
- Contract Interaction: Calls all four functions from your deployed
DataFeedReadercontract - Data Formatting: Handles BigInt values and formats prices with proper decimals
- Error Handling: Comprehensive error handling and troubleshooting guidance
- Multiple Examples: Shows both detailed and simplified price reading patterns
The script includes helper functions for formatting timestamps, prices, and round IDs, plus demonstrates reading from multiple price feeds with structured console output.
View the complete script code: reader.js
Update the Contract Address
Edit offchain/reader.js and replace YOUR_DEPLOYED_CONTRACT_ADDRESS_HERE with your actual contract address from the deployment step.
Run the Price Reader
node offchain/reader.js
Expected Output
When you run the script, you should see output similar to:
๐ Starting Chainlink Data Feed Reader
๐ Network: TRON Nile Testnet
๐ Contract: TTZEzaRUfrSm2ENfkhrPzk5mMEkZVwS3eD
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Reading BTC/USD Price Feed Data...
๐ Feed Address: TD3hrfAtPcnkLSsRh4UTgjXBo6KyRfT1AR
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ BTC / USD
๐ฐ Current Price: $104,569.43
๐ข Raw Price Value: 10456943400942
๐ Decimals: 8
๐ Round ID: 18446744073709556842
๐ Started At: 6/4/2025, 9:00:21 AM
๐ Updated At: 6/4/2025, 9:00:24 AM
โ
Answered In Round: 18446744073709556842
โฐ Data Age: 65 minutes ago
๐ Reading ETH/USD Price Feed Data...
๐ Feed Address: TYaLVmqGzz33ghKEMTdC64dUnde5LZc6Y3
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ ETH / USD
๐ฐ Current Price: $2,638.74
๐ข Raw Price Value: 263874000000
๐ Decimals: 8
๐ Round ID: 18446744073709564831
๐ Started At: 6/4/2025, 9:58:19 AM
๐ Updated At: 6/4/2025, 9:58:24 AM
โ
Answered In Round: 18446744073709564831
โฐ Data Age: 7 minutes ago
Understanding the Results
Each price feed returns several important pieces of data:
- Current Price: The formatted price in USD
- Raw Price Value: The unformatted price value from the contract
- Decimals: Number of decimal places to properly format the price
- Round ID: Unique identifier for this price update
- Timestamps: When the price round started and was last updated
- Data Age: How long ago the price was last updated
Next Steps
Explore More Data Feeds
Now that you have successfully deployed and interacted with Chainlink Data Feeds on TRON, you can:
- Browse All Available Feeds: Check out the complete list of price feeds available on TRON
- Access Historical Data: Getting the latest price is not the only data that aggregators can retrieve. You can also retrieve historical price data. To learn more, see the Historical Price Data page.