Smart Contracts
Lock assets at a script address and spend them with a Plutus script, a reference script or a native script.
Overview
A script holds assets through an ordinary output at its address, and a spend.plutus part unlocks them. The builder reads the datum from the output, adds collateral (ada the network can take if a script fails), evaluates execution units and orders inputs and redeemers. You do not set them. Use this page to lock funds at a script or to spend them.
Examples stop at build(): call built.sign() and built.submit() as in Transaction Basics. They use builder, wallet and recipient from there, and these names, which your application defines:
changeAddress- the wallet's address, fromawait wallet.getChangeAddress()script- a Plutus script,{ code, version }scriptUtxo- an output locked at that scriptnativeUtxo- an output locked at a native script's addressrefTxHash- the hash of a transaction that holds a script in one of its outputs
Lock Assets at a Script
A datum is data stored with an output so a script can read it. builder.scriptAddress(script) gives the script's address on the builder's network. Send assets there with an inline datum. For a datum hash use datum: { type: "hash", data }, and add embed: true to publish the datum with the transaction.
import { mConStr0, output } from "@meshsdk/core";
const built = await builder.build(
output({
address: builder.scriptAddress(script),
value: { lovelace: 10_000_000n },
datum: { type: "inline", data: mConStr0([]) },
}),
{ payer: wallet },
);Spend from a Script
Give spend.plutus the output, the script and a redeemer (the data you give the script when you spend).
- Find the output with your provider's
fetchAddressUTxOs(address), or name it by{ txHash, outputIndex }. - The datum is read from the output. For an output that holds only a datum hash, add
datum: { data }. - The assets go to the payer's change address unless an
outputpart sends them elsewhere. - The builder picks collateral: see Choose the Collateral.
import { guard, mConStr0, spend } from "@meshsdk/core";
const built = await builder.build(
[
spend.plutus({
utxo: scriptUtxo,
script,
redeemer: { data: mConStr0([]) },
}),
guard.pubKey({ address: changeAddress }), // for a script that checks a signer
],
{ payer: wallet },
);A redeemer may be a function of the final transaction, for a script that wants positions. tx.self is the index of the spend itself, and tx.output(label) the index of the output with that label. Such a part cannot be recorded by a plan store.
import { mConStr0, output, spend } from "@meshsdk/core";
const built = await builder.build(
[
spend.plutus({
utxo: scriptUtxo,
script,
redeemer: { data: (tx) => mConStr0([tx.self, tx.output("payout")]) },
}),
output({ address: recipient, value: { lovelace: 5_000_000n }, label: "payout" }),
],
{ payer: wallet },
);Deploy and Use a Reference Script
A reference script is a script stored in an output, so a transaction does not send its code again. Put one on chain by attaching it to an output with scriptRef. Leave lovelace out and the output holds the minimum it needs. Later, name that output in script instead of sending the code, and the builder adds it as a reference input.
Lock the output at an address the payer does not pay from, or keep it out of selection with canSelect: (c) => !c.script. Otherwise a later build by the same payer can spend it.
import { mConStr0, output, spend } from "@meshsdk/core";
const deploy = await builder.build(
output({ address: changeAddress, value: {}, scriptRef: script }),
{ payer: wallet },
);
// sign and submit deploy once; its txHash is the refTxHash below
const built = await builder.build(
spend.plutus({
utxo: scriptUtxo,
script: { reference: { txHash: refTxHash, outputIndex: 0 } },
redeemer: { data: mConStr0([]) },
}),
{ payer: wallet, canSelect: (c) => !c.script },
);Minting works the same way: see Mint and Burn Assets.
Read an Output by Reference
A reference input puts an output into the transaction without spending it: it has no redeemer, runs no script of its own, and stays where it is. A Plutus script sees it in its context among the reference inputs, with its address, value and datum, so it can read data published there, such as an oracle price or a configuration. That datum belongs to the reference input, not to the output being spent, which has its own. Of a reference script held there, the context shows only its hash, not the script. The rules for choosing one are in Read an Output without Spending It.
import { mConStr0, referenceInput, spend } from "@meshsdk/core";
const built = await builder.build(
[
spend.plutus({
utxo: scriptUtxo,
script,
redeemer: { data: mConStr0([]) },
}),
referenceInput({ utxo: { txHash: refTxHash, outputIndex: 1 } }),
],
{ payer: wallet },
);Spend from a Native Script
spend.native takes a NativeScript, its CBOR or a reference. Native scripts need no redeemer and no collateral. The payer signs by default. Keys the script names that the payer does not hold go in built.sign({ signers: [wallet, keySigner(key)] }), and built.missing lists the keys that have not signed yet.
import { ForgeScript, spend } from "@meshsdk/core";
const built = await builder.build(
spend.native({
utxo: nativeUtxo,
script: ForgeScript.withOneSignature(changeAddress),
}),
{ payer: wallet },
);Lock and Unlock with a Plan
A plan sends several transactions in order, waits for each to land, and can be repeated safely. The unlock step names the output of the lock step by its label. The change of the lock step pays for the unlock. A plan needs a provider that returns block heights and the validity flag: see Providers and Networks.
import { mConStr0, output, spend } from "@meshsdk/core";
const plan = await builder.plan(
{
lock: output({
address: builder.scriptAddress(script),
value: { lovelace: 10_000_000n },
datum: { type: "inline", data: mConStr0([]) },
label: "vault",
}),
unlock: [
spend.plutus({
utxo: { step: "lock", output: "vault" },
script,
redeemer: { data: mConStr0([]) },
}),
spend.pubKey({ utxo: { step: "lock", change: true } }),
],
},
{ payer: wallet },
);
await plan.run(); // lock, a block, then unlockRelated
- Transaction Basics - the builder, outputs, signing and submitting
- Mint and Burn Assets - native and Plutus minting policies
- UTxOs and Inputs - read an output, choose collateral and inputs
- Plans - lock and unlock in order, with links between steps
- Testing on the Emulator - test that a script accepts and refuses
- Staking Transactions - scripts that run from withdrawals and certificates
- Migrating to TxBuilder - move from the
MeshTxBuilderchain - Data Types - build datums and redeemers
- Blueprints - get a script's code from a blueprint
- Transaction Parser - read and test transactions
- Package README - every part, option and error