Mesh LogoMesh

Migrating to TxBuilder

Move code from the deprecated MeshTxBuilder chain to TxBuilder parts, with before and after examples.

Overview

A transaction is now a list of parts, such as output(...) or mint.native(...). You give the list and a payer to builder.build(), and the builder picks inputs, adds change and sets the fee. MeshTxBuilder is deprecated but still works. Import the new names from @meshsdk/core, which re-exports @meshsdk/transaction.

ForgeScript, mConStr0, resolveScriptHash and the other Data and script helpers do not change. Before blocks use them without an import.

The Four Steps

  1. Make one TxBuilder with a network and a provider. See Basics.
  2. Turn each chain into a list of parts, one area at a time:
    • Basics - payer, outputs, metadata, validity, signers, signing and submitting
    • Minting - mint and burn with native and Plutus scripts
    • Smart Contracts - lock funds at a script and spend from it
    • Staking - register, delegate and withdraw
    • Governance - DRep certificates, votes and proposals
  3. Replace signTx and submitTx with sign() and submit(). See Build, Sign and Submit.
  4. Check the error codes. See Migration Errors and Differences.

A Payment, Before and After

Before:

import { MeshTxBuilder } from "@meshsdk/core";

const txBuilder = new MeshTxBuilder({ fetcher: provider });

const unsignedTx = await txBuilder
  .txOut(recipient, [{ unit: "lovelace", quantity: "5000000" }])
  .changeAddress(changeAddress).selectUtxosFrom(utxos).complete();
const signedTx = await wallet.signTx(unsignedTx);
const txHash = await wallet.submitTx(signedTx);

After:

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

const builder = new TxBuilder({ network: "preprod", provider });

const built = await builder.build(
  output({ address: recipient, value: { lovelace: 5_000_000n } }),
  { payer: wallet },
);
await built.sign();
await built.submit();
console.log(built.txHash);

Three rules hold on every page:

OldNew
A chain: .txOut(), .mint(), .txIn()A list of parts: output, mint.native, spend.plutus, ...
.changeAddress() and .selectUtxosFrom(){ payer }: a wallet, an address, or { changeAddress, utxos }
Amounts as strings: "5000000"Amounts as bigint: 5_000_000n

If You Keep MeshTxBuilder

MeshTxBuilder now runs on TxBuilder, so it throws some errors where it used to return a transaction the ledger refused, such as OUTPUT_BELOW_MIN_COIN and FEE_TOO_LOW. See If You Keep MeshTxBuilder.

Names in the Examples

Your application defines these names. Every page of this guide uses them the same way.

NameWhat it is
txBuilderthe old builder: new MeshTxBuilder({ fetcher: provider })
builderthe new builder: new TxBuilder({ network: "preprod", provider })
providera provider, such as BlockfrostProvider
wallet, mintingWallet, utxos, changeAddresswallets that sign (a MeshWallet, a BrowserWallet or a CIP-30 api), and the UTxOs and the change address of wallet
recipient, partner, owner, rewardAddressaddresses. rewardAddress is a stake address.
assetUtxo, collateralUtxoUTxOs from a provider or a wallet
script, policyCodea Plutus script ({ code, version }) and the CBOR of a policy script
refTxHashthe hash of a transaction that holds a reference script
unita token unit: policy id plus asset name in hex
keythe text of a signing key
params, costModelsprotocol parameters and cost models
drepId, poolId, actionTx, anchora DRep id, a pool id, the transaction hash of a governance action, and an anchor { url, dataHash }
partsa list of parts
built, unsignedTxthe BuiltTx from build() and the string from complete(), where an example starts from them

On this page