First transaction
A couple fundamentals to remember are that in Cardano's eUTxO model, a transaction can consume one or more UTxOs as inputs. Create one or more UTxOs as outputs and that a transaction must be balanced (sum of inputs = sum of outputs + fees).
Build
Let's create a simple transaction where we send 5 ADA
to two recipients each:
const tx = await lucid
.newTx()
.pay.ToAddress("addr_testa...", { lovelace: 5000000n })
.pay.ToAddress("addr_testb...", { lovelace: 5000000n })
.complete();
Sign
const signedTx = await tx.sign.withWallet().complete();
Submit
const txHash = await signedTx.submit();
⚠️
Remember to select a wallet before attempting to build and submit transactions.
Putting it all together
import { Lucid, Koios, generateSeedPhrase } from "@lucid-evolution/lucid";
// Initialize Lucid with a provider
const lucid = await Lucid(
new Koios("https://preprod.koios.rest/api/v1"),
"Preprod"
);
const seedPhrase = generateSeedPhrase(); // BIP-39
lucid.selectWallet.fromSeed(seedPhrase); // Select a wallet for signing
// Build, sign and submit transaction
const tx = await lucid
.newTx()
.pay.ToAddress("addr_testa...", { lovelace: 5000000n }) // Pay 5 ADA to addr_testa...
.pay.ToAddress("addr_testb...", { lovelace: 5000000n }) // Pay 5 ADA to addr_testb...
.complete(); // Balance the transaction and initiate UTxO selection
const signedTx = await tx.sign.withWallet().complete();
const txHash = await signedTx.submit();
console.log("Transaction Submitted:", txHash);