Add Pricing Data

This section focuses on retrieving individual prices for each token in our previously-saved transactions.

Script to Add Pricing Data

Create a new file addPricingData.js and add the following code:

require('dotenv').config();
const axios = require('axios');
const fs = require('fs');

const transactions = JSON.parse(fs.readFileSync('transactions.json', 'utf-8'));

const fetchPrice = async (chain, tokenAddress, blockNumber) => {
    try {
        const response = await axios.get(
            `https://pricing.noves.fi/evm/${chain}/price/${tokenAddress}`,
            {
                params: { blockNumber },
                headers: { apiKey: `${process.env.NOVES_API_KEY}` },
            }
        );
        return response.data.price;
    } catch (error) {
        console.error(`Error fetching price for ${tokenAddress}:`, error);
    }
};

const enrichWithPrice = async (tx, chain) => {
  for (const item of tx.classificationData.sent.concat(tx.classificationData.received)) {
    const token = item.token;
    const priceData = await fetchPrice(chain, token.address, tx.rawTransactionData.blockNumber);
    item.price = priceData;
  }
};

const addPricingData = async () => {
  for (const tx of transactions) {
    await enrichWithPrice(tx, tx.chain);
  }

  fs.writeFileSync('transactions_with_prices.json', JSON.stringify(transactions, null, 2));
};

addPricingData().then(() => {
  console.log('Transactions enriched with pricing data.');
});

This script reads the transactions from the transactions.json file, iterates over each transaction, and fetches the price for each token involved.

The fetched prices are added to the transaction data, and finally, the enriched transactions are saved to a new file, transactions_with_prices.json.

We're done with pricing!

In the final step, we'll learn how to turn this data into an export that fits your requirements.