-
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.
- Loading branch information
Showing
44 changed files
with
1,164 additions
and
296 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
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,29 @@ | ||
name: PR test check | ||
|
||
on: | ||
pull_request: | ||
types: [opened, reopened, synchronize] | ||
|
||
jobs: | ||
build-check: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
project: [client] | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ${{ matrix.project }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: "16" | ||
|
||
- name: Install Dependencies | ||
run: npm install | ||
|
||
- name: Tests Check | ||
run: npm run test run |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "explorer-api", | ||
"description": "API for Tangle Explorer", | ||
"version": "3.3.3-rc.1", | ||
"version": "3.3.4-rc.1", | ||
"author": "Martyn Janes <[email protected]>", | ||
"repository": { | ||
"type": "git", | ||
|
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
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: string = 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.formatUnits(Number(addressBalanceChange), "Mi"), | ||
}; | ||
} |
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
Oops, something went wrong.