All files / src/storage/schema/entities Transaction.ts

98.9% Statements 90/91
83.33% Branches 30/36
100% Functions 45/45
100% Lines 82/82

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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260  57x 57x 57x   57x           6x 3x               3x 3x 2x                 4x   4x   3x 3x   4x       982x 982x                                             14x     1147x       4x     1x       387x     2x       2x     2x       2x     2x       3x     2x       3x     2x       12x     574x       1103x     526x       3x     2x       3x     2x       3x     1x       3x     2x       3x     2x       12x     4x               960x     1x     1x     1x       3x   3x   2x                     1x   1x   1x       955x 955x               573x 573x 573x 573x       383x 383x             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   383x       3x 2x 2x 1x      
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Transaction as BsvTransaction, TransactionInput } from '@bsv/sdk'
import { entity, optionalArraysEqual, sdk, table, verifyId, verifyOneOrNone } from '../../../index.client'
import { EntityBase } from '.'
 
export class Transaction extends EntityBase<table.Transaction> {
  /**
   * @returns @bsv/sdk Transaction object from parsed rawTx.
   * If rawTx is undefined, returns undefined.
   */
  getBsvTx(): BsvTransaction | undefined {
    if (!this.rawTx) return undefined
    return BsvTransaction.fromBinary(this.rawTx)
  }
 
  /**
   * @returns array of @bsv/sdk TransactionInput objects from parsed rawTx.
   * If rawTx is undefined, an empty array is returned.
   */
  getBsvTxIns(): TransactionInput[] {
    const tx = this.getBsvTx()
    if (!tx) return []
    return tx.inputs
  }
 
  /**
   * Returns an array of "known" inputs to this transaction which belong to the same userId.
   * Uses both spentBy and rawTx inputs (if available) to locate inputs from among user's outputs.
   * Not all transaction inputs correspond to prior dojo outputs.
   */
  async getInputs(storage: entity.EntityStorage, trx?: sdk.TrxToken): Promise<table.Output[]> {
    const inputs = await storage.findOutputs({ partial: { userId: this.userId, spentBy: this.id }, trx })
    // Merge "inputs" by spentBy and userId
    for (const input of this.getBsvTxIns()) {
      //console.log(`getInputs of ${this.id}: ${input.txid()} ${input.txOutNum}`)
      const pso = verifyOneOrNone(await storage.findOutputs({ partial: { userId: this.userId, txid: input.sourceTXID, vout: input.sourceOutputIndex }, trx }))
      if (pso && !inputs.some(i => i.outputId === pso.outputId)) inputs.push(pso)
    }
    return inputs
  }
 
  constructor(api?: table.Transaction) {
    const now = new Date()
    super(
      api || {
        transactionId: 0,
        created_at: now,
        updated_at: now,
        userId: 0,
        txid: '',
        status: 'unprocessed',
        reference: '',
        satoshis: 0,
        description: '',
        isOutgoing: false,
        rawTx: undefined,
        inputBEEF: undefined
      }
    )
  }
 
  override updateApi(): void {
    /* nothing needed yet... */
  }
 
  get transactionId() {
    return this.api.transactionId
  }
  set transactionId(v: number) {
    this.api.transactionId = v
  }
 
  get created_at() {
    return this.api.created_at
  }
  set created_at(v: Date) {
    this.api.created_at = v
  }
 
  get updated_at() {
    return this.api.updated_at
  }
  set updated_at(v: Date) {
    this.api.updated_at = v
  }
 
  get version() {
    return this.api.version
  }
  set version(v: number | undefined) {
    this.api.version = v
  }
 
  get lockTime() {
    return this.api.lockTime
  }
  set lockTime(v: number | undefined) {
    this.api.lockTime = v
  }
 
  get isOutgoing() {
    return this.api.isOutgoing
  }
  set isOutgoing(v: boolean) {
    this.api.isOutgoing = v
  }
 
  get status() {
    return this.api.status
  }
  set status(v: sdk.TransactionStatus) {
    this.api.status = v
  }
 
  get userId() {
    return this.api.userId
  }
  set userId(v: number) {
    this.api.userId = v
  }
 
  get provenTxId() {
    return this.api.provenTxId
  }
  set provenTxId(v: number | undefined) {
    this.api.provenTxId = v
  }
 
