-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: download report for Chrysalis.
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
- Loading branch information
1 parent
e2a6662
commit 1487a2b
Showing
10 changed files
with
440 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
api/src/routes/chrysalis/transactionhistory/download/post.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { UnitsHelper } from "@iota/iota.js"; | ||
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 { IConfiguration } from "../../../../models/configuration/IConfiguration"; | ||
import { CHRYSALIS } from "../../../../models/db/protocolVersion"; | ||
import { NetworkService } from "../../../../services/networkService"; | ||
import { ChrysalisTangleHelper } from "../../../../utils/chrysalis/chrysalisTangleHelper"; | ||
import { ValidationHelper } from "../../../../utils/validationHelper"; | ||
|
||
interface IParsedRow { | ||
messageId: string; | ||
transactionId: string; | ||
referencedByMilestoneIndex: string; | ||
milestoneTimestampReferenced: string; | ||
timestampFormatted: string; | ||
ledgerInclusionState: string; | ||
conflictReason: string; | ||
inputsCount: string; | ||
outputsCount: string; | ||
addressBalanceChange: string; | ||
addressBalanceChangeFormatted: string; | ||
} | ||
|
||
/** | ||
* Download the transaction history from chronicle stardust. | ||
* @param _ The configuration. | ||
* @param request The request. | ||
* @param body The request body | ||
* @returns The response. | ||
*/ | ||
export async function post( | ||
_: IConfiguration, | ||
request: ITransactionHistoryRequest, | ||
body: ITransactionHistoryDownloadBody, | ||
): Promise<IDataResponse | null> { | ||
const networkService = ServiceFactory.get<NetworkService>("network"); | ||
ValidationHelper.oneOf(request.network, networkService.networkNames(), "network"); | ||
|
||
const networkConfig = networkService.get(request.network); | ||
|
||
if (networkConfig.protocolVersion !== CHRYSALIS || !networkConfig.permaNodeEndpoint || !request.address) { | ||
return null; | ||
} | ||
|
||
const transactionHistoryDownload = await ChrysalisTangleHelper.transactionHistoryDownload(networkConfig, request.address); | ||
|
||
const parsed = parseResponse(transactionHistoryDownload); | ||
|
||
let csvContent = `${["Timestamp", "TransactionId", "Balance changes"].join(",")}\n`; | ||
|
||
const filtered = parsed.body.filter((row) => { | ||
return moment(row.milestoneTimestampReferenced).isAfter(body.targetDate); | ||
}); | ||
|
||
for (const i of filtered) { | ||
const row = [i.timestampFormatted, i.transactionId, i.addressBalanceChangeFormatted].join(","); | ||
csvContent += `${row}\n`; | ||
} | ||
|
||
const jsZip = new JSZip(); | ||
let response: IDataResponse = null; | ||
|
||
try { | ||
jsZip.file("history.csv", csvContent); | ||
const content = await jsZip.generateAsync({ type: "nodebuffer" }); | ||
|
||
response = { | ||
data: content, | ||
contentType: "application/octet-stream", | ||
}; | ||
} catch (e) { | ||
logger.error(`Failed to zip transaction history for download. Cause: ${e}`); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
/** | ||
* Split response into lines, format each line | ||
* @param response The response from endpoint to parse. | ||
* @returns Object with headers and body. | ||
*/ | ||
function parseResponse(response: string) { | ||
const lines = response.split("\n"); | ||
let isHeadersSet = false; | ||
let headers: IParsedRow; // Headers: "MessageID", "TransactionID", "ReferencedByMilestoneIndex", "MilestoneTimestampReferenced", "LedgerInclusionState", "ConflictReason", "InputsCount", "OutputsCount", "AddressBalanceChange" | ||
const body: IParsedRow[] = []; | ||
|
||
for (const line of lines) { | ||
const row = parseRow(line); | ||
|
||
if (row) { | ||
if (isHeadersSet) { | ||
body.push(row); | ||
} else { | ||
headers = row; | ||
isHeadersSet = true; | ||
} | ||
} | ||
} | ||
|
||
return { headers, body }; | ||
} | ||
|
||
/** | ||
* @param row The row to parse. | ||
* @returns Object with parsed and formatted values. | ||
*/ | ||
function parseRow(row: string): IParsedRow { | ||
const cols = row.split(","); | ||
if (!cols || cols.length < 9) { | ||
return null; | ||
} | ||
|
||
const [ | ||
messageId, | ||
transactionId, | ||
referencedByMilestoneIndex, | ||
milestoneTimestampReferenced, | ||
ledgerInclusionState, | ||
conflictReason, | ||
inputsCount, | ||
outputsCount, | ||
addressBalanceChange, | ||
] = cols; | ||
|
||
const timestamp = milestoneTimestampReferenced.replaceAll("\"", ""); | ||
|
||
return { | ||
messageId, | ||
transactionId, | ||
referencedByMilestoneIndex, | ||
milestoneTimestampReferenced: timestamp, | ||
timestampFormatted: moment(timestamp).format("YYYY-MM-DD HH:mm:ss"), | ||
ledgerInclusionState, | ||
conflictReason, | ||
inputsCount, | ||
outputsCount, | ||
addressBalanceChange, | ||
addressBalanceChangeFormatted: UnitsHelper.formatBest(Number(addressBalanceChange)), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
@import "../../../scss/fonts"; | ||
@import "../../../scss/mixins"; | ||
@import "../../../scss/media-queries"; | ||
@import "../../../scss/variables"; | ||
@import "../../../scss/themes"; | ||
|
||
.download-modal { | ||
display: inline-block; | ||
|
||
button { | ||
border: none; | ||
background-color: transparent; | ||
|
||
&:focus { | ||
box-shadow: none; | ||
} | ||
} | ||
|
||
.modal--icon { | ||
span { | ||
color: #b0bfd9; | ||
font-size: 20px; | ||
} | ||
} | ||
|
||
.modal--bg { | ||
position: fixed; | ||
z-index: 2000; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100vh; | ||
background: rgba(19, 31, 55, 0.75); | ||
} | ||
|
||
.modal--content { | ||
position: fixed; | ||
z-index: 3000; | ||
top: 50%; | ||
left: 50%; | ||
width: 100%; | ||
max-width: 660px; | ||
max-height: 100%; | ||
padding: 32px; | ||
transform: translate(-50%, -50%); | ||
border: 1px solid #e8eefb; | ||
border-radius: 6px; | ||
background-color: var(--body-background); | ||
box-shadow: 0 4px 8px rgba(19, 31, 55, 0.04); | ||
|
||
@include tablet-down { | ||
width: 100%; | ||
overflow-y: auto; | ||
} | ||
|
||
.modal--header { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
padding-bottom: 22px; | ||
border-bottom: 1px solid #e8eefb; | ||
letter-spacing: 0.02em; | ||
|
||
.modal--title { | ||
@include font-size(20px); | ||
|
||
color: var(--body-color); | ||
font-family: $metropolis; | ||
font-weight: 600; | ||
} | ||
|
||
button { | ||
color: var(--body-color); | ||
} | ||
} | ||
|
||
.modal--body { | ||
margin-top: 24px; | ||
|
||
.input-container { | ||
width: 100%; | ||
|
||
.date-label { | ||
color: var(--body-color); | ||
font-family: $inter; | ||
font-weight: 500; | ||
margin-bottom: 8px; | ||
margin-right: 8px; | ||
} | ||
} | ||
|
||
.confirm-button { | ||
cursor: pointer; | ||
margin: 24px 0 8px 0; | ||
align-self: center; | ||
width: fit-content; | ||
padding: 8px 12px; | ||
border: 1px solid #ccc; | ||
color: $mint-green-7; | ||
|
||
&.disabled { | ||
color: $mint-green-1; | ||
} | ||
|
||
.spinner-container { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
width: 57px; | ||
height: 16px; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.