Skip to content

Commit

Permalink
feat: Improve Address Transaction History export (#853)
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
  • Loading branch information
panteleymonchuk committed Dec 11, 2023
1 parent b8dd40e commit bcd5642
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion api/src/routes/stardust/transactionhistory/download/post.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import JSZip from "jszip";
import moment from "moment";
import { ServiceFactory } from "../../../../factories/serviceFactory";
import logger from "../../../../logger";
import { IDataResponse } from "../../../../models/api/IDataResponse";
import { ITransactionHistoryDownloadBody } from "../../../../models/api/stardust/chronicle/ITransactionHistoryDownloadBody";
import { ITransactionHistoryRequest } from "../../../../models/api/stardust/chronicle/ITransactionHistoryRequest";
import { IOutputDetailsResponse } from "../../../../models/api/stardust/IOutputDetailsResponse";
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { ChronicleService } from "../../../../services/stardust/chronicleService";
import { StardustTangleHelper } from "../../../../utils/stardust/stardustTangleHelper";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
Expand Down Expand Up @@ -42,11 +45,47 @@ export async function post(

const result = await chronicleService.transactionHistoryDownload(request.address, body.targetDate);

const outputDetails: IOutputDetailsResponse[] = await Promise.all(result.items.map(async item =>
StardustTangleHelper.outputDetails(networkConfig, item.outputId)
));

const changeBalanceByTransactionId = new Map<string, { balance: number; timestamp: number }>();

for (const details of outputDetails) {
const metadata = details.output.metadata;
const amount = Number(details.output.output.amount);
const timestamp = metadata.isSpent ? metadata.milestoneTimestampSpent : metadata.milestoneTimestampBooked;
const transactionId = metadata.isSpent ? metadata.transactionIdSpent : metadata.transactionId;

const initTransactionInfo = {
balance: 0,
timestamp: 0
};
if (!changeBalanceByTransactionId.has(transactionId)) {
changeBalanceByTransactionId.set(transactionId, initTransactionInfo);
}
const prev = changeBalanceByTransactionId.get(transactionId);
prev.balance = metadata?.isSpent ? prev.balance - amount : prev.balance + amount;
prev.timestamp = Math.max(timestamp * 1000, prev.timestamp);
changeBalanceByTransactionId.set(transactionId, prev);
}

const headers = ["Timestamp", "TransactionId", "Balance changes"];

let csvContent = `${headers.join(",")}\n`;

for (const key of changeBalanceByTransactionId.keys()) {
const value = changeBalanceByTransactionId.get(key);
const row = [moment(value.timestamp).format("YYYY-MM-DD HH:mm:ss"), key, value.balance].join(",");
csvContent += `${row}\n`;
}


const jsZip = new JSZip();
let response: IDataResponse = null;

try {
jsZip.file("history.json", JSON.stringify(result));
jsZip.file("history.csv", csvContent);
const content = await jsZip.generateAsync({ type: "nodebuffer" });

response = {
Expand All @@ -60,3 +99,4 @@ export async function post(
return response;
}


0 comments on commit bcd5642

Please sign in to comment.