Class TransactionBuilder

A high-level builder for constructing Cardano transactions.

This class provides a fluent interface to incrementally add inputs, outputs, certificates, metadata, and other components to a transaction. It encapsulates the complexities of transaction assembly, fee calculation, and balancing.

Hierarchy

  • TransactionBuilderBase
    • TransactionBuilder

Properties

callbackState: { error?: unknown } = {}
native: Record<string, (...args: any[]) => any>
registeredHandlers: { id: number; type: InstanceType }[]
resources: BuilderResources

Accessors

Methods

  • Adds a certificate to the transaction.

    Certificates are used to perform various on-chain actions like staking, delegation, or registering entities like stake pools and DReps.

    Parameters

    • params: { certificate: Certificate; redeemer?: PlutusData }

      The parameters for the function.

      • certificate: Certificate

        The certificate object to add to the transaction.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer data required if the certificate action is controlled by a Plutus script.

    Returns this

    The builder instance for chaining.

  • Adds a Plutus datum to the transaction's witness set.

    This allows the datum to be referenced by its hash from a transaction output, satisfying a script's requirement without including the full datum directly in the output itself.

    Parameters

    • datum: PlutusData

      The PlutusData object to add to the transaction witnesses.

    Returns this

    The builder instance for chaining.

  • Adds an input to the transaction to be spent.

    Optionally, a redeemer and datum can be provided. These are typically required when spending a UTxO that is locked at a Plutus script address.

    Parameters

    • params: { datum?: PlutusData; redeemer?: PlutusData; utxo: UTxO }

      The parameters for the function.

      • Optionaldatum?: PlutusData

        Optional. The datum that was locking the script output.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer to use for unlocking the script.

      • utxo: UTxO

        The Unspent Transaction Output (UTxO) to be spent.

    Returns this

    The builder instance for chaining.

  • Adds a transaction output to send a specific value to an address.

    Parameters

    • output: TxOut

      The transaction output to add.

    Returns this

    The builder instance for chaining.

  • Adds a reference input to the transaction.

    Reference inputs allow Plutus scripts to inspect the data, datum, and script of a UTxO without actually spending it. This is useful for looking up on-chain information.

    Parameters

    • utxo: UTxO

      The UTxO to be used as a reference input.

    Returns this

    The builder instance for chaining.

  • Adds a script to the transaction's witness set.

    This is necessary when using a script that is not already available on-chain via a reference input, such as a minting policy or a native script for a required signer.

    Parameters

    • script: Script

      The script object (Plutus or Native) to add.

    Returns this

    The builder instance for chaining.

  • Adds a required signer to the transaction.

    Parameters

    • pkh: string

      The public key hash (PKH) of the required signer as a hex string.

    Returns this

    The builder instance for chaining.

  • Include a serialized sub-transaction and its resolved spending/reference inputs.

    Parameters

    • cbor: string
    • resolvedUtxos: UTxO[]

    Returns this

  • Finalizes the transaction by performing coin selection, balancing, and fee calculation. This is the final step in the building process.

    Returns Promise<string>

    string - The built transaction as a CBOR hex string.

    If the transaction build fails due to insufficient funds, invalid inputs, or other errors.

  • Adds a stake delegation certificate to the transaction.

    This delegates the staking power of a reward address to a specified stake pool. An optional redeemer can be provided for script-controlled stake credentials.

    Parameters

    • params: { poolId: string; redeemer?: PlutusData; rewardAddress: string | RewardAddress }

      The parameters for the function.

      • poolId: string

        The bech32-encoded ID of the stake pool to delegate to.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer for a script-controlled stake credential.

      • rewardAddress: string | RewardAddress

        The bech32-encoded stake address to delegate from.

    Returns this

    The builder instance for chaining.

  • Adds a voting delegation certificate to the transaction.

    This delegates the voting power of a stake address to a specified DRep (Decentralized Representative) for on-chain governance.

    Parameters

    • params: { drepId: string; redeemer?: PlutusData; rewardAddress: string | RewardAddress }

      The parameters for the function.

      • drepId: string

        The bech32-encoded ID of the DRep to delegate to. The DRep ID can be in CIP-129 format or CIP-105 format.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer for a script-controlled stake credential.

      • rewardAddress: string | RewardAddress

        The bech32-encoded stake address to delegate from.

    Returns this

    The builder instance for chaining.

  • Adds a DRep deregistration certificate to the transaction.

    This action retires the DRep, making them ineligible for future governance proposals. An optional redeemer can be provided for script-controlled DRep credentials.

    Parameters

    • params: { drepId: string; redeemer?: PlutusData }

      The parameters for the function.

      • drepId: string

        The bech32-encoded ID of the DRep to deregister. This can be in either CIP-105 or CIP-129 format.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer for a script-controlled DRep credential.

    Returns this

    The builder instance for chaining.

  • Adds a stake deregistration certificate to the transaction.

    This action retires the stake address and initiates the refund of the key deposit. An optional redeemer can be provided for script-controlled stake credentials.

    Parameters

    • params: { redeemer?: PlutusData; rewardAddress: string | RewardAddress }

      The parameters for the function.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer for a script-controlled stake credential.

      • rewardAddress: string | RewardAddress

        The stake address to deregister, as a bech32 string or a RewardAddress object.

    Returns this

    The builder instance for chaining.

  • Release the builder and its callbacks deterministically. Safe to call more than once.

    Returns void

  • Sets the transaction's expiration to a specific, absolute date and time.

    This function marks the transaction as invalid if it is not included in a block before the specified date.

    Parameters

    • date: Date

      The absolute date and time after which the transaction will be invalid.

    Returns this

    The builder instance for chaining.

    // Set the transaction to expire on New Year's Day, 2026.
    const expiryDate = new Date('2026-01-01T00:00:00Z');
    txBuilder.expiresAfter(expiryDate);
  • Sets the transaction to be valid for a specific duration from now.

    This is a convenience method that calculates a future expiration date and sets it. The transaction will be marked as invalid if it is not included in a block within the specified duration.

    Parameters

    • seconds: number

      The number of seconds from now that the transaction should remain valid.

    Returns this

    The builder instance for chaining.

    // Set the transaction to expire in 3 minutes (180 seconds) from now.
    txBuilder.setValidFor(180);
  • Retrieves the current reference count of the native object. This is primarily for debugging and diagnostic purposes.

    Returns number

    The reference count.

  • Adds a transaction output to lock a specific amount of lovelace at a script address.

    This is used to send funds to a smart contract, where the funds are locked until the script conditions are met. A datum must be provided to be attached to the script output.

    Parameters

    • params: { amount: number | bigint; datum?: Datum; scriptAddress: string | Address }

      The parameters for the function.

      • amount: number | bigint

        The amount of lovelace to lock.

      • Optionaldatum?: Datum

        The datum to attach to the output, which will be available to the script.

      • scriptAddress: string | Address

        The recipient script address as a bech32 string or an Address object.

    Returns this

    The builder instance for chaining.

  • Adds a transaction output to lock a value (lovelace and/or other assets) at a script address.

    This is used to send funds to a smart contract, where they are locked until the script conditions are met. A datum must be provided with the output.

    Parameters

    • params: { datum: Datum; scriptAddress: string | Address; value: Value }

      The parameters for the function.

      • datum: Datum

        The datum to attach to the output, which will be available to the script.

      • scriptAddress: string | Address

        The recipient script address as a bech32 string or an Address object.

      • value: Value

        The value object, containing the coins and/or other assets to lock.

    Returns this

    The builder instance for chaining.

  • Adds a token minting or burning operation to the transaction using a concatenated Asset ID.

    Parameters

    • params: { amount: number | bigint; assetIdHex: string; redeemer?: PlutusData }

      The parameters for the function.

      • amount: number | bigint

        The quantity to mint (positive integer) or burn (negative integer).

      • assetIdHex: string

        The hex-encoded Asset ID (concatenation of the policy ID and asset name).

      • Optionalredeemer?: PlutusData

        Optional. The redeemer required if the minting policy is a Plutus script.

    Returns this

    The builder instance for chaining.

  • Mint or burn tokens with a deferred redeemer.

    Parameters

    Returns this

  • Pads the transaction with space for a specified number of additional signers.

    This is used for fee calculation purposes to ensure the final transaction fee is sufficient to cover signatures that will be added after the transaction is built (e.g., by co-signers). This method does not add any actual required signers to the transaction witnesses.

    Parameters

    • count: number

      The number of additional signer "slots" to account for in the fee calculation.

    Returns this

    The builder instance for chaining.

  • Adds a proposal to update the constitutional committee to the transaction.

    This governance action can add new members, remove existing members, and change the required quorum for committee votes.

    Parameters

    • params: {
          anchor: Anchor;
          governanceActionId?: GovernanceActionId;
          membersToBeAdded: CommitteeMembers;
          membersToBeRemoved: CredentialSet;
          newQuorum: UnitInterval;
          rewardAddress: string | RewardAddress;
      }

      The parameters for the function.

      • anchor: Anchor

        The anchor containing the URL and hash of off-chain metadata for the proposal.

      • OptionalgovernanceActionId?: GovernanceActionId

        Optional. The ID of the most recent hard fork initiation action. Can be undefined if this is the first proposal of this type on the network.

      • membersToBeAdded: CommitteeMembers

        An array of CommitteeMember objects (credential and epoch) for new members.

      • membersToBeRemoved: CredentialSet

        An array of Credential objects for committee members to be removed.

      • newQuorum: UnitInterval

        The new quorum for the committee, as a unit interval (e.g., { numerator: 1n, denominator: 2n }).

      • rewardAddress: string | RewardAddress

        The reward address where the governance action deposit will be refunded.

    Returns this

    The builder instance for chaining.

  • Adds a hard fork initiation proposal to the transaction.

    This governance action is used to propose an upgrade to a new major protocol version.

    Parameters

    • params: {
          anchor: Anchor;
          governanceActionId?: GovernanceActionId;
          rewardAddress: string | RewardAddress;
          version: ProtocolVersion;
      }

      The parameters for the function.

      • anchor: Anchor

        The anchor containing the URL and hash of off-chain metadata for the proposal.

      • OptionalgovernanceActionId?: GovernanceActionId

        Optional. The ID of the most recent hard fork initiation action. Can be undefined if this is the first proposal of this type on the network.

      • rewardAddress: string | RewardAddress

        The reward address where the governance action deposit will be refunded.

      • version: ProtocolVersion

        The protocol version to upgrade to.

    Returns this

    The builder instance for chaining.

  • Adds an informational governance action proposal to the transaction.

    This type of action has no direct on-chain effect but serves to record information or a statement on the blockchain, retrievable via its metadata anchor.

    Parameters

    • params: { anchor: Anchor; rewardAddress: string | RewardAddress }

      The parameters for the function.

      • anchor: Anchor

        The anchor containing the URL and hash of the off-chain information.

      • rewardAddress: string | RewardAddress

        The reward address where the governance action deposit will be refunded.

    Returns this

    The builder instance for chaining.

  • Adds a new constitution proposal to the transaction.

    This governance action proposes to replace the current on-chain constitution with a new one.

    Parameters

    • params: {
          anchor: Anchor;
          constitution: Constitution;
          governanceActionId?: GovernanceActionId;
          rewardAddress: string | RewardAddress;
      }

      The parameters for the function.

      • anchor: Anchor

        The anchor containing the URL and hash of off-chain metadata for this specific proposal.

      • constitution: Constitution

        The new constitution being proposed, which includes its own anchor and an optional script hash.

      • OptionalgovernanceActionId?: GovernanceActionId

        Optional. The ID of the most recent hard fork initiation action. Can be undefined if this is the first proposal of this type on the network.

      • rewardAddress: string | RewardAddress

        The reward address where the governance action deposit will be refunded.

    Returns this

    The builder instance for chaining.

  • Adds a "no confidence" proposal to the transaction.

    This governance action is used to express a lack of confidence in the current constitutional committee.

    Parameters

    • params: {
          anchor: Anchor;
          governanceActionId?: GovernanceActionId;
          rewardAddress: string | RewardAddress;
      }

      The parameters for the function.

      • anchor: Anchor

        The anchor containing the URL and hash of off-chain metadata for the proposal.

      • OptionalgovernanceActionId?: GovernanceActionId

        Optional. The ID of the most recent hard fork initiation action. Can be undefined if this is the first proposal of this type on the network.

      • rewardAddress: string | RewardAddress

        The reward address where the governance action deposit will be refunded.

    Returns this

    The builder instance for chaining.

  • Adds a protocol parameter change proposal to the transaction.

    This governance action is used to propose modifications to the network's protocol parameters.

    Parameters

    • params: {
          anchor: Anchor;
          governanceActionId?: GovernanceActionId;
          policyHash: string;
          protocolParamUpdate: Partial<ProtocolParameters>;
          rewardAddress: string | RewardAddress;
      }

      The parameters for the function.

      • anchor: Anchor

        The anchor containing the URL and hash of off-chain metadata for the proposal.

      • OptionalgovernanceActionId?: GovernanceActionId

        Optional. The ID of the most recent parameter change action. Can be undefined if this is the first proposal of this type on the network.

      • policyHash: string

        The hex-encoded hash of the governance action policy script (guardrails script).

      • protocolParamUpdate: Partial<ProtocolParameters>

        An object containing the specific protocol parameters to be updated.

      • rewardAddress: string | RewardAddress

        The reward address where the governance action deposit will be refunded.

    Returns this

    The builder instance for chaining.

  • Adds a treasury withdrawals proposal to the transaction.

    This governance action proposes the withdrawal of funds from the treasury to be distributed to a set of specified reward accounts.

    Parameters

    • params: {
          anchor: Anchor;
          policyHash?: string;
          rewardAddress: string | RewardAddress;
          withdrawals: Withdrawals;
      }

      The parameters for the function.

      • anchor: Anchor

        The anchor containing the URL and hash of off-chain metadata for the proposal.

      • OptionalpolicyHash?: string

        Optional. The hex-encoded hash of the governance action policy script (guardrails script).

      • rewardAddress: string | RewardAddress

        The reward address where the governance action deposit will be refunded.

      • withdrawals: Withdrawals

        A map where keys are the Bech32-encoded reward addresses of the recipients and values are the corresponding lovelace amounts to be withdrawn.

    Returns this

    The builder instance for chaining.

  • Adds a DRep registration certificate to the transaction.

    Parameters

    • params: { anchor: Anchor; drepId: string; redeemer?: PlutusData }

      The parameters for the function.

      • anchor: Anchor

        An object containing the URL and hash of the DRep's metadata.

      • drepId: string

        The bech32-encoded ID of the DRep to delegate to. The DRep ID can be in CIP-129 format or CIP-105 format.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer for a script-controlled DRep credential.

    Returns this

    The builder instance for chaining.

  • Adds a stake registration certificate to the transaction.

    This allows the specified stake address to start participating in staking and receiving rewards. An optional redeemer can be provided for script-controlled stake credentials.

    Parameters

    • params: { redeemer?: PlutusData; rewardAddress: string | RewardAddress }

      The parameters for the function.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer for a script-controlled stake credential.

      • rewardAddress: string | RewardAddress

        The stake address to register, as a bech32 string or a RewardAddress object.

    Returns this

    The builder instance for chaining.

  • Adds a transaction output to send a specific amount of lovelace to an address.

    Parameters

    • params: { address: string | Address; amount: number | bigint }

      The parameters for the function.

      • address: string | Address

        The recipient's address as a bech32 string or an Address object.

      • amount: number | bigint

        The amount of lovelace to send.

    Returns this

    The builder instance for chaining.

  • Adds a transaction output to send a value (lovelace and/or other assets) to an address.

    Parameters

    • params: { address: string | Address; value: Value }

      The parameters for the function.

      • address: string | Address

        The recipient's address as a bech32 string or an Address object.

      • value: Value

        The value object, containing the coins and/or other assets to send.

    Returns this

    The builder instance for chaining.

  • Sets the change address for the transaction. This is where any remaining funds from inputs will be sent after covering outputs and fees.

    Parameters

    • address: string | Address

      The change address as a bech32 string or an Address object.

    Returns this

    The builder instance for chaining.

  • Sets a custom coin selection strategy for the transaction.

    The coin selector determines how UTxOs are chosen to cover the transaction's required value. If this method is not called, the builder will use a default coin selection strategy ('round-robin random-improve').

    Parameters

    • coinSelector: CoinSelector

      The coin selection strategy to use.

    Returns this

    The builder instance for chaining.

  • Sets the collateral change address for the transaction.

    This address specifies where any remaining balance from collateral inputs will be sent. Collateral is required for transactions involving Plutus scripts.

    Parameters

    • address: string | Address

      The collateral return address as a bech32 string or an Address object.

    Returns this

    The builder instance for chaining.

  • Sets a specific list of UTXOs to be used for collateral.

    Collateral is required for transactions involving Plutus scripts. If this method is not called, the builder will attempt to select collateral from the general UTXO set provided via the setUtxos method.

    Parameters

    • utxos: UTxO[]

      An array of UTxOs to be designated for collateral.

    Returns this

    The builder instance for chaining.

  • Sets the donation amount for the transaction being built.

    Donations are a Conway-era feature that contribute a specific amount to the treasury.

    Parameters

    • donation: number | bigint

      The donation amount in lovelace.

    Returns this

    The builder instance for chaining.

  • Sets the transaction's expiration, also known as Time To Live (TTL).

    This function marks the transaction as invalid if it is not included in a block before the specified time.

    Parameters

    • slot: number | bigint

      The absolute slot number after which this transaction will be invalid.

    Returns this

    The builder instance for chaining.

  • Sets the transaction's validity start time using an absolute slot number.

    This function marks the transaction as invalid if it is included in a block created before the specified slot.

    Parameters

    • slot: number | bigint

      The absolute slot number before which this transaction is invalid.

    Returns this

    The builder instance for chaining.

  • Attaches metadata to the transaction under a specific label (tag).

    Parameters

    • params: { metadata: string | object; tag: number | bigint }

      The parameters for the function.

      • metadata: string | object

        The metadata content. Can be a plain object, or a pre-serialized JSON string.

      • tag: number | bigint

        A unique integer label for this piece of metadata.

    Returns this

    The builder instance for chaining.

  • Sets a minimum transaction fee.

    This function allows you to specify a minimum fee for the transaction in lovelace. The final fee calculated by the builder during the build process will be at least this amount.

    Parameters

    • fee: number | bigint

      The minimum fee amount in lovelace.

    Returns this

    The builder instance for chaining.

  • Sets the network ID for the transaction.

    This specifies the target network (e.g., Mainnet or a Testnet) for which the transaction is intended.

    Parameters

    • networkId: NetworkId

      The network ID to assign to the transaction.

    Returns this

    The builder instance for chaining.

  • Sets a custom transaction evaluator.

    The evaluator is responsible for calculating the execution units (memory and steps) required for any Plutus scripts in the transaction. If this method is not called, the builder will use the native Plutus evaluator.

    Parameters

    • txEvaluator: TxEvaluator

      The transaction evaluator to use.

    Returns this

    The builder instance for chaining.

  • Sets the available UTXOs for the builder to use for coin selection.

    Parameters

    • utxos: UTxO[]

      An array of available UTxOs.

    Returns this

    The builder instance for chaining.

  • Adds an update DRep certificate in the transaction.

    Parameters

    • params: { anchor: Anchor; drepId: string; redeemer?: PlutusData }

      The parameters for the function.

      • anchor: Anchor

        An object containing the URL and hash of the DRep's metadata.

      • drepId: string

        The bech32-encoded ID of the DRep to deregister. This can be in either CIP-105 or CIP-129 format.

      • Optionalredeemer?: PlutusData

        Optional redeemer data for script-based DReps.

    Returns this

    The builder instance for chaining.

  • Sets the transaction to become valid only after a specified duration from now. This is a convenience method that calculates a future start date.

    Parameters

    • seconds: number

      The number of seconds from now to wait before the transaction becomes valid.

    Returns this

    The builder instance for chaining.

    // Make the transaction valid only after 1 hour (3600 seconds) has passed.
    txBuilder.validAfter(3600);
  • Sets the transaction's validity start time to a specific, absolute date and time. The transaction will be invalid if processed before this date.

    Parameters

    • date: Date

      The absolute date and time from which the transaction will be valid.

    Returns this

    The builder instance for chaining.

    // Make the transaction valid starting on New Year's Day, 2026.
    const startDate = new Date('2026-01-01T00:00:00Z');
    txBuilder.validFromDate(startDate);
  • Adds a vote for a specific governance action to the transaction.

    This function allows a voter to submit their vote for a given governance action.

    Parameters

    • params: {
          actionId: string | GovernanceActionId;
          redeemer?: PlutusData;
          voter: Voter;
          votingProcedure: VotingProcedure;
      }

      The parameters for the function.

      • actionId: string | GovernanceActionId

        The ID of the governance action being voted on.

      • Optionalredeemer?: PlutusData

        Optional. Redeemer data required if the voter is script-controlled.

      • voter: Voter

        The voter participating in the governance action.

      • votingProcedure: VotingProcedure

        The voting procedure, including the vote choice.

    Returns this

    The builder instance for chaining.

  • Adds a withdrawal from a reward account to the transaction.

    The amount specified must be the full available reward balance for the account. An optional redeemer can be provided for script-locked withdrawals.

    Parameters

    • params: {
          amount: number | bigint;
          redeemer?: PlutusData;
          rewardAddress: string | RewardAddress;
      }

      The parameters for the function.

      • amount: number | bigint

        The full amount of rewards to withdraw in lovelace.

      • Optionalredeemer?: PlutusData

        Optional. The redeemer for a script-locked withdrawal.

      • rewardAddress: string | RewardAddress

        The reward account address as a bech32 string or a RewardAddress object.

    Returns this

    The builder instance for chaining.