feat(santoTirso): logic from santo-tirso branch merged

This commit is contained in:
Damian Mee 2014-11-07 01:36:41 +01:00
parent adfb5f48fc
commit bd3d51b9f2
3 changed files with 148 additions and 13 deletions

View file

@ -17,6 +17,8 @@ var traderPlugin = null;
var walletPlugin = null;
var idVerifierPlugin = null;
var temporaryName = null;
var currentlyUsedPlugins = {};
@ -32,6 +34,7 @@ var tradeInterval = null;
var tradesQueue = [];
var sessions = {};
var dispenseStatuses = {};
// that's basically a constructor
@ -157,6 +160,10 @@ exports.configure = function configure(config) {
if (newTrader === null) stopTrader();
else startTrader();
}
// NOTE: temp solution
if (temporaryName === null)
temporaryName = require('./temporary_name');
);
// ID VERIFIER [optional] configure (or load)
@ -290,6 +297,97 @@ exports.sendBitcoins = function sendBitcoins(deviceFingerprint, rawTx, cb) {
executeTx(deviceFingerprint, rawTx.txId, true, cb);
};
function _monitorAddress(address, cb) {
var confs = 0;
var received = 0;
var t0 = Date.now();
var timeOut = 90000; // TODO make config
var interval = 300; // TODO make config
function checkAddress(_cb) {
temporaryName.addressReceived(address, confs, function(err, _received) {
if (err) logger.error(err);
if (_received > 0) received = _received;
setTimeout(_cb, interval);
});
}
function test() {
return received > 0 || Date.now() - t0 > timeOut;
}
function handler() {
if (received === 0)
return cb(new Error('Timeout while monitoring address'));
cb(null, received);
}
async.doUntil(checkAddress, test, handler);
}
function _waitDeposit(deviceFingerprint, tx) {
_monitorAddress(tx.toAddress, function(err, received) {
var status = 'fullDeposit';
if (err) status = 'timeout';
else if (received < tx.satoshis) status = 'insufficientDeposit';
var dispenseFiat = received >= tx.satoshis ? tx.fiat : 0;
dispenseStatuses[deviceFingerprint] = {
status: status,
txId: tx.txId,
deposit: received,
dispenseFiat: dispenseFiat,
expectedDeposit: tx.satoshis
};
// TODO db.dispenseReady(tx);
});
}
exports.cashOut = function cashOut(deviceFingerprint, tx, cb) {
var tmpInfo = {
label: 'TX ' + Date.now(),
account: 'deposit'
};
walletPlugin.newAddress('deposit', function(err, address) {
if (err) return cb(new Error(err));
tx.toAddress = address;
// WARN: final db structure will determine if we can use this method
db.insertTx(deviceFingerprint, tx, function(err) {
if (err) return cb(new Error(err));
_waitDeposit(deviceFingerprint, tx);
return cb(null, address);
// NOTE: logic here will depend on a way we want to handle those txs
});
});
};
exports.depositAck = function depositAck(deviceFingerprint, tx, cb) {
/* TODO
var status = ispenseStatuses[deviceFingerprint];
if (status === 'dispense') {
db.dispensing(tx, function (err) {
if (err) return cb(new Error(err));
dispenseStatuses[deviceFingerprint] = null;
return cb();
});
}
*/
dispenseStatuses[deviceFingerprint] = null;
cb();
};
exports.dispenseStatus = function dispenseStatus(deviceFingerprint) {
return dispenseStatuses[deviceFingerprint];
};
exports.fiatBalance = function fiatBalance() {
var rawRate = exports.getDeviceRate().rates.ask;

View file

@ -50,9 +50,14 @@ function poll(req, res) {
var response = {
err: null,
rate: rate * config.exchanges.settings.commission,
// TODO this should actually be based on the sell rate
fiatRate: rate / config.exchanges.settings.commission,
fiat: fiatBalance,
locale: config.brain.locale,
txLimit: parseInt(complianceSettings.maximum.limit, 10),
dispenseStatus: plugins.dispenseStatus(fingerprint),
idVerificationEnabled: complianceSettings.idVerificationEnabled
};
@ -69,6 +74,40 @@ function trade(req, res) {
});
}
function send(req, res) {
plugins.sendBitcoins(getFingerprint(req), req.body, function(err, status) {
// TODO: use status.statusCode here after confirming machine compatibility
res.json({
errType: err && err.name,
err: err && err.message,
txHash: status && status.txHash,
txId: status && status.txId
});
});
}
function cashOut(req, res) {
logger.info({tx: req.body, cmd: 'cashOut'});
plugins.cashOut(getFingerprint(req), req.body, function(err, bitcoinAddress) {
if (err) logger.error(err);
res.json({
err: err && err.message,
errType: err && err.name,
bitcoinAddress: bitcoinAddress
});
});
}
function depositAck(req, res) {
plugins.depositAck(getFingerprint(req), req.body, function(err) {
res.json({
err: err && err.message,
errType: err && err.name
});
});
}
function deviceEvent(req, res) {
plugins.logEvent(req.body, getFingerprint(req));
res.json({err: null});
@ -100,18 +139,6 @@ function verifyTx(req, res) {
});
}
function send(req, res) {
plugins.sendBitcoins(getFingerprint(req), req.body, function(err, status) {
// TODO: use status.statusCode here after confirming machine compatibility
res.json({
errType: err && err.name,
err: err && err.message,
txHash: status && status.txHash,
txId: status && status.txId
});
});
}
function pair(req, res) {
var token = req.body.token;
var name = req.body.name;
@ -137,8 +164,13 @@ function init(localConfig) {
var app = localConfig.app;
app.get('/poll', authMiddleware, poll);
app.post('/send', authMiddleware, send);
app.post('/trade', authMiddleware, trade);
app.post('/send', authMiddleware, send);
app.post('/cash_out', authMiddleware, cashOut);
app.post('/deposit_ack', authMiddleware, depositAck);
app.post('/event', authMiddleware, deviceEvent);
app.post('/verify_user', authMiddleware, verifyUser);
app.post('/verify_transaction', authMiddleware, verifyTx);

5
lib/temporary_name.js Normal file
View file

@ -0,0 +1,5 @@
'use strict';
exports.addressReceived = function addressReceived(address, confirmations, cb) {
cb();
};