wallet-storage
Version:
BRC100 conforming wallet, wallet storage and wallet signer components
69 lines • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.reviewStatus = reviewStatus;
const index_client_1 = require("../../index.client");
async function reviewStatus(storage, args) {
const r = { log: '' };
const runReviewStatusQuery = async (pq) => {
try {
pq.sql = pq.q.toString();
const count = await pq.q;
if (count > 0) {
r.log += `${count} ${pq.log}\n`;
}
}
catch (eu) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const e = index_client_1.sdk.WalletError.fromUnknown(eu);
throw eu;
}
};
const k = storage.toDb(args.trx);
const qs = [];
qs.push({
log: `transactions updated to status of 'failed' where provenTxReq with matching txid is 'invalid'`,
/*
UPDATE transactions SET status = 'failed'
WHERE exists(select 1 from proven_tx_reqs as r where transactions.txid = r.txid and r.status = 'invalid')
*/
q: k('transactions')
.update({ status: 'failed' })
.whereNot({ status: 'failed' })
.whereExists(function () {
this.select(k.raw(1)).from('proven_tx_reqs as r').whereRaw(`transactions.txid = r.txid and r.status = 'invalid'`);
})
});
qs.push({
log: `outputs updated to spendable where spentBy is a transaction with status 'failed'`,
/*
UPDATE outputs SET spentBy = null, spendable = 1
where exists(select 1 from transactions as t where outputs.spentBy = t.transactionId and t.status = 'failed')
*/
q: k('outputs')
.update({ spentBy: undefined, spendable: true })
.whereExists(function () {
this.select(k.raw(1)).from('transactions as t').whereRaw(`outputs.spentBy = t.transactionId and t.status = 'failed'`);
})
});
qs.push({
log: `transactions updated with provenTxId and status of 'completed' where provenTx with matching txid exists`,
/*
UPDATE transactions SET status = 'completed', provenTxId = p.provenTxId
FROM proven_txs p
WHERE transactions.txid = p.txid AND transactions.provenTxId IS NULL
*/
q: k('transactions')
.update({
status: 'completed',
provenTxId: k.raw('(SELECT provenTxId FROM proven_txs AS p WHERE transactions.txid = p.txid)')
})
.whereNull('provenTxId')
.whereExists(function () {
this.select(k.raw(1)).from('proven_txs as p').whereRaw('transactions.txid = p.txid');
})
});
for (const q of qs)
await runReviewStatusQuery(q);
return r;
}
//# sourceMappingURL=reviewStatus.js.map