From d964be2ea7bf1c101924acc76a8a98c6ac9c1d03 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Mon, 21 Dec 2020 13:50:57 -0800 Subject: [PATCH] refactor getPendingAccountings --- dist/src/controllers/queries.js | 67 ++++++++++++++++-------- dist/src/controllers/queries.js.map | 2 +- src/controllers/queries.ts | 80 ++++++++++++++++++++--------- 3 files changed, 105 insertions(+), 44 deletions(-) diff --git a/dist/src/controllers/queries.js b/dist/src/controllers/queries.js index 35fb8ce..cb12ad7 100644 --- a/dist/src/controllers/queries.js +++ b/dist/src/controllers/queries.js @@ -20,29 +20,39 @@ const jsonUtils = require("../utils/json"); const sequelize_1 = require("sequelize"); let queries = {}; const hub_pubkey = '023d70f2f76d283c6c4e58109ee3a2816eb9d8feb40b23d62469060a2b2867b77f'; +function getPendingAccountings() { + return __awaiter(this, void 0, void 0, function* () { + const utxos = yield wallet_1.listUnspent(); // at least 1 confg + const accountings = yield models_1.models.Accounting.findAll({ + where: { + onchain_address: { + [sequelize_1.Op.in]: utxos.map(utxo => utxo.address) + }, + status: constants_1.default.statuses.pending + } + }); + const ret = []; + accountings.forEach(a => { + const utxo = utxos.find(u => u.address === a.onchainAddress); + if (utxo) { + ret.push({ + id: a.id, + pubkey: a.pubkey, + address: utxo.address, + amount: utxo.amount_sat, + confirmations: utxo.confirmations, + sourceApp: a.sourceApp, + date: a.sourceApp, + }); + } + }); + return ret; + }); +} function listUTXOs(req, res) { return __awaiter(this, void 0, void 0, function* () { try { - const utxos = yield wallet_1.listUnspent(); // at least 1 confg - const addys = utxos.map(utxo => utxo.address); - const accountings = yield models_1.models.Accounting.findAll({ - where: { - onchain_address: { - [sequelize_1.Op.in]: addys - }, - status: constants_1.default.statuses.pending - } - }); - const ret = []; - accountings.forEach(a => { - const acc = Object.assign({}, a.dataValues); - const utxo = utxos.find(u => u.address === a.onchainAddress); - if (utxo) { - acc.amount = utxo.amount_sat; - acc.confirmations = utxo.confirmations; - ret.push(acc); - } - }); + const ret = yield getPendingAccountings(); res_1.success(res, ret.map(acc => jsonUtils.accountingToJson(acc))); } catch (e) { @@ -51,6 +61,23 @@ function listUTXOs(req, res) { }); } exports.listUTXOs = listUTXOs; +// function genChannel(acc: Accounting) { +// } +function pollUTXOs() { + return __awaiter(this, void 0, void 0, function* () { + const accs = yield getPendingAccountings(); + if (!accs) + return; + accs.forEach(acc => { + if (acc.confirmations < 1) + return; + }); + }); +} +function startWatchingUTXOs() { + setInterval(pollUTXOs, 600000); // every 10 minutes +} +exports.startWatchingUTXOs = startWatchingUTXOs; function queryOnchainAddress(req, res) { return __awaiter(this, void 0, void 0, function* () { console.log('=> queryOnchainAddress'); diff --git a/dist/src/controllers/queries.js.map b/dist/src/controllers/queries.js.map index 9df3757..e91f0f0 100644 --- a/dist/src/controllers/queries.js.map +++ b/dist/src/controllers/queries.js.map @@ -1 +1 @@ -{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../../src/controllers/queries.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,sCAA+C;AAC/C,sCAAkC;AAClC,sCAAqC;AACrC,4CAAoC;AACpC,oCAAmC;AACnC,gDAA+C;AAC/C,4CAAmD;AACnD,2CAA0C;AAC1C,yCAA8B;AAU9B,IAAI,OAAO,GAA2B,EAAE,CAAA;AAExC,MAAM,UAAU,GAAG,oEAAoE,CAAA;AAEvF,SAAsB,SAAS,CAAC,GAAG,EAAE,GAAG;;QACtC,IAAI;YACF,MAAM,KAAK,GAAW,MAAM,oBAAW,EAAE,CAAA,CAAC,mBAAmB;YAC7D,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YAE7C,MAAM,WAAW,GAAG,MAAM,eAAM,CAAC,UAAU,CAAC,OAAO,CAAC;gBAClD,KAAK,EAAE;oBACL,eAAe,EAAE;wBACf,CAAC,cAAE,CAAC,EAAE,CAAC,EAAE,KAAK;qBACf;oBACD,MAAM,EAAE,mBAAS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;QACV,MAAM;KACP,CAAA;IACD,MAAM,IAAI,GAAG;QACX,GAAG,EAAE,mBAAS,CAAC,cAAc;QAC7B,IAAI,EAAE,cAAc;QACpB,IAAI,EAAe;YACjB,IAAI,EAAE,mBAAS,CAAC,aAAa,CAAC,cAAc;YAC5C,OAAO,EAAE;gBACP,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC;aAC7B;YACD,MAAM,EAAE,EAAE,OAAO,EAAE,KAAK,CAAC,SAAS,EAAE;SACrC;KACF,CAAA;IACD,IAAI;QACF,MAAM,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;KAChC;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,+BAA+B,CAAC,CAAA;QAC5C,OAAM;KACP;AACH,CAAC,CAAA,CAAA;AAEY,QAAA,oBAAoB,GAAG,CAAO,OAAO,EAAE,EAAE;IACpD,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAA;IACtC,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAA;IACtC,4CAA4C;IAC5C,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAA;IACnC,IAAI;QACF,MAAM,CAAC,GAAU,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QACpC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;KACpB;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,gCAAgC,EAAE,CAAC,CAAC,CAAA;KACjD;AACH,CAAC,CAAA,CAAA"} \ No newline at end of file +{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../../src/controllers/queries.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,sCAA+C;AAC/C,sCAAkC;AAClC,sCAAqC;AACrC,4CAAoC;AACpC,oCAAmC;AACnC,gDAA+C;AAC/C,4CAAmD;AACnD,2CAA0C;AAC1C,yCAA8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BACP,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC;aAC7B;YACD,MAAM,EAAE,EAAE,OAAO,EAAE,KAAK,CAAC,SAAS,EAAE;SACrC;KACF,CAAA;IACD,IAAI;QACF,MAAM,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;KAChC;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,+BAA+B,CAAC,CAAA;QAC5C,OAAM;KACP;AACH,CAAC,CAAA,CAAA;AAEY,QAAA,oBAAoB,GAAG,CAAO,OAAO,EAAE,EAAE;IACpD,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAA;IACtC,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAA;IACtC,4CAA4C;IAC5C,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAA;IACnC,IAAI;QACF,MAAM,CAAC,GAAU,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QACpC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;KACpB;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,gCAAgC,EAAE,CAAC,CAAC,CAAA;KACjD;AACH,CAAC,CAAA,CAAA"} \ No newline at end of file diff --git a/src/controllers/queries.ts b/src/controllers/queries.ts index b6fff4f..b609b69 100644 --- a/src/controllers/queries.ts +++ b/src/controllers/queries.ts @@ -20,37 +20,71 @@ let queries: { [k: string]: Query } = {} const hub_pubkey = '023d70f2f76d283c6c4e58109ee3a2816eb9d8feb40b23d62469060a2b2867b77f' +interface Accounting { + id: number + pubkey: string + address: string + amount: number + confirmations: number + sourceApp: string + date: string +} + +async function getPendingAccountings():Promise { + const utxos: UTXO[] = await listUnspent() // at least 1 confg + const accountings = await models.Accounting.findAll({ + where: { + onchain_address: { + [Op.in]: utxos.map(utxo => utxo.address) + }, + status: constants.statuses.pending + } + }) + + const ret: Accounting[] = [] + accountings.forEach(a => { + const utxo = utxos.find(u => u.address === a.onchainAddress) + if (utxo) { + ret.push({ + id: a.id, + pubkey: a.pubkey, + address: utxo.address, + amount: utxo.amount_sat, + confirmations: utxo.confirmations, + sourceApp: a.sourceApp, + date: a.sourceApp, + }) + } + }) + return ret +} + export async function listUTXOs(req, res) { try { - const utxos: UTXO[] = await listUnspent() // at least 1 confg - const addys = utxos.map(utxo => utxo.address) - - const accountings = await models.Accounting.findAll({ - where: { - onchain_address: { - [Op.in]: addys - }, - status: constants.statuses.pending - } - }) - - const ret: any[] = [] - accountings.forEach(a => { - const acc = { ...a.dataValues } - const utxo = utxos.find(u => u.address === a.onchainAddress) - if (utxo) { - acc.amount = utxo.amount_sat - acc.confirmations = utxo.confirmations - ret.push(acc) - } - }) - + const ret: Accounting[] = await getPendingAccountings() success(res, ret.map(acc => jsonUtils.accountingToJson(acc))) } catch (e) { failure(res, e) } } +// function genChannel(acc: Accounting) { + +// } + +async function pollUTXOs(){ + const accs: Accounting[] = await getPendingAccountings() + if(!accs) return + accs.forEach(acc=>{ + if(acc.confirmations<1) return + + }) +} + +export function startWatchingUTXOs() { + setInterval(pollUTXOs, 600000) // every 10 minutes +} + export async function queryOnchainAddress(req, res) { console.log('=> queryOnchainAddress') const uuid = short.generate()