Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #19: Add coinchoose.com as alternative technical source
- Loading branch information
Showing
9 changed files
with
185 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); |