Skip to content

Commit

Permalink
Update to new changes in main
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperzuk-neti committed Oct 1, 2024
1 parent d11ffeb commit 618b370
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 130 deletions.
3 changes: 1 addition & 2 deletions src/routers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const axios = require ('axios');
const {BN, BN_ONE, BN_ZERO, BN_MILLION, hexToU8a} = require("@polkadot/util")
const config = require("../../config");
const generateCertificate = require('./generate-certificate')
const { fetchAllCongressSpendings } = require('../utils/spendings');
const { getLastWeekEraPaidEvents } = require("../utils/explorer");
const { getLastWeekEraPaidEvents, fetchAllCongressSpendings } = require("../utils/explorer");
const { stringify } = require('csv-stringify/sync');
const pako = require('pako');

Expand Down
125 changes: 125 additions & 0 deletions src/utils/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const axios = require("axios");
const config = require("../../config");

const CONGRESS_ADDRESS = "5EYCAe5g8CDuMsTief7QBxfvzDFEfws6ueXTUhsbx5V81nGH";

const eraPaidEventsQuery = `
query EraPaidEvents {
events(
Expand Down Expand Up @@ -32,6 +34,129 @@ const getLastWeekEraPaidEvents = async () => {
return data.data.events.nodes.map(v => v.data);
};


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,
getLastWeekEraPaidEvents,
}
128 changes: 0 additions & 128 deletions src/utils/spendings.js

This file was deleted.

0 comments on commit 618b370

Please sign in to comment.