Your first transaction
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();
💡
To balance the transaction and initiate coin selection, transactions always
need to end with .complete()
Sign
const signedTx = await tx.sign.withWallet().complete();
You could also choose to sign the transaction with a private key:
const signedTx = await tx.sign.withPrivateKey(privateKey).complete();
Submit
Lastly we submit the transaction:
const txHash = await signedTx.submit();
console.log(txHash);
💡
Remember to select a wallet before attempting to build and submit transactions. The wallet selection methods we discussed in previous section Core Concept 2: Choose Wallet should be implemented before building the transaction.
Putting everything together
import { Lucid, Blockfrost } from "@lucid-evolution/lucid";
const lucid = await Lucid(
new Blockfrost("https://cardano-preprod.blockfrost.io/api/v0", "<projectId>"),
"Preprod"
);
lucid.selectWallet.fromPrivateKey(privateKey);
const tx = await lucid
.newTx()
.pay.ToAddress("addr_testa...", { lovelace: 5000000n })
.pay.ToAddress("addr_testb...", { lovelace: 5000000n })
.complete();
const signedTx = await tx.sign.withWallet().complete();
const txHash = await signedTx.submit();
console.log(txHash);