Skip to content

Commit

Permalink
Endpoint for fetching congress spendings as CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperzuk-neti committed Aug 21, 2024
1 parent 7b1226e commit 7ab1976
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
},
CENTRALIZED_API_URL: "http://localhost:8010",
IS_LIVE: false,
ONBOARDER_PHRASE: 'REPLACE ME WITH AUTO LLD ONBOARDER ACCOUNT PHRASE'
ONBOARDER_PHRASE: 'REPLACE ME WITH AUTO LLD ONBOARDER ACCOUNT PHRASE',
EXPLORER_API_URL: "http://localhost:3000",
};
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const config = {
LAND_NFTs_ID: 0,
ONBOARDER_PHRASE: 'REPLACE ME WITH AUTO LLD ONBOARDER ACCOUNT PHRASE',
CENTRALIZED_API_URL: "http://localhost:8010",
EXPLORER_API_URL: "http://localhost:3000",
};

try {
Expand Down
3 changes: 2 additions & 1 deletion config.production.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
},
IS_LIVE: true,
CENTRALIZED_API_URL: "https://api.liberland.org",
ONBOARDER_PHRASE: 'REPLACE ME WITH AUTO LLD ONBOARDER ACCOUNT PHRASE'
ONBOARDER_PHRASE: 'REPLACE ME WITH AUTO LLD ONBOARDER ACCOUNT PHRASE',
EXPLORER_API_URL: "https://archive.mainnet.liberland.org",
};
1 change: 1 addition & 0 deletions config.staging.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ module.exports = {
IS_LIVE: true,
CENTRALIZED_API_URL: "https://staging.api.liberland.org",
ONBOARDER_PHRASE: "REPLACE ME WITH AUTO LLD ONBOARDER ACCOUNT PHRASE",
EXPLORER_API_URL: "https://archive.testchain.liberland.org/graphql",
};
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@polkadot/util": "*11.1.2",
"axios": "^1.6.2",
"cors": "^2.8.5",
"csv-stringify": "^6.5.1",
"debug": "^4.3.4",
"ejs": "^3.1.9",
"express": "^4.18.2",
Expand Down
25 changes: 24 additions & 1 deletion src/routers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const axios = require ('axios');
const {BN, BN_ONE} = require("@polkadot/util")
const config = require("../../config");
const generateCertificate = require('./generate-certificate')
const { fetchAllCongressSpendings } = require('../utils/spendings');
const { stringify } = require('csv-stringify/sync');


const provider = new WsProvider(config.RPC_NODE_URL);
Expand Down Expand Up @@ -328,4 +330,25 @@ router.get(
})
);

module.exports = router;
router.get(
"/congress_spendings",
wrap(async (req, res) => {
try {
const allSpendings = await fetchAllCongressSpendings();
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"')
.status(200)
.send(csv)
} catch(e) {
res.status(400).json({ error: e.message })
}
})
)

module.exports = router;
128 changes: 128 additions & 0 deletions src/utils/spendings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const config = require("../../config");

const CONGRESS_ADDRESS = "5EYCAe5g8CDuMsTief7QBxfvzDFEfws6ueXTUhsbx5V81nGH";

async function queryAllPages(query, variables, key) {
let allData = [];
let after = undefined;
while (true) {
const result = await fetch(config.EXPLORER_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ query, variables: { after, ...variables } }),
});
const data = await result.json();
allData.push(data.data[key].nodes);
if (data.data[key].pageInfo.hasNextPage) {
after = data.data[key].pageInfo.endCursor;
} else {
break;
}
}
return allData;
}

async function getLLMSpendings() {
const data = await queryAllPages(
`
query LLM($after: Cursor, $userId: String) {
merits(first: 50, after: $after, filter: { fromId: { equalTo: $userId } }) {
nodes {
id
toId
value
remark
block {
number
timestamp
}
}
pageInfo {
hasNextPage,
endCursor
}
}
}
`,
{ userId: CONGRESS_ADDRESS },
"merits"
);
return data.flat().map((v) => ({ asset: "LLM", ...v }));
}

async function getAssetsSpendings() {
const data = await queryAllPages(
`
query Assets($after: Cursor, $userId: String) {
assetTransfers(first: 50, after: $after, filter: { asset: { notEqualTo: "1" }, fromId: { equalTo: $userId } }) {
nodes {
id
asset
toId
value
remark
block {
number
timestamp
}
}
pageInfo {
hasNextPage,
endCursor
}
}
}
`,
{ userId: CONGRESS_ADDRESS },
"assetTransfers"
);
return data.flat();
}

async function getLLDSpendings() {
const data = await queryAllPages(
`
query LLD($after: Cursor, $userId: String) {
transfers(first: 50, after: $after, filter: { fromId: { equalTo: $userId } }) {
nodes {
id
toId
value
remark
block {
number
timestamp
}
}
pageInfo {
hasNextPage,
endCursor
}
}
}
`,
{ userId: CONGRESS_ADDRESS },
"transfers"
);
return data.flat().map((v) => ({ asset: "LLD", ...v }));
}

async function fetchAllCongressSpendings() {
const allSpendings = [
await getLLDSpendings(),
await getLLMSpendings(),
await getAssetsSpendings(),
]
.flat()
.sort((a, b) =>
parseInt(a.block.number) > parseInt(b.block.number) ? -1 : 1
);

return allSpendings;
}

module.exports = {
fetchAllCongressSpendings,
};

0 comments on commit 7ab1976

Please sign in to comment.