Skip to content

Commit

Permalink
Merge pull request #10 from nirvanatikku/enhancement/est_unit_btc_pri…
Browse files Browse the repository at this point in the history
…ce_in_balance

Add Estimated BTC price for asset in balance view
  • Loading branch information
nirvanatikku authored Aug 28, 2017
2 parents 32c7ee9 + c6e118f commit 00abc3b
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions enhancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,30 @@ Number.prototype.format = function(n, x) {
return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,');
};

function updateHeader(headerId, col){
function updateHeader(headerId, col, text){
if(document.getElementById(headerId)){
return;
}
if(!text){
text = ' (USD)';
}
const enhancedHeader = document.createElement('span');
enhancedHeader.id = headerId;
enhancedHeader.style.color = CONSTANTS.color;
enhancedHeader.innerText = ' (USD)';
enhancedHeader.innerText = text;
col.appendChild(enhancedHeader);
}

function updateColumn(colId, col, marketType){
function getPriceFromNode(marketType, col){
let ret = parseFloat(col.childNodes.length > 0 ? col.childNodes[0].textContent : col.innerText);
return '$' + (Enhancer.getCurrentPrice(marketType) * ret).format(2);
}

function updateColumn(colId, col, marketType, value){
let rowId = 'enhanced-val-'+colId.replace(/\./g, '-');
let existingNodes = document.querySelectorAll('.'+rowId);
const btcVal = parseFloat(col.childNodes.length > 0 ? col.childNodes[0].textContent : col.innerText);
let price = '$' + (Enhancer.getCurrentPrice(marketType) * btcVal).format(2);
const price = typeof(value) === 'undefined' ? getPriceFromNode(marketType, col)
: value;
if(existingNodes.length === 0){
const newNode = document.createElement('span');
newNode.style.color = CONSTANTS.color;
Expand Down Expand Up @@ -158,12 +166,21 @@ Enhancer.enhanceBalanceTable = function balanceTable(table){
let row = rows[i];
if(i===0) {
const columns = row.getElementsByTagName('th');
updateHeader('enhanced-header-balance', columns[7]);
updateHeader('enhanced-header-balance-est-price', columns[2], ' (Est. BTC)')
updateHeader('enhanced-header-balance-btc-price', columns[7]);
continue;
}
// BTC Value + USD
const columns = row.getElementsByTagName('td');
updateColumn(columns[2].innerText+'-price',
const estBtcPrice = parseFloat(columns[7].innerText) / parseFloat(columns[3].innerText);
let tkn = columns[1].innerText.replace(/\s+/g, '').toLowerCase();
if(tkn !== 'bitcoin'){
updateColumn(tkn+'-est-price',
columns[2],
'btc',
estBtcPrice.format(8));
}
updateColumn(columns[2].innerText+'-btc-price',
columns[7],
'btc');
}
Expand Down

0 comments on commit 00abc3b

Please sign in to comment.