-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchTransactions.js
90 lines (85 loc) · 3.52 KB
/
fetchTransactions.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
require('dotenv').config();
const { fetchCampaigns, updateCampaign } = require('./lib/data/campaigns');
const { fetchEthereumSupports, getTotalFundsFromEthereum, getCampaignEndDate, getCampaignWithdrawPeriod } = require('./lib/ethereum-clients');
const { getTotalFundsFromAvalanche, getCampaignEndDateFromAvalanche, getCampaignWithdrawPeriodFromAvalanche, fetchAvalancheSupportsFromSnowTrace } = require('./lib/avalanche-clients');
const getCampaignEthereumAttributes = async (campaign) => {
if (campaign.ethereumAddress) {
const ethSupports = await fetchEthereumSupports(campaign.ethereumAddress);
campaign.transactions.push(...ethSupports);
}
if (campaign.fundingType === 'long-term') {
campaign.totalFunds = await getTotalFundsFromEthereum(
campaign.ethereumAddress
);
}
campaign.endDate = await getCampaignEndDate(campaign.ethereumAddress);
if (campaign.campaignType === 'LongTerm') {
campaign.endDate += await getCampaignWithdrawPeriod(
campaign.ethereumAddress
);
}
return campaign;
};
const getCampaignAvalancheAttributes = async (campaign) => {
const avalancheSupports = await fetchAvalancheSupportsFromSnowTrace(campaign.avalancheAddress);
campaign.transactions.push(...avalancheSupports);
try {
if (campaign.fundingType === 'long-term') {
campaign.totalFunds = await getTotalFundsFromAvalanche(
campaign.avalancheAddress
);
}
campaign.endDate = await getCampaignEndDateFromAvalanche(campaign.avalancheAddress);
campaign.endDate += await getCampaignWithdrawPeriodFromAvalanche(
campaign.avalancheAddress
);
} catch (e) {
console.log(`Error occcured on ${campaign.campaignId}. Skipping Avalanche reading...`);
}
return campaign;
};
module.exports.index = async () => {
const campaigns = await fetchCampaigns();
let count = 0;
const totalCount = campaigns.length;
console.log(`Total number of campaigns to fill: ${totalCount}`);
const errorList = [];
for (campaign of campaigns) {
console.log(`Filling for ${campaign.campaignId} (${++count}/${totalCount})`);
campaign.transactions = [];
if (campaign.ethereumAddress) {
try {
console.log(`\tFetching for Ethereum details`);
campaign = await getCampaignEthereumAttributes(campaign);
}
catch (e) {
console.log(`Error occcured on ${campaign.campaignId}. Skipping...`);
errorList.push({
campaignId: campaign.campaignId,
source: 'ethereum',
error: e
});
}
}
if (campaign.avalancheAddress) {
try {
console.log(`\tFetching for Avalanche details`);
campaign = await getCampaignAvalancheAttributes(campaign);
}
catch (e) {
console.log(`Error occcured on ${campaign.campaignId}. Skipping... \nError: \n\t${e}`);
errorList.push({
campaignId: campaign.campaignId,
source: 'avalanche',
error: e
});
}
}
campaign.transactions = campaign.transactions.sort((s1, s2) => s2.when - s1.when);
await updateCampaign(campaign);
}
console.log(`Done. Failed records:`);
errorList.map(item => {
console.log(`\t${item.campaignId} (${item.source})`);
});
}