-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash-script.js
145 lines (122 loc) · 4.59 KB
/
dash-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
$('document').ready(function() {
var exchangeRate;
const maxBTC = 2099999997690000 / 100000000;
function addCommas(input, decimalPlaces) {
// Separate the whole number part and the decimal part
let [whole, decimal] = input.toFixed(decimalPlaces).split('.');
// Add commas to the whole number part
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
// Combine the whole number part with the decimal part
return decimal ? `${whole}.${decimal}` : whole;
}
function changeSats(dollars) {
var sats = addCommas(100000000 / exchangeRate * dollars, 0);
$('#sats').val(sats);
}
function changeDollars(sats) {
var dollars = sats * exchangeRate / 100000000;
var decimalPlaces = (dollars < 0.01) ? 3 : 2;
if (sats == 1)
decimalPlaces = 4;
const formattedDollars = addCommas(dollars, decimalPlaces);
$("#dollars").val(formattedDollars);
}
$('#sats').on('input', function () {
var sats = $(this).val();
sats = parseInt(sats.replace(/,/g,''));
// there will never be more than 2,099,999,997,690,000 satoshis!
if (sats > 2099999997690000)
sats = 2099999997690000;
if (sats < 0 || isNaN(sats)) {
$(this).val("");
$('#dollars').val("0.00");
return;
}
$(this).val(addCommas(sats, 0));
changeDollars(sats);
resizeInput();
});
$('#dollars').on('input', function () {
var dollars = $(this).val();
// Check if the input starts with a dot, and prepend a zero if necessary
if (dollars.charAt(0) === '.')
dollars = '0' + dollars;
dollars = dollars.replace(/,/g, ''); // Remove commas
dollars = dollars.replace(/[^0-9.]/g, ''); // Remove non-numeric characters except for the period
dollars = dollars.replace(/(\..*)\./g, '$1'); // Remove any additional periods after the first one
//dollars = parseFloat(dollars.replace(/,/g,''));
// Limit decimal places to a maximum of 4
if (dollars.toString().split('.')[1]?.length > 4)
dollars = dollars.replace(/(\.\d{4})\d+/, '$1');
// can't be more than total supply
if (dollars > exchangeRate * maxBTC) {
dollars = exchangeRate * maxBTC;
dollars = parseFloat(dollars.toFixed(2));
}
if (dollars < 0 || isNaN(dollars)) {
console.log('NaN');
$(this).val("");
$('#sats').val("0");
return;
}
// add commas to part before decimal place
const parts = dollars.toString().split('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
$(this).val(parts.join('.'));
changeSats(dollars);
resizeInput();
});
$('input').click(function() {
$(this).select();
resizeInput();
});
function resizeInput() {
// sats will be more numbers than dollars (at least for now muhahaha)
$('input').each(function() {
// don't want to include commas or periods
var length = $(this).val().length;
var newLength = length * 9;
// console.log("new length of " + $(this).attr('id') + " is " + newLength);
if (newLength > 98) {
$(this).width(newLength);
}
else {
$(this).width(83);
}
});
// console.log("dollars width is " + $('#dollars').width());
if ($('#dollars').width() > $('#sats').width()) {
$('#sats').width($('#dollars').width() - 24);
}
else {
$('#dollars').width($('#sats').width() + 24);
}
}
function updateExchangeRate() {
$.getJSON("https://api.coindesk.com/v1/bpi/currentprice/mxn.json", function(data) {
exchangeRate = Number(data.bpi.MXN.rate_float);
console.log(exchangeRate);
});
setTimeout(updateExchangeRate, 1000 * 100);
}
function startHere() {
$.getJSON("https://api.coindesk.com/v1/bpi/currentprice/mxn.json", function(data) {
exchangeRate = Number(data.bpi.MXN.rate_float);
console.log(exchangeRate);
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('sats')) {
const amount = urlParams.get('sats');
$('#sats').val(addCommas(Number(amount), 0));
changeDollars(amount);
resizeInput();
}
else {
// initialize with $1.00 to start
changeSats(1);
}
});
// updates the exchange rate every 100 seconds
setTimeout(updateExchangeRate, 1000 * 100);
}
startHere();
});