All files / src/chain transaction.ts

91.8% Statements 56/61
70% Branches 7/10
88.23% Functions 15/17
91.66% Lines 55/60

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                                                          2x 1x 1x                                         2x   1x   1x   1x   1x   1x   1x     99x                                                           2x   1x   1x     1x           85x 174x 171x 3x 2x 2x 1x   1x     174x 85x 83x 83x           83x         1x 1x       13x       9x 9x       11x 11x 11x 11x                           2x   1x   1x       1x               1x       32x                                   2x 1x 1x 1x 1x     20x                 8x 8x                                                       2x 1x 1x 1x    
import {abiEncode} from '../serializer/encoder'
import {Signature, SignatureType} from './signature'
import {abiDecode} from '../serializer/decoder'
 
import {
    ABIDef,
    Action,
    ActionType,
    AnyAction,
    Bytes,
    BytesType,
    Checksum256,
    Checksum256Type,
    Name,
    NameType,
    Struct,
    TimePointSec,
    TimePointType,
    UInt16,
    UInt16Type,
    UInt32,
    UInt32Type,
    UInt8,
    UInt8Type,
    VarUInt,
    VarUIntType,
} from '../'
 
@Struct.type('transaction_extension')
export class TransactionExtension extends Struct {
    @Struct.field('uint16') declare type: UInt16
    @Struct.field('bytes') declare data: Bytes
}
 
export interface TransactionHeaderFields {
    /** The time at which a transaction expires. */
    expiration: TimePointType
    /** *Specifies a block num in the last 2^16 blocks. */
    ref_block_num: UInt16Type
    /** Specifies the lower 32 bits of the block id. */
    ref_block_prefix: UInt32Type
    /** Upper limit on total network bandwidth (in 8 byte words) billed for this transaction. */
    max_net_usage_words?: VarUIntType
    /** Upper limit on the total CPU time billed for this transaction. */
    max_cpu_usage_ms?: UInt8Type
    /** Number of seconds to delay this transaction for during which it may be canceled. */
    delay_sec?: VarUIntType
}
 
export type TransactionHeaderType = TransactionHeader | TransactionHeaderFields
 
@Struct.type('transaction_header')
export class TransactionHeader extends Struct {
    /** The time at which a transaction expires. */
    @Struct.field('time_point_sec') declare expiration: TimePointSec
    /** *Specifies a block num in the last 2^16 blocks. */
    @Struct.field('uint16') declare ref_block_num: UInt16
    /** Specifies the lower 32 bits of the block id. */
    @Struct.field('uint32') declare ref_block_prefix: UInt32
    /** Upper limit on total network bandwidth (in 8 byte words) billed for this transaction. */
    @Struct.field('varuint32') declare max_net_usage_words: VarUInt
    /** Upper limit on the total CPU time billed for this transaction. */
    @Struct.field('uint8') declare max_cpu_usage_ms: UInt8
    /** Number of seconds to delay this transaction for during which it may be canceled. */
    @Struct.field('varuint32') declare delay_sec: VarUInt
 
    static from(object: TransactionHeaderType) {
        return super.from({
            max_net_usage_words: 0,
            max_cpu_usage_ms: 0,
            delay_sec: 0,
            ...object,
        }) as TransactionHeader
    }
}
 
export interface TransactionFields extends TransactionHeaderFields {
    /** The context free actions in the transaction. */
    context_free_actions?: ActionType[]
    /** The actions in the transaction. */
    actions?: ActionType[]
    /** Transaction extensions. */
    transaction_extensions?: {type: UInt16Type; data: BytesType}[]
}
 
export interface AnyTransaction extends TransactionHeaderFields {
    /** The context free actions in the transaction. */
    context_free_actions?: AnyAction[]
    /** The actions in the transaction. */
    actions?: AnyAction[]
    /** Transaction extensions. */
    transaction_extensions?: {type: UInt16Type; data: BytesType}[]
}
 
export type TransactionType = Transaction | TransactionFields
 
@Struct.type('transaction')
export class Transaction extends TransactionHeader {
    /** The context free actions in the transaction. */
    @Struct.field(Action, {array: true}) declare context_free_actions: Action[]
    /** The actions in the transaction. */
    @Struct.field(Action, {array: true}) declare actions: Action[]
    /** Transaction extensions. */
    @Struct.field(TransactionExtension, {array: true})
    declare transaction_extensions: TransactionExtension[]
 
    static from(
        object: TransactionType | AnyTransaction,
        abis?: ABIDef | {contract: NameType; abi: ABIDef}[]
    ): Transaction {
        const abiFor = (contract: NameType) => {
            if (!abis) {
                return
            } else if (Array.isArray(abis)) {
                return abis
                    .filter((abi) => Name.from(abi.contract).equals(contract))
                    .map(({abi}) => abi)[0]
            } else {
                return abis
            }
        }
        const resolveAction = (action: AnyAction) => Action.from(action, abiFor(action.account))
        const actions = (object.actions || []).map(resolveAction)
        const context_free_actions = (object.context_free_actions || []).map(resolveAction)
        const transaction = {
            transaction_extensions: [],
            ...object,
            context_free_actions,
            actions,
        }
        return super.from(transaction) as Transaction
    }
 
    /** Return true if this transaction is equal to given transaction. */
    equals(other: TransactionType) {
        const tx = Transaction.from(other)
        return this.id.equals(tx.id)
    }
 
    get id(): Checksum256 {
        return Checksum256.hash(abiEncode({object: this}))
    }
 
    signingDigest(chainId: Checksum256Type): Checksum256 {
        const data = this.signingData(chainId)
        return Checksum256.hash(data)
    }
 
    signingData(chainId: Checksum256Type): Bytes {
        let data = Bytes.from(Checksum256.from(chainId).array)
        data = data.appending(abiEncode({object: this}))
        data = data.appending(new Uint8Array(32))
        return data
    }
}
 
export interface SignedTransactionFields extends TransactionFields {
    /** List of signatures. */
    signatures?: SignatureType[]
    /** Context-free action data, for each context-free action, there is an entry here. */
    context_free_data?: BytesType[]
}
 
export type SignedTransactionType = SignedTransaction | SignedTransactionFields
 
@Struct.type('signed_transaction')
export class SignedTransaction extends Transaction {
    /** List of signatures. */
    @Struct.field('signature[]') declare signatures: Signature[]
    /** Context-free action data, for each context-free action, there is an entry here. */
    @Struct.field('bytes[]') declare context_free_data: Bytes[]
 
    /** The transaction without the signatures. */
    get transaction(): Transaction {
        return Transaction.from({
            ...this,
            signatures: undefined,
            context_free_data: undefined,
        })
    }
 
    get id(): Checksum256 {
        return this.transaction.id
    }
 
    static from(object: SignedTransactionType) {
        return super.from({
            signatures: [],
            context_free_data: [],
            ...object,
        }) as SignedTransaction
    }
}
 
export type PackedTransactionType =
    | PackedTransaction
    | {
          signatures?: SignatureType[]
          compression?: UInt8Type
          packed_context_free_data?: BytesType
          packed_trx: BytesType
      }
 
@Struct.type('packed_transaction')
export class PackedTransaction extends Struct {
    @Struct.field('signature[]') declare signatures: Signature[]
    @Struct.field('uint8') declare compression: UInt8
    @Struct.field('bytes') declare packed_context_free_data: Bytes
    @Struct.field('bytes') declare packed_trx: Bytes
 
    static from(object: PackedTransactionType) {
        return super.from({
            signatures: [],
            packed_context_free_data: '',
            compression: 0,
            ...object,
        }) as PackedTransaction
    }
 
    static fromSigned(signed: SignedTransaction) {
        const tx = Transaction.from(signed)
        return this.from({
            signatures: signed.signatures,
            packed_context_free_data: abiEncode({
                object: signed.context_free_data,
                type: 'bytes[]',
            }),
            packed_trx: abiEncode({object: tx}),
        }) as PackedTransaction
    }
 
    getTransaction(): Transaction {
        if (Number(this.compression) !== 0) {
            throw new Error('Transaction compression not supported yet')
        }
        return abiDecode({data: this.packed_trx, type: Transaction})
    }
 
    getSignedTransaction(): SignedTransaction {
        const transaction = this.getTransaction()
        // TODO: decode context free data
        return SignedTransaction.from({
            ...transaction,
            signatures: this.signatures,
        })
    }
}
 
@Struct.type('transaction_receipt')
export class TransactionReceipt extends Struct {
    @Struct.field('string') declare status: string
    @Struct.field('uint32') declare cpu_usage_us: UInt32
    @Struct.field('uint32') declare net_usage_words: UInt32
}