@@ -56,33 +56,45 @@ export const parseSat = satoshis => {
5656} ;
5757
5858/**
59- * Convert a string formatted BTC amount to satoshis
60- * @param {string } amount The amount e.g. '0.0001'
61- * @param {string } unit The BTC unit e.g. 'btc' or 'bit'
62- * @return {number } The satoshis as an integer
59+ * Convert a string formatted btc/fiat amount to satoshis
60+ * @param {string } amount The amount e.g. '0.0001'
61+ * @param {Object } settings Contains the current exchange rate
62+ * @return {number } The satoshis as an integer
6363 */
64- export const toSatoshis = ( amount , unit ) => {
64+ export const toSatoshis = ( amount , settings ) => {
6565 if (
6666 typeof amount !== 'string' ||
6767 ! / ^ [ 0 - 9 ] * [ . ] ? [ 0 - 9 ] * $ / . test ( amount ) ||
68- ! UNITS [ unit ]
68+ ! settings ||
69+ typeof settings . displayFiat !== 'boolean'
6970 ) {
70- throw new Error ( 'Missing args!' ) ;
71+ throw new Error ( 'Invalid input!' ) ;
72+ }
73+ if ( settings . displayFiat ) {
74+ const rate = settings . exchangeRate [ settings . fiat ] || 0 ;
75+ return Math . round ( Number ( amount ) * rate * UNITS . btc . denominator ) ;
76+ } else {
77+ return Math . round ( Number ( amount ) * UNITS [ settings . unit ] . denominator ) ;
7178 }
72- return Math . round ( Number ( amount ) * UNITS [ unit ] . denominator ) ;
7379} ;
7480
7581/**
7682 * Convert satoshis to a BTC values than can set as a text input value
7783 * @param {number } satoshis The value as a string or number
78- * @param {string } unit The BTC unit e.g. 'btc' or 'bit'
84+ * @param {Object } settings Contains the current exchange rate
7985 * @return {string } The amount formatted as '0.0001'
8086 */
81- export const toAmount = ( satoshis , unit ) => {
82- if ( ! Number . isInteger ( satoshis ) || ! UNITS [ unit ] ) {
87+ export const toAmount = ( satoshis , settings ) => {
88+ if (
89+ ! Number . isInteger ( satoshis ) ||
90+ ! settings ||
91+ typeof settings . displayFiat !== 'boolean'
92+ ) {
8393 throw new Error ( 'Invalid input!' ) ;
8494 }
85- const num = satoshis / UNITS [ unit ] . denominator ;
95+ const num = settings . displayFiat
96+ ? calculateExchangeRate ( satoshis , settings )
97+ : satoshis / UNITS [ settings . unit ] . denominator ;
8698 return num . toLocaleString ( 'en-US' , {
8799 useGrouping : false ,
88100 maximumFractionDigits : 8 ,
@@ -103,8 +115,7 @@ export const calculateExchangeRate = (satoshis, settings) => {
103115 throw new Error ( 'Invalid input!' ) ;
104116 }
105117 const rate = settings . exchangeRate [ settings . fiat ] || 0 ;
106- const balance = satoshis / rate / UNITS . btc . denominator ;
107- return formatFiat ( balance , settings . fiat ) ;
118+ return satoshis / rate / UNITS . btc . denominator ;
108119} ;
109120
110121/**
@@ -122,22 +133,19 @@ export const toAmountLabel = (satoshis, settings) => {
122133 throw new Error ( 'Invalid input!' ) ;
123134 }
124135 return settings . displayFiat
125- ? calculateExchangeRate ( satoshis , settings )
126- : formatNumber ( toAmount ( satoshis , settings . unit ) ) ;
136+ ? formatFiat ( calculateExchangeRate ( satoshis , settings ) , settings . fiat )
137+ : formatNumber ( toAmount ( satoshis , settings ) ) ;
127138} ;
128139
129140/**
130- * Convert a string formatted BTC amount either to fiat or the selected BTC unit.
141+ * Convert a string formatted btc/fiat amount either to fiat or the selected BTC unit.
131142 * The output should be used throughout the UI for value labels.
132- * @param {string } amount The amount e.g. '0.0001'
143+ * @param {string } amount The amount e.g. '0.0001'
133144 * @param {Object } settings Contains the current exchange rate
134145 * @return {string } The corresponding value label
135146 */
136147export const toLabel = ( amount , settings ) => {
137- if ( ! settings ) {
138- throw new Error ( 'Missing args!' ) ;
139- }
140- const satoshis = toSatoshis ( amount , settings . unit ) ;
148+ const satoshis = toSatoshis ( amount , settings ) ;
141149 return toAmountLabel ( satoshis , settings ) ;
142150} ;
143151
0 commit comments