Mesh LogoMesh

Cardano Transaction Builder

Build Cardano transactions from parts with TxBuilder: it selects inputs, adds change and sets the fee.

Note: Coming from MeshTxBuilder, the chain of calls? It is deprecated but still works. See Migrating to TxBuilder.

Overview

TxBuilder builds a Cardano transaction from a list of parts, such as output(...) or mint.native(...). You name a payer, and the builder selects inputs, adds change and sets the fee; for scripts it also adds collateral and execution units. Then you sign and submit. Examples use wallet (a wallet: a MeshWallet or BrowserWallet) and recipient (an address) from your application.

Quick Start

import { BlockfrostProvider, output, TxBuilder } from "@meshsdk/core";

// A provider reads the chain
const provider = new BlockfrostProvider("<YOUR_API_KEY>");
const builder = new TxBuilder({ network: "preprod", provider });

// Build: the builder picks inputs, adds change and sets the fee
const built = await builder.build(
  output({ address: recipient, value: { lovelace: 5_000_000n } }), // 5 ada
  { payer: wallet },
);

await built.sign(); // the payer signs
await built.submit(); // sent through the provider
console.log(built.txHash);

TxBuilder lives in @meshsdk/transaction, and @meshsdk/core re-exports it. Amounts are bigint. built.tx is the unsigned transaction as a hex string (CBOR). For other providers, see Providers.

How It Works

You make one builder; each build() or plan() call takes the parts and { payer }. Build one transaction, or plan one or several. Every underlined name in the scheme is a link to the page that explains it.

Working with TxBuilderCreate a builder1Create a builder (Transaction Basics)Create a buildernew TxBuilder({ network, provider })Networks and providers (Providers and Networks)Networks and providersOffline, no provider: params, costModels. Without a provider you can build and sign; plans and awaitInclusion() need one, and submit() needs a provider or a submitter (Offline and CBOR)Offline, no provider: params, costModelsWho pays2Who pays: the payer of each build() or plan(); the builder holds none (Transaction Basics)Who pays{ payer } of each call: a wallet, an address,or your own { changeAddress, utxos }Signers and submitter: signers are named in sign() or run(), the submitter in submit(), run() or on the builder (Signing and Submitting)Signers and submitter3Partsone part or a list; the builder picks inputs, adds change and sets the feeWhich part for what (this page)Which part for whatOutputs: send lovelace and assets (Transaction Basics)OutputsoutputInputs: give, limit and chain UTxOs (UTxOs and Inputs)Inputsspend.pubKeyTime: expiry, start time and slots (Time and Validity)TimevalidityMint: mint and burn assets (Mint and Burn Assets)Mintmint.nativemint.plutusScripts: lock, spend and read at a script (Smart Contracts)Scriptsspend.plutusspend.nativeStaking: withdraw rewards; the page also walks through stake keys, delegation and pools (Staking Transactions)Stakingwithdrawal.*Certificates: what the chain records, such as stake keys, delegation of stake and votes, pools, DReps and the committee (Certificates)Certificatescertificate.*Governance: DReps, votes and proposals (Governance Transactions)Governancevoteproposal.*4One transactionbuild, sign and send; you follow the outcomeusual pathanother pathBuild: build() signs nothing and sends nothing (Transaction Basics)Buildbuilder.build(parts, { payer })Result: the BuiltTx that build() returns (Transaction Basics)ResultBuiltTxSign: sign() (Signing and Submitting)Signsign()Send: submit() (Signing and Submitting)Sendsubmit()Wait for a block: one sighting, a rollback can undo it; to follow a payment to final, use a plan (Signing and Submitting)WaitawaitInclusion()5Or a planone or several transactions, sent in order, never rebuiltNeeds a provider with block heights: plan() reads the chain tip, so a builder without a provider cannot plan (Providers and Networks)Needs a provider with block heightsPlan: one list of parts is one step, main; an object of named steps is several. plan() builds every step, sends nothing, and needs a provider that returns block heights (Plans)Planbuilder.plan(parts, { payer })Result: the plan object (a TxPlan) that plan() returns, with run(), step() and status (Plans)ResultTxPlanRun: sign, send and wait until each step is in a block, included by default (Plans)Runrun()Statuses: read the status of a plan and of its steps (Plans)Statusesplan.statusFinal: run({ waitFor: "final" }) waits for confirmations; this status never changes (Plans)FinalwaitFor: "final"A step that does not land: replan() pays what is left (Plans)A step that does not land: replan()Plans in a store, on a server (Servers and Storage)Plans in a store, on a server6Around the flowA transaction from elsewhere: fromCbor() and restore() give a BuiltTx to sign and submit, or a plan step; requiredSigners() lists its keys (Offline and CBOR)A transactionfrom elsewherefromCbor, restore,requiredSignersTests with a local ledger (Testing on the Emulator)TestsTxEmulatorErrors: TxError.is checks a code (Errors)ErrorsTxError.isOptions of the builder, build() and plan() (Options)Optionsnew TxBuilder,build(), plan()Coming from MeshTxBuilder (Migrating to TxBuilder)Coming fromMeshTxBuilder

Heavy arrows are the usual path. Every underlined name is a link to the page that explains it.

  1. Create a builder: new TxBuilder({ network, provider }); see networks and providers, and offline use without a provider (params, costModels), which can build() and sign but cannot plan.
  2. Choose who pays: { payer } is a wallet, an address or { changeAddress, utxos }, given to each call; the payer signs by default. Name other signers in sign() or run(), and a submitter in submit(), run() or on the builder (else the provider sends).
  3. Give the parts (which part for what): outputs, inputs, time, mint, scripts, staking, certificates and governance.
  4. One transaction: builder.build(parts, { payer }) returns a BuiltTx; then sign(), submit() and awaitInclusion(), which waits for a block, not for final.
  5. Or a plan of one or several transactions: builder.plan(parts, { payer }) returns a TxPlan and needs a provider that returns block heights (Use Blockfrost for Plans); run() it (it returns at included), then run({ waitFor: "final" }) or refresh() until the statuses show final; replan() pays what is left after a step that did not land; on a server, keep plans in a store.
  6. Around the flow: a transaction from elsewhere (fromCbor and restore give a BuiltTx; requiredSigners lists its keys), tests, errors (TxError.is), options, and coming from MeshTxBuilder.

Which Part for What

PartUse it to
output, metadata, message, validity, guard.pubKeysend assets, add metadata, set validity, require a signer
mint.native, mint.plutusmint and burn assets
spend.pubKey, collateral, collateralReturnspend a particular output, set the collateral
spend.plutus, spend.native, referenceInputlock and spend at a script, read an output
withdrawal.*take staking rewards out
certificate.*change what the chain records: stake keys, delegation of stake and votes, pools, DReps, the committee
vote, proposal.*take part in governance

For signing, UTxOs, time, plans, offline use, testing and errors, see the cards below.

Pick a Task

On this page