|
| 1 | +--- |
| 2 | +section: dataFeeds |
| 3 | +date: Last Modified |
| 4 | +title: "Using Data Feeds on Tron" |
| 5 | +isIndex: true |
| 6 | +whatsnext: { "Tron Price Feed Addresses": "/data-feeds/price-feeds/addresses?network=tron" } |
| 7 | +metadata: |
| 8 | + description: "Complete step-by-step tutorial for integrating Chainlink Data Feeds on TRON blockchain. Learn to deploy smart contracts with TronBox, read real-time price data using TronWeb, and build decentralized applications with reliable oracle data on TRON Nile Testnet." |
| 9 | + excerpt: "TRON blockchain tutorial, Chainlink price feeds, TronBox deployment, TronWeb integration, smart contract development, oracle data, cryptocurrency prices, BTC/USD, ETH/USD, decentralized finance, DeFi development, blockchain oracles, TRON testnet, JavaScript Web3" |
| 10 | +--- |
| 11 | + |
| 12 | +import { Aside, CopyText } from "@components" |
| 13 | + |
| 14 | +This tutorial demonstrates how to read Chainlink Data Feeds on the TRON blockchain. You will learn to deploy a smart contract on TRON Nile Testnet using [TronBox](https://tronbox.io/) and interact with it offchain using [TronWeb](https://tronweb.network/). |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +Chainlink Data Feeds on TRON provide reliable, decentralized price data for your applications. This tutorial covers: |
| 19 | + |
| 20 | +1. **Smart Contract Development**: Creating and deploying a contract that reads price feeds |
| 21 | +1. **Offchain Interaction**: Using TronWeb to interact with your deployed contract |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +Before you begin, ensure you have: |
| 28 | + |
| 29 | +- **Knowledge of JavaScript and Solidity** |
| 30 | +- **Node.js 20 or higher** - [Install the latest release](https://nodejs.org/en/download/). Optionally, use [nvm](https://github.com/nvm-sh/nvm/blob/master/README.md) to switch between Node.js versions with <CopyText text="nvm use 20" code/> |
| 31 | + ```bash |
| 32 | + $ node -v |
| 33 | + v20.11.0 |
| 34 | + ``` |
| 35 | +- **TronBox** (version 3.3 or higher) - Install globally with <CopyText text="npm install -g tronbox" code/> |
| 36 | + |
| 37 | + Verify your installation: |
| 38 | + |
| 39 | + ```bash |
| 40 | + $ tronbox version |
| 41 | + Tronbox v4.2.0 |
| 42 | + Solidity v0.8.23 (tron-solc) |
| 43 | + ``` |
| 44 | + |
| 45 | +- **npm** package manager |
| 46 | +- **A TRON compatible Web3 wallet** (e.g., [TronLink](https://www.tronlink.org/)) |
| 47 | + |
| 48 | +### Setting Up Your TRON Wallet |
| 49 | + |
| 50 | +1. Install the TronLink browser extension |
| 51 | +1. Create a new wallet or import an existing one |
| 52 | +1. Export your private key (you will need this for deploying your smart contract): |
| 53 | + - Open TronLink wallet extension |
| 54 | + - Open the **Wallet Management** menu |
| 55 | + - Select **Export Account** |
| 56 | + - Select **Private Key** |
| 57 | + - Enter your password |
| 58 | + - Copy your private key |
| 59 | + |
| 60 | +<Aside type="caution" title="Private Key Security"> |
| 61 | + Never share your private key or commit it to version control. Store it securely and only use testnet accounts for |
| 62 | + development. |
| 63 | +</Aside> |
| 64 | + |
| 65 | +### Getting Test Tokens |
| 66 | + |
| 67 | +Fund your wallet with test TRX from the TRON Nile Testnet faucet: |
| 68 | + |
| 69 | +1. Visit [https://nileex.io/join/getJoinPage](https://nileex.io/join/getJoinPage) |
| 70 | +1. Enter your TRON wallet address |
| 71 | +1. Complete the verification and receive 2000 test TRX |
| 72 | + |
| 73 | +## Setup |
| 74 | + |
| 75 | +### Clone the Example Repository |
| 76 | + |
| 77 | +<CopyText |
| 78 | + text="git clone https://github.com/smartcontractkit/smart-contract-examples.git && cd smart-contract-examples/data-feeds/tron/getting-started" |
| 79 | + code |
| 80 | +/> |
| 81 | + |
| 82 | +### Install Dependencies |
| 83 | + |
| 84 | +<CopyText text="npm install" code /> |
| 85 | + |
| 86 | +### Configure Environment |
| 87 | + |
| 88 | +Create a `.env` file by copying the sample: |
| 89 | + |
| 90 | +<CopyText text="cp sample-env .env" code /> |
| 91 | + |
| 92 | +Edit the `.env` file and add your private key: |
| 93 | + |
| 94 | +```bash |
| 95 | +export PRIVATE_KEY_NILE=your_nile_testnet_private_key_here |
| 96 | +``` |
| 97 | + |
| 98 | +## Part 1: Smart Contract Development and Deployment |
| 99 | + |
| 100 | +### Understanding the Smart Contract |
| 101 | + |
| 102 | +The `DataFeedReader.sol` contract provides four key functions to interact with Chainlink Data Feeds on TRON: |
| 103 | + |
| 104 | +- **`getChainlinkDataFeedLatestAnswer()`**: Returns complete round data including price, timestamps, and round IDs |
| 105 | +- **`getLatestPrice()`**: Returns only the current price for simplified usage |
| 106 | +- **`getDecimals()`**: Returns the number of decimal places for proper price formatting |
| 107 | +- **`getDescription()`**: Returns human-readable feed description (e.g., "BTC / USD") |
| 108 | + |
| 109 | +The contract uses the [`AggregatorV3Interface`](/data-feeds/api-reference#aggregatorv3interface) to interact with Chainlink data feeds. |
| 110 | + |
| 111 | +**View the complete contract code**: [DataFeedReader.sol](https://github.com/smartcontractkit/smart-contract-examples/blob/32bb95e558bdd6a4ebab4b6c1eadbfc83668c539/data-feeds/tron/getting-started/contracts/DataFeedReader.sol) |
| 112 | + |
| 113 | +### Price Feeds Used in This Tutorial |
| 114 | + |
| 115 | +This tutorial uses the following price feeds on TRON Nile Testnet: |
| 116 | + |
| 117 | +| Asset Pair | Contract Address | |
| 118 | +| ---------- | ------------------------------------ | |
| 119 | +| BTC/USD | `TD3hrfAtPcnkLSsRh4UTgjXBo6KyRfT1AR` | |
| 120 | +| ETH/USD | `TYaLVmqGzz33ghKEMTdC64dUnde5LZc6Y3` | |
| 121 | + |
| 122 | +<Aside type="note" title="More Price Feeds Available"> |
| 123 | + These are just two examples for the tutorial. For a complete list of all available price feeds on TRON, visit the |
| 124 | + [Price Feed Addresses](/data-feeds/price-feeds/addresses?network=tron) page. |
| 125 | +</Aside> |
| 126 | + |
| 127 | +### Compile the Contract |
| 128 | + |
| 129 | +<CopyText text="tronbox compile" code /> |
| 130 | + |
| 131 | +### Deploy to TRON Nile Testnet |
| 132 | + |
| 133 | +Load your environment variables and deploy: |
| 134 | + |
| 135 | +<CopyText text="source .env && tronbox migrate --network nile" code /> |
| 136 | + |
| 137 | +After successful deployment, you will see output similar to: |
| 138 | + |
| 139 | +``` |
| 140 | +Deploying 'DataFeedReader' |
| 141 | +... |
| 142 | +DataFeedReader: TTZEzaRUfrSm2ENfkhrPzk5mMEkZVwS3eD |
| 143 | +``` |
| 144 | + |
| 145 | +<Aside type="note" title="Save Your Contract Address"> |
| 146 | + Copy the deployed contract address - you will need it for the offchain interaction in Part 2. |
| 147 | +</Aside> |
| 148 | + |
| 149 | +## Part 2: Offchain Interaction with TronWeb |
| 150 | + |
| 151 | +### Understanding the TronWeb Script |
| 152 | + |
| 153 | +The `reader.js` script demonstrates how to interact with your deployed contract using [TronWeb](https://tronweb.network/). The script provides several key features: |
| 154 | + |
| 155 | +- **TronWeb Configuration**: Connects to TRON Nile Testnet with proper endpoints |
| 156 | +- **Contract Interaction**: Calls all four functions from your deployed DataFeedReader contract |
| 157 | +- **Data Formatting**: Handles BigInt values and formats prices with proper decimals |
| 158 | +- **Error Handling**: Comprehensive error handling and troubleshooting guidance |
| 159 | +- **Multiple Examples**: Shows both detailed and simplified price reading patterns |
| 160 | + |
| 161 | +The script includes helper functions for formatting timestamps, prices, and round IDs, plus demonstrates reading from multiple price feeds with structured console output. |
| 162 | + |
| 163 | +**View the complete script code**: [reader.js](https://github.com/smartcontractkit/smart-contract-examples/blob/32bb95e558bdd6a4ebab4b6c1eadbfc83668c539/data-feeds/tron/getting-started/offchain/reader.js) |
| 164 | + |
| 165 | +### Update the Contract Address |
| 166 | + |
| 167 | +Edit `offchain/reader.js` and replace `YOUR_DEPLOYED_CONTRACT_ADDRESS_HERE` with your actual contract address from the deployment step. |
| 168 | + |
| 169 | +### Run the Price Reader |
| 170 | + |
| 171 | +<CopyText text="node offchain/reader.js" code /> |
| 172 | + |
| 173 | +### Expected Output |
| 174 | + |
| 175 | +When you run the script, you should see output similar to: |
| 176 | + |
| 177 | +``` |
| 178 | +🚀 Starting Chainlink Data Feed Reader |
| 179 | +🌐 Network: TRON Nile Testnet |
| 180 | +📋 Contract: TTZEzaRUfrSm2ENfkhrPzk5mMEkZVwS3eD |
| 181 | +════════════════════════════════════════════════════════════ |
| 182 | +
|
| 183 | +🔍 Reading BTC/USD Price Feed Data... |
| 184 | +📍 Feed Address: TD3hrfAtPcnkLSsRh4UTgjXBo6KyRfT1AR |
| 185 | +────────────────────────────────────────────────── |
| 186 | +📊 BTC / USD |
| 187 | +💰 Current Price: $105,191.97 |
| 188 | +🔢 Raw Price Value: 10519197220314 |
| 189 | +📏 Decimals: 8 |
| 190 | +🆔 Round ID: 18446744073709556841 |
| 191 | +🕐 Started At: 04/06/2025 13:55:40 |
| 192 | +🕒 Updated At: 04/06/2025 13:55:45 |
| 193 | +✅ Answered In Round: 18446744073709556841 |
| 194 | +⏰ Data Age: 96 minutes ago |
| 195 | +
|
| 196 | +🔍 Reading ETH/USD Price Feed Data... |
| 197 | +📍 Feed Address: TYaLVmqGzz33ghKEMTdC64dUnde5LZc6Y3 |
| 198 | +────────────────────────────────────────────────── |
| 199 | +📊 ETH / USD |
| 200 | +💰 Current Price: $3,845.12 |
| 201 | +🔢 Raw Price Value: 384512000000 |
| 202 | +📏 Decimals: 8 |
| 203 | +🆔 Round ID: 18446744073709556789 |
| 204 | +🕐 Started At: 04/06/2025 13:54:15 |
| 205 | +🕒 Updated At: 04/06/2025 13:54:20 |
| 206 | +✅ Answered In Round: 18446744073709556789 |
| 207 | +⏰ Data Age: 98 minutes ago |
| 208 | +``` |
| 209 | + |
| 210 | +## Understanding the Results |
| 211 | + |
| 212 | +Each price feed returns several important pieces of data: |
| 213 | + |
| 214 | +- **Current Price**: The formatted price in USD |
| 215 | +- **Raw Price Value**: The unformatted price value from the contract |
| 216 | +- **Decimals**: Number of decimal places to properly format the price |
| 217 | +- **Round ID**: Unique identifier for this price update |
| 218 | +- **Timestamps**: When the price round started and was last updated |
| 219 | +- **Data Age**: How long ago the price was last updated |
| 220 | + |
| 221 | +## Next Steps |
| 222 | + |
| 223 | +### Explore More Data Feeds |
| 224 | + |
| 225 | +Now that you have successfully deployed and interacted with Chainlink Data Feeds on TRON, you can: |
| 226 | + |
| 227 | +- **Browse All Available Feeds**: Check out the complete list of [price feeds available on TRON](/data-feeds/price-feeds/addresses?network=tron) |
| 228 | +- **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](/data-feeds/historical-data) page. |
| 229 | + |
| 230 | +### Learn Advanced Use Cases |
| 231 | + |
| 232 | +To understand different use cases for Chainlink Price Feeds, refer to [Other Tutorials](/getting-started/other-tutorials). |
0 commit comments