Skip to content

Commit

Permalink
Close #19: Add coinchoose.com as alternative technical source
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed May 14, 2014
1 parent 1b81e24 commit a2c96ef
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/modules/revenue/solo.js
Expand Up @@ -61,7 +61,7 @@ module.exports = Module.extend({

totalHashrate = _.reduce(this.minerData, function (sum, miner) { return sum + (miner.averageHashrate || 0); }, 0);
ask = this.config.market ? this.marketData.ask : 1;
currency = this.config.market ? this.marketData.currency : 'BTC';
currency = this.config.market ? this.marketData.currency : this.technicalData.coin;

this.set({
value: this.technicalData.blockReward * (24 * 60 * 60) * totalHashrate * 1e6 * this.technicalData.probability * ask,
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/revenue/triplemining.js
Expand Up @@ -77,7 +77,7 @@ module.exports = Module.extend({
return;
}

currency = this.config.market ? this.marketData.currency : 'BTC';
currency = this.config.market ? this.marketData.currency : this.technicalData.coin;
bid = this.config.market ? this.marketData.bid : 1;

this.set({
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/technical/blockchainInfo.js
Expand Up @@ -36,6 +36,8 @@ module.exports = Module.extend({
);

self.set({
coin: 'BTC',
algorithm: 'SHA-256',
blockReward: responses[1] / 1e8,
probability: 1 / (4295032833 * responses[0].difficulty),
difficulty: responses[0].difficulty,
Expand Down
52 changes: 52 additions & 0 deletions lib/modules/technical/coinchooseCom.js
@@ -0,0 +1,52 @@
'use strict';

var _ = require('lodash'),
request = require('../../utils/request'),
Module =require('../../Module');

module.exports = Module.extend({

defaults: {
interval: 6 * 60 * 60 * 1e3,
coin: 'BTC'
},

viewId: 'technical',

initialize: function () {
var self = this;

self.title = 'Coinchoose.com - ' + self.config.coin;

setInterval(function () { self.updateStats(); }, self.config.interval);
self.updateStats();
},

updateStats: function () {
var self = this;

request('http://www.coinchoose.com/api.php?base=BTC').then(function (response) {
var coin = _.find(response, function (coin) {
return coin.symbol === self.config.coin;
});

if (coin) {
self.app.logger.debug('%s - fetched data from coinchoose.com', self.id, JSON.stringify(response));
self.set({
coin: self.config.coin,
algorithm: coin.algo,
blockReward: parseFloat(coin.reward),
probability: 1 / (4295032833 * parseFloat(coin.difficulty)),
difficulty: parseFloat(coin.difficulty),
networkHashrate: parseFloat(coin.networkhashrate) / 1e6,
blockChainLength: parseFloat(coin.currentBlocks),
timeBetweenBlocks: parseFloat(coin.minBlockTime)
});
} else {
self.app.logger.info('%s - error fetching data from coinchoose.com', self.id, 'Coin ' + self.config.coin + ' not found');
}
}).catch(function (err) {
self.app.logger.info('%s - error fetching data from coinchoose.com', self.id, err.toString());
});
}
});
20 changes: 13 additions & 7 deletions templates/technical.hbs
Expand Up @@ -4,7 +4,11 @@
<div class="panel-body row">
<div class="col-xs-12 col-md-3">
<table class="table table-bordered">
<tr><td>Total Hashrate</td><td>{{hashrate networkHashrate}}</td></tr>
{{#if networkHashrate}}
<tr><td>Total Hashrate</td><td>{{hashrate networkHashrate}}</td></tr>
{{else}}
<tr><td>Algorithm</td><td>{{algorithm}}</td></tr>
{{/if}}
<tr><td>Difficulty</td><td>{{number difficulty precision="2"}}</td></tr>
</table>
</div>
Expand All @@ -14,11 +18,13 @@
<tr><td>Time between Blocks</td><td>{{number timeBetweenBlocks precision="2"}} Minutes</td></tr>
</table>
</div>
<div class="col-xs-12 col-md-3">
<table class="table table-bordered">
<tr><td>Number of Transactions</td><td>{{number numberOfTransactions precision="1"}}</td></tr>
<tr><td>Total Output Volume</td><td>{{number totalTransactionValue}} BTC</td></tr>
</table>
</div>
{{#if numberOfTransactions}}
<div class="col-xs-12 col-md-3">
<table class="table table-bordered">
<tr><td>Number of Transactions</td><td>{{number numberOfTransactions precision="1"}}</td></tr>
<tr><td>Total Output Volume</td><td>{{number totalTransactionValue}} BTC</td></tr>
</table>
</div>
{{/if}}
</div>
<footer class="text-muted">Updated: {{time lastUpdated}}</footer>
4 changes: 2 additions & 2 deletions test/specs/lib/modules/revenue/soloSpec.js
Expand Up @@ -83,12 +83,12 @@ describe('modules/revenue/solo', function () {
technical: 'technicalId'
});

solo.technicalData = { blockReward: 10, probability: 0.0001 };
solo.technicalData = { blockReward: 10, probability: 0.0001, coin: 'NMC' };

solo.on('change', function () {
expect(_.omit(solo.toJSON(), 'historicalData')).to.deep.equal({
value: 86.4,
currency: 'BTC',
currency: 'NMC',
interval: 'Day'
});
done();
Expand Down
1 change: 1 addition & 0 deletions test/specs/lib/modules/revenue/tripleminingSpec.js
Expand Up @@ -6,6 +6,7 @@ var EventEmitter = require('events').EventEmitter,
_ = require('lodash'),

technicalData = {
coin: 'BTC',
probability: 1
},
marketData = {
Expand Down
2 changes: 2 additions & 0 deletions test/specs/lib/modules/technical/blockchainInfoSpec.js
Expand Up @@ -38,6 +38,8 @@ describe('modules/technical/blockchainInfo', function () {
blockchainInfo.on('change', function () {
setImmediate(function () {
expect(blockchainInfo.toJSON()).to.deep.equal({
coin: 'BTC',
algorithm: 'SHA-256',
blockReward: 25,
probability: 0.01,
difficulty: 100 * (1 / 4295032833),
Expand Down
111 changes: 111 additions & 0 deletions test/specs/lib/modules/technical/coinchooseComSpec.js
@@ -0,0 +1,111 @@
'use strict';

var SandboxedModule = require('sandboxed-module'),
Bluebird = require('bluebird'),
coinchooseResponse = [
{
symbol:'ALF',
name:'Alphacoin',
algo:'scrypt',
currentBlocks:'561272',
difficulty:'2',
reward:'50',
minBlockTime:'0.5',
networkhashrate:'106463482'
},
{
symbol:'BTC',
name:'Bitcoin',
algo:'SHA-256',
currentBlocks:'12345',
difficulty:'1.5',
reward:'25',
minBlockTime:'10',
networkhashrate:'100000000000'
}
];

describe('modules/technical/coinchooseCom', function () {
var requestStub,
Coinchoose,
app;

beforeEach(function () {
requestStub = sinon.stub(),
Coinchoose = SandboxedModule.require('../../../../../lib/modules/technical/coinchooseCom', {
requires: {
'../../utils/request': requestStub
}
});
app = { logger: { debug: sinon.stub(), info: sinon.stub() } };
});

it('should get data from coinchoose correctly', function (done) {
var coinchoose;

requestStub.withArgs('http://www.coinchoose.com/api.php?base=BTC').returns(Bluebird.resolve(coinchooseResponse));

coinchoose = new Coinchoose(app, { coin: 'ALF' });
coinchoose.on('change', function () {
setImmediate(function () {
expect(app.logger.debug).to.have.been.calledOnce;
expect(app.logger.debug).to.have.been.calledWith(
'%s - fetched data from coinchoose.com',
coinchoose.id,
JSON.stringify(coinchooseResponse)
);

expect(coinchoose.toJSON()).to.deep.equal({
coin: 'ALF',
algorithm: 'scrypt',
blockReward: 50,
probability: 1 / (4295032833 * 2),
difficulty: 2,
networkHashrate: 106.463482,
blockChainLength: 561272,
timeBetweenBlocks: 0.5
});

done();
});
});
});

it('should log if the coin could not be found', function (done) {
var coinchoose;

requestStub.withArgs('http://www.coinchoose.com/api.php?base=BTC').returns(Bluebird.resolve(coinchooseResponse));

coinchoose = new Coinchoose(app, { coin: 'PPC' });
setTimeout(function () {
expect(coinchoose.toJSON()).to.be.empty;
expect(app.logger.info).to.have.been.calledOnce;
expect(app.logger.info).to.have.been.calledWith('%s - error fetching data from coinchoose.com', coinchoose.id, 'Coin PPC not found');
done();
}, 10);
});

it('should log if the request errors', function (done) {
var err = new Error('Test Error'),
coinchoose;

requestStub.withArgs('http://www.coinchoose.com/api.php?base=BTC').returns(Bluebird.reject(err));

coinchoose = new Coinchoose(app);
setTimeout(function () {
expect(coinchoose.toJSON()).to.be.empty;
expect(app.logger.info).to.have.been.calledOnce;
expect(app.logger.info).to.have.been.calledWith('%s - error fetching data from coinchoose.com', coinchoose.id, err.toString());
done();
}, 10);
});

it('should have the title set correctly', function () {
var coinchoose;

requestStub.withArgs('http://www.coinchoose.com/api.php?base=BTC').returns(Bluebird.resolve(coinchooseResponse));

coinchoose = new Coinchoose(app);
expect(coinchoose.title).to.equal('Coinchoose.com - BTC');
});
});

0 comments on commit a2c96ef

Please sign in to comment.