Skip to content

Commit

Permalink
chore: Identify 'outputId' within genesis block during the export of …
Browse files Browse the repository at this point in the history
…transactions

Signed-off-by: Eugene Panteleymonchuk <[email protected]>
  • Loading branch information
panteleymonchuk committed Feb 14, 2024
1 parent 3ce0422 commit 1ec092e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/src/models/db/networkType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
const LEGACY_MAINNET = "legacy-mainnet";
const CHRYSALIS_MAINNET = "chrysalis-mainnet";
const MAINNET = "mainnet";
export const MAINNET = "mainnet";
const DEVNET = "devnet";
export const SHIMMER = "shimmer";
const TESTNET = "testnet";
Expand Down
40 changes: 37 additions & 3 deletions api/src/routes/stardust/transactionhistory/download/post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OutputResponse, INodeInfoBaseToken, CommonOutput } from "@iota/sdk";
import { CommonOutput, INodeInfoBaseToken, OutputResponse } from "@iota/sdk";
import JSZip from "jszip";
import moment from "moment";
import { ServiceFactory } from "../../../../factories/serviceFactory";
Expand All @@ -8,6 +8,8 @@ import { ITransactionHistoryDownloadBody } from "../../../../models/api/stardust
import { ITransactionHistoryRequest } from "../../../../models/api/stardust/chronicle/ITransactionHistoryRequest";
import { ITransactionHistoryItem } from "../../../../models/api/stardust/chronicle/ITransactionHistoryResponse";
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { INetwork } from "../../../../models/db/INetwork";
import { MAINNET } from "../../../../models/db/networkType";
import { STARDUST } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { ChronicleService } from "../../../../services/stardust/chronicleService";
Expand All @@ -20,6 +22,7 @@ export interface ITransactionHistoryRecord {
isGenesisByDate: boolean;
isSpent: boolean;
transactionId: string;
outputIdFromGenesis?: string;
timestamp: number;
dateFormatted: string;
balanceChange: number;
Expand Down Expand Up @@ -73,14 +76,18 @@ export async function post(
});
const tokenInfo = nodeInfoService.getNodeInfo().baseToken;

const transactions = getTransactionHistoryRecords(groupOutputsByTransactionId(fulfilledOutputs), tokenInfo);
const transactions = getTransactionHistoryRecords(groupOutputsByTransactionId(fulfilledOutputs), tokenInfo, networkConfig);

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

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

for (const transaction of transactions) {
const row = [transaction.dateFormatted, transaction.transactionId, transaction.balanceChangeFormatted].join(",");
const row = [
transaction.dateFormatted,
transaction.outputIdFromGenesis ? `Stardust Genesis output: ${transaction.outputIdFromGenesis}` : transaction.transactionId,
transaction.balanceChangeFormatted,
].join(",");
csvContent += `${row}\n`;
}

Expand Down Expand Up @@ -140,6 +147,7 @@ export const groupOutputsByTransactionId = (outputsWithDetails: OutputWithDetail
export const getTransactionHistoryRecords = (
transactionIdToOutputs: Map<string, OutputWithDetails[]>,
tokenInfo: INodeInfoBaseToken,
networkConfig: INetwork,
): ITransactionHistoryRecord[] => {
const calculatedTransactions: ITransactionHistoryRecord[] = [];

Expand All @@ -149,12 +157,15 @@ export const getTransactionHistoryRecords = (

const isGenesisByDate = outputs.map((t) => t.milestoneTimestamp).includes(0);

const outputIdFromGenesis = getStardustGenesisOutputId(outputs, networkConfig, transactionId);

const isSpent = balanceChange <= 0;

calculatedTransactions.push({
isGenesisByDate,
isSpent,
transactionId,
outputIdFromGenesis,
timestamp: lastOutputTime,
dateFormatted: moment(lastOutputTime * 1000).format("YYYY-MM-DD HH:mm:ss"),
balanceChange,
Expand All @@ -165,6 +176,29 @@ export const getTransactionHistoryRecords = (
return calculatedTransactions;
};

/**
* Get the output id from the stardust genesis.
* @param outputs List of outputs related to transaction
* @param networkConfig Network configuration
* @param transactionId Current trancaction
* @returns The output id from the stardust genesis or undefined.
*/
function getStardustGenesisOutputId(outputs: OutputWithDetails[], networkConfig: INetwork, transactionId: string): string | undefined {
const STARDUST_GENESIS_MILESTONE = 7669900;
const STARDUST_SUPPLY_INCREASE_OUTPUT_TICKER = "0xb191c4bc825ac6983789e50545d5ef07a1d293a98ad974fc9498cb18";

const outputFromStardustGenesis = outputs.find((output) => {
if (
networkConfig.network === MAINNET &&
output.milestoneIndex === STARDUST_GENESIS_MILESTONE &&
transactionId.includes(STARDUST_SUPPLY_INCREASE_OUTPUT_TICKER)
) {
return true;
}
});
return outputFromStardustGenesis?.outputId;
}

export const calculateBalanceChange = (outputs: OutputWithDetails[]) => {
let totalAmount = 0;

Expand Down

0 comments on commit 1ec092e

Please sign in to comment.