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:

  1. Smart Contract Development: Creating and deploying a contract that reads price feeds
  2. 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 tronbox

    Verify 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

  1. Install the TronLink browser extension
  2. Create a new wallet or import an existing one
  3. 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:

  1. Visit https://nileex.io/join/getJoinPage
  2. Enter your TRON wallet address
  3. 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 IDs
  • getLatestPrice(): Returns only the current price for simplified usage
  • getDecimals(): Returns the number of decimal places for proper price formatting
  • getDescription(): 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 PairContract Address
BTC/USDTD3hrfAtPcnkLSsRh4UTgjXBo6KyRfT1AR
ETH/USDTYaLVmqGzz33ghKEMTdC64dUnde5LZc6Y3

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 DataFeedReader contract
  • 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.

What's next

Get the latest Chainlink content straight to your inbox.