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.
Heavy arrows are the usual path. Every underlined name is a link to the page that explains it.
- Create a builder:
new TxBuilder({ network, provider }); see networks and providers, and offline use without a provider (params,costModels), which canbuild()and sign but cannot plan. - Choose who pays:
{ payer }is a wallet, an address or{ changeAddress, utxos }, given to each call; the payer signs by default. Name other signers insign()orrun(), and a submitter insubmit(),run()or on the builder (else the provider sends). - Give the parts (which part for what): outputs, inputs, time, mint, scripts, staking, certificates and governance.
- One transaction:
builder.build(parts, { payer })returns aBuiltTx; thensign(),submit()andawaitInclusion(), which waits for a block, not forfinal. - Or a plan of one or several transactions:
builder.plan(parts, { payer })returns aTxPlanand needs a provider that returns block heights (Use Blockfrost for Plans);run()it (it returns atincluded), thenrun({ waitFor: "final" })orrefresh()until the statuses showfinal;replan()pays what is left after a step that did not land; on a server, keep plans in a store. - Around the flow: a transaction from elsewhere (
fromCborandrestoregive aBuiltTx;requiredSignerslists its keys), tests, errors (TxError.is), options, and coming fromMeshTxBuilder.
Which Part for What
| Part | Use it to |
|---|---|
output, metadata, message, validity, guard.pubKey | send assets, add metadata, set validity, require a signer |
mint.native, mint.plutus | mint and burn assets |
spend.pubKey, collateral, collateralReturn | spend a particular output, set the collateral |
spend.plutus, spend.native, referenceInput | lock 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
Related
- Migrating to TxBuilder - move from the
MeshTxBuilderchain - Transaction Parser - parse and test transactions
- Providers - blockchain data providers
- Wallets - wallet integration options
- Package README - every part, option and error