Last active
August 28, 2024 08:10
-
-
Save giventofly/f09a614b8d4f6fdaa51af645a7fba3e4 to your computer and use it in GitHub Desktop.
Levvy.fi calculate loans+interest per token and total
This file contains 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
// Seleciona todos os elementos com a classe 'h-24' | |
let elements = document.querySelectorAll('.h-24'); | |
// Inicializa uma variável para manter a soma total | |
let totalSum = {loan: 0, interest: 0, total: 0}; | |
// guardar os valores | |
let loans = {}; | |
// Itera através de cada elemento selecionado | |
elements.forEach(element => { | |
// Extrai o primeiro valor desejado | |
let loan = parseInt( | |
element.children[0].children[2].children[0].innerText.replace(',', '').substring(2) | |
); | |
// Extrai o segundo valor desejado | |
let interest = parseInt( | |
element.children[0].children[4].children[0].innerText.replace(',', '').substring(2) | |
); | |
// valor total | |
let total = loan + interest; | |
//token | |
let token = element.children[0].children[0].children[2].children[0].innerText | |
if(loans[token]){ | |
loans[token].loan += loan; | |
loans[token].interest += interest; | |
loans[token].total += total; | |
} | |
else{ | |
loans[token] = { | |
loan: loan, | |
interest: interest, | |
total: total | |
} | |
} | |
// Adiciona o valor ao total | |
totalSum.loan += loan; | |
totalSum.interest += interest; | |
totalSum.total += total; | |
}); | |
//generate a string with \n from the loans object with total, loan and interest | |
let loansString = ''; | |
for (let token in loans) { | |
loansString += token + ' - ' + loans[token].total + ' [ loan: ' + loans[token].loan + ' interest: ' + loans[token].interest + ' ]\n'; | |
} | |
loansString += '--------------------\n\n'; | |
//add the total to the string | |
loansString += 'Total: ' + totalSum.total + ' [ loan: ' + totalSum.loan + ' interest: ' + totalSum.interest + ' ]\n'; | |
// Loga ou retorna a soma total | |
alert(loansString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment