Skip to content

Commit

Permalink
Parse new format of remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperzuk-neti committed Sep 27, 2024
1 parent 7ab1976 commit f82c869
Showing 1 changed file with 79 additions and 13 deletions.
92 changes: 79 additions & 13 deletions src/routers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const router = require("express").Router();
const wrap = require("express-async-handler");
const { ApiPromise, WsProvider, Keyring } = require("@polkadot/api");
const axios = require ('axios');
const {BN, BN_ONE} = require("@polkadot/util")
const {BN, BN_ONE, hexToU8a} = require("@polkadot/util")
const config = require("../../config");
const generateCertificate = require('./generate-certificate')
const { fetchAllCongressSpendings } = require('../utils/spendings');
const { stringify } = require('csv-stringify/sync');
const pako = require('pako');


const provider = new WsProvider(config.RPC_NODE_URL);
Expand All @@ -28,6 +29,16 @@ const apiPromise = ApiPromise.create({
name: "Text",
purpose: "Text",
},
RemarkInfo: {
category: 'Text',
project: 'Text',
supplier: 'Text',
description: 'Text',
finalDestination: 'Text',
amountInUSDAtDateOfPayment: 'u64',
date: 'u64',
currency: 'Text',
},
},
});

Expand Down Expand Up @@ -331,24 +342,79 @@ router.get(
);

router.get(
"/congress_spendings",
"/congress-spendings",
wrap(async (req, res) => {
try {
const allSpendings = await fetchAllCongressSpendings();
const csv = stringify(
const api = await apiPromise;
const csv = stringify([
[
["Timestamp", "Block Number", "Recipient", "Asset", "Value", "Remark"],
...allSpendings.map(v => [v.block.timestamp, v.block.number, v.toId, v.asset, v.value, v.remark])
]
);
res
.set('Content-Disposition', 'attachment; filename="congress-spendings.csv"')
"Timestamp",
"Block Number",
"Recipient",
"Asset",
"Value",
"Category",
"Project",
"Supplier",
"Description",
"Final Destination",
"Amount In USD At Date Of Payment",
"Date",
"Currency",
"Text Remark",
"Raw Remark",
],
...allSpendings.map((v) => {
let parsedRemark;
let textRemark;
try {
if (v.remark) {
const compressedData = hexToU8a(v.remark);
const decompressed = pako.inflate(compressedData);
parsedRemark = api
.createType("RemarkInfo", decompressed)
.toJSON();
parsedRemark.date = new Date(
parsedRemark.date
).toISOString();
}
} catch (e) {
console.log(e);
textRemark = Buffer.from(
v.remark.substring(2),
"hex"
).toString("utf-8");
}
return [
v.block.timestamp,
v.block.number,
v.toId,
v.asset,
v.value,
parsedRemark?.category ?? "-",
parsedRemark?.project ?? "-",
parsedRemark?.supplier ?? "-",
parsedRemark?.description ?? "-",
parsedRemark?.finalDestination ?? "-",
parsedRemark?.amountInUSDAtDateOfPayment ?? "-",
parsedRemark?.date ?? "-",
parsedRemark?.currency ?? "-",
textRemark ?? "-",
v.remark,
];
}),
]);
res.set(
"Content-Disposition",
'attachment; filename="congress-spendings.csv"'
)
.status(200)
.send(csv)
} catch(e) {
res.status(400).json({ error: e.message })
.send(csv);
} catch (e) {
res.status(400).json({ error: e.message });
}
})
)
);

module.exports = router;

0 comments on commit f82c869

Please sign in to comment.