### Full Transaction Example with Lucid Initialization Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/basics/your-first-tx.md This comprehensive example shows the complete workflow: initializing Lucid with Blockfrost, selecting a wallet, creating, signing, and submitting a transaction. Replace `` with your actual Blockfrost project ID. ```javascript import { Blockfrost, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = await Lucid.new( new Blockfrost("https://cardano-preprod.blockfrost.io/api/v0", ""), "Preprod", ); lucid.selectWalletFromPrivateKey(privateKey); const tx = await lucid.newTx() .payToAddress("addr_testa...", { lovelace: 5000000n }) .payToAddress("addr_testb...", { lovelace: 5000000n }) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); console.log(txHash); ``` -------------------------------- ### Install Lucid via NPM Source: https://github.com/jpg-store/lucid/blob/main/README.md Install the Lucid library for use in Node.js projects. ```bash npm install lucid-cardano ``` -------------------------------- ### Build Lucid Docs Locally Source: https://github.com/jpg-store/lucid/blob/main/docs/README.md Run this command to build the Lucid project documentation locally. Ensure you have Deno installed and the project dependencies are set up. ```sh deno task build ``` -------------------------------- ### Initialize Lucid with a Custom Provider Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md This example shows how to initialize Lucid with a custom provider. You need to implement the Provider interface yourself. ```js import { Lucid } from "https://deno.land/x/lucid/mod.ts" class MyProvider { ... } const lucid = await Lucid.new( new MyProvider(), "Preprod" ); ``` -------------------------------- ### Basic Lucid Transaction Example Source: https://github.com/jpg-store/lucid/blob/main/README.md Demonstrates basic Lucid usage for creating and submitting a Cardano transaction. Assumes a browser environment with Nami wallet enabled. For NPM usage, import 'lucid-cardano'. ```javascript // import { Blockfrost, Lucid } from "https://deno.land/x/lucid@0.10.7/mod.ts"; Deno import { Blockfrost, Lucid } from "lucid-cardano"; // NPM const lucid = await Lucid.new( new Blockfrost("https://cardano-preview.blockfrost.io/api/v0", ""), "Preview", ); // Assumes you are in a browser environment const api = await window.cardano.nami.enable(); lucid.selectWallet(api); const tx = await lucid.newTx() .payToAddress("addr...", { lovelace: 5000000n }) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); console.log(txHash); ``` -------------------------------- ### Import Lucid in NPM/Node.js Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/overview/import.md Import Lucid in your Node.js project after installation. Remember to set `"type": "module"` in your package.json. ```javascript import { Lucid } from "lucid-cardano"; const lucid = await Lucid.new(); ``` -------------------------------- ### Single-line Comment (TypeScript) Source: https://github.com/jpg-store/lucid/blob/main/CONTRIBUTING.md Use single-line comments starting with `//` for brief explanations. ```typescript // This is a comment. ``` -------------------------------- ### Get Wallet Address Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Retrieve the Bech32 encoded address of the currently selected wallet. ```javascript const address = await lucid.wallet.address(); //Bech32 address ``` -------------------------------- ### Initialize Emulator and Lucid Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/test-emulate.md Instantiate the emulator and Lucid provider. This allows for easy substitution with a real network provider. ```javascript const emulator = new Emulator([{ address: "addr_test...", assets: { lovelace: 3000000000n }, }]); const lucid = await Lucid.new(emulator); ``` -------------------------------- ### Serve Lucid Docs Locally Source: https://github.com/jpg-store/lucid/blob/main/docs/README.md Run this command to serve the Lucid project documentation locally for development. This allows you to preview changes as you make them. ```sh deno task serve ``` -------------------------------- ### Generate Lucid Documentation Source: https://github.com/jpg-store/lucid/blob/main/README.md Generate documentation for the Lucid library using Deno's documentation tool. ```bash deno doc ``` -------------------------------- ### Instantiate Lucid with Blockfrost Provider Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/basics/instantiate-lucid.md Instantiate Lucid with the Blockfrost provider for the Preprod network. Replace '' with your actual Blockfrost project ID. ```javascript import { Blockfrost, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = await Lucid.new( new Blockfrost("https://cardano-preprod.blockfrost.io/api/v0", ""), "Preprod", ); ``` -------------------------------- ### Initialize Lucid with Maestro Provider Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md Configure Lucid to use the Maestro provider. Remember to replace '' with your key obtained from Maestro and set the correct network ('Preprod' or 'Mainnet'). ```js import { Lucid, Maestro } from "https://deno.land/x/lucid/mod.ts"; const lucid = await Lucid.new( new Maestro({ network: "Preprod", // For MAINNET: "Mainnet". apiKey: "", // 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". ); ``` -------------------------------- ### Select Wallet from Browser Extension (Nami) Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-wallet.md This method is for browser environments and works with any CIP-0030 compliant wallet, such as Nami. It requires enabling the wallet's API. ```javascript const api = await window.cardano.nami.enable(); lucid.selectWallet(api); ``` -------------------------------- ### Initialize Lucid with Kupmios Provider Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md Connect Lucid to the Cardano network using the Kupmios provider, which combines Ogmios and Kupo. Ensure Ogmios and Kupo are running locally. ```js import { Kupmios, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = await Lucid.new( new Kupmios( "http://localhost:1442", "ws://localhost:1337", ), "Preprod", ); ``` -------------------------------- ### Initialize Lucid with Blockfrost Provider Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md Use this snippet to connect Lucid to the Cardano network via the Blockfrost provider. Replace '' with your actual Blockfrost project ID. ```js import { Blockfrost, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = await Lucid.new( new Blockfrost( "https://cardano-preprod.blockfrost.io/api/v0", "", ), "Preprod", ); ``` -------------------------------- ### Import Lucid in Web Environment Source: https://github.com/jpg-store/lucid/blob/main/README.md Import the Lucid library for use in web projects via a script tag. ```html ``` -------------------------------- ### Import Lucid Locally in Deno Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/overview/import.md Import Lucid directly from the local file system when working with Deno after cloning the repository. ```javascript import { Lucid } from "./mod.ts"; ``` -------------------------------- ### Query UTxOs using Lucid convenience method Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md A more convenient way to query UTxOs for an address directly through Lucid. ```js const utxos = await lucid.utxosAt("addr_test..."); ``` -------------------------------- ### Import Lucid in Deno Source: https://github.com/jpg-store/lucid/blob/main/README.md Import the Lucid library for use in Deno projects. ```javascript import { Lucid } from "https://deno.land/x/lucid@0.10.7/mod.ts"; ``` -------------------------------- ### Import Lucid in Deno Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/overview/import.md Use this snippet to import Lucid when working with Deno. It's recommended to include version tags for stability. ```javascript import { Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = await Lucid.new(); ``` -------------------------------- ### Clone Lucid Repository Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/overview/import.md Clone the Lucid repository from GitHub to build it locally. ```bash git clone https://github.com/spacebudz/lucid.git ``` -------------------------------- ### Run Lucid Tests Source: https://github.com/jpg-store/lucid/blob/main/README.md Execute the test suite for the Lucid library. ```bash deno task test ``` -------------------------------- ### Generate a Private Key Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/basics/create-wallet.md Use this method to generate a new private key. The generated key is Bech32 encoded. This is the first step before selecting a wallet. ```javascript const privateKey = lucid.utils.generatePrivateKey(); //Bech32 encoded private key console.log(privateKey); ``` -------------------------------- ### Build Lucid Core Components Source: https://github.com/jpg-store/lucid/blob/main/README.md Build the core components of the Lucid library, which are written in Rust. ```bash deno task build:core ``` -------------------------------- ### Select Wallet from Private Key Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Use this method to select a wallet by providing a Bech32 encoded private key. This is a common way to manage your wallet securely. ```javascript const privateKey = lucid.utils.generatePrivateKey(); //Bech32 encoded private key lucid.selectWalletFromPrivateKey(privateKey); ``` -------------------------------- ### Distribute Staking Rewards with Emulator Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/test-emulate.md Conveniently distribute rewards to all delegated stake addresses within the emulator. ```javascript emulator.distributeRewards(100000000n); ``` -------------------------------- ### Select Wallet from Custom Data Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Use this method to select a wallet with custom data, including an address and UTxOs. Note that signing operations are not supported with this method as no private key is provided. ```javascript lucid.selectWalletFrom({address: "addr_test...", utxos: [...]}); ``` -------------------------------- ### Register Stake Pool Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/create-stakepool.md Use this snippet to register a new stake pool. Ensure you have your cold and VRF keys, and that pool metadata is hosted. The pledge, cost, and margin are specified in Lovelace and percentage respectively. ```javascript /** StakePoolSigningKey_ed25519 cborHex from the cardano-cli */ const coldKey = C.PrivateKey.from_bytes( fromHex( "58204de30f983ed860524d00059c7f2b1d63240fba805bee043604aa7ccb13d387e9", ), ); /** VrfVerificationKey_PraosVRF cborHex from the cardano-cli */ const vrfKeyHash = C.VRFVKey.from_bytes( fromHex( "5820c9cf07d863c8a2351662c9759ca1d9858b536bab50ad575b5de161e1af18f887", ), ).hash().to_hex(); const poolId = coldKey.to_public().hash().to_bech32("pool"); const rewardOwnerAddress = await lucid.wallet.rewardAddress(); const poolParams = { poolId, vrfKeyHash, pledge: 100000000n, cost: 340000000n, margin: 0.025, // 2.5% rewardAddress: rewardOwnerAddress, owners: [rewardOwnerAddress], relays: [{ type: "SingleHostIp", ipV4: "123.456.789.0", port: 3000 }], metadataUrl: "https://...", // metadata needs to be hosted already before registering the pool }; const tx = await lucid.newTx() .registerPool(poolParams).complete(); const signedTx = await tx.sign() .signWithPrivateKey(coldKey.to_bech32()) .complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Apply Parameters to Minting Policy Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/smart-contract.md Applies parameters to a parameterized Plutus V2 minting policy script. Ensure `applyParamsToScript` is correctly implemented and the script is valid. ```javascript const mintingPolicy = { type: "PlutusV2", script: applyParamsToScript( "5907945907910100...", [10n], ), }; ``` -------------------------------- ### Test Lucid Core Components Source: https://github.com/jpg-store/lucid/blob/main/README.md Run tests for the core components of the Lucid library. ```bash deno task test:core ``` -------------------------------- ### Select Wallet from Private Key Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/basics/create-wallet.md After generating or obtaining a private key, use this function to select it as the active wallet for your Lucid instance. Wallet selection is required for building and submitting transactions. ```javascript lucid.selectWalletFromPrivateKey(privateKey); ``` -------------------------------- ### Query Protocol Parameters Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md Fetch the current protocol parameters of the Cardano network using the provider. ```js const protocolParameters = await lucid.provider.getProtocolParameters(); ``` -------------------------------- ### Sign and Complete a Transaction Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/basics/your-first-tx.md After constructing a transaction, sign it using your private key and call `complete()` again to finalize the signing process. ```javascript const signedTx = await tx.sign().complete(); ``` -------------------------------- ### Webpack Configuration for Lucid Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/overview/import.md Adjust your Webpack configuration to enable necessary experiments for Lucid, such as top-level await. ```json { experiments: { "asyncWebAssembly": true, "topLevelAwait": true, "layers": true // optional, with some bundlers/frameworks it doesn't work without } } ``` -------------------------------- ### Node.js Package.json Configuration for Lucid Source: https://github.com/jpg-store/lucid/blob/main/README.md Configure your Node.js project's package.json to enable ES Module support for Lucid. ```json {"type" : "module"} ``` -------------------------------- ### Send Simple ADA Payment Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/make-payments.md Use this snippet to send a basic ADA payment to a specified address. Ensure a wallet and provider are selected before execution. ```javascript const tx = await lucid.newTx() .payToAddress("addr_test...", { lovelace: 5000000n }) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Query UTxOs using Provider Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md Retrieve Unspent Transaction Outputs (UTxOs) for a given address using the provider's getUtxos method. ```js const utxos = await lucid.provider.getUtxos("addr_test..."); ``` -------------------------------- ### Query Datum by Hash Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md Fetch a datum from the blockchain using its hash via the provider. ```js const datum = await lucid.provider.getDatum(""); ``` -------------------------------- ### Submit a Signed Transaction Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/basics/your-first-tx.md Submit the signed transaction to the Cardano network and log the resulting transaction hash. ```javascript const txHash = await signedTx.submit(); console.log(txHash); ``` -------------------------------- ### Create a Transaction with Multiple Payments Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/basics/your-first-tx.md Use this snippet to construct a transaction that sends ADA to two different addresses. Ensure you have initialized Lucid and selected a wallet before calling this. ```javascript const tx = await lucid.newTx() .payToAddress("addr_testa...", { lovelace: 5000000n }) .payToAddress("addr_testb...", { lovelace: 5000000n }) .complete(); ``` -------------------------------- ### Mint Native Tokens Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/mint-assets.md Construct and submit a transaction to mint new native tokens. This involves specifying the token unit (policy ID + token name) and the quantity to mint, attaching the minting policy, and completing the transaction. ```javascript const unit = policyId + fromText("MyMintedToken"); const tx = await lucid.newTx() .mintAssets({ [unit]: 1n }) .validTo(Date.now() + 200000) .attachMintingPolicy(mintingPolicy) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Register Stake Key Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/delegate.md Registers a stake key with a 2 ADA pledge. Requires a selected wallet and provider. ```javascript const rewardAddress = await lucid.wallet.rewardAddress(); const tx = await lucid.newTx() .registerStake(rewardAddress) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Retire Stake Pool Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/create-stakepool.md Use this snippet to retire an existing stake pool. You need the pool ID and the epoch number at which to retire it. The cold key is used for signing. ```javascript const retirementEpoch = 100; const tx = await lucid.newTx() .retirePool(poolId, retirementEpoch) .complete(); const signedTx = await tx.sign() .signWithPrivateKey(coldKey.to_bech32()) .complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Log Emulator Blockchain State Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/test-emulate.md Call `emulator.log()` to inspect the current state of the emulated blockchain, including fund distribution and block height. ```javascript emulator.log(); ``` -------------------------------- ### Select Wallet from Seed Phrase Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Select a wallet using a generated seed phrase. Ensure you securely store your seed phrase. ```javascript const seed = lucid.utils.generateSeedPhrase(); lucid.selectWalletFromSeed(seed); ``` -------------------------------- ### Webpack Configuration for Lucid Source: https://github.com/jpg-store/lucid/blob/main/README.md Configure Webpack 5 for Lucid compatibility in browser environments. Ensure top-level await and async WebAssembly are enabled. ```javascript experiments: { asyncWebAssembly: true, topLevelAwait: true, layers: true // optional, with some bundlers/frameworks it doesn't work without } ``` -------------------------------- ### Import Lucid in Web Browser Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/overview/import.md Import Lucid directly into your HTML file using a script tag with type module for web applications. ```html ``` -------------------------------- ### Create CML TransactionOutput Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/advanced/cml.md Use this snippet to create a CML TransactionOutput directly. Ensure you have imported the CML library correctly. ```typescript import { C } from "https://deno.land/x/lucid/mod.ts"; const output: C.TransactionOutput = C.TransactionOutput.new( C.Address.from_bech32("..."), C.Value.zero(), ); ``` -------------------------------- ### Query Wallet Delegation Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Obtain information about the delegation status of the selected wallet. ```javascript const delegation = await lucid.wallet.getDelegation(); ``` -------------------------------- ### Plutus Script Purposes in Lucid Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/smart-contract.md Demonstrates how to specify redeemer for various Plutus script purposes like minting, delegation, and withdrawals. If no redeemer is provided, Lucid assumes public keys or native scripts. ```javascript .collectFrom(utxos, redeemer) .mintAssets(assets, redeemer) .delegateTo(stakeAddress, poolId, redeemer) .deregisterStake(stakeAddress, redeemer) .withdraw(stakeAddress, rewardAmount, redeemer) ``` -------------------------------- ### Query Wallet UTxOs Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Fetch the Unspent Transaction Outputs (UTxOs) associated with the selected wallet. ```javascript const utxos = await lucid.wallet.getUtxos(); ``` -------------------------------- ### Send ADA with Datum Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/make-payments.md This snippet shows how to send ADA with an attached datum. The datum is added to the witness set, and its hash is stored in the UTxO. Lucid implicitly adds the minimum ADA requirement for datums. ```javascript const tx = await lucid.newTx() .payToAddressWithData("addr_test...", Data.to("31313131"), { lovelace: 5000000n, }) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Create and Instantiate Plutus Validator Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/smart-contract.md Defines a Plutus V2 validator script and derives its address using Lucid's utility functions. Ensure the script is compiled before use. ```javascript const matchingNumberScript = { type: "PlutusV2", script: "59099a590997010000...", }; const matchingNumberAddress = lucid.utils.validatorToAddress( matchingNumberScript, ); ``` -------------------------------- ### Set Transaction Validity Period with Emulator Time Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/test-emulate.md Use `emulator.now()` to set the validity period for transactions, as `Date.now()` is not supported in the emulator. ```javascript const tx = await lucid.newTx() .validTo(emulator.now()) .complete(); ``` -------------------------------- ### Delegate to Stake Pool Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/delegate.md Delegates stake to a specified pool using the reward address. Requires a selected wallet and provider. ```javascript const rewardAddress = await lucid.wallet.rewardAddress(); const tx = await lucid.newTx() .delegateTo(rewardAddress, "poolabc...") .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Disable Native UPLC for Plutus Core Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/advanced/plutus-core.md When using the Blockfrost provider, set `nativeUplc` to `false` in the `complete` method to utilize the Haskell Plutus Core engine instead of the default Aiken engine. ```javascript const tx = await lucid.newTx() .collectFrom([scriptUtxo], Data.void()) .complete({ nativeUplc: false }); ``` -------------------------------- ### Send Native Tokens Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/make-payments.md Use this to send native tokens along with ADA. Lucid automatically includes the minimum ADA required for the native tokens. ```javascript const policyId = "00..."; const assetName = "MyToken"; const tx = await lucid.newTx() .payToAddress("addr_test...", { [policyId + fromText(assetName)]: 10n }) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Read UTxOs with Plutus Scripts Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/smart-contract.md References UTxOs containing Plutus scripts without explicitly attaching them to the transaction, saving costs. This is useful when the script is already on-chain. ```javascript const tx = await lucid .newTx() .readFrom([scriptUtxo]) .complete(); ``` -------------------------------- ### Burn Native Tokens Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/mint-assets.md Construct and submit a transaction to burn existing native tokens. This is achieved by specifying a negative quantity for the token unit in the `mintAssets` function, effectively removing tokens from circulation. ```javascript const unit = policyId + fromText("MyMintedToken"); const tx = await lucid .newTx() .mintAssets({ [unit]: -1n }) .validTo(Date.now() + 200000) .attachMintingPolicy(mintingPolicy) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Create Native Script Time-Locking Minting Policy Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/mint-assets.md Define a minting policy using a native script that includes a signature requirement from your wallet and a time lock. This policy dictates the conditions under which tokens can be minted. ```javascript const { paymentCredential } = lucid.utils.getAddressDetails( await lucid.wallet.address(), ); const mintingPolicy = lucid.utils.nativeScriptFromJson( { type: "all", scripts: [ { type: "sig", keyHash: paymentCredential.hash }, { type: "before", slot: lucid.utils.unixTimeToSlot(Date.now() + 1000000), }, ], }, ); ``` -------------------------------- ### Send ADA with Metadata Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/make-payments.md This snippet demonstrates how to attach metadata to an ADA payment transaction. Metadata can be used for various purposes, such as including a message. ```javascript const tx = await lucid.newTx() .payToAddress("addr_test...", { lovelace: 5000000n }) .attachMetadata(1, { msg: "Hello from Lucid." }) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Function with Options Object (TypeScript) Source: https://github.com/jpg-store/lucid/blob/main/CONTRIBUTING.md Use an options object for function arguments when there are more than 2-3 parameters, especially if some are optional. This improves readability and maintainability. ```typescript export interface SeedOptions { addressType?: "Base" | "Enterprise"; accountIndex?: number; } export function fromSeed( address: string, options: SeedOptions = {}, ): string {} ``` -------------------------------- ### Descriptive Multi-line Comment (TypeScript) Source: https://github.com/jpg-store/lucid/blob/main/CONTRIBUTING.md Use multi-line JSDoc-style comments `/** ... */` for detailed descriptions of functions or variables. End the comment with a period. ```typescript /** * This functions adds two numbers together. * This is another random comment. */ export function add(a: number, b: number): number { return a + b; } ``` -------------------------------- ### Query Datum from UTxO Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/choose-provider.md Retrieve the datum associated with a specific UTxO. Lucid automatically caches fetched datums for efficiency. ```js const [scriptUtxo] = await lucid.utxosAt("addr_test..."); const datum = await lucid.datumOf(scriptUtxo); ``` -------------------------------- ### Multi-Validator Interaction Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/smart-contract.md Combines multiple spending and minting validators within a single transaction. Be mindful of the transaction's execution units limit. ```javascript const tx = await lucid .newTx() .collectFrom([scriptUtxoA, scriptUtxoB], Data.void()) .collectFrom([scriptUtxoC], Data.void()) .collectFrom([scriptUtxoD], Data.void()) .mintAssets([plutusPolicyId]: 10n, Data.void()) .attachSpendingValidator(spendingScript1) .attachSpendingValidator(spendingScript2) .attachMintingPolicy(mintingPolicy) .complete(); ``` -------------------------------- ### JSDoc @param Usage (TypeScript) Source: https://github.com/jpg-store/lucid/blob/main/CONTRIBUTING.md When using JSDoc `@param`, avoid specifying the type as TypeScript handles it. Provide a description for non-obvious parameters. ```typescript /** * Function with non-obvious param. * @param foo Description of non-obvious parameter. */ ``` -------------------------------- ### Multi-line Comment (TypeScript) Source: https://github.com/jpg-store/lucid/blob/main/CONTRIBUTING.md Use multi-line comments enclosed in `/* ... */` for longer explanations spanning multiple lines. ```typescript /* This is a comment on line 1. This is a comment on line 2. */ ``` -------------------------------- ### Send ADA to Multiple Recipients Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/make-payments.md This snippet shows how to send ADA to multiple addresses in a single transaction. Each `payToAddress` call creates a new UTxO. ```javascript const tx = await lucid.newTx() .payToAddress("addr_testa...", { lovelace: 5000000n }) .payToAddress("addr_testb...", { lovelace: 5000000n }) .payToAddress("addr_testc...", { lovelace: 5000000n }) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Descriptive Single-line Comment (TypeScript) Source: https://github.com/jpg-store/lucid/blob/main/CONTRIBUTING.md Use a single-line JSDoc-style comment `/** ... */` to describe a function or variable. End the comment with a period if it's a short phrase. ```typescript /** This functions adds two numbers together. */ export function add(a: number, b: number): number { return a + b; } ``` -------------------------------- ### Top-level Function Declaration (TypeScript) Source: https://github.com/jpg-store/lucid/blob/main/CONTRIBUTING.md Prefer using the `function` keyword for top-level exported functions instead of arrow function syntax for consistency. ```typescript export function add(a: number, b: number): number { return a + b; } ``` -------------------------------- ### Withdraw Rewards Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/delegate.md Withdraws accumulated rewards from a stake address. Requires a selected wallet and provider. ```javascript const rewardAddress = await lucid.wallet.rewardAddress(); const delegation = await lucid.wallet.getDelegation(); const tx = await lucid.newTx() .withdraw(rewardAddress, delegation.rewards) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Redeem Funds from Plutus Script Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/smart-contract.md Collects funds from a Plutus script UTxO by providing the correct datum and attaching the spending validator. Requires the transaction to be signed and submitted. ```javascript const [scriptUtxo] = await lucid.utxosAt(matchingNumberAddress); const tx = await lucid .newTx() .collectFrom([scriptUtxo], Data.to(100n)) .attachSpendingValidator(matchingNumberScript) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Deregister Stake Key Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/delegate.md Deregisters a stake key, reclaiming the 2 ADA pledge. Requires a selected wallet and provider. ```javascript const rewardAddress = await lucid.wallet.rewardAddress(); const tx = await lucid.newTx() .deregisterStake(rewardAddress) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Generate TypeScript Type from Schema Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/advanced/type-casting.md Generate a TypeScript type definition from a Plutus data schema. This allows for static type checking. ```typescript type Listing = Data.Static; const Listing = ListingSchema as unknown as Listing; ``` -------------------------------- ### Lock Funds at Plutus Script Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/smart-contract.md Locks ADA into a Plutus script address with a specific datum. Requires a selected wallet and provider. The transaction must be signed and submitted. ```javascript const tx = await lucid .newTx() .payToContract(matchingNumberAddress, { inline: Data.to(100n) }, { lovelace: 20000000n, }) .complete(); const signedTx = await tx.sign().complete(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Define Plutus Data Schema Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/advanced/type-casting.md Define a schema for Plutus data using Lucid's Data API. This schema will be used for type casting. ```typescript import { Data } from "https://deno.land/x/lucid/mod.ts"; const ListingSchema = Data.Object({ owner: Data.Bytes(), amount: Data.Integer(), private: Data.Boolean(), }); ``` -------------------------------- ### Derive Minting Policy ID Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/getting-started/mint-assets.md Obtain the unique identifier for your minting policy. This ID is crucial for referencing the policy when minting or burning tokens. ```javascript const policyId = lucid.utils.mintingPolicyToId(mintingPolicy); ``` -------------------------------- ### Void Return Type Declaration (TypeScript) Source: https://github.com/jpg-store/lucid/blob/main/CONTRIBUTING.md When a function's return type is `void`, do not explicitly mention it. This is a stylistic convention to keep the code concise. ```typescript export function log(str: string) { console.log(str); } ``` -------------------------------- ### Cast to Plutus Data Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/advanced/type-casting.md Convert a JavaScript object to Plutus data according to the defined schema. Ensure the input object matches the schema's structure and types. ```typescript const listing = Data.to( { owner: "31313131313131", amount: 5252352323n, private: false }, Listing, ); ``` -------------------------------- ### Cast from Plutus Data Source: https://github.com/jpg-store/lucid/blob/main/docs/docs/advanced/type-casting.md Convert a Plutus data string (CBOR encoded) to a JavaScript object conforming to the defined schema. The input string must be a valid Plutus data representation. ```typescript const listing: Listing = Data.from( "d8799f47313131313131311b0000000139108943d87980ff", Listing, ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.