aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/deposit.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2014-07-15 00:44:25 -0700
committerAdam Soltys <[email protected]>2014-07-15 00:44:25 -0700
commit5255f026a68be987dbccc191900752d7e9daf1af (patch)
tree62e17454a7d364b39d9a7adca27fcfec32bd4a78 /public/js/deposit.js
parent3b565f4f4dcea55779e00493e7cf507cf8392010 (diff)
downloadcoinos-server-5255f026a68be987dbccc191900752d7e9daf1af.tar.gz
coinos-server-5255f026a68be987dbccc191900752d7e9daf1af.zip
Add menu, change view engine, use Grunt
Diffstat (limited to 'public/js/deposit.js')
-rw-r--r--public/js/deposit.js102
1 files changed, 102 insertions, 0 deletions
diff --git a/public/js/deposit.js b/public/js/deposit.js
new file mode 100644
index 00000000..3bb5bf23
--- /dev/null
+++ b/public/js/deposit.js
@@ -0,0 +1,102 @@
+(function() {
+ var ADDRESS_FAIL, EXCHANGE_FAIL, SOCKET_FAIL, clear, fail, g, get, setupSocket;
+
+ EXCHANGE_FAIL = "Error fetching exchange rate";
+
+ SOCKET_FAIL = "Error connecting to payment server";
+
+ ADDRESS_FAIL = "Invalid address";
+
+ g = typeof exports !== "undefined" && exports !== null ? exports : this;
+
+ $(function() {
+ g.address || (g.address = '1VAnbtCAnYccECnjaMCPnWwt81EHCVgNr');
+ setupSocket();
+ setupQR();
+ return displayQR('bitcoin:' + g.address);
+ });
+
+ setupSocket = function() {
+ g.address = "1VAnbtCAnYccECnjaMCPnWwt81EHCVgNr";
+ setTimeout(setupSocket, 10000);
+ if (!(g.websocket && g.websocket.readyState === 1)) {
+ g.websocket = new WebSocket("ws://api.blockchain.info:8335/inv");
+ g.websocket.onopen = function() {
+ return g.websocket.send('{"op":"addr_sub", "addr":"' + g.address + '"}');
+ };
+ g.websocket.onerror = g.websocket.onclose = function() {
+ return fail(SOCKET_FAIL);
+ };
+ return g.websocket.onmessage = function(e) {
+ var from_address, received, results, total;
+ results = eval('(' + e.data + ')');
+ from_address = '';
+ total = 0;
+ received = 0;
+ $.each(results.x.out, function(i, v) {
+ if (v.addr === g.address) {
+ return received += v.value;
+ }
+ });
+ $.each(results.x.inputs, function(i, v) {
+ from_address = v.prev_out.addr;
+ if (v.prev_out.addr === g.address) {
+ return input -= v.prev_out.value;
+ }
+ });
+ return $.get("/issue/" + received, function(data) {
+ return $('#received').text(data);
+ });
+ };
+ }
+ };
+
+ fail = function(msg) {
+ g.errors.push(msg);
+ g.errors = g.errors.uniq();
+ $('#calculator').hide();
+ return $('#error').show().html(g.errors.toString());
+ };
+
+ clear = function(msg) {
+ var i;
+ i = g.errors.indexOf(msg);
+ if (i >= 0) {
+ g.errors.splice(i, 1);
+ }
+ if (g.errors.length > 0) {
+ return $('#error').show().html(g.errors.toString());
+ } else {
+ $('#error').hide();
+ return $('#calculator').fadeIn('slow');
+ }
+ };
+
+ get = function(name) {
+ var regex, regexS, results;
+ name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
+ regexS = "[\\?&]" + name + "=([^&#]*)";
+ regex = new RegExp(regexS);
+ results = regex.exec(window.location.search);
+ if (results === null) {
+ return "";
+ } else {
+ return decodeURIComponent(results[1].replace(/\+/g, " "));
+ }
+ };
+
+ Array.prototype.uniq = function() {
+ var key, output, value, _i, _ref, _results;
+ output = {};
+ for (key = _i = 0, _ref = this.length; 0 <= _ref ? _i < _ref : _i > _ref; key = 0 <= _ref ? ++_i : --_i) {
+ output[this[key]] = this[key];
+ }
+ _results = [];
+ for (key in output) {
+ value = output[key];
+ _results.push(value);
+ }
+ return _results;
+ };
+
+}).call(this);