  get satoshis() {
    return this.api.satoshis
  }
  set satoshis(v: number) {
    this.api.satoshis = v
  }
 
  get txid() {
    return this.api.txid
  }
  set txid(v: string | undefined) {
    this.api.txid = v
  }
 
  get reference() {
    return this.api.reference
  }
  set reference(v: string) {
    this.api.reference = v
  }
 
  get inputBEEF() {
    return this.api.inputBEEF
  }
  set inputBEEF(v: number[] | undefined) {
    this.api.inputBEEF = v
  }
 
  get description() {
    return this.api.description
  }
  set description(v: string) {
    this.api.description = v
  }
 
  get rawTx() {
    return this.api.rawTx
  }
  set rawTx(v: number[] | undefined) {
    this.api.rawTx = v
  }
 
  // Extended (computed / dependent entity) Properties
  //get labels() { return this.api.labels }
  //set labels(v: string[] | undefined) { this.api.labels = v }
 
  override get id(): number {
    return this.api.transactionId
  }
  override set id(v: number) {
    this.api.transactionId = v
  }
  override get entityName(): string {
    return 'ojoTransaction'
  }
  override get entityTable(): string {
    return 'transactions'
  }
 
  override equals(ei: table.Transaction, syncMap?: entity.SyncMap | undefined): boolean {
    const eo = this.toApi()
    // Properties that are never updated
    if (eo.transactionId !== (syncMap ? syncMap.transaction.idMap[verifyId(ei.transactionId)] : ei.transactionId) || eo.reference !== ei.reference) return false
 
    if (
      eo.version !== ei.version ||
      eo.lockTime !== ei.lockTime ||
      eo.isOutgoing !== ei.isOutgoing ||
      eo.status !== ei.status ||
      eo.satoshis !== ei.satoshis ||
      eo.txid !== ei.txid ||
      eo.description !== ei.description ||
      !optionalArraysEqual(eo.rawTx, ei.rawTx) ||
      !optionalArraysEqual(eo.inputBEEF, ei.inputBEEF)
    )
      return false
 
    Iif (!eo.provenTxId !== !ei.provenTxId || (ei.provenTxId && eo.provenTxId !== (syncMap ? syncMap.transaction.idMap[verifyId(ei.provenTxId)] : ei.provenTxId))) return false
 
    return true
  }
 
  static async mergeFind(storage: entity.EntityStorage, userId: number, ei: table.Transaction, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<{ found: boolean; eo: entity.Transaction; eiId: number }> {
    const ef = verifyOneOrNone(await storage.findTransactions({ partial: { reference: ei.reference, userId }, trx }))
    return {
      found: !!ef,
      eo: new entity.Transaction(ef || { ...ei }),
      eiId: verifyId(ei.transactionId)
    }
  }
 
  override async mergeNew(storage: entity.EntityStorage, userId: number, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<void> {
    if (this.provenTxId) this.provenTxId = syncMap.provenTx.idMap[this.provenTxId]
    this.userId = userId
    this.transactionId = 0
    this.transactionId = await storage.insertTransaction(this.toApi(), trx)
  }
 
  override async mergeExisting(storage: entity.EntityStorage, since: Date | undefined, ei: table.Transaction, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<boolean> {
    let wasMerged = false
    if (ei.updated_at > this.updated_at) {
      // Properties that are never updated:
      // transactionId
      // userId
      // reference
 
      // Merged properties
      this.version = ei.version
      this.lockTime = ei.lockTime
      this.isOutgoing = ei.isOutgoing
      this.status = ei.status
      this.provenTxId = ei.provenTxId ? syncMap.transaction.idMap[ei.provenTxId] : undefined
      this.satoshis = ei.satoshis
      this.txid = ei.txid
      this.description = ei.description
      this.rawTx = ei.rawTx
      this.inputBEEF = ei.inputBEEF
      this.updated_at = new Date()
      await storage.updateTransaction(this.id, this.toApi(), trx)
      wasMerged = true
    }
    return wasMerged
  }
 
  async getProvenTx(storage: entity.EntityStorage, trx?: sdk.TrxToken): Promise<entity.ProvenTx | undefined> {
    if (!this.provenTxId) return undefined
    const p = verifyOneOrNone(await storage.findProvenTxs({ partial: { provenTxId: this.provenTxId }, trx }))
    if (!p) return undefined
    return new entity.ProvenTx(p)
  }
}