Lucid can be run pretty much anywhere. From backend server, browser, to mobile. The library is written in Deno with TypeScript and can additionally be bundled into an NPM package or web bundle.
// Deno
import { Lucid } from "https://deno.land/x/lucid/mod.ts"
const lucid = await Lucid.new();
// Deno (TypeScript)
import { Lucid } from "https://deno.land/x/lucid/mod.ts"
const lucid: Lucid = await Lucid.new();
// Node.js
import { Lucid } from "lucid-cardano"
const lucid = await Lucid.new();
// Node.js (TypeScript)
import { Lucid } from "lucid-cardano"
const lucid: Lucid = await Lucid.new();
<!-- Web -->
<script type="module">
import { Lucid } from "https://unpkg.com/lucid-cardano/web/mod.js"
const lucid = await Lucid.new();
</script>
Don't bother anymore with balancing your transaction inputs/outputs, calculating fees and script costs. Lucid abstracts away all that complexity.
const tx = await lucid.newTx()
.payToAddress("addr..", {lovelace: 40000000n})
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
const tx = await lucid.newTx()
.payToAddress("addra..", {lovelace: 10000000n})
.payToAddress("addrb..", {lovelace: 20000000n})
.payToAddress("addrc..", {lovelace: 30000000n})
.payToAddress("addrd..", {lovelace: 40000000n})
.payToAddress("addre..", {lovelace: 50000000n})
.complete();
const tx = await lucid.newTx()
.collectFrom([scriptUtxo])
.payToAddress("addr..", {lovelace: 50000000n})
.attachSpendingValidator(multisigScript)
.complete();
const signedTx = await tx.assemble(["<sig_1>", "<sig_2>"]).complete();
const txHash = await signedTx.submit();
const [utxo] = await lucid.utxosAt("addr.."); // Contract address
const tx = await lucid.newTx()
.collectFrom([utxo], Data.empty()) // Redeemer
.payToContract("addr..", Data.to(new Constr(0, ["3131", 5n])), {
lovelace: 35000000n,
})
.complete();
const txA = lucid.newTx().addSigner("addr..");
const txB = (utxo) => lucid.newTx().collectFrom([utxo]);
const [utxo] = await lucid.wallet.getUtxos();
const tx = await lucid.newTx()
.payToAddress("addra..", {lovelace: 10000000n})
.compose(txA)
.compose(txB(utxo))
.complete();
Use one of the existing blockchain providers in Lucid or implement your own provider.
import { Lucid, Blockfrost } from "https://deno.land/x/lucid/mod.ts"
const lucid = await Lucid.new(
new Blockfrost(
"https://cardano-mainnet.blockfrost.io/api/v0",
"<project_id>",
),
);
import { Lucid, Kupmios } from "https://deno.land/x/lucid/mod.ts"
const lucid = await Lucid.new(
new Kupmios(
"http://localhost:1442",
"ws://localhost:1337",
),
);
import { Maestro, Lucid } from "https://deno.land/x/lucid/mod.ts";
const lucid = await Lucid.new(
new Maestro({
network: "Preprod", // For MAINNET: "Mainnet".
apiKey: "<Your-API-Key>", // Get yours by visiting https://docs.gomaestro.org/docs/Getting-started/Sign-up-login.
turboSubmit: false // Read about paid turbo transaction submission feature at https://docs.gomaestro.org/docs/Dapp%20Platform/Turbo%20Transaction.
}),
"Preprod", // For MAINNET: "Mainnet".
);
import { Lucid, Provider } from "https://deno.land/x/lucid/mod.ts"
class MyProvider implements Provider { ... }
const lucid = await Lucid.new(
new MyProvider(),
);
Select CIP-0030 compatible browser wallets, import a wallet from a private key or simply view a certain address.
const api = await window.cardano.nami.enable();
lucid.selectWallet(api);
const seedPhrase = lucid.utils.generateSeedPhrase();
lucid.selectWalletFromSeed(seedPhrase);
const privateKey = lucid.utils.generatePrivateKey();
lucid.selectWalletFromPrivateKey(privateKey);
lucid.selectWalletFrom({address:"addr..."})
Instead of having to deal with raw plutus data Lucid allows you to leverage the underlying data structures and primitives of JavaScript.
const ListingSchema = Data.Object({
owner: Data.Bytes(),
amount: Data.Integer(),
private: Data.Boolean(),
});
type Listing = Data.Static<typeof ListingSchema>;
const Listing = ListingSchema as unknown as Listing;
const listing = Data.to(
{ owner: "31313131313131", amount: 5252352323n, private: false },
Listing,
);
const listing: Listing = Data.from(
"d8799f47313131313131311b0000000139108943d87980ff",
Listing,
);
Get started with Lucid straightaway, no package management needed or whatsoever. But Node.js developers are not left behind, Lucid is also available as NPM package.
Since Lucid is JavaScript you get a lot of flexibility and ease of use out of the box. Off-chain code from apps can be reused, imported or composed together with other apps.
Run the library entirely locally without the need to fetch data from external APIs. Thanks to Aiken Lucid can even evaluate smart contracts.