Export To CSV

Now that we have our transactions enriched with pricing data, let's prepare a CSV export.

This will make it easier to analyze and use for various purposes like running calcs on a spreadsheet.

Script for CSV Export

First, make sure you have the following dependency installed:

npm install json2csv

Now, create a file called exportToCSV.js and add the following code:

const fs = require('fs');
const { Parser } = require('json2csv');

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

const csv_columns_with_prices = [
    'Chain',
    'Transaction Hash',
    'Date',
    'Account Address',
    'Transaction Type',
    'Description',
    'Asset Sent Address',
    'Asset Sent Amount',
    'Asset Sent USD Price',
    'Asset Sent USD Value',
    'Asset Sent Action',
    'Asset Sent Name',
    'Asset Sent Symbol',
    'Asset Sent Type',
    'Asset Received Address',
    'Asset Received Amount',
    'Asset Received USD Price',
    'Asset Received USD Value',
    'Asset Received Action',
    'Asset Received Name',
    'Asset Received Symbol',
    'Asset Received Type'
];

const formatTransactionForCSV = (tx) => {
  const formattedSent = tx.classificationData.sent.map(sent => ({
    Chain: tx.chain,
    'Transaction Hash': tx.rawTransactionData.transactionHash,
    Date: new Date(tx.rawTransactionData.timestamp * 1000).toISOString(),
    'Account Address': tx.accountAddress,
    'Transaction Type': tx.classificationData.type,
    Description: tx.classificationData.description,
    'Asset Sent Address': sent.token.address,
    'Asset Sent Amount': sent.amount,
    'Asset Sent USD Price': sent.price ? sent.price.amount : '',
    'Asset Sent USD Value': sent.price ? (parseFloat(sent.amount) * parseFloat(sent.price.amount)).toFixed(2) : '',
    'Asset Sent Action': sent.action,
    'Asset Sent Name': sent.token.name,
    'Asset Sent Symbol': sent.token.symbol,
    'Asset Sent Type': sent.token.decimals,
    'Asset Received Address': '',
    'Asset Received Amount': '',
    'Asset Received USD Value': '',
    'Asset Received Action': '',
    'Asset Received Name': '',
    'Asset Received Symbol': '',
    'Asset Received Type': ''
  });

  const formattedReceived = tx.classificationData.received.map(received => ({
    Chain: tx.chain,
    'Transaction Hash': tx.rawTransactionData.transactionHash,
    Date: new Date(tx.rawTransactionData.timestamp * 1000).toISOString(),
    'Account Address': tx.accountAddress,
    'Transaction Type': tx.classificationData.type,
    Description: tx.classificationData.description,
    'Asset Sent Address': '',
    'Asset Sent Amount': '',
    'Asset Sent USD Value': '',
    'Asset Sent Action': '',
    'Asset Sent Name': '',
    'Asset Sent Symbol': '',
    'Asset Sent Type': '',
    'Asset Received Address': received.token.address,
    'Asset Received Amount': received.amount,
    'Asset Received USD Price': received.price ? received.price.amount : '',
    'Asset Received USD Value': received.price ? (parseFloat(received.amount) * parseFloat(received.price.amount)).toFixed(2) : '',
    'Asset Received Action': received.action,
    'Asset Received Name': received.token.name,
    'Asset Received Symbol': received.token.symbol,
    'Asset Received Type': received.token.decimals
  }));

  return [...formattedSent, ...formattedReceived];
};

const csvData = transactions.map(formatTransactionForCSV).flat();

const parser = new Parser({ fields: csv_columns_with_prices });
const csv = parser.parse(csvData);

fs.writeFileSync('transactions.csv', csv);

console.log('CSV file created successfully.');

This script reads the enriched transactions, formats each transaction for CSV output, and then uses the json2csv library to convert the data into CSV format.

The resulting file, transactions.csv, will contain detailed transaction data including asset prices in USD.

Note that when calculating the total USD value of each asset, we are defaulting to a rounding precision of 2 decimal places. You can change this as needed.

That's it!

You now have a fully working sample for:

  1. Pulling transaction data
  2. Enriching it with USD prices
  3. Exporting it to an accounting-friendly CSV format