diff --git a/bin/lamassu-cancel b/bin/lamassu-cancel new file mode 100755 index 00000000..22434b91 --- /dev/null +++ b/bin/lamassu-cancel @@ -0,0 +1,25 @@ +#!/usr/bin/env node + +const uuid = require('@fczbkk/uuid4') + +const tx = require('../lib/cash-out-tx') + +const argv = process.argv.slice(2) + +if (argv.length !== 1) { + console.log('Usage: lamassu-cancel ') + console.log('Cancels the cash out transaction with given tx-id so it cannot be dispensed.') + process.exit(1) +} + +const txId = argv[0] + +if (!uuid.validate(txId)) { + console.log('tx-id must be valid uuid. e.g.: f8093ded-c542-4916-8ab5-6ebeceb287c1') + process.exit(2) +} + +tx.cancel(txId) +.then(() => console.log('Success.')) +.catch(err => console.log(`Error: ${err.message}`)) +.then(() => process.exit(0)) diff --git a/lib/cash-out-tx.js b/lib/cash-out-tx.js index 1b6457ec..49d315aa 100644 --- a/lib/cash-out-tx.js +++ b/lib/cash-out-tx.js @@ -12,7 +12,8 @@ module.exports = { post, monitorLiveIncoming, monitorStaleIncoming, - monitorUnnotified + monitorUnnotified, + cancel } const mapValuesWithKey = _.mapValues.convert({cap: false}) @@ -273,3 +274,21 @@ function monitorUnnotified (settings) { .then(txs => Promise.all(txs.map(notify))) .catch(logger.error) } + +function cancel (txId) { + const updateRec = { + 'dispense_time': 'now()^', + dispense_error: 'Operator cancel', + dispensed: true + } + + return Promise.resolve() + .then(() => { + return pgp.helpers.update(updateRec, null, 'cash_out_txs') + + pgp.as.format(' where id=$1', [txId]) + }) + .then(sql => db.result(sql, false)) + .then(res => { + if (res.rowCount !== 1) throw new Error('No such tx-id') + }) +} diff --git a/package.json b/package.json index 3d7a7532..1f155976 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "license": "Unlicense", "author": "Lamassu (https://lamassu.is)", "dependencies": { + "@fczbkk/uuid4": "^3.0.0", "axios": "^0.16.1", "base-x": "^3.0.0", "bignumber.js": "^4.0.1", @@ -56,7 +57,8 @@ "lamassu-backup-pg": "./bin/lamassu-backup-pg", "lamassu-install-bitcoind": "./bin/lamassu-install-bitcoind", "lamassu-install-geth": "./bin/lamassu-install-geth", - "lamassu-mnemonic": "./bin/lamassu-mnemonic" + "lamassu-mnemonic": "./bin/lamassu-mnemonic", + "lamassu-cancel": "./bin/lamassu-cancel" }, "scripts": { "test": "ava" diff --git a/yarn.lock b/yarn.lock index 1876b60c..312cb1d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -37,6 +37,10 @@ ansi-styles "^2.2.1" esutils "^2.0.2" +"@fczbkk/uuid4@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@fczbkk/uuid4/-/uuid4-3.0.0.tgz#964b221cb955e1cb0f044767a966828023a384bb" + abbrev@1: version "1.1.0" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f" @@ -1341,9 +1345,9 @@ dateformat@^1.0.12: get-stdin "^4.0.1" meow "^3.3.0" -debug@2, debug@2.6.3, debug@^2.1.1: - version "2.6.3" - resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.3.tgz#0f7eb8c30965ec08c72accfa0130c8b79984141d" +debug@2, debug@2.6.1, debug@^2.2.0, debug@^2.4.5: + version "2.6.1" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351" dependencies: ms "0.7.2" @@ -1359,9 +1363,9 @@ debug@2.3.3: dependencies: ms "0.7.2" -debug@2.6.1, debug@^2.2.0, debug@^2.4.5: - version "2.6.1" - resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351" +debug@2.6.3, debug@^2.1.1: + version "2.6.3" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.3.tgz#0f7eb8c30965ec08c72accfa0130c8b79984141d" dependencies: ms "0.7.2"