@@ -368,6 +368,23 @@ Ethplorer = {
368
368
}
369
369
$ ( '#transfer-operation-value' ) . html ( value ) ;
370
370
371
+ var usdPrice = '' ;
372
+ if ( op . usdPrice && Ethplorer . showHistoricalPrice && valFloat ) {
373
+ var diff = Ethplorer . Utils . round ( Ethplorer . Utils . pdiff ( op . usdPrice , oToken . price . rate ) , 2 ) ;
374
+ var hint = 'estimated at tx date' ;
375
+
376
+ var cls = getDiffClass ( diff ) ;
377
+ if ( diff > 0 ) {
378
+ diff = '+' + Ethplorer . Utils . formatNum ( diff , true , 2 , true , true ) ; ;
379
+ }
380
+ var historyPrice = Ethplorer . Utils . formatNum ( op . usdPrice * valFloat , true , 2 , true , true ) ;
381
+
382
+ usdPrice = '<span title="' + hint + '">~$ ' + historyPrice +
383
+ ' <span class="' + cls + '">' + diff + '%</span></span>'
384
+ }
385
+ $ ( '#historical-price' ) . html ( usdPrice ) ;
386
+
387
+
371
388
titleAdd += oOperation . type ;
372
389
$ ( '.token-operation-type' ) . text ( oOperation [ 'type' ] ) ;
373
390
Ethplorer . fillValues ( 'transfer' , txData , [ 'operation' , 'operation.from' , 'operation.to' ] ) ;
@@ -609,6 +626,21 @@ Ethplorer = {
609
626
value = value + '<br><span class="tx-value-price">($ ' + Ethplorer . Utils . formatNum ( oToken . price . rate * valFloat , true , 2 , true , true ) + ')</span>' ;
610
627
}
611
628
$ ( '#transfer-operation-value' ) . html ( value ) ;
629
+ var usdPrice = '' ;
630
+ if ( oOperation . usdPrice && Ethplorer . showHistoricalPrice && valFloat ) {
631
+ var diff = Ethplorer . Utils . round ( Ethplorer . Utils . pdiff ( oOperation . usdPrice , oToken . price . rate ) , 2 ) ;
632
+ var hint = 'estimated at tx date' ;
633
+
634
+ var cls = getDiffClass ( diff ) ;
635
+ if ( diff > 0 ) {
636
+ diff = '+' + Ethplorer . Utils . formatNum ( diff , true , 2 , true , true ) ; ;
637
+ }
638
+ var historyPrice = Ethplorer . Utils . formatNum ( oOperation . usdPrice * valFloat , true , 2 , true , true ) ;
639
+
640
+ usdPrice = '<span title="' + hint + '">~$ ' + historyPrice +
641
+ ' <span class="' + cls + '">(' + diff + '%)</span></span>'
642
+ }
643
+ $ ( '#historical-price' ) . html ( usdPrice ) ;
612
644
}
613
645
614
646
if ( oTx . blockNumber ) {
@@ -648,20 +680,6 @@ Ethplorer = {
648
680
$ ( '.tx-details-close' ) . hide ( ) ;
649
681
}
650
682
651
- var usdPrice = '' ;
652
- if ( oTx . usdPrice && Ethplorer . showHistoricalPrice && valFloat ) {
653
- var diff = Ethplorer . Utils . round ( Ethplorer . Utils . pdiff ( oTx . usdPrice , oToken . price . rate ) , 2 ) ;
654
- var hint = 'estimated at tx date' ;
655
-
656
- var cls = ( diff > 0 ? 'diff-up' : 'diff-down' )
657
- if ( diff > 0 ) {
658
- diff = '+' + Ethplorer . Utils . round ( diff , 2 ) ;
659
- }
660
- var historyPrice = Ethplorer . Utils . formatNum ( oTx . usdPrice * valFloat , true , 2 , true , true ) ;
661
-
662
- usdPrice = '<span title="' + hint + '">~$ ' + historyPrice +
663
- ' <span class="' + cls + '">(' + diff + '%)</span></span>'
664
- }
665
683
document . title = 'Ethplorer' ;
666
684
document . title += ( ': ' + ( titleAdd ? ( titleAdd + ' -' ) : '' ) ) ;
667
685
document . title += ( ' hash ' + txHash ) ;
@@ -680,7 +698,6 @@ Ethplorer = {
680
698
} else if ( multiop ) {
681
699
Ethplorer . showOpDetails ( oTx , txData . operations [ 0 ] ) ;
682
700
}
683
- $ ( '#historical-price' ) . html ( usdPrice ) ;
684
701
685
702
Ethplorer . Events . fire ( 'ethp_showTxDetails_finish' , txData ) ;
686
703
Ethplorer . Utils . hideEmptyFields ( ) ;
@@ -922,7 +939,7 @@ Ethplorer = {
922
939
var price = balances [ k ] . balanceUSD ;
923
940
value += ( '<br><div class="balances-price" title="$' + price + '">$ ' + Ethplorer . Utils . formatNum ( price , true , 2 , true ) + ' ' ) ;
924
941
if ( rate . diff ) {
925
- var cls = rate . diff > 0 ? 'diff-up' : 'diff-down' ;
942
+ var cls = getDiffClass ( rate . diff ) ;
926
943
var hint = 'Updated at ' + Ethplorer . Utils . ts2date ( rate . ts , true ) ;
927
944
if ( rate . diff > 0 ) {
928
945
rate . diff = '+' + rate . diff ;
@@ -955,7 +972,7 @@ Ethplorer = {
955
972
if ( totalPrice ) {
956
973
var value = '~ $ ' + Ethplorer . Utils . formatNum ( totalPrice , true , 2 , true , true ) ;
957
974
if ( totalDiff ) {
958
- var cls = totalDiff > 0 ? 'diff-up' : 'diff-down' ;
975
+ var cls = getDiffClass ( totalDiff ) ;
959
976
if ( totalDiff > 0 ) {
960
977
totalDiff = '+' + totalDiff ;
961
978
}
@@ -1143,12 +1160,16 @@ Ethplorer = {
1143
1160
var usdval = Ethplorer . Utils . formatNum ( Math . abs ( Ethplorer . Utils . round ( pf * txToken . price . rate , 2 ) ) , true , 2 , true ) ;
1144
1161
value = value + '<br><span class="transfer-usd" title="now">$ ' + usdval + '</span>' ;
1145
1162
}
1163
+ // Fill the tx.usdPrice if tx age less 10 minutes, because of delay price update scripts
1164
+ if ( ! tx . usdPrice && txToken . price . rate && ( ( new Date ( ) . getTime ( ) / 1000 - tx . timestamp ) / 60 < 10 ) ) {
1165
+ tx . usdPrice = txToken . price . rate ;
1166
+ }
1146
1167
if ( tx . usdPrice && Ethplorer . showHistoricalPrice ) {
1147
1168
1148
1169
var diff = Ethplorer . Utils . round ( Ethplorer . Utils . pdiff ( tx . usdPrice , txToken . price . rate ) , 2 ) ;
1149
1170
var hint = 'estimated at tx date' ;
1150
1171
1151
- var cls = ( diff > 0 ? 'diff-up' : 'diff-down' )
1172
+ var cls = getDiffClass ( diff ) ;
1152
1173
if ( diff > 0 ) {
1153
1174
diff = '+' + Ethplorer . Utils . round ( diff , 2 ) ;
1154
1175
}
@@ -1646,31 +1667,21 @@ Ethplorer = {
1646
1667
case 'price' :
1647
1668
if ( value && value . rate ) {
1648
1669
var rate = value ;
1649
- value = '$ ' + Ethplorer . Utils . formatNum ( rate . rate , true , 2 , true ) ;
1650
- if ( rate . diff ) {
1651
- var cls = rate . diff > 0 ? 'diff-up' : 'diff-down' ;
1652
- var hint = 'Updated at ' + Ethplorer . Utils . ts2date ( rate . ts , true ) + ' in 24 hour period' ;
1653
- if ( rate . diff > 0 ) {
1654
- rate . diff = '+' + rate . diff ;
1655
- }
1656
- value = value + ' <span class="' + cls + '" title="' + hint + '">(24h ' + Ethplorer . Utils . round ( rate . diff , 2 ) + '%)</span>'
1670
+ var hint = 'Updated at ' + Ethplorer . Utils . ts2date ( rate . ts , true ) ;
1671
+
1672
+ value = '<span title="' + hint + '">$ ' + Ethplorer . Utils . formatNum ( rate . rate , true , 2 , true ) + '</span><br>' ;
1673
+ if ( 'undefined' !== typeof rate . diff ) {
1674
+ value = value + '<span class="diff-span">24h<span class="' + getDiffClass ( rate . diff ) + '">'
1675
+ + getDiffString ( rate . diff ) + '</span></span>'
1657
1676
}
1658
- if ( rate . diff7d ) {
1659
- var cls = rate . diff7d > 0 ? 'diff-up' : 'diff-down' ;
1660
- var hint = 'Updated at ' + Ethplorer . Utils . ts2date ( rate . ts , true ) + ' in 7 days period' ;
1661
- if ( rate . diff7d > 0 ) {
1662
- rate . diff7d = '+' + rate . diff7d ;
1663
- }
1664
- value = value + ' <span class="' + cls + '" title="' + hint + '">(7d ' + Ethplorer . Utils . round ( rate . diff7d , 2 ) + '%)</span>'
1677
+ if ( 'undefined' !== typeof rate . diff7d ) {
1678
+ value = value + '<span class="diff-span">7d<span class="' + getDiffClass ( rate . diff7d ) + '">'
1679
+ + getDiffString ( rate . diff7d ) + '</span></span>'
1665
1680
}
1666
- if ( rate . diff && rate . diff7d ) {
1667
- var cls = rate . diff7d > 0 ? 'diff-up' : 'diff-down' ;
1668
- var hint = 'Updated at ' + Ethplorer . Utils . ts2date ( rate . ts , true ) + ' in 30 days period' ;
1669
- var diff30d = 123456.789 ;
1670
- if ( rate . diff7d > 0 ) {
1671
- diff30d = '+' + diff30d ;
1672
- }
1673
- value = value + ' <span class="' + cls + '" title="' + hint + '">(30d ' + Ethplorer . Utils . round ( diff30d , 2 ) + '%)</span>'
1681
+ if ( 'undefined' !== typeof rate . diff && 'undefined' !== typeof rate . diff7d || 'undefined' !== typeof rate . diff30d ) {
1682
+ var diff30d = rate . diff30d || 123456.789 ;
1683
+ value = value + '<span class="diff-span">30d<span class="' + getDiffClass ( diff30d ) + '">'
1684
+ + getDiffString ( diff30d ) + '</span></span>'
1674
1685
}
1675
1686
} else {
1676
1687
value = '' ;
@@ -1954,6 +1965,77 @@ Ethplorer = {
1954
1965
}
1955
1966
return res ;
1956
1967
} ,
1968
+
1969
+ /**
1970
+ * Number formatter (separates thousands with comma, adds zeroes to decimal part).
1971
+ *
1972
+ * @param {int } num
1973
+ * @param {bool } withDecimals
1974
+ * @param {int } decimals
1975
+ * @param {bool } cutZeroes
1976
+ * @returns {string }
1977
+ */
1978
+ formatNumWidget : function ( num , withDecimals /* = false */ , decimals /* = 2 */ , cutZeroes /* = false */ , withPostfix /* = false */ , numLimitPostfix /* = 999999 */ ) {
1979
+ var postfix = '' ;
1980
+ if ( withPostfix ) {
1981
+ if ( ! numLimitPostfix ) numLimitPostfix = 999999 ;
1982
+ if ( num > 999 && num <= numLimitPostfix ) {
1983
+ num = num / 1000 ;
1984
+ postfix = ' K' ;
1985
+ } else if ( num > numLimitPostfix ) {
1986
+ num = num / 1000000 ;
1987
+ postfix = ' M' ;
1988
+ }
1989
+ }
1990
+ function math ( command , val , decimals ) {
1991
+ var k = Math . pow ( 10 , decimals ? parseInt ( decimals ) : 0 ) ;
1992
+ return Math [ command ] ( val * k ) / k ;
1993
+ }
1994
+ function padZero ( s , len ) {
1995
+ while ( s . length < len ) s += '0' ;
1996
+ return s ;
1997
+ }
1998
+ if ( ( 'object' === typeof ( num ) ) && ( 'undefined' !== typeof ( num . c ) ) ) {
1999
+ num = parseFloat ( Ethplorer . Utils . toBig ( num ) . toString ( ) ) ;
2000
+ }
2001
+ cutZeroes = ! ! cutZeroes ;
2002
+ withDecimals = ! ! withDecimals ;
2003
+ // decimals = decimals || (cutZeroes ? 0 : 2);
2004
+
2005
+ if ( ( num . toString ( ) . indexOf ( "e+" ) > 0 ) ) {
2006
+ return num . toString ( ) ;
2007
+ }
2008
+
2009
+ if ( ( num . toString ( ) . indexOf ( "e-" ) > 0 ) && withDecimals ) {
2010
+ var parts = num . toString ( ) . split ( "e-" ) ;
2011
+ var res = parts [ 0 ] . replace ( '.' , '' ) ;
2012
+ for ( var i = 1 ; i < parseInt ( parts [ 1 ] ) ; i ++ ) {
2013
+ res = '0' + res ;
2014
+ }
2015
+ return '0.' + res ;
2016
+ }
2017
+
2018
+ if ( withDecimals ) {
2019
+ num = math ( 'round' , num , decimals ) ;
2020
+ }
2021
+ var parts = num . toString ( ) . split ( '.' ) ;
2022
+ var res = parts [ 0 ] . toString ( ) . replace ( / \B (? = ( \d { 3 } ) + (? ! \d ) ) / g, "," ) ;
2023
+ var zeroCount = cutZeroes ? 0 : decimals ;
2024
+ if ( withDecimals && decimals ) {
2025
+ if ( parts . length > 1 ) {
2026
+ res += '.' ;
2027
+ var tail = parts [ 1 ] . substring ( 0 , decimals ) ;
2028
+ if ( tail . length < zeroCount ) {
2029
+ tail = padZero ( tail , zeroCount ) ;
2030
+ }
2031
+ res += tail ;
2032
+ } else {
2033
+ res += padZero ( '.' , parseInt ( zeroCount ) + 1 ) ;
2034
+ }
2035
+ }
2036
+ res = res . replace ( / \. $ / , '' ) ;
2037
+ return res + postfix ;
2038
+ } ,
1957
2039
/**
1958
2040
* Parses URL path
1959
2041
* @returns {string }
@@ -2092,6 +2174,9 @@ Ethplorer = {
2092
2174
} ,
2093
2175
toBig : function ( obj ) {
2094
2176
var res = new BigNumber ( 0 ) ;
2177
+ if ( obj && 'string' === typeof ( obj ) ) {
2178
+ obj = obj . replace ( / , / g, '' ) ;
2179
+ }
2095
2180
if ( obj && 'undefined' !== typeof ( obj . c ) ) {
2096
2181
res . c = obj . c ;
2097
2182
res . e = obj . e ;
@@ -2195,3 +2280,19 @@ Ethplorer = {
2195
2280
location . reload ( ) ;
2196
2281
}
2197
2282
} ;
2283
+
2284
+ function getDiffClass ( value ) {
2285
+ if ( value === 0 ) {
2286
+ return 'diff-zero'
2287
+ }
2288
+ return value > 0 ? 'diff-up' : 'diff-down' ;
2289
+ }
2290
+
2291
+ function getDiffString ( diff ) {
2292
+ if ( diff === 0 ) {
2293
+ return '--' ;
2294
+ }
2295
+ var str = ( diff > 0 ? '+' : '' ) ;
2296
+ str += Ethplorer . Utils . formatNumWidget ( diff , true , 2 , true , true ) + '%' ;
2297
+ return str ;
2298
+ }
0 commit comments