Created
November 22, 2022 23:48
-
-
Save BubuMVX/0f28e4f89ab5a09395c9a7376fdd7796 to your computer and use it in GitHub Desktop.
An (experimental) userscript to export your rewards as a CSV file from your Verko account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Verko Rewards | |
// @namespace https://artmakers.io/ | |
// @version 1.0 | |
// @description Export all rewards from Verko.io | |
// @author Bubu | |
// @match https://wallet.verko.io/rewards | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=verko.io | |
// @grant none | |
// @require https://code.jquery.com/jquery-3.6.1.min.js | |
// ==/UserScript== | |
/* global $ */ | |
(function() { | |
'use strict'; | |
let button, storage, rewards; | |
function isReady() { | |
button = $('<button type="button" class="btn btn-outline-primary btn-custom-outline ms-3">Export rewards</button>'); | |
$('.page-title .col-auto .breadcrumbs').append(button); | |
storage = JSON.parse(localStorage.getItem('oidc.user:https://auth.verko.io:wallet_app')) | |
button.on('click', function() { | |
exportRewards(); | |
}); | |
} | |
function exportRewards() { | |
button | |
.addClass('disabled') | |
.text('Exporting...'); | |
rewards = []; | |
doRequest(1); | |
} | |
function doRequest(page) { | |
$.ajax({ | |
url: 'https://wallet.verko.io/api/rewards/activity?size=10&page=' + page, | |
method: 'GET', | |
'headers': { | |
'Authorization': '' + storage.id_token | |
}, | |
beforeSend: function() { | |
button.text('Exporting... (' + page + ')'); | |
}, | |
success: function (data) { | |
if(data.length == 0) { | |
rewardsToCsv(); | |
} else { | |
rewards.push(data); | |
doRequest(++page); | |
} | |
} | |
}); | |
} | |
function rewardsToCsv() { | |
let csv = ""; | |
let data = []; | |
data.push([ | |
'Date', | |
'Amount', | |
'Token', | |
'Fee', | |
'Description' | |
]); | |
for(const rows of rewards) { | |
for(const line of rows) { | |
let divisor = 10 ** line.token.decimals; | |
let amount = parseInt(line.amount) / divisor; | |
let fee = line.fee / divisor; | |
if(line.type == 2) { | |
amount *= -1; | |
} | |
data.push([ | |
line.createdAt, | |
formatInt(amount), | |
line.token.id, | |
formatInt(fee), | |
line.description | |
]); | |
} | |
} | |
for(const values of data) { | |
let row = values.join(";"); | |
csv += row + "\r\n"; | |
} | |
let date = new Date(); | |
let month = date.getMonth() + 1; | |
let day = date.getDate(); | |
let hour = date.getHours(); | |
let minute = date.getMinutes(); | |
let second = date.getSeconds(); | |
if(month < 10) month = '0' + month; | |
if(day < 10) day = '0' + day; | |
if(hour < 10) hour = '0' + hour; | |
if(minute < 10) minute = '0' + minute; | |
if(second < 10) second = '0' + second; | |
let filename = date.getFullYear() + '-' + month + '-' + day + '_' + hour + minute + second + '_Verko_rewards.csv'; | |
var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); | |
let link = document.createElement('a'); | |
var url = URL.createObjectURL(blob); | |
link.setAttribute('href', url); | |
link.setAttribute('download', filename); | |
link.style.visibility = 'hidden'; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
button | |
.removeClass('disabled') | |
.text('Export rewards'); | |
} | |
function formatInt(int) { | |
return String(int).replace('.', ','); | |
} | |
let interval = setInterval(function() { | |
if($('.page-title').length > 0) { | |
clearInterval(interval); | |
isReady(); | |
} | |
}, 1000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment