update contract end
This commit is contained in:
parent
88dffc7792
commit
b6f5646c3e
@ -5,18 +5,22 @@
|
||||
let BitcoinModel = Backbone.Model.extend({
|
||||
initialize: function () {
|
||||
this.set('url', '/btc');
|
||||
this.set('balanceUrl', '/balance');
|
||||
let data = {
|
||||
lastGBP: 0.0,
|
||||
lastUSD: 0.0,
|
||||
lows: {gbp: 0, usd: 0},
|
||||
highs: {gbp: 0, usd: 0},
|
||||
eclass: ''
|
||||
eclass: '',
|
||||
balance: 0.0
|
||||
};
|
||||
this.set('btcdata', data);
|
||||
this.update();
|
||||
this.updateHourly();
|
||||
},
|
||||
update: function () {
|
||||
this.getBTC();
|
||||
//this.getBalance();
|
||||
const now = new Date();
|
||||
const mod = 60000 - (now.getTime() % 60000);
|
||||
|
||||
@ -25,6 +29,17 @@ let BitcoinModel = Backbone.Model.extend({
|
||||
};
|
||||
setTimeout(btcupdateFn.bind(this), mod + 10);
|
||||
},
|
||||
updateHourly: function () {
|
||||
|
||||
this.getBalance();
|
||||
const now = new Date();
|
||||
const mod = 3600000 - (now.getTime() % 3600000);
|
||||
|
||||
let btcupdateFn = function() {
|
||||
this.update();
|
||||
};
|
||||
setTimeout(btcupdateFn.bind(this), mod + 10);
|
||||
},
|
||||
recalc: function () {
|
||||
let data = this.get('btcdata');
|
||||
let lastGBP = data.lastGBP;
|
||||
@ -34,36 +49,41 @@ let BitcoinModel = Backbone.Model.extend({
|
||||
let lows = data.lows;
|
||||
let highs = data.highs;
|
||||
let eclass = data.eclass;
|
||||
|
||||
if (data.lastGBP !== 0) {
|
||||
if (g > lastGBP) {
|
||||
eclass = 'up';
|
||||
let balance = data.balance;
|
||||
if (g !== undefined) {
|
||||
if (data.lastGBP !== 0) {
|
||||
if (g > lastGBP) {
|
||||
eclass = 'up';
|
||||
} else {
|
||||
eclass = 'down';
|
||||
}
|
||||
} else {
|
||||
eclass = 'down';
|
||||
lows.gbp = g;
|
||||
lows.usd = u;
|
||||
|
||||
highs.gbp = g;
|
||||
highs.usd = u;
|
||||
}
|
||||
} else {
|
||||
lows.gbp = g;
|
||||
lows.usd = u;
|
||||
lastGBP = g;
|
||||
lastUSD = u;
|
||||
|
||||
highs.gbp = g;
|
||||
highs.usd = u;
|
||||
if (g < lows.gbp) lows.gbp = g;
|
||||
if (u < lows.usd) lows.usd = u;
|
||||
|
||||
if (highs.gbp < g) highs.gbp = g;
|
||||
if (highs.usd < u) highs.usd = u;
|
||||
|
||||
data = {
|
||||
lastGBP,
|
||||
lastUSD,
|
||||
lows,
|
||||
highs,
|
||||
eclass,
|
||||
balance
|
||||
};
|
||||
}
|
||||
lastGBP = g;
|
||||
lastUSD = u;
|
||||
|
||||
if (g < lows.gbp) lows.gbp = g;
|
||||
if (u < lows.usd) lows.usd = u;
|
||||
|
||||
if (highs.gbp < g) highs.gbp = g;
|
||||
if (highs.usd < u) highs.usd = u;
|
||||
|
||||
data = {
|
||||
lastGBP,
|
||||
lastUSD,
|
||||
lows,
|
||||
highs,
|
||||
eclass
|
||||
};
|
||||
this.set('btcdata', data);
|
||||
// total = myBTC * g;
|
||||
},
|
||||
@ -97,6 +117,36 @@ let BitcoinModel = Backbone.Model.extend({
|
||||
console.log(type);
|
||||
}
|
||||
});
|
||||
},
|
||||
getBalance: function() {
|
||||
let self = this;
|
||||
let url = this.get('balanceUrl');
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: url,
|
||||
data: '',
|
||||
dataType: 'json',
|
||||
timeout: 10000,
|
||||
//contentType: ('application/json'),
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type'
|
||||
},
|
||||
success: function (data) {
|
||||
let balance = data.balance;
|
||||
let btcdata = self.get('btcdata');
|
||||
btcdata.balance = balance;
|
||||
|
||||
self.set('btcdata', btcdata);
|
||||
self.recalc();
|
||||
},
|
||||
error: function (xhr, type) {
|
||||
console.log('ajax error');
|
||||
console.log(xhr);
|
||||
console.log(type);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
let Bitcoin = Backbone.View.extend({
|
||||
@ -108,10 +158,14 @@ let Bitcoin = Backbone.View.extend({
|
||||
},
|
||||
render: function () {
|
||||
let btcdata = this.model.get('btcdata');
|
||||
//console.log(`Balance: ${btcdata.balance.toFixed(4)}`);
|
||||
//console.log(btcdata.lastGBP);
|
||||
let owned = parseFloat(btcdata.lastGBP) * parseFloat(btcdata.balance);
|
||||
//console.log(`owned: ${owned}`);
|
||||
let title = 'High: $' + parseFloat(btcdata.highs.usd.toFixed(2)) + ' / Low $' + parseFloat(btcdata.lows.usd.toFixed(2));
|
||||
this.$btc.removeClass();
|
||||
this.$btc.addClass(btcdata.eclass);
|
||||
this.$btc.html(`$${parseFloat(btcdata.lastUSD.toFixed(2)) } / £${parseFloat(btcdata.lastGBP.toFixed(2))}` );
|
||||
this.$btc.html(`$${parseFloat(btcdata.lastUSD.toFixed(2)) } / £${parseFloat(btcdata.lastGBP.toFixed(2))} <div>${btcdata.balance} £${parseFloat(owned.toFixed(2))}</div>` );
|
||||
this.$btc.prop('title', title);
|
||||
}
|
||||
});
|
||||
|
File diff suppressed because one or more lines are too long
66
lib/btc.js
66
lib/btc.js
@ -1,5 +1,7 @@
|
||||
let http = require('http');
|
||||
let https = require('https');
|
||||
let btcCache = {};
|
||||
let balanceCache = {};
|
||||
exports.doBTC = function (req, res) {
|
||||
console.log('Bitcoin request');
|
||||
function btcQuery (callback, r) {
|
||||
@ -60,3 +62,67 @@ exports.doBTC = function (req, res) {
|
||||
res.end(JSON.stringify(btcCache));
|
||||
}
|
||||
};
|
||||
|
||||
exports.doBalance = function (req, res) {
|
||||
console.log('Bitcoin request');
|
||||
function balanceQuery (callback, r) {
|
||||
var req = r;
|
||||
|
||||
// https://blockexplorer.com/api/addr/18sLVW5Aswp2KWLr4hMFZsuSPtvAauFiif
|
||||
var options = {
|
||||
host: 'blockexplorer.com',
|
||||
// port: 80,
|
||||
path: '/api/addr/18sLVW5Aswp2KWLr4hMFZsuSPtvAauFiif',
|
||||
// method: 'GET',
|
||||
headers: {
|
||||
/* 'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(data)*/
|
||||
|
||||
}
|
||||
};
|
||||
try {
|
||||
https.get('https://blockexplorer.com/api/addr/18sLVW5Aswp2KWLr4hMFZsuSPtvAauFiif').on('response', function (response) {
|
||||
var data = '';
|
||||
response.on('data', function (chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
response.on('end', function () {
|
||||
console.log('>> data', data);
|
||||
let cData = {};
|
||||
try {
|
||||
cData = JSON.parse(data);
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
finally{
|
||||
callback(cData, r);
|
||||
}
|
||||
|
||||
});
|
||||
response.on('error', function (e) {
|
||||
console.error(e);
|
||||
});
|
||||
}).end();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
let now = new Date();
|
||||
if (now - GLOBAL.lastcheck > (59000 )) {
|
||||
balanceQuery(function (a) {
|
||||
// console.log(a);
|
||||
console.log('Got balance data.');
|
||||
balanceCache = a;
|
||||
GLOBAL.lastcheck = now;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify(balanceCache));
|
||||
}, res);
|
||||
} else {
|
||||
console.log('Using cache');
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify(balanceCache));
|
||||
}
|
||||
|
||||
};
|
||||
|
File diff suppressed because one or more lines are too long
@ -80,6 +80,7 @@ app.use(express.static(path.join(__dirname, 'app')));
|
||||
app.use(errorhandler({dumpExceptions: true, showStack: true}));
|
||||
|
||||
app.use('/btc', btc.doBTC);
|
||||
app.use('/balance', btc.doBalance);
|
||||
|
||||
app.use('/fx', fx.doFx);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user