Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | 50x 50x 50x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 1x 19x 19x 19x 19x 19x 6x 6x 6x 19x 19x 19x 19x 19x 19x 19x 19x 3x 3x 3x 3x 39x 3x 3x 3x 1x 2x 3x 3x 3x 3x 19x 16x 16x 16x 16x 16x 16x 19x 19x 19x 19x 3x 16x 16x 19x 19x 19x 197x 197x 197x 1x 197x 47x 24x 47x 197x 152x 197x 74x 74x 74x 197x 3x 19x 1x 19x | import { Beef, ListOutputsResult, OriginatorDomainNameStringUnder250Bytes, WalletOutput } from "@bsv/sdk"
import { table } from "../index.client"
import { asString, sdk, verifyId, verifyOne } from "../../index.client"
import { StorageKnex } from "../StorageKnex"
export async function listOutputs(
dsk: StorageKnex,
auth: sdk.AuthId,
vargs: sdk.ValidListOutputsArgs,
originator?: OriginatorDomainNameStringUnder250Bytes,
)
: Promise<ListOutputsResult>
{
const trx: sdk.TrxToken | undefined = undefined
const userId = verifyId(auth.userId)
const limit = vargs.limit
const offset = vargs.offset
const k = dsk.toDb(trx)
const r: ListOutputsResult = {
totalOutputs: 0,
outputs: []
}
/*
ListOutputsArgs {
basket: BasketStringUnder300Bytes
tags?: OutputTagStringUnder300Bytes[]
tagQueryMode?: 'all' | 'any' // default any
limit?: PositiveIntegerDefault10Max10000
offset?: PositiveIntegerOrZero
}
*/
let basketId: number | undefined = undefined
const basketsById: Record<number, table.OutputBasket> = {}
if (vargs.basket) {
const baskets = await dsk.findOutputBaskets({ partial: { userId, name: vargs.basket }, trx })
if (baskets.length !== 1)
throw new sdk.WERR_INVALID_PARAMETER('basket', 'valid basket name.')
const basket = baskets[0]
basketId = basket.basketId!
basketsById[basketId!] = basket
}
let tagIds: number[] = []
if (vargs.tags && vargs.tags.length > 0) {
const q = k<table.OutputTag>('output_tags')
.where({
'userId': userId,
'isDeleted': false
})
.whereNotNull('outputTagId')
.whereIn('tag', vargs.tags)
.select('outputTagId')
const r = await q
tagIds = r.map(r => r.outputTagId!)
}
const isQueryModeAll = vargs.tagQueryMode === 'all'
Iif (isQueryModeAll && tagIds.length < vargs.tags.length)
return r
const columns: string[] = [
'outputId',
'transactionId',
'basketId',
'spendable',
'txid',
'vout',
'satoshis',
'lockingScript',
'customInstructions',
'outputDescription',
'spendingDescription',
'scriptLength',
'scriptOffset'
]
const noTags = tagIds.length === 0
const includeSpent = false
const txStatusOk = `(select status as tstatus from transactions where transactions.transactionId = outputs.transactionId) in ('completed', 'unproven', 'nosend')`
const txStatusOkCteq = `(select status as tstatus from transactions where transactions.transactionId = o.transactionId) in ('completed', 'unproven', 'nosend')`
const makeWithTagsQueries = () => {
let cteqOptions = ''
if (basketId) cteqOptions += ` AND o.basketId = ${basketId}`
if (!includeSpent) cteqOptions += ` AND o.spendable`
const cteq = k.raw(`
SELECT ${columns.map(c => 'o.' + c).join(',')},
(SELECT COUNT(*)
FROM output_tags_map AS m
WHERE m.OutputId = o.OutputId
AND m.outputTagId IN (${tagIds.join(',')})
) AS tc
FROM outputs AS o
WHERE o.userId = ${userId} ${cteqOptions} AND ${txStatusOkCteq}
`);
const q = k.with('otc', cteq)
q.from('otc')
if (isQueryModeAll)
q.where('tc', tagIds.length)
else
q.where('tc', '>', 0)
const qcount = q.clone()
q.select(columns)
qcount.count('outputId as total')
return { q, qcount }
}
const makeWithoutTagsQueries = () => {
const where: Partial<table.Output> = { userId }
if (basketId) where.basketId = basketId
if (!includeSpent) where.spendable = true
const q = k('outputs').where(where).whereRaw(txStatusOk)
const qcount = q.clone().count('outputId as total')
return { q, qcount }
}
const { q, qcount } = noTags
? makeWithoutTagsQueries()
: makeWithTagsQueries();
// Sort order when limit and offset are possible must be ascending for determinism.
q.limit(limit).offset(offset).orderBy('outputId', 'asc')
const outputs: table.Output[] = await q
if (!limit || outputs.length < limit)
r.totalOutputs = outputs.length
else {
const total = verifyOne(await qcount)['total']
r.totalOutputs = Number(total)
}
/*
ListOutputsArgs {
include?: 'locking scripts' | 'entire transactions'
includeCustomInstructions?: BooleanDefaultFalse
includeTags?: BooleanDefaultFalse
includeLabels?: BooleanDefaultFalse
}
ListOutputsResult {
totalOutputs: PositiveIntegerOrZero
BEEF?: BEEF
outputs: Array<WalletOutput>
}
WalletOutput {
satoshis: SatoshiValue
spendable: boolean
outpoint: OutpointString
customInstructions?: string
lockingScript?: HexString
tags?: OutputTagStringUnder300Bytes[]
labels?: LabelStringUnder300Bytes[]
}
*/
const labelsByTxid: Record<string, string[]> = {}
const beef = new Beef()
for (const o of outputs) {
const wo: WalletOutput = {
satoshis: Number(o.satoshis),
spendable: !!o.spendable,
outpoint: `${o.txid}.${o.vout}`
}
r.outputs.push(wo)
//if (vargs.includeBasket && o.basketId) {
// if (!basketsById[o.basketId]) {
// basketsById[o.basketId] = verifyTruthy(await dsk.findOutputBasketId(o.basketId!, trx))
// }
// wo.basket = basketsById[o.basketId].name
//}
if (vargs.includeCustomInstructions && o.customInstructions)
wo.customInstructions = o.customInstructions
if (vargs.includeLabels && o.txid) {
if (labelsByTxid[o.txid] === undefined) {
labelsByTxid[o.txid] = (await dsk.getLabelsForTransactionId(o.transactionId, trx)).map(l => l.label)
}
wo.labels = labelsByTxid[o.txid]
}
if (vargs.includeTags) {
wo.tags = (await dsk.getTagsForOutputId(o.outputId, trx)).map(t => t.tag)
}
if (vargs.includeLockingScripts) {
await dsk.validateOutputScript(o, trx)
if (o.lockingScript)
wo.lockingScript = asString(o.lockingScript)
}
if (vargs.includeTransactions && !beef.findTxid(o.txid!)) {
await dsk.getValidBeefForKnownTxid(o.txid!, beef, undefined, vargs.knownTxids, trx)
}
}
if (vargs.includeTransactions) {
r.BEEF = beef.toBinary()
}
return r
} |