diff --git a/lib/bill-math.js b/lib/bill-math.js index 2d902cc2..d5d4247a 100644 --- a/lib/bill-math.js +++ b/lib/bill-math.js @@ -11,7 +11,6 @@ exports.makeChange = function makeChange (cartridges, amount) { // since they're all integers, well within JS number range, // and this is way more efficient in a tight loop. - console.log('DEBUG777: %j', cartridges) const small = cartridges[0] const large = cartridges[1] diff --git a/lib/plugins.js b/lib/plugins.js index 16b8a9ff..5ea5d9a6 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -36,15 +36,15 @@ function plugins (settings, deviceId) { const cryptoCodes = config.cryptoCurrencies const cashOut = config.cashOutEnabled - const cashInCommission = BN(1).minus(BN(config.cashInCommission).div(100)) - const cashOutCommission = cashOut && BN(1).plus(BN(config.cashOutCommission).div(100)) - const rates = {} - console.log('DEBUG444: %j', tickers) - console.log('DEBUG445: %j', [config.cashOutCommission, cashOutCommission]) cryptoCodes.forEach((cryptoCode, i) => { + const cryptoConfig = configManager.scoped(cryptoCode, deviceId, settings.config) const rateRec = tickers[i] + + const cashInCommission = BN(1).minus(BN(cryptoConfig.cashInCommission).div(100)) + const cashOutCommission = cashOut && BN(1).plus(BN(cryptoConfig.cashOutCommission).div(100)) + if (Date.now() - rateRec.timestamp > STALE_TICKER) return logger.warn('Stale rate for ' + cryptoCode) const rate = rateRec.rates rates[cryptoCode] = { @@ -83,19 +83,39 @@ function plugins (settings, deviceId) { const virtualCartridges = [config.virtualCashOutDenomination] return dbm.cartridgeCounts(deviceId) - .then(rec => ({ - cartridges: [ - { - denomination: parseInt(cartridges[0], 10), - count: parseInt(rec.counts[0], 10) - }, - { - denomination: parseInt(cartridges[1], 10), - count: parseInt(rec.counts[1], 10) + .then(rec => { + if (argv.cassettes) { + const counts = argv.cassettes.split(',') + + return { + cartridges: [ + { + denomination: parseInt(cartridges[0], 10), + count: parseInt(counts[0], 10) + }, + { + denomination: parseInt(cartridges[1], 10), + count: parseInt(counts[1], 10) + } + ], + virtualCartridges } - ], - virtualCartridges - })) + } + + return { + cartridges: [ + { + denomination: parseInt(cartridges[0], 10), + count: parseInt(rec.counts[0], 10) + }, + { + denomination: parseInt(cartridges[1], 10), + count: parseInt(rec.counts[1], 10) + } + ], + virtualCartridges + } + }) } function fetchCurrentConfigVersion () { @@ -200,7 +220,8 @@ function plugins (settings, deviceId) { function fiatBalance (fiatCode, cryptoCode) { const config = configManager.scoped(cryptoCode, deviceId, settings.config) - + console.log('DEBUG222: %j', config) + console.log('DEBUG223: %j', [cryptoCode, deviceId]) return Promise.all([ ticker.getRates(settings, fiatCode, cryptoCode), wallet.balance(settings, cryptoCode) @@ -221,7 +242,6 @@ function plugins (settings, deviceId) { const unitScale = BN(10).pow(coins[cryptoCode].unitScale) const fiatTransferBalance = balance.mul(rate.div(unitScale)).div(lowBalanceMargin) - console.log('DEBUG444: %s', fiatTransferBalance) return {timestamp: balanceRec.timestamp, balance: fiatTransferBalance.truncated().toString()} }) } diff --git a/lib/route-helpers.js b/lib/route-helpers.js index a298ef3a..8571120c 100644 --- a/lib/route-helpers.js +++ b/lib/route-helpers.js @@ -72,15 +72,12 @@ function fetchPhoneTx (phone) { } function fetchStatusTx (txId, status) { - console.log('DEBUG444') const sql = 'select * from cash_out_txs where id=$1' return db.oneOrNone(sql, [txId]) .then(toObj) .then(tx => { - console.log('DEBUG445') if (!tx) throw httpError('No transaction', 404) - console.log('DEBUG446') if (tx.status === status) throw httpError('Not Modified', 304) return tx }) diff --git a/lib/settings-loader.js b/lib/settings-loader.js index 00cde6aa..0494f4a2 100644 --- a/lib/settings-loader.js +++ b/lib/settings-loader.js @@ -6,68 +6,27 @@ const argv = require('minimist')(process.argv.slice(2)) const pify = require('pify') const db = require('./db') -const schema = require('../lamassu-schema.json') - -const mapWithKey = _.map.convert({cap: false}) let settingsCache -function expandFixture (fixture) { - const deviceId = argv.machine - - function findField (code) { - const field = _.find(_.matchesProperty('code', code), schema.fields) - const group = _.find(r => _.includes(code, r.fields), schema.groups) - - if (!field || !group) { - throw new Error('No such field from fixture: ' + code) - } - - return _.merge({cryptoScope: group.cryptoScope, machineScope: group.machineScope}, field) - } - - function expand (value, code) { - const field = findField(code) - - const machine = field.machineScope === 'specific' - ? deviceId - : 'global' - - const crypto = field.cryptoScope === 'specific' - ? 'BTC' - : 'global' - - return { - fieldLocator: { - fieldScope: {crypto, machine}, - code, - fieldType: field.fieldType, - fieldClass: field.fieldClass - }, - fieldValue: { - fieldType: field.fieldType, - value - } - } - } - - return mapWithKey(expand, fixture) -} - function loadFixture () { const fixture = argv.fixture - const deviceId = argv.machine + const machine = argv.machine - if (fixture && !deviceId) throw new Error('Missing --machine parameter for --fixture') + if (fixture && !machine) throw new Error('Missing --machine parameter for --fixture') const fixturePath = fixture => path.resolve(__dirname, '..', 'test', 'fixtures', fixture + '.json') const promise = fixture ? pify(fs.readFile)(fixturePath(fixture)).then(JSON.parse) - : Promise.resolve({}) + : Promise.resolve([]) return promise - .then(expandFixture) + .then(values => _.map(v => { + return (v.fieldLocator.fieldScope.machine === 'machine') + ? _.set('fieldLocator.fieldScope.machine', machine, v) + : v + }, values)) } function isEquivalentField (a, b) { @@ -112,13 +71,15 @@ function loadConfig (versionId) { } function loadLatestConfig () { + if (argv.fixture) return loadFixture() + const sql = `select data from user_config where type=$1 order by id desc limit 1` - return db.oneOrNone(sql, ['config']) + db.oneOrNone(sql, ['config']) .then(row => row ? row.data.config : []) } diff --git a/test/fixtures/two-way-btc.json b/test/fixtures/two-way-btc.json index a624443f..8ca25d60 100644 --- a/test/fixtures/two-way-btc.json +++ b/test/fixtures/two-way-btc.json @@ -2,8 +2,8 @@ { "fieldLocator": { "fieldScope": { - "crypto": "BTC", - "machine": "machine" + "crypto": "global", + "machine": "global" }, "code": "cashInCommission", "fieldType": "percentage", @@ -17,8 +17,23 @@ { "fieldLocator": { "fieldScope": { - "crypto": "BTC", - "machine": "machine" + "crypto": "global", + "machine": "global" + }, + "code": "cashOutCommission", + "fieldType": "percentage", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "percentage", + "value": 5 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" }, "code": "lowBalanceMargin", "fieldType": "percentage", @@ -242,5 +257,95 @@ "fieldType": "integer", "value": 100 } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "topCashOutDenomination", + "fieldType": "integer", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "integer", + "value": 5 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "bottomCashOutDenomination", + "fieldType": "integer", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "integer", + "value": 10 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "virtualCashOutDenomination", + "fieldType": "integer", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "integer", + "value": 20 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "cashOutTransactionLimit", + "fieldType": "integer", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "integer", + "value": 1000 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "zeroConfLimit", + "fieldType": "integer", + "fieldClass": null + }, + "fieldValue": { + "fieldType": "integer", + "value": 20 + } + }, + { + "fieldLocator": { + "fieldScope": { + "crypto": "global", + "machine": "global" + }, + "code": "sms", + "fieldType": "account", + "fieldClass": "sms" + }, + "fieldValue": { + "fieldType": "account", + "value": "mock-sms" + } } ]