Skip to content

Instantly share code, notes, and snippets.

@pingec
Last active June 30, 2019 23:50
Show Gist options
  • Save pingec/0724d06083f4d574b6c6ea7615e60690 to your computer and use it in GitHub Desktop.
Save pingec/0724d06083f4d574b6c6ea7615e60690 to your computer and use it in GitHub Desktop.
Expand bitstamp transactions page with running balance in USD in BTC
function processBitstampTx(balanceEur = 0, balanceUsd = 0, balanceBtc = 0, account = "Main Account"){
const balances = { };
function deposit(textAmount){
console.log("%cDeposit: " + textAmount, "color:green");
const split = textAmount.split(" ");
const amount = parseFloat(split[0]);
const currency = split[1];
balances[currency] = balances[currency] || 0;
balances[currency] += amount;
}
function withdrawal(textAmount){
console.log("%cWithdraw: " + textAmount, "color: red");
const split = textAmount.split(" ");
const amount = parseFloat(split[0]);
const currency = split[1];
balances[currency] = balances[currency] || 0;
balances[currency] -= amount;
}
function buy(row){
const textAmount = $(row).find("div.table__cell").eq(3).text();
const split = textAmount.split(" ");
const boughtAmount = parseFloat(split[0]);
const boughtCurrency = split[1];
const spentAmountText = $(row).find("div.table__cell").eq(4).text();
const spentSplit = spentAmountText.split(" ");
const spentAmount = parseFloat(spentSplit[0]);
const spentCurrency = spentSplit[1];
const spentCurrencyFeesText = $(row).find("div.table__cell").eq(6).text();
const spentCurrencyFees = parseFloat(spentCurrencyFeesText.split(" ")[0]) || 0; //no fees when buying crypto with crypto, default NaN to 0
console.log(`BUY ${textAmount} for ${spentAmountText} fee:${spentCurrencyFeesText}`);
balances[boughtCurrency] = balances[boughtCurrency] || 0;
balances[boughtCurrency] += boughtAmount;
balances[spentCurrency] = balances[spentCurrency] || 0;
balances[spentCurrency] -= (spentAmount + spentCurrencyFees);
}
function sell(row){
const textAmount = $(row).find("div.table__cell").eq(3).text();
const split = textAmount.split(" ");
const soldAmount = parseFloat(split[0]);
const soldCurrency = split[1];
const gainedAmountText = $(row).find("div.table__cell").eq(4).text();
const gainedSplit = gainedAmountText.split(" ");
const gainedAmount = parseFloat(gainedSplit[0]);
const gainedCurrency = gainedSplit[1];
const gainedCurrencyFeesText = $(row).find("div.table__cell").eq(6).text();
const gainedCurrencyFees = parseFloat(gainedCurrencyFeesText.split(" ")[0]) || 0; //no fees when selling crypto for crypto, default NaN to 0
console.log(`SELL ${textAmount} for ${gainedAmountText} with fee:${gainedCurrencyFeesText}`);
balances[soldCurrency] = balances[soldCurrency] || 0;
balances[soldCurrency] -= soldAmount;
balances[gainedCurrency] = balances[gainedCurrency] || 0;
balances[gainedCurrency] += (gainedAmount - gainedCurrencyFees);
}
const rows = Array.from($("div.table__row")
.not(".details")
.not(".dropdown-filters")
.not(".table__row--header"))
.reverse();
rows.forEach((row) => {
if(row.textContent.indexOf(account) < 0){
// only process one account
row.remove();
return;
}
const isSell = row.textContent.startsWith("Sell");
const isBuy = row.textContent.startsWith("Buy");
const isWithdrawal = row.textContent.startsWith("Withdrawal");
const isDeposit = row.textContent.startsWith("Deposit");
const textAmount = $(row).find("div.table__cell").eq(3).text();
const isEur = textAmount.indexOf("EUR") > -1;
const isUsd = textAmount.indexOf("USD") > -1;
const isBtc = textAmount.indexOf("BTC") > -1;
const isBch = textAmount.indexOf("BCH") > -1;
const operation = $(row).find("div.table__cell").eq(0).text();
switch(operation){
case "Sell":
sell(row);
break;
case "Buy":
buy(row);
break;
case "Deposit":
deposit(textAmount);
break;
case "Withdrawal":
withdrawal(textAmount);
break;
}
const balancesNoENotation = {...balances};
Object.entries(balancesNoENotation).forEach(entry => {
let key = entry[0];
let value = entry[1];
balancesNoENotation[key] = Math.round(value*100000000)/100000000;
});
console.log(balancesNoENotation);
//const fiatBalanceCell = $(`<div class="table__cell">${: balanceEur.toFixed(2)+"€"}</div>`);
//const btcBalanceCell = $(`<div class="table__cell">${balanceBtc.toFixed(8)}</div>`);
//$(row).append(fiatBalanceCell, btcBalanceCell);
});
}
processBitstampTx();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment