-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
74 lines (61 loc) · 1.91 KB
/
app.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
const addressInput = document.getElementById( 'address' );
const exportButton = document.getElementById( 'submit' );
const exportForm = document.getElementById( 'export-transactions' );
let apiBase = '';
let currentPage = 0;
const pageSize = 100;
let totalTransactions = 0;
let transactions = [];
const getTransactions = () => {
const url = new URL( apiBase );
url.searchParams.append( 'limit', pageSize );
url.searchParams.append( 'offset', currentPage * pageSize );
return fetch( url.href )
.then( response => {
if ( ! response.ok ) {
throw response;
}
return response;
} )
.then( response => response.json() )
.then( data => {
transactions = [
...transactions,
...data.rewardDetails.map( ( { rewardTime, reward } ) => [
rewardTime,
reward,
'BNB',
'reward',
] )
];
totalTransactions = totalTransactions ? totalTransactions : data.total;
if ( currentPage !== Math.ceil( totalTransactions / pageSize ) ) {
++currentPage;
return getTransactions();
}
} )
.catch( response => {
response.text().then( errorMessage => {
alert( errorMessage );
} )
} );
}
exportForm.addEventListener( 'submit', (e) => {
e.preventDefault();
addressInput.setAttribute( 'disabled', '' );
exportButton.ariaBusy = 'true';
exportButton.textContent = 'Please wait…';
apiBase = `https://api.binance.org/v1/staking/chains/bsc/delegators/${addressInput.value}/rewards`
currentPage = 0;
totalTransactions = 0;
transactions = [];
getTransactions()
.then( () => {
addressInput.removeAttribute( 'disabled' );
exportButton.removeAttribute( 'aria-busy' );
exportButton.textContent = 'Export transactions to CSV';
const koinlyCsvColumns = [ 'Koinly Date', 'Amount', 'Currency', 'Label' ];
exportToCsv( 'bnb-staking-rewards.csv', [ koinlyCsvColumns, ...transactions ] );
} );
} )
document.querySelector( '.year' ).textContent = ( new Date() ).getFullYear();