Skip to content

Commit a4a7ec0

Browse files
aelmanaakhadni
andauthored
Tron data feeds (#2711)
* update tron data feeds * update tron data feeds * nit --------- Co-authored-by: Karim <98668332+khadni@users.noreply.github.com>
1 parent 9090c38 commit a4a7ec0

3 files changed

Lines changed: 247 additions & 5 deletions

File tree

src/config/sidebar.ts

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

src/content/data-feeds/using-data-feeds.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,3 @@ If you require a denomination other than what is provided, you can use two data
9898
## More aggregator functions
9999

100100
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.
101-
102-
To understand different use cases for Chainlink Price Feeds, refer to [Other Tutorials](/getting-started/other-tutorials).

0 commit comments

Comments
 (0)