Software Projects
Build A Price Reporter For The TRB Oracles + Dexscreener
Below is a step-by-step instruction guides to how to build a Node.js application that reports prices for TRB (Tellor Oracles) using the specified packages: ethers
, commander
, dotenv
, and axios
to get the price from Dexscreener.
Step 1: Set up the Project
Initialize your Node.js project:
mkdir trb-price-reporter
cd trb-price-reporter
npm init -y
Install required packages:
npm install ethers commander dotenv axios
Step 2: Set Up Environment Variables
Create a .env
file to store your environment variables, such as API keys or addresses. For this example, we’ll just create a placeholder.
DEXSCREENER_API_URL=https://api.dexscreener.io/latest/dex/tokens/
TRB_TOKEN_ADDRESS=0x88df592f8eb5d7bd38bfef7deb0fbc02cf3778a0
Step 3: Create the Application
Create an index.js
file as the entry point of your application.
// index.js
require('dotenv').config();
const { Command } = require('commander');
const axios = require('axios');
const { ethers } = require('ethers');
const program = new Command();
const DEXSCREENER_API_URL = process.env.DEXSCREENER_API_URL;
const TRB_TOKEN_ADDRESS = process.env.TRB_TOKEN_ADDRESS;
// Function to get TRB price from Dexscreener
async function getTrbPrice() {
try {
const response = await axios.get(`${DEXSCREENER_API_URL}${TRB_TOKEN_ADDRESS}`);
const data = response.data;
if (data && data.pairs && data.pairs.length > 0) {
const price = data.pairs[0].priceUsd;
console.log(`The current price of TRB is $${price}`);
} else {
console.error('No data available for TRB token.');
}
} catch (error) {
console.error('Error fetching TRB price:', error.message);
}
}
program
.version('1.0.0')
.description('TRB Price Reporter CLI')
.option('-p, --price', 'Get the current price of TRB', getTrbPrice);
program.parse(process.argv);
Step 4: Run the Application
To run the application and fetch the TRB price, use the following command:
node index.js --price