forked from jackmatt2/koinly-csv-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_changetransaction.js
147 lines (136 loc) · 5.85 KB
/
test_changetransaction.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
/*********************************************************************************/
/*
Quick test one transaction - WORKING!
*/
(function(TRANSACTION, MAIN_WALLET_ID, FUTURES_WALLET_ID) {
const PAGE_COUNT = 25;
const getCookie = (name) => {
const cookies = document.cookie.split('; ');
const cookieMap = cookies.map(it => it.split('='))
.reduce((prev, curr) => {
const [key, value] = curr;
return {
...prev,
[key]: value,
}
}, {})
return cookieMap[name]
}
const fetchHeaders = () => {
const headers = new Headers();
headers.append('authority', 'api.koinly.io');
headers.append('accept', 'application/json, text/plain, */*');
headers.append('accept-language', 'en-GB,en-US;q=0.9,en;q=0.8');
headers.append('access-control-allow-credentials', 'true');
headers.append('caches-requests', '1');
headers.append('cookie', document.cookie);
headers.append('origin', 'https://app.koinly.io');
headers.append('referer', 'https://app.koinly.io/');
headers.append('sec-fetch-dest', 'empty');
headers.append('sec-fetch-mode', 'cors');
headers.append('sec-fetch-site', 'same-site');
headers.append('sec-gpc', '1');
headers.append('user-agent', navigator.userAgent);
headers.append('x-auth-token', getCookie('API_KEY'));
headers.append('x-portfolio-token', getCookie('PORTFOLIO_ID'));
return headers;
}
const fetchSession = async () => {
const requestOptions = {
method: 'GET',
headers: fetchHeaders(),
redirect: 'follow'
};
try {
const response = await fetch('https://api.koinly.io/api/sessions', requestOptions);
return response.json();
} catch(err) {
console.error(err)
throw new Error('Fetch session failed')
}
}
const fetchPage = async (pageNumber, wallet) => {
const requestOptions = {
method: 'GET',
headers: fetchHeaders(),
redirect: 'follow'
};
try {
var postfix = '';
if (wallet !== undefined)
postfix = `&q[from_wallet_id_or_to_wallet_id_eq]=${wallet}`;
const response = await fetch(`https://api.koinly.io/api/transactions?per_page=${PAGE_COUNT}&order=date&page=${pageNumber}` + postfix, requestOptions);
return response.json();
} catch(err) {
console.error(err)
throw new Error(`Fetch failed for page=${pageNumber}`)
}
}
const getAllTransactions = async (wallet) => {
const firstPage = await fetchPage(1, wallet);
const totalPages = firstPage.meta.page.total_pages;
const promises = [];
for (let i=2; i <= totalPages; i++) {
promises.push(fetchPage(i, wallet));
}
const remainingPages = await Promise.all(promises);
const allPages = [firstPage, ...remainingPages];
return allPages.flatMap(it => it.transactions);
}
const changeTransaction = async (TRANSACTION, DATE, FROM_WALLET_ID, TO_WALLET_ID, AMOUNT, CURRENCY_ID) => {
var obj = {
transaction: {
label: null,
date: DATE,//2021-04-21T14:08:15.000Z
from_wallet_id: FROM_WALLET_ID,
from_currency_id: CURRENCY_ID,
from_amount: AMOUNT,
type: "transfer",
to_amount: AMOUNT,
to_wallet_id: TO_WALLET_ID,
to_currency_id: CURRENCY_ID
}
};
var header = fetchHeaders();
header.append('Content-type', 'application/json; charset=UTF-8');
const requestOptions = {
method: 'PUT',
headers: header,
redirect: 'follow',
body: JSON.stringify(obj)
};
try {
const response = await fetch(`https://api.koinly.io/api/transactions/${TRANSACTION}`, requestOptions);
return response.json();
} catch(err) {
console.error(err)
throw new Error('Change transaction failed')
}
}
const run = async (TRANSACTION, MAIN_WALLET_ID, FUTURES_WALLET_ID) => {
const session = await fetchSession();
const transactions = await getAllTransactions(FUTURES_WALLET_ID);
var DATE, FROM_WALLET_ID, TO_WALLET_ID, AMOUNT, CURRENCY_ID;
for (var i=0; i<transactions.length; i++) {
var t = transactions[i];
if (t.id == TRANSACTION) {
DATE = t.date;
if (t.type == "crypto_withdrawal" && t.txsrc == "KuCoin Futures Account") {
AMOUNT = t.from.amount;
CURRENCY_ID = t.from.currency.id;
FROM_WALLET_ID = FUTURES_WALLET_ID;
TO_WALLET_ID = MAIN_WALLET_ID;
} else if (t.type == "crypto_deposit" && t.txsrc == "KuCoin Trading Account") {
AMOUNT = t.to.amount;
CURRENCY_ID = t.to.currency.id;
FROM_WALLET_ID = MAIN_WALLET_ID;
TO_WALLET_ID = FUTURES_WALLET_ID;
}
break;
}
}
console.log(TRANSACTION + ", " + DATE + ", " + FROM_WALLET_ID + ", " + TO_WALLET_ID + ", " + AMOUNT + ", " + CURRENCY_ID)
await changeTransaction(TRANSACTION, DATE, FROM_WALLET_ID, TO_WALLET_ID, AMOUNT, CURRENCY_ID);
}
run(TRANSACTION, MAIN_WALLET_ID, FUTURES_WALLET_ID);
})("8868C6237DD05680F92D3C96D4252627", "371BA16D1B860E8C255582ADB8BB7350", "2A3E135325A2060E6615E38127ABD0D4")