Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit cedc7df

Browse files
authored
Merge pull request #1823 from richardschneider/bitcoin-units
Bitcoin units
2 parents 24022b6 + 278976f commit cedc7df

File tree

16 files changed

+105
-42
lines changed

16 files changed

+105
-42
lines changed

js/App.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var ipcRenderer = require('electron').ipcRenderer,
44
$ = require('jquery'),
55
Socket = require('./utils/Socket'),
6+
btcConvert = require('bitcoin-convert'),
67
_app;
78

89

@@ -125,11 +126,43 @@ App.prototype.setUnreadChatMessageCount = function(count) {
125126
}
126127
};
127128

128-
App.prototype.intlNumFormat = function(numberToFormat, maxDigits){
129-
maxDigits = maxDigits || 8; //default to show down to the satoshi (.00000001)
129+
App.prototype.intlNumFormat = function(numberToFormat, maxDigits = 8) {
130130
return new Intl.NumberFormat(window.lang, {maximumFractionDigits: maxDigits}).format(numberToFormat);
131131
};
132132

133+
App.prototype.getBitcoinUnit = function() {
134+
if (!this._bitcoinUnit) {
135+
this._bitcoinUnit = localStorage.getItem('BitcoinUnit') || 'BTC';
136+
}
137+
return this._bitcoinUnit;
138+
};
139+
140+
App.prototype.setBitcoinUnit = function(unit) {
141+
this._bitcoinUnit = unit;
142+
localStorage.setItem('BitcoinUnit', unit);
143+
};
144+
145+
/**
146+
* Format a bitcoin amount in the user's locale.
147+
*
148+
* @param {Number} amount - Bitcoin (BTC) amount.
149+
* @param {Number} [maxDigits=8] - Maximum number of fraction digits to display.
150+
* @returns {String} localised amount ending with ' BTC' or the default bitcoin unit.
151+
*
152+
* @see intlNumFormat
153+
*/
154+
App.prototype.formatBitcoin = function(amount, maxDigits) {
155+
let unit = this.getBitcoinUnit();
156+
if (unit === 'mBTC') {
157+
maxDigits = 2;
158+
} else if (unit !== 'BTC') {
159+
maxDigits = 0;
160+
}
161+
amount = btcConvert(amount, 'BTC', unit);
162+
return this.intlNumFormat(amount, maxDigits)
163+
+ ' ' + unit;
164+
};
165+
133166
App.getApp = function() {
134167
if (!_app) {
135168
throw new Error('The app instance was never instantiated and is therefore not available.');

js/languages/en-US.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
"BitcoinReturnAddressPlaceholder": "Enter Bitcoin address...",
159159
"BitcoinReturnAddressInfo": "If a refund is issued, the funds will need to be sent to a Bitcoin address. Please provide an address to your Wallet below. You must be able to access this address later to receive a refund.",
160160
"LocalCurrency": "Local Currency",
161+
"BitcoinUnit": "Bitcoin Unit",
161162
"TimeZone": "Time Zone",
162163
"ShipToName": "Recipient Name",
163164
"ShipToStreet": "Street",

js/models/itemMd.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ module.exports = window.Backbone.Model.extend({
245245
}).format(newAttributes.internationalShipping);
246246
} else {
247247
newAttributes.price = vendorPriceInBitCoin;
248-
newAttributes.displayPrice = app.intlNumFormat(vendorPriceInBitCoin, 4) + " BTC";
248+
newAttributes.displayPrice = app.formatBitcoin(vendorPriceInBitCoin, 4);
249249
newAttributes.domesticShipping = vendorDomesticShippingInBitCoin;
250-
newAttributes.displayDomesticShipping = app.intlNumFormat(vendorDomesticShippingInBitCoin, 4) + " BTC";
250+
newAttributes.displayDomesticShipping = app.formatBitcoin(vendorDomesticShippingInBitCoin, 4);
251251
newAttributes.internationalShipping = vendorInternationalShippingInBitCoin;
252-
newAttributes.displayInternationalShipping = app.intlNumFormat(vendorInternationalShippingInBitCoin, 4) + " BTC";
252+
newAttributes.displayInternationalShipping = app.formatBitcoin(vendorInternationalShippingInBitCoin, 4);
253253
}
254254
//set to random so a change event is always fired
255255
newAttributes.priceSet = Math.random();

js/models/itemShortMd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = Backbone.Model.extend({
5656
currency: userCCode
5757
}).format(vendorPrice*vendToUserBTCRatio);
5858
} else {
59-
newAttributes.displayPrice = app.intlNumFormat(vendorBitCoinPrice, 4) + " BTC";
59+
newAttributes.displayPrice = app.formatBitcoin(vendorBitCoinPrice, 4);
6060
}
6161
//set to random so a change event is always fired
6262
newAttributes.priceSet = Math.random();

js/templates/buyAddresses.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<% } else { %>
4040
<%= ob.displayDomesticShipping %>
4141
<% if(ob.userCurrencyCode != 'BTC'){ %>
42-
<div class="textOpacity75 textSize12px letterSpacing02">(<%= ob.intlNumFormat(ob.domesticShippingBTC, 4) %> BTC)</div>
42+
<div class="textOpacity75 textSize12px letterSpacing02">(<%= ob.formatBitcoin(ob.domesticShippingBTC, 4) %>)</div>
4343
<% } %>
4444
<% } %>
4545
<% } else { %>
@@ -48,7 +48,7 @@
4848
<% } else { %>
4949
<%= ob.displayInternationalShipping %>
5050
<% if(ob.userCurrencyCode != 'BTC'){ %>
51-
<div class="textOpacity75 textSize12px letterSpacing02">(<%= ob.intlNumFormat(ob.internationalShippingBTC, 4) %> BTC)</div>
51+
<div class="textOpacity75 textSize12px letterSpacing02">(<%= ob.formatBitcoin(ob.internationalShippingBTC, 4) %>)</div>
5252
<% } %>
5353
<% } %>
5454
<% } %>

js/templates/buyDetails.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@
9999
<% } else { %>
100100
<span class="noOverflow fontSize13 js-buyWizardBTCShippingPrice">
101101
<% if(ob.shippingType === "domestic") { %>
102-
<%= ob.intlNumFormat(ob.domesticShippingBTC, 8) %> BTC
102+
<%= ob.formatBitcoin(ob.domesticShippingBTC) %>
103103
<% }else{ %>
104-
<%= ob.intlNumFormat(ob.internationalShippingBTC, 8) %> BTC
104+
<%= ob.formatBitcoin(ob.internationalShipping) %>
105105
<% } %>
106106
</span>
107107
<span class="noOverflow js-buyWizardShippingPrice textOpacity75 fontSize13">

js/templates/item.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h2 class="page-contractTitle noOverflow textOpacity1 marginLeft20 marginBottom8
4141
<ul class="itemMeta">
4242
<li class="alignCenter itemPrice">
4343
<span class="textSize22px textOpacity1 fontWeight500 marginRight2">
44-
<%= ob.intlNumFormat(ob.vendorBTCPrice, 8) %> BTC
44+
<%= ob.formatBitcoin(ob.vendorBTCPrice) %>
4545
</span>
4646
<% if(ob.userCurrencyCode != 'BTC'){ %>
4747
<span class="textSize22px textOpacity65">(<%= ob.displayPrice %>)</span>
@@ -209,9 +209,9 @@ <h4><%= polyglot.t('CanBeShippedTo') %></h4>
209209
<h3><%= polyglot.t('Local') %></h3>
210210
<div>
211211
<% if(ob.userCurrencyCode != "BTC"){ %>
212-
<%= polyglot.t('Shipping2') %> <%= ob.displayDomesticShipping %> (<%= ob.intlNumFormat(ob.domesticShippingBTC, 8) %> BTC)
212+
<%= polyglot.t('Shipping2') %> <%= ob.displayDomesticShipping %> (<%= ob.formatBitcoin(ob.domesticShippingBTC) %>)
213213
<% } else { %>
214-
<%= polyglot.t('Shipping2') %> <%= ob.intlNumFormat(ob.domesticShippingBTC, 8) %> BTC
214+
<%= polyglot.t('Shipping2') %> <%= ob.formatBitcoin(ob.domesticShippingBTC) %>
215215
<% } %>
216216
</div>
217217
<p><%= polyglot.t('transactions.EstimatedDelivery') %> <%= ob.vendor_offer.listing.shipping.est_delivery.domestic %></p>
@@ -223,9 +223,9 @@ <h3><%= polyglot.t('Local') %></h3>
223223
<h3><%= polyglot.t('International') %></h3>
224224
<div>
225225
<% if(ob.userCurrencyCode != "BTC"){ %>
226-
<%= polyglot.t('Shipping2') %> <%= ob.displayInternationalShipping %> (<%= ob.intlNumFormat(ob.internationalShippingBTC, 8) %> BTC)
226+
<%= polyglot.t('Shipping2') %> <%= ob.displayInternationalShipping %> (<%= ob.formatBitcoin(ob.internationalShippingBTC) %>)
227227
<% } else { %>
228-
<%= polyglot.t('Shipping2') %> <%= ob.intlNumFormat(ob.internationalShippingBTC, 8) %> BTC
228+
<%= polyglot.t('Shipping2') %> <%= ob.formatBitcoin(ob.internationalShippingBTC) %>
229229
<% } %>
230230
</div>
231231
<p><%= polyglot.t('transactions.EstimatedDelivery') %> <%= ob.vendor_offer.listing.shipping.est_delivery.international %></p>

js/templates/itemShort.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<%= ob.title %>
2424
</div>
2525
<div class="textSize13px txt-fade textWeightNormal">
26-
<%= ob.intlNumFormat(ob.vendorBTCPrice, 4) %> BTC
26+
<%= ob.formatBitcoin(ob.vendorBTCPrice, 4) %>
2727
<% if(ob.userCurrencyCode != 'BTC'){ %>
2828
(<%= ob.displayPrice %>)
2929
<% } %>

js/templates/orderShort.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<tr>
3939
<td class="textOpacity50 padding2"><%= polyglot.t('transactions.OrderTotal') %> </td>
4040
<td class="padding2 textOpacity75 js-searchPrice">
41-
<%= ob.intlNumFormat(ob.btc_total, 8) %> BTC
41+
<%= ob.formatBitcoin(ob.btc_total) %>
4242
<% if(ob.cCode != "BTC"){ %>
4343
(<%= ob.displayPrice %><!-- Would like to get this working at some point <span class="tooltip ion-information-circled fontSize12" data-tooltip="You received *VAL* in Bitcoin at the time of the sale."></span> -->)
4444
<% } %>

js/templates/settings.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ <h3 class="padding15 margin0 fontWeight500"><%= polyglot.t('General') %></h3>
123123

124124
</div>
125125

126+
<div class="flexRow">
127+
<div class="flexCol-3 borderRight custCol-border">
128+
<div class="fieldItem">
129+
<label><%= polyglot.t('BitcoinUnit') %></label>
130+
</div>
131+
</div>
132+
133+
<div class="flexCol-9 borderRight0 custCol-border">
134+
<div class="fieldItem">
135+
<input type="radio" class="fieldItem" id="bitcoinUnitBTC" name="bitcoinUnit" value="BTC" />
136+
<label for="bitcoinUnitBTC" class="radioLabel">BTC</label>
137+
<input type="radio" class="fieldItem" id="bitcoinUnitmBTC" name="bitcoinUnit" value="mBTC" />
138+
<label for="bitcoinUnitmBTC" class="radioLabel">mBTC</label>
139+
<input type="radio" class="fieldItem" id="bitcoinUnitμBTC" name="bitcoinUnit" value="μBTC" />
140+
<label for="bitcoinUnitμBTC" class="radioLabel">μBTC</label>
141+
<input type="radio" class="fieldItem" id="bitcoinUnitSat" name="bitcoinUnit" value="sat" />
142+
<label for="bitcoinUnitSat" class="radioLabel">Satoshi (sat)</label>
143+
</div>
144+
</div>
145+
</div>
146+
126147
<div class="flexRow">
127148
<div class="flexCol-3 borderRight custCol-border">
128149
<div class="fieldItem">

0 commit comments

Comments
 (0)