-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (155 loc) · 7.81 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const { execute, ProjectOverviewDocument, PaymentsDocument, AdditionsDocument, RedemptionsDocument, v1PayoutsDocument, v2PayoutsDocument } = require('./.graphclient');
const { writeFile } = require('fs').promises;
const readline = require('readline');
async function main() {
// Get user input
let ProjectId = +(await question('Project ID? '));
let Pv = `${(await question('Protocol version? '))}`;
let Fiat = `${(await question('Fiat currency? '))}`;
// Grab main project information from Juicebox subgraph
const overview = (await execute(ProjectOverviewDocument, { ProjectId: ProjectId, Pv: Pv }, {})).data.projects[0];
const Id = overview.id;
// Get project name and description from IPFS via Juicebox's Pinata gateway
const metadata = await apifetch(`https://jbx.mypinata.cloud/ipfs/${overview.metadataUri}`);
console.log("\nName: ", metadata.name);
console.log("Description: ", metadata.description);
// Print main project info
console.log(`Handle: ${overview.handle}`);
console.log(`Owner: ${overview.owner}`);
console.log(`Created at: ${new Date(overview.createdAt * 1000)}`);
console.log(`Current balance: ${overview.currentBalance / 1000000000000000000} ETH`);
console.log(`Total paid: ${overview.totalPaid / 1000000000000000000} ETH`);
console.log(`Total redeemed: ${overview.totalRedeemed / 1000000000000000000} ETH\n`);
// Fetch ETH prices from project's creation to current time
console.log("Retreiving historical price data . . .");
const prices = (await apifetch(`https://api.coingecko.com/api/v3/coins/ethereum/market_chart?vs_currency=${Fiat}&days=max&interval=hourly`)).prices;
// Participants
console.log("Evaluating participants . . .");
for(const i in overview.participants) {
overview.participants[i].totalPaid /= 1000000000000000000;
overview.participants[i].balance /= 1000000000000000000;
overview.participants[i].totalPaidFiat = prices[prices.length-1][1]*overview.participants[i].totalPaid;
}
writecsv("./output/participants.csv", array2csv(overview.participants));
// Payments
console.log("Retreiving payments . . .");
let payEvents = (await execute(PaymentsDocument, {Id: Id}, {})).data.projects[0].payEvents;
for(const i in payEvents) {
payEvents[i].amount /= 1000000000000000000;
payEvents[i].timestamp *= 1000;
payEvents[i].amountFiat = (await prices.find(el => el[0] > payEvents[i].timestamp)[1])*payEvents[i].amount;
payEvents[i].date = new Date(payEvents[i].timestamp).toLocaleString();
}
writecsv("./output/payments.csv", array2csv(payEvents));
// Additions to balance
console.log("Retreiving additions to balance . . .");
let addEvents = (await execute(AdditionsDocument, {Id: Id}, {})).data.projects[0].addToBalanceEvents;
for(const i in addEvents) {
addEvents[i].amount /= 1000000000000000000;
addEvents[i].timestamp *= 1000;
addEvents[i].amountFiat = (await prices.find(el => el[0] > addEvents[i].timestamp)[1])*addEvents[i].amount;
addEvents[i].date = new Date(addEvents[i].timestamp).toLocaleString();
}
writecsv("./output/additions.csv", array2csv(addEvents));
// Redemptions
console.log("Retreiving redemptions . . .");
let redeemEvents = (await execute(RedemptionsDocument, {Id: Id}, {})).data.projects[0].redeemEvents;
for(const i in redeemEvents) {
redeemEvents[i].amount /= 1000000000000000000;
redeemEvents[i].returnAmount /= 1000000000000000000;
redeemEvents[i].timestamp *= 1000;
redeemEvents[i].returnAmountFiat = (await prices.find(el => el[0] > redeemEvents[i].timestamp)[1])*redeemEvents[i].returnAmount;
redeemEvents[i].date = new Date(redeemEvents[i].timestamp).toLocaleString();
}
writecsv("./output/redemptions.csv", array2csv(redeemEvents));
// Payouts
console.log("Retreiving payouts . . .");
let payoutsEvents;
if (Pv === "2") {
// Use v2 subgraph schema
payoutsEvents = (await execute(v2PayoutsDocument, {Id: Id}, {})).data.projects[0].distributePayoutsEvents;
// Formatting outputs and fetching fiat prices
for(const i in payoutsEvents) {
payoutsEvents[i].timestamp *= 1000;
let fiatPrice = await prices.find(el => el[0] > payoutsEvents[i].timestamp)[1];
for(const j in payoutsEvents[i].splitDistributions) {
payoutsEvents[i].splitDistributions[j].amount /= 1000000000000000000;
payoutsEvents[i].splitDistributions[j].amountFiat = fiatPrice*payoutsEvents[i].splitDistributions[j].amount;
payoutsEvents[i].splitDistributions[j].date = new Date(payoutsEvents[i].timestamp);
}
payoutsEvents[i].distributedAmount /= 1000000000000000000;
payoutsEvents[i].distributedAmountFiat = fiatPrice*payoutsEvents[i].distributedAmount;
payoutsEvents[i].fee /= 1000000000000000000;
payoutsEvents[i].feeFiat = fiatPrice*payoutsEvents[i].fee;
payoutsEvents[i].beneficiaryDistributionAmount /= 1000000000000000000;
payoutsEvents[i].beneficiaryDistributionAmountFiat = fiatPrice*payoutsEvents[i].beneficiaryDistributionAmount;
payoutsEvents[i].date = new Date(payoutsEvents[i].timestamp).toLocaleString();
}
// Write CSVs to disk
writecsv("./output/splits.csv", array2csv(payoutsEvents.map(el => el.splitDistributions).flat()));
payoutsEvents.forEach(el => delete el.splitDistributions);
writecsv("./output/distributions.csv", array2csv(payoutsEvents))
} else {
// Use v1/1.1 subgraph schema
payoutsEvents = (await execute(v1PayoutsDocument, {Id: Id}, {})).data.projects[0].tapEvents;
// Formatting outputs and fetching fiat prices
for(const i in payoutsEvents) {
payoutsEvents[i].timestamp *= 1000;
let fiatPrice = await prices.find(el => el[0] > payoutsEvents[i].timestamp)[1];
for(const j in payoutsEvents[i].distributions) {
payoutsEvents[i].distributions[j].modCut /= 1000000000000000000;
payoutsEvents[i].distributions[j].modCutFiat = fiatPrice*payoutsEvents[i].distributions[j].modCut;
payoutsEvents[i].distributions[j].date = new Date(payoutsEvents[i].timestamp).toLocaleString();
}
payoutsEvents[i].netTransferAmount /= 1000000000000000000;
payoutsEvents[i].netTransferAmountFiat = fiatPrice*payoutsEvents[i].netTransferAmount;
payoutsEvents[i].govFeeAmount /= 1000000000000000000;
payoutsEvents[i].govFeeAmountFiat = fiatPrice*payoutsEvents[i].govFeeAmount;
payoutsEvents[i].beneficiaryTransferAmount /= 1000000000000000000;
payoutsEvents[i].beneficiaryTransferAmountFiat = fiatPrice*payoutsEvents[i].beneficiaryTransferAmount;
payoutsEvents[i].date = new Date(payoutsEvents[i].timestamp).toLocaleString();
}
// Write CSVs to disk
writecsv("./output/splits.csv", array2csv(payoutsEvents.map(el => el.distributions).flat()));
payoutsEvents.forEach(el => delete el.distributions);
writecsv("./output/distributions.csv", array2csv(payoutsEvents))
}
console.log("\nComplete ✅");
}
function apifetch (url) {
return new Promise(resolve => {
require('https').get(url, (resp)=>{
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Resolve the result.
resp.on('end', () => {
resolve(JSON.parse(data));
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
});
}
function question(query) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, });
return new Promise(resolve => rl.question(query, answer => {
rl.close();
resolve(answer);
}));
}
function array2csv (data) {
let csv = data.map(row => Object.values(row));
csv.unshift(Object.keys(data[0]));
return csv.join('\n');
}
async function writecsv (fileName, data) {
try {
await writeFile(fileName, data, 'utf8');
} catch (e) {
console.error(e);
}
}
main().catch((e) => console.error(e));