-
Notifications
You must be signed in to change notification settings - Fork 0
/
interest_funding_aggr.js
127 lines (101 loc) · 4.11 KB
/
interest_funding_aggr.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
const XLSX = require('xlsx');
const moment = require('moment-timezone');
process.env.TZ = 'UTC';
(async () => {
start('XXXXXX'); // XXXXXX is the account ID
})();
async function start(sAccId) {
const sFundingSheet = 'Funding Fee History';
const sInterestSheet = 'Interest History';
const inFilePath = `./Input/Wallet-History-${sAccId}.xlsx`;
var aggregatedInterestHistory = [];
var aggregatedFundingFeeHistory = [];
const interestHistoryData = await readExcel(inFilePath, sInterestSheet);
if (interestHistoryData) {
aggregatedInterestHistory = aggregateInterestHistory(interestHistoryData);
} else {
console.error('Interest History sheet not found');
}
const fundingFeeHistoryData = await readExcel(inFilePath, sFundingSheet);
if (fundingFeeHistoryData) {
aggregatedFundingFeeHistory = aggregateFundingFeeHistory(fundingFeeHistoryData);
} else {
console.error('Funding Fee History sheet not found');
}
if (interestHistoryData && fundingFeeHistoryData) {
const mergedData = [...aggregatedInterestHistory, ...aggregatedFundingFeeHistory];
writeExcel(mergedData, `./Output/Interest_Funding-${sAccId}.xlsx`, sFundingSheet);
}
}
function aggregateInterestHistory(data) {
const aggregatedData = [];
// sort data by time ascending
data.sort((a, b) => new Date(a['Time']) - new Date(b['Time']));
data.forEach(row => {
if (row['Action'] === 'LOAN') {
const timestamp = new Date(row['Time']);
const date = new Date(timestamp.getFullYear(), timestamp.getMonth(), timestamp.getDate()).toISOString().split('T')[0];
const existingEntry = aggregatedData.find(entry => entry.Date === date);
// Remove the " USDT" part from the Quantity value
const quantity = parseFloat(row['Quantity'].replace(' USDT', ''));
if (existingEntry) {
existingEntry.Amount += quantity;
} else {
aggregatedData.push({ Date: date, Amount: quantity });
}
}
});
const formattedData = aggregatedData.map(entry => ({
'Koinly Date': entry.Date,
Amount: entry.Amount,
Currency: 'USDT',
Label: 'loan interest',
}));
return formattedData;
}
function aggregateFundingFeeHistory(data) {
const aggregatedData = [];
data.sort((a, b) => new Date(a['Time']) - new Date(b['Time']));
data.forEach(row => {
const timestamp = new Date(row['Time']);
const date = new Date(timestamp.getFullYear(), timestamp.getMonth(), timestamp.getDate()).toISOString().split('T')[0];
const existingEntry = aggregatedData.find(entry => entry.Date === date);
// Extract the Funding Fee Amount
const fundingFeeAmount = parseFloat(row['Funding Fee Amount']);
if (existingEntry) {
existingEntry.Amount += fundingFeeAmount;
} else {
aggregatedData.push({ Date: date, Amount: fundingFeeAmount });
}
});
const formattedData = aggregatedData.map(entry => ({
'Koinly Date': entry.Date,
Amount: entry.Amount,
Currency: 'USDT',
Label: 'realized gain',
}));
return formattedData;
}
async function readExcel(filePath, sheetName) {
return new Promise((resolve) => {
const workbook = XLSX.readFile(filePath);
const worksheet = workbook.Sheets[sheetName];
const rawData = XLSX.utils.sheet_to_json(worksheet);
const data = rawData.map((line) => {
const excelTimestampFilled = line['Time'];
const filledTime = moment((excelTimestampFilled - (25567 + 2)) * 86400 * 1000).format('YYYY-MM-DD HH:mm:ss');
return { ...line, 'Time': filledTime };
});
resolve(data);
});
}
function writeExcel(data, fileName, sheetName) {
if (data.length === 0) {
console.error('No data to write');
return;
}
const worksheet = XLSX.utils.json_to_sheet(data);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
XLSX.writeFile(workbook, fileName);
}