Browse Source

refactor getPendingAccountings

master^2
Evan Feenstra 4 years ago
parent
commit
d964be2ea7
  1. 67
      dist/src/controllers/queries.js
  2. 2
      dist/src/controllers/queries.js.map
  3. 80
      src/controllers/queries.ts

67
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');

2
dist/src/controllers/queries.js.map

File diff suppressed because one or more lines are too long

80
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<Accounting[]> {
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(<Accounting>{
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()

Loading…
Cancel
Save