feat(santoTirso): logic from santo-tirso branch merged
This commit is contained in:
parent
adfb5f48fc
commit
bd3d51b9f2
3 changed files with 148 additions and 13 deletions
|
|
@ -17,6 +17,8 @@ var traderPlugin = null;
|
||||||
var walletPlugin = null;
|
var walletPlugin = null;
|
||||||
var idVerifierPlugin = null;
|
var idVerifierPlugin = null;
|
||||||
|
|
||||||
|
var temporaryName = null;
|
||||||
|
|
||||||
var currentlyUsedPlugins = {};
|
var currentlyUsedPlugins = {};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -32,6 +34,7 @@ var tradeInterval = null;
|
||||||
|
|
||||||
var tradesQueue = [];
|
var tradesQueue = [];
|
||||||
var sessions = {};
|
var sessions = {};
|
||||||
|
var dispenseStatuses = {};
|
||||||
|
|
||||||
|
|
||||||
// that's basically a constructor
|
// that's basically a constructor
|
||||||
|
|
@ -157,6 +160,10 @@ exports.configure = function configure(config) {
|
||||||
if (newTrader === null) stopTrader();
|
if (newTrader === null) stopTrader();
|
||||||
else startTrader();
|
else startTrader();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: temp solution
|
||||||
|
if (temporaryName === null)
|
||||||
|
temporaryName = require('./temporary_name');
|
||||||
);
|
);
|
||||||
|
|
||||||
// ID VERIFIER [optional] configure (or load)
|
// ID VERIFIER [optional] configure (or load)
|
||||||
|
|
@ -290,6 +297,97 @@ exports.sendBitcoins = function sendBitcoins(deviceFingerprint, rawTx, cb) {
|
||||||
executeTx(deviceFingerprint, rawTx.txId, true, 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() {
|
exports.fiatBalance = function fiatBalance() {
|
||||||
var rawRate = exports.getDeviceRate().rates.ask;
|
var rawRate = exports.getDeviceRate().rates.ask;
|
||||||
|
|
|
||||||
|
|
@ -50,9 +50,14 @@ function poll(req, res) {
|
||||||
var response = {
|
var response = {
|
||||||
err: null,
|
err: null,
|
||||||
rate: rate * config.exchanges.settings.commission,
|
rate: rate * config.exchanges.settings.commission,
|
||||||
|
|
||||||
|
// TODO this should actually be based on the sell rate
|
||||||
|
fiatRate: rate / config.exchanges.settings.commission,
|
||||||
|
|
||||||
fiat: fiatBalance,
|
fiat: fiatBalance,
|
||||||
locale: config.brain.locale,
|
locale: config.brain.locale,
|
||||||
txLimit: parseInt(complianceSettings.maximum.limit, 10),
|
txLimit: parseInt(complianceSettings.maximum.limit, 10),
|
||||||
|
dispenseStatus: plugins.dispenseStatus(fingerprint),
|
||||||
idVerificationEnabled: complianceSettings.idVerificationEnabled
|
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) {
|
function deviceEvent(req, res) {
|
||||||
plugins.logEvent(req.body, getFingerprint(req));
|
plugins.logEvent(req.body, getFingerprint(req));
|
||||||
res.json({err: null});
|
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) {
|
function pair(req, res) {
|
||||||
var token = req.body.token;
|
var token = req.body.token;
|
||||||
var name = req.body.name;
|
var name = req.body.name;
|
||||||
|
|
@ -137,8 +164,13 @@ function init(localConfig) {
|
||||||
var app = localConfig.app;
|
var app = localConfig.app;
|
||||||
|
|
||||||
app.get('/poll', authMiddleware, poll);
|
app.get('/poll', authMiddleware, poll);
|
||||||
app.post('/send', authMiddleware, send);
|
|
||||||
app.post('/trade', authMiddleware, trade);
|
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('/event', authMiddleware, deviceEvent);
|
||||||
app.post('/verify_user', authMiddleware, verifyUser);
|
app.post('/verify_user', authMiddleware, verifyUser);
|
||||||
app.post('/verify_transaction', authMiddleware, verifyTx);
|
app.post('/verify_transaction', authMiddleware, verifyTx);
|
||||||
|
|
|
||||||
5
lib/temporary_name.js
Normal file
5
lib/temporary_name.js
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
exports.addressReceived = function addressReceived(address, confirmations, cb) {
|
||||||
|
cb();
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue