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;