Skip to main content

Tx

Represents a Cardano transaction. For transaction-building, see TxBuilder instead.

export interface Tx {
  addSignature(
    signature: Signature,
    verify: boolean
  ): Tx
  addSignatures(
    signatures: Signature[],
    verify: boolean
  ): Tx
  body: TxBody
  calcMinCollateral(
    params: NetworkParams,
    recalcMinBaseFee: boolean
  ): bigint
  calcMinFee(params: NetworkParams): bigint
  calcSize(forFeeCalculation: boolean): number
  clearMetadata(): Tx
  dump(): any
  hasValidationError: undefined
    | string
    | false
    | UplcRuntimeError
  id(): TxId
  isSmart(): boolean
  isValid(): boolean
  isValidSlot(slot: bigint): boolean
  kind: "Tx"
  metadata: undefined | TxMetadata
  recover(network: {getUtxo(id: TxOutputId): Promise<TxInput<SpendingCredential>>}): Promise<void>
  toCbor(forFeeCalculation: boolean): number[]
  validate(
    params: NetworkParams,
    options: TxValidationOptions
  ): void
  validateSignatures(): void
  validateUnsafe(
    params: NetworkParams,
    options: TxValidationOptions
  ): Tx
  witnesses: TxWitnesses
}

Properties

addSignature

Adds a signature created by a wallet. Only available after the transaction has been finalized. Optionally verifies that the signature is correct (defaults to true)

tx.addSignature satisfies (
  signature: Signature,
  verify: boolean
) => Tx

addSignatures

Adds multiple signatures at once. Only available after the transaction has been finalized. Optionally verifies each signature is correct (defaults to true)

tx.addSignatures satisfies (
  signatures: Signature[],
  verify: boolean
) => Tx

body

tx.body satisfies TxBody

calcMinCollateral

Returns a quantity in lovelace

tx.calcMinCollateral satisfies (
  params: NetworkParams,
  recalcMinBaseFee: boolean
) => bigint

calcMinFee

Returns a quantity in lovelace

tx.calcMinFee satisfies (params: NetworkParams) => bigint

calcSize

Number of bytes of CBOR encoding of Tx Is used for two things:

  • tx fee calculation
  • tx size validation
tx.calcSize satisfies (forFeeCalculation: boolean) => number

clearMetadata

Creates a new Tx without the metadata for client-side signing where the client can't know the metadata before tx-submission.

tx.clearMetadata satisfies () => Tx

dump

tx.dump satisfies () => any

hasValidationError

Indicates if a built transaction has passed all consistency checks.

  • null if the transaction hasn't been validated yet
  • false when the transaction is valid
  • a string with the error message if any validation check failed
  • a UplcRuntimeError in case of any UPLC script failure
tx.hasValidationError satisfies undefined
  | string
  | false
  | UplcRuntimeError

id

tx.id satisfies () => TxId

isSmart

tx.isSmart satisfies () => boolean

isValid

Indicates if the necessary signatures are present and valid

tx.isValid satisfies () => boolean

isValidSlot

Used by emulator to check if tx is valid.

tx.isValidSlot satisfies (slot: bigint) => boolean

kind

tx.kind satisfies "Tx"

metadata

tx.metadata satisfies undefined | TxMetadata

recover

Restores input information after deserializing a CBOR-encoded transaction A serialized tx throws away input information This must be refetched from the network if the tx needs to be analyzed

tx.recover satisfies (network: {getUtxo(id: TxOutputId): Promise<TxInput<SpendingCredential>>}) => Promise<void>

toCbor

Serializes a transaction. Note: Babbage still follows Alonzo for the Tx size fee. According to https://github.com/IntersectMBO/cardano-ledger/blob/cardano-ledger-spec-2023-04-03/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs#L316, the isValid field is omitted when calculating the size of the tx for fee calculation. This is to stay compatible with Mary (?why though, the txFeeFixed could've been changed instead?)

tx.toCbor satisfies (forFeeCalculation: boolean) => number[]

validate

Throws an error if the tx isn't valid Checks that are performed:

  • size of tx <= params.maxTxSize
  • body.fee >= calculated min fee
  • value is conserved (minus what is burned, plus what is minted)
  • enough collateral if smart
  • no collateral if not smart
  • all necessary scripts are attached
  • no redundant scripts are attached (only checked if strict=true)
  • each redeemer must have enough ex budget
  • total ex budget can't exceed max tx ex budget for either mem or cpu
  • each output contains enough lovelace (minDeposit)
  • the assets in the output values are correctly sorted (only checked if strict=true, because only needed by some wallets)
  • inputs are in the correct order
  • ref inputs are in the correct order
  • minted assets are in the correct order
  • staking withdrawals are in the correct order
  • metadatahash corresponds to metadata
  • metadatahash is null if there isn't any metadata
  • script data hash is correct Checks that aren't performed:
  • all necessary signatures are included (must done after tx has been signed)
  • validity time range, which can only be checked upon submission
tx.validate satisfies (
  params: NetworkParams,
  options: TxValidationOptions
) => void

validateSignatures

Throws an error if all necessary signatures haven't yet been added Separate from the other validation checks If valid: this.valid is mutated to true

tx.validateSignatures satisfies () => void

validateUnsafe

Validates the transaction without throwing an error if it isn't valid If the transaction doesn't validate, the tx's validationError will be set

tx.validateUnsafe satisfies (
  params: NetworkParams,
  options: TxValidationOptions
) => Tx

witnesses

tx.witnesses satisfies TxWitnesses