-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphQl.js
157 lines (139 loc) · 4.46 KB
/
graphQl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import db from "./db.js";
const INCOMING = 2;
const OUTGOING = 1;
export async function getBlock(height) {
return db.blocks.findOne({ height });
}
export async function getClosestBlock(timestamp) {
return db.blocks.findOne(
{ timestamp: { $lte: timestamp } },
{ sort: { timestamp: -1 } }
);
}
export async function getAllTransfers(start, end, cid) {
const cursor = await db.indexer.collection("events").find(
{
section: "encointerBalances",
method: "Transferred",
"data.0": cid,
timestamp: { $gte: start, $lte: end },
},
{ sort: { timestamp: 1 } }
);
return await cursor.toArray();
}
export async function getTransfers(start, end, address, cid, direction) {
let query = {
section: "encointerBalances",
method: "Transferred",
"data.0": cid,
timestamp: { $gte: start, $lte: end },
};
query[`data.${direction}`] = address;
const cursor = await db.indexer
.collection("events")
.find(query, { sort: { timestamp: 1 } });
return await cursor.toArray();
}
async function getIssues(start, end, address, cid) {
const cursor = await db.indexer.collection("events").find(
{
section: "encointerBalances",
method: "Issued",
"data.1": address,
"data.0": cid,
timestamp: { $gte: start, $lte: end },
},
{ sort: { timestamp: 1 } }
);
return await cursor.toArray();
}
export async function getAllIssues(cid) {
const cursor = await db.indexer.collection("events").find(
{
section: "encointerBalances",
method: "Issued",
"data.0": cid,
},
{ sort: { timestamp: 1 } }
);
return await cursor.toArray();
}
export async function getRewardsIssueds(cid) {
const cursor = await db.events.find(
{
section: "encointerCeremonies",
method: "RewardsIssued",
"data.0": cid,
},
{ sort: { timestamp: -1 } }
);
return await cursor.toArray();
}
export async function getBlocksByBlockHeights(heights) {
const cursor = await db.indexer.collection("blocks").find({
height: { $in: heights },
});
return await cursor.toArray();
}
export async function getReputableRegistrations(cid) {
const cursor = await db.events.find({
section: "encointerCeremonies",
method: "ParticipantRegistered",
"data.0": cid,
"data.1": { $in: ["Reputable", "Bootstrapper"] },
});
return await cursor.toArray();
}
export async function gatherTransactionData(start, end, address, cid) {
let incoming = await getTransfers(start, end, address, cid, INCOMING);
const outgoing = await getTransfers(start, end, address, cid, OUTGOING);
// hack to exclude cid fuckup transactions
// const excludeEvents = ["1064499-1161", "820314-1", "819843-1", "1064499-275"];
// incoming = incoming.filter((e) => !excludeEvents.includes(e.id));
const issues = await getIssues(start, end, address, cid);
const sumIssues = issues.reduce((acc, cur) => acc + cur.data[2], 0);
const sumIncoming = incoming.reduce((acc, cur) => acc + cur.data[3], 0);
const sumOutgoing = outgoing.reduce((acc, cur) => acc + cur.data[3], 0);
const numDistinctClients = new Set(incoming.map((e) => e.data[1])).size;
return [
incoming,
outgoing,
issues,
sumIncoming,
sumOutgoing,
sumIssues,
numDistinctClients,
];
}
export async function getBlockNumberByTimestamp(timestamp) {
let block = await getClosestBlock(timestamp);
const blockNumber = block.height;
return blockNumber;
}
export async function getTransactionVolume(cid, start, end) {
return (await getAllTransfers(start, end, cid)).reduce(
(acc, cur) => acc + cur.data[3],
0
);
}
const blockCache = {};
export async function getAllBlocksByBlockHeights(heights) {
const result = [];
const remainingHeights = [];
heights.forEach((h) => {
if (h in blockCache) {
result.push(blockCache[h]);
} else {
remainingHeights.push(h);
}
});
for (let i = 0; i < remainingHeights.length; i += 10) {
let blocks = await getBlocksByBlockHeights(
remainingHeights.slice(i, i + 10)
);
blocks.forEach((b) => (blockCache[b.blockHeight] = b));
result.push(...blocks);
}
return result;
}