Cookbook
Complete TxBuilder recipes to copy: payments, tokens, scripts, shared signing, servers, staking, governance and tests.
Overview
Each recipe is one complete example for one job, where the task pages explain one part at a time. Recipes take the safe path: a transaction is built once and a retry is the same call on the same object, a transaction from another party is read before you sign it, and transactions that depend on each other go through a plan, or are chained with after. A plan is not atomic: a step may land while the next never does, so read the status of each step.
Before you start
Recipes use the names below, and a builder made like this:
import { TxBuilder } from "@meshsdk/core";
const builder = new TxBuilder({ network: "preprod", provider });plan() needs a provider that returns block heights and the validity flag. The plain BlockfrostProvider does not, and plan() throws CANNOT_OBSERVE: see Use Blockfrost for Plans. run() returns when the transaction is in a block. Add { waitFor: "final" } before you release goods or ship an order: see Read the Statuses.
| Name | What it is |
|---|---|
builder, provider | the builder above, and the provider it reads through |
wallet, partnerWallet | the MeshWallet that pays, and the wallet of the other party |
changeAddress, recipient, partner | addresses: the wallet's own, a payee, the other party's |
unit | a policy id plus an asset name in hex |
script | a Plutus script, { code, version } |
store, db | a plan store, and your own list of open payouts: see Server Recipes |
validator, words | the emulator's TxValidator, and a test wallet's mnemonic: see Test Recipes |
Payments
- Pay from a Browser Wallet - a user signs in a CIP-30 wallet, and a closed window changes nothing
- Pay Many Recipients - a payroll cut into batches, as the steps of one plan
- Send a Whole Wallet to a New Address - ada and tokens, less the fee, in one transaction
- Chain Payments Without Waiting - build each payment on the one before and send them in a row
- Make an Offer That Expires - sign a payment the recipient may send until its deadline
Tokens and NFTs
- Mint an NFT Collection - CIP-25 metadata under a native policy that closes
- Mint a CIP-68 Token - a reference token that holds the metadata, and a user token
- Burn Tokens - a negative quantity under the policy that minted them
Scripts
- Vest Funds and Claim Them Later - lock funds until a deadline, then unlock them
- Hold a Payment in Escrow - lock funds under a datum and spend them with a redeemer
- Deploy a Script Once and Use It by Reference - store a script in an output and name it instead of sending its code
Several Parties
- Share a Wallet with a Native Script - funds that two keys control
- Check a Partner's Swap Before You Sign - read a partner's transaction and sign it only if it is the deal
- Build Online, Sign on a Cold Machine - build online and sign where the key lives
Server Recipes
- Take a Payment on a Server - build for a user, collect the signature and send
- Finish Payouts After a Restart - a pass that finds every open payout and finishes it
Staking and Governance
- Register and Delegate in One Transaction - a stake key registered, delegated to a pool and its vote delegated
- Withdraw All Rewards - take out the whole reward balance
- Stop Staking and Take the Deposit Back - deregister a stake key and withdraw its rewards in one transaction
- Become a DRep and Vote - register a DRep and cast a vote
Test Recipes
- Test a Payment Plan on the Emulator - a plan run to final, and the balances
- Test That a Payout Finishes After a Restart - drop the builder after a send, resume the plan on a new one, and pay once
- Test an Expected Error - check the code of a refused build