### Full end-to-end transaction example with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/basics/your-first-tx.md This comprehensive example demonstrates the entire workflow of setting up Lucid, connecting to a Blockfrost provider, selecting a wallet, creating a multi-output transaction, signing it, and finally submitting it to the Cardano network. It includes all necessary imports and steps for a complete transaction flow. ```js import { Blockfrost, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = new Lucid({ provider: new Blockfrost( "https://cardano-preprod.blockfrost.io/api/v0", "" ) }); lucid.selectWalletFromPrivateKey(privateKey); const tx = await lucid.newTx() .payTo("addr_testa...", { lovelace: 5000000n }) .payTo("addr_testb...", { lovelace: 5000000n }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); console.log(txHash); ``` -------------------------------- ### Instantiate Lucid with Blockfrost Provider (JavaScript) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/basics/instantiate-lucid.md This JavaScript example demonstrates how to initialize the Lucid library using the Blockfrost provider. It connects to the Cardano Preprod network, requiring a Blockfrost project ID for authentication. This setup allows Lucid to query blockchain data and submit transactions. ```js import { Blockfrost, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = new Lucid({ provider: new Blockfrost( "https://cardano-preprod.blockfrost.io/api/v0", "" ) }); ``` -------------------------------- ### Import and Initialize Lucid in Deno Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/overview/import.md This snippet demonstrates how to import the Lucid library directly from Deno's `deno.land/x` registry and initialize a new `Lucid` instance. It assumes Deno is already installed and configured. ```js import { Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = new Lucid(); ``` -------------------------------- ### Select Wallet from Browser (CIP-0030 Compliant) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Shows how to select a wallet from the browser using a CIP-0030 compliant wallet API, such as Nami. This method is specifically designed for browser environments and interacts with installed wallet extensions. ```js const api = await window.cardano.nami.enable(); lucid.selectWalletFromApi(api); ``` -------------------------------- ### Install Lucid in Deno Source: https://github.com/spacebudz/lucid/blob/main/README.md Instructions for importing the Lucid library in a Deno project using JSR or a direct URL import from the Deno Land x registry. ```JavaScript import { Lucid } from "jsr:@spacebudz/lucid"; ``` ```JavaScript import { Lucid } from "https://deno.land/x/lucid/mod.ts"; ``` -------------------------------- ### Install Lucid in Node.js Source: https://github.com/spacebudz/lucid/blob/main/README.md Commands to add Lucid to a Node.js project using `npx jsr` for the recommended JSR package or the legacy `npm install` method. Note that Node.js usage requires specific flags and package.json configuration. ```Shell npx jsr add @spacebudz/lucid ``` ```Shell npm install lucid-cardano ``` -------------------------------- ### Redeem Funds from Plutus Script with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/smart-contract.md Illustrates how to collect UTxOs from a Plutus script address and redeem them. The example attaches the necessary script and includes a redeemer for the transaction. ```js const [scriptUtxo] = await lucid.utxosAt(matchingNumberAddress); const tx = await lucid .newTx() .collectFrom([scriptUtxo], Data.to(100n)) .attachScript(matchingNumberScript) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Example Lucid Transaction Instructions JSON Source: https://github.com/spacebudz/lucid/blob/main/README.md A JSON representation of a Lucid transaction converted into a list of instructions. This structure details actions like delegation and payment, making transactions highly portable. ```JSON [ { "type": "DelegateTo", "delegation": { "rewardAddress": "stake...", "variant": { "Pool": "pool...", }, }, "redeemer": undefined, }, { "type": "PayTo", "address": "addr_test1...", "assets": { "lovelace": 1000000n }, } ] ``` -------------------------------- ### Install Lucid via npm for Node.js Projects Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/overview/import.md This command adds the `lucid-cardano` package to your Node.js project's dependencies using npm. Node.js version 20 or higher is required for compatibility. ```sh npm install lucid-cardano ``` -------------------------------- ### Example Aiken Plutus Validator Source: https://github.com/spacebudz/lucid/blob/main/README.md An example Aiken validator definition, including a custom data type (`MyData`) and a minting policy. This code illustrates the structure of a Plutus contract written in Aiken that can be integrated with Lucid. ```Aiken pub type MyData { a: Int, b: ByteArray, } validator validate(my_data: MyData) { mint(redeemer: MyData, _policy_id: PolicyId, _transaction: Transaction) { my_data == redeemer } } ``` -------------------------------- ### Import and Initialize Lucid in Node.js Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/overview/import.md After installing via npm, this JavaScript code imports the `Lucid` class and creates an instance. Note that Node.js requires `--experimental-wasm-modules` flag and `"type": "module"` in `package.json` due to Lucid being an ES Module. ```js import { Lucid } from "lucid-cardano"; const lucid = new Lucid(); ``` -------------------------------- ### Get Wallet Address (Lucid) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Retrieves the Bech32 encoded address of the currently selected wallet in Lucid. This is a common operation for displaying the wallet's public identifier. ```js const address = await lucid.wallet.address(); // Bech32 address ``` -------------------------------- ### Attach Metadata to ADA Payment with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/make-payments.md This example demonstrates how to include arbitrary metadata with an ADA payment using Lucid. Metadata can be attached to the transaction witness set, allowing for additional information to be stored on-chain. ```js const tx = await lucid.newTx() .payTo("addr_test...", { lovelace: 5000000n }) .attachMetadata(1, { msg: "Hello from Lucid." }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### TypeScript: Standard Commenting Practices Source: https://github.com/spacebudz/lucid/blob/main/CONTRIBUTING.md Provides examples of various commenting styles in TypeScript, including single-line, multi-line, and JSDoc-style comments for functions and variables. It outlines conventions for proper formatting, such as ending comments with a dot, and advises against including type information in JSDoc `@param` tags due to TypeScript's strong typing. ```typescript // This is a comment. ``` ```typescript /* This is a comment on line 1. This is a comment on line 2. */ ``` ```typescript /** This functions adds two numbers together. */ export function add(a: number, b: number): number { return a + b; } ``` ```typescript /** * This functions adds two numbers together. * This is another random comment. */ export function add(a: number, b: number): number { return a + b; } ``` ```typescript /** * Function with non-obvious param. * @param foo Description of non-obvious parameter. */ ``` -------------------------------- ### Handle Time Ranges with Lucid Emulator Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/test-emulate.md Explains how to correctly set transaction validity periods when using the Lucid emulator. Since `Date.now()` is unsuitable for an emulated environment, `emulator.now()` must be used to get the current emulated time. ```js const tx = await lucid.newTx() .validTo(emulator.now()) .commit(); ``` -------------------------------- ### Send ADA to Multiple Recipients with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/make-payments.md This example illustrates how to send ADA to multiple distinct recipients within a single transaction using Lucid. Each `payTo` call creates a new UTxO, and Lucid processes the outputs in the specified order. ```js const tx = await lucid.newTx() .payTo("addr_testa...", { lovelace: 5000000n }) .payTo("addr_testb...", { lovelace: 5000000n }) .payTo("addr_testc...", { lovelace: 5000000n }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Execute Multiple Plutus Validators in a Single Lucid Transaction Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/smart-contract.md Shows how to construct a single Lucid transaction that interacts with multiple Plutus validators. This example demonstrates collecting from several script UTxOs, minting assets, and attaching multiple spending and minting scripts. ```js const tx = await lucid .newTx() .collectFrom([scriptUtxoA, scriptUtxoB], Data.void()) .collectFrom([scriptUtxoC], Data.void()) .collectFrom([scriptUtxoD], Data.void()) .mint([plutusPolicyId]: 10n, Data.void()) .attachScript(spendingScript1) .attachScript(spendingScript2) .attachScript(mintingPolicy) .commit(); ``` -------------------------------- ### Initialize Lucid Emulator Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/test-emulate.md Demonstrates how to instantiate the Lucid emulator, which acts as a provider, allowing for testing and emulation of transactions. It shows how to set up an initial address with assets for the emulator. ```js const emulator = new Emulator([ { address: "addr_test...", assets: { lovelace: 3000000000n } } ]); const lucid = new Lucid({ provider: emulator }); ``` -------------------------------- ### Serve Lucid Documentation Locally Source: https://github.com/spacebudz/lucid/blob/main/docs/README.md This command serves the Lucid documentation project locally, making it accessible via a web browser. It's useful for previewing changes during development. ```sh deno task serve ``` -------------------------------- ### Create a new transaction with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/basics/your-first-tx.md This snippet demonstrates how to initialize a new transaction using `lucid.newTx()` and add multiple recipients with `payTo()`, specifying the amount in lovelace. The transaction must be finalized with `.commit()` to balance and perform coin selection. ```js const tx = await lucid.newTx() .payTo("addr_testa...", { lovelace: 5000000n }) .payTo("addr_testb...", { lovelace: 5000000n }) .commit(); ``` -------------------------------- ### Build NPM and Web Bundles for Local Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/overview/import.md Executing this Deno task command from the root of the cloned Lucid repository compiles the library into distributable NPM and web bundles. The output will be placed in a `dist` folder, ready for use in other projects or deployment. ```sh deno task build ``` -------------------------------- ### Configure Lucid with Blockfrost Provider Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md Initializes the Lucid library with Blockfrost as the blockchain provider, requiring a Blockfrost API URL and project ID. ```js import { Blockfrost, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = new Lucid({ provider: new Blockfrost( "https://cardano-preprod.blockfrost.io/api/v0", "", ), }); ``` -------------------------------- ### Configure Lucid with Maestro Provider Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md Initializes Lucid with Maestro as the blockchain provider. Configuration includes network, API key, and an optional turbo submit flag for transactions. ```js import { Lucid, Maestro } from "https://deno.land/x/lucid/mod.ts"; const lucid = new Lucid({ provider: 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. }), }); ``` -------------------------------- ### Query UTxOs Using Lucid Convenience Method Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md A convenient alternative method to fetch unspent transaction outputs (UTxOs) for a given address using Lucid's built-in utility. ```js const utxos = await lucid.utxosAt("addr_test..."); ``` -------------------------------- ### Build Lucid Documentation Locally Source: https://github.com/spacebudz/lucid/blob/main/docs/README.md This command builds the Lucid documentation project locally using Deno tasks. It compiles the necessary files for deployment or local viewing. ```sh deno task build ``` -------------------------------- ### Implement and Use a Custom Lucid Provider Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md Demonstrates how to create a custom blockchain provider by extending the `Provider` interface in Lucid and then configuring Lucid to use this custom implementation. ```ts import { Lucid, type Provider } from "https://deno.land/x/lucid/mod.ts" class MyProvider extends Provider { ... } const lucid = new Lucid( { provider: new MyProvider() }, ); ``` -------------------------------- ### Select Wallet from Private Key in Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/basics/create-wallet.md Shows how to select a wallet in a Lucid instance using a previously generated or provided private key. This step is necessary before building and submitting any transactions. ```js lucid.selectWalletFromPrivateKey(privateKey); ``` -------------------------------- ### Submit a signed transaction to the network Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/basics/your-first-tx.md This snippet illustrates the final step of submitting a signed transaction to the blockchain network using `signedTx.submit()`. The function returns the transaction hash, which is then logged to the console. ```js const txHash = await signedTx.submit(); console.log(txHash); ``` -------------------------------- ### Configure Lucid with Kupmios Provider Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md Sets up the Lucid library to use Kupmios, a combination of Ogmios and Kupo, as the blockchain provider. Requires Kupo and Ogmios URLs and network configuration. ```js import { Kupmios, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = new Lucid({ provider: new Kupmios({ kupoUrl: "http://localhost:1442", ogmiosUrl: "ws://localhost:1337", network: "Preprod", }), }); ``` -------------------------------- ### Basic Lucid Transaction Usage Source: https://github.com/spacebudz/lucid/blob/main/README.md Demonstrates how to initialize Lucid with a Blockfrost provider, enable a wallet (e.g., Nami) in a browser environment, create a new transaction to pay lovelace to an address, sign, commit, and submit the transaction to the Cardano blockchain. ```JavaScript import { Blockfrost, Lucid } from "https://deno.land/x/lucid/mod.ts"; const lucid = new Lucid({ provider: new Blockfrost( "https://cardano-preview.blockfrost.io/api/v0", "", ), }); // Assumes you are in a browser environment const api = await window.cardano.nami.enable(); lucid.selectWalletFromApi(api); const tx = await lucid.newTx() .payTo("addr...", { lovelace: 5000000n }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); console.log(txHash); ``` -------------------------------- ### Query Wallet UTxOs (Lucid) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Fetches the Unspent Transaction Outputs (UTxOs) associated with the selected wallet in Lucid. UTxOs are fundamental for constructing new transactions. ```js const utxos = await lucid.wallet.getUtxos(); ``` -------------------------------- ### Sign a transaction with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/basics/your-first-tx.md This code shows how to sign a previously created transaction. The `tx.sign()` method is used, and the signing process is completed by calling `.commit()` to finalize the signature. ```js const signedTx = await tx.sign().commit(); ``` -------------------------------- ### Build Lucid from Source Source: https://github.com/spacebudz/lucid/blob/main/README.md Command to build the Lucid library from its source code, which compiles the project and outputs the necessary files into a `dist` folder. ```Shell deno task build ``` -------------------------------- ### Query UTxOs Using Lucid Provider Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md Fetches unspent transaction outputs (UTxOs) for a given address directly through the configured Lucid provider. ```js const utxos = await lucid.provider.getUtxos("addr_test..."); ``` -------------------------------- ### Consume Lucid Instructions to Create Transaction Source: https://github.com/spacebudz/lucid/blob/main/README.md Demonstrates how to reconstruct a Lucid transaction from a previously generated JSON array of instructions using the `fromInstructions()` method, enabling the re-creation of complex transactions from a portable format. ```JavaScript const tx = await lucid.fromInstructions([ { "type": "DelegateTo", "delegation": { "rewardAddress": "stake...", "variant": { "Pool": "pool...", }, }, "redeemer": undefined, }, { "type": "PayTo", "address": "addr_test1...", "assets": { "lovelace": 1000000n }, }, ]); ``` -------------------------------- ### Select Wallet from Private Key (Lucid) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Demonstrates how to select a wallet in Lucid using a generated private key. This method requires the `Crypto` utility from the Lucid library to generate a Bech32 encoded private key. ```js import { Crypto } from "https://deno.land/x/lucid/mod.ts"; const privateKey = Crypto.generatePrivateKey(); // Bech32 encoded private key lucid.selectWalletFromPrivateKey(privateKey); ``` -------------------------------- ### Select Wallet from Seed Phrase (Lucid) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Illustrates selecting a wallet in Lucid using a generated seed phrase. Similar to the private key method, it utilizes the `Crypto` utility from the Lucid library to generate the seed. ```js import { Crypto } from "https://deno.land/x/lucid/mod.ts"; const seed = Crypto.generateSeed(); lucid.selectWalletFromSeed(seed); ``` -------------------------------- ### Convert Lucid Transaction to Instructions Source: https://github.com/spacebudz/lucid/blob/main/README.md Shows how to convert a Lucid transaction into a portable JSON array of instructions using the `toInstructions()` method. This allows for serialization and external processing of transaction details. ```JavaScript const instructions = await lucid.newTx() .delegateTo("{{own}}", { Pool: "pool..." }) .payTo("addr_test1...", { lovelace: 1000000n }) .toInstructions(); ``` -------------------------------- ### Query Datum from UTxO Using Lucid Convenience Method Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md Fetches the datum associated with a specific UTxO. Lucid automatically caches the datum for subsequent queries of the same UTxO. ```js const [scriptUtxo] = await lucid.utxosAt("addr_test..."); const datum = await lucid.datumOf(scriptUtxo); ``` -------------------------------- ### Distribute Staking Rewards with Lucid Emulator Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/test-emulate.md Illustrates how to use the emulator's functionality to simulate the distribution of staking rewards. The `distributeRewards` method allows for easy testing of reward mechanisms within the emulated environment. ```js emulator.distributeRewards(100000000n); ``` -------------------------------- ### Query Protocol Parameters Using Lucid Provider Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md Retrieves the current blockchain protocol parameters, such as min fee A, min fee B, and pool deposit, through the configured Lucid provider. ```js const protocolParameters = await lucid.provider.getProtocolParameters(); ``` -------------------------------- ### Clone Lucid GitHub Repository Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/overview/import.md This shell command clones the official Lucid repository from GitHub to your local machine. This is the first step for local development or building custom versions of the library. ```sh git clone https://github.com/spacebudz/lucid.git ``` -------------------------------- ### Read/Reference UTxOs with Plutus Scripts in Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/smart-contract.md Illustrates how Lucid allows reading or referencing UTxOs, particularly useful when a Plutus script is already embedded in the UTxO. This approach avoids explicit script attachment, leading to cost savings. ```js const tx = await lucid .newTx() .readFrom([scriptUtxo]) .commit(); ``` -------------------------------- ### Generate Private Key with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/basics/create-wallet.md Demonstrates how to generate a new extended bech32 encoded private key using the Addresses module from the Lucid library. This private key is essential for creating or importing a wallet. ```js import { Addresses } from "https://deno.land/x/lucid/mod.ts"; const privateKey = Addresses.generatePrivateKey(); // Extended bech32 encoded private key console.log(privateKey); ``` -------------------------------- ### Apply Parameters to Plutus Script in Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/smart-contract.md Demonstrates how to dynamically apply parameters to a Plutus script using `applyParamsToScript` when defining a minting policy. This allows for flexible script instantiation. ```js const mintingPolicy = { type: "PlutusV2", script: applyParamsToScript( [10n], "5907945907910100...", ), }; ``` -------------------------------- ### Query Datum by Hash Using Lucid Provider Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-provider.md Retrieves a datum object from the blockchain by its hash using the configured Lucid provider. ```js const datum = await lucid.provider.getDatum(""); ``` -------------------------------- ### Mint Native Tokens (JavaScript) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/mint-assets.md This snippet illustrates the process of minting new native tokens. It constructs the asset unit using the policy ID and token name, then creates a new transaction, attaches the minting policy, and commits the transaction to the blockchain after signing. ```js const unit = policyId + fromText("MyMintedToken"); const tx = await lucid.newTx() .mint({ [unit]: 1n }) .validTo(Date.now() + 200000) .attachScript(mintingPolicy) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Query Wallet Delegation (Lucid) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Retrieves the delegation information for the selected wallet in Lucid. This is useful for checking if a wallet is delegated to a stake pool and to which one. ```js const delegation = await lucid.wallet.getDelegation(); ``` -------------------------------- ### Webpack Configuration for Async WebAssembly with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/overview/import.md This JSON snippet shows the necessary `experiments` configuration within `webpack.config.json` to enable asynchronous WebAssembly support. This is crucial when bundling Lucid in a Webpack project, as Lucid utilizes WebAssembly modules. ```json experiments: { "asyncWebAssembly": true, } ``` -------------------------------- ### Generate Lucid Documentation Source: https://github.com/spacebudz/lucid/blob/main/README.md Command to generate API documentation for the Lucid library using Deno's built-in documentation tool, providing detailed insights into its functions and modules. ```Shell deno doc ``` -------------------------------- ### Configure Webpack for Lucid Compatibility Source: https://github.com/spacebudz/lucid/blob/main/README.md A configuration snippet for Webpack 5, enabling `asyncWebAssembly`. This setting is crucial for bundling Lucid in a browser environment, as the library relies on ES Modules and WebAssembly. ```JavaScript experiments: { asyncWebAssembly: true, } ``` -------------------------------- ### Perform Simple ADA Payment with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/make-payments.md This snippet demonstrates how to send a basic ADA payment to a single recipient using the Lucid library. It covers creating a new transaction, specifying the recipient address and amount, signing, and submitting the transaction to the blockchain. ```js const tx = await lucid.newTx() .payTo("addr_test...", { lovelace: 5000000n }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Run Lucid Blueprint Command Source: https://github.com/spacebudz/lucid/blob/main/README.md Command to execute the Lucid blueprint tool, which processes a `plutus.json` file to generate a `plutus.ts` file, facilitating the integration of Plutus contracts into Lucid applications. ```Shell deno run -A https://deno.land/x/lucid/blueprint.ts ``` -------------------------------- ### Run Lucid Tests Source: https://github.com/spacebudz/lucid/blob/main/README.md Command to execute the comprehensive test suite for the Lucid library, ensuring its functionality and stability. ```Shell deno task test ``` -------------------------------- ### Select Read-Only Wallet from Custom Data (Lucid) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/choose-wallet.md Explains how to select a read-only wallet in Lucid by providing an address and a list of UTxOs. This method is suitable for querying wallet information but does not support signing operations as no private key is selected. ```js lucid.selectReadOnlyWallet({address: "addr_test...", utxos: [...]}); ``` -------------------------------- ### Create and Instantiate Plutus Validator in Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/smart-contract.md Demonstrates how to define a Plutus V2 script and derive its address using Lucid's `validatorToAddress` utility. This script is intended for a validator that requires matching numbers in datum and redeemer. ```js const matchingNumberScript = { type: "PlutusV2", script: "59099a590997010000..." }; const matchingNumberAddress = lucid.utils.validatorToAddress( matchingNumberScript, ); ``` -------------------------------- ### Create Native Script Minting Policy (JavaScript) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/mint-assets.md This snippet demonstrates how to create a native script minting policy using the Lucid library. It defines an 'All' policy type, requiring a signature from the wallet's payment credential and a 'Before' time-locking slot, ensuring the policy is valid only before a specified future time. ```js import { Addresses } from "https://deno.land/x/lucid/mod.ts"; const { payment } = Addresses.inspect( await lucid.wallet.address(), ); const mintingPolicy = lucid.newScript( { type: "All", scripts: [ { type: "Sig", keyHash: paymentCredential.hash }, { type: "Before", slot: lucid.utils.unixTimeToSlots(Date.now() + 1000000), }, ], }, ); ``` -------------------------------- ### Import Locally Built Lucid in Deno Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/overview/import.md When working within the cloned Lucid repository, this Deno JavaScript import statement allows you to use the local source code directly. It assumes the current working directory is the root of the Lucid project. ```js import { Lucid } from "./mod.ts"; ``` -------------------------------- ### Test Lucid Core Library Source: https://github.com/spacebudz/lucid/blob/main/README.md Command to execute the test suite specifically for the Rust-based core library of Lucid, verifying its low-level functionalities. ```Shell deno task test:core ``` -------------------------------- ### Delegate Wallet to Stake Pool with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/delegate.md Delegates the current wallet's stake key to a specified stake pool. This allows the wallet to earn rewards from the pool based on its delegated stake. ```js const rewardAddress = await lucid.wallet.rewardAddress(); const tx = await lucid.newTx() .delegateTo(rewardAddress, { Pool: "poolabc..." }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Register Cardano Stake Pool with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/create-stakepool.md This JavaScript snippet demonstrates how to register a new stake pool on the Cardano blockchain using the Lucid library. It includes steps for deriving keys, defining pool parameters such as pledge, cost, margin, reward address, owners, and relays, and finally committing and submitting the transaction. A hosted metadata URL is required before registration. ```js import { Crypto, Hasher, type PoolRegistration, Utils } from "https://deno.land/x/lucid/mod.ts"; /** StakePoolSigningKey_ed25519 cborHex from the cardano-cli */ const { privateKey: coldKey, credential: coldCredential } = Crypto .privateKeyToDetails( "58204de30f983ed860524d00059c7f2b1d63240fba805bee043604aa7ccb13d387e9", ); /** VrfVerificationKey_PraosVRF cborHex from the cardano-cli */ const vrfKeyHash = Hasher.hashVrfKey( "5820c9cf07d863c8a2351662c9759ca1d9858b536bab50ad575b5de161e1af18f887", ); const poolId = Utils.encodeBech32("pool", coldCredential.hash); const rewardOwnerAddress = (await lucid.wallet.rewardAddress())!; const poolParams: PoolRegistration = { poolId, vrfKeyHash, pledge: 100000000, cost: 340000000, margin: 0.025, // 2.5% rewardAddress: rewardOwnerAddress, owners: [rewardOwnerAddress], relays: [{ type: "SingleHostIp", ipV4: "123.456.789.0", ipV6: undefined, port: 3000, }], metadataUrl: "https://...", // metadata needs to be hosted already before registering the pool }; const tx = await lucid.newTx() .registerPool(poolParams).commit(); const signedTx = await tx.sign() .signWithPrivateKey(coldKey) .commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Build Lucid Core Library Source: https://github.com/spacebudz/lucid/blob/main/README.md Command to build the core library of Lucid, which is implemented in Rust and compiled to WebAssembly (WASM) for performance-critical operations. ```Shell deno task build:core ``` -------------------------------- ### TypeScript: Refactoring Functions with Many Arguments into Options Object Source: https://github.com/spacebudz/lucid/blob/main/CONTRIBUTING.md Demonstrates the preferred TypeScript style for handling functions with multiple optional arguments. Instead of listing many parameters, it's recommended to group them into a single options object, improving readability and maintainability. ```typescript // BAD. If the second argument was not optional, it would be OKAY to do it like this. export function fromSeed( address: string, addressType?: "Base" | "Enterprise", accountIndex?: number, ): string {} ``` ```typescript // GOOD. export interface SeedOptions { addressType?: "Base" | "Enterprise"; accountIndex?: number; } export function fromSeed( address: string, options: SeedOptions = {}, ): string {} ``` -------------------------------- ### Integrate Aiken Validator with Lucid Source: https://github.com/spacebudz/lucid/blob/main/README.md Demonstrates how to import a generated Plutus validator (e.g., from `plutus.ts`), create a new script from it, and use it within a Lucid transaction for minting tokens with a specified redeemer. ```TypeScript import { ValidateMint } from "./plutus.ts"; const validator = new ValidateMint({ a: 123n, b: "0000" }); const policyId = lucid.newScript(validator).toHash(); const tx = await lucid .newTx() .mint( { [policyId]: 1n }, Data.to({ a: 123n, b: "0000" }, ValidateMint.redeemer), ) .attachScript(validator) .commit(); ``` -------------------------------- ### Send ADA with Datum using Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/make-payments.md This snippet illustrates how to attach a datum to an ADA payment. The datum hash is stored in the UTxO, and Lucid implicitly adds the minimum ADA required for datums. It also mentions the option to inline the datum directly in the UTxO. ```js const tx = await lucid.newTx() .payToWithData("addr_test...", Data.to("31313131"), { lovelace: 5000000n, }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Register Stake Key with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/delegate.md Registers a stake key for the current wallet, requiring a 2 ADA pledge. This operation makes the wallet eligible for staking rewards on the Cardano network. ```js const rewardAddress = await lucid.wallet.rewardAddress(); const tx = await lucid.newTx() .registerStake(rewardAddress) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Lucid Transaction Constraints with Redeemer Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/smart-contract.md Outlines various transaction constraints in Lucid that accept a redeemer as the last parameter. These methods are used for verifying conditions related to spending UTxOs, minting, delegations, and withdrawals, requiring a redeemer for successful script execution. ```js .collectFrom(utxos, redeemer) .mint(assets, redeemer) .delegateTo(stakeAddress, poolId, redeemer) .deregisterStake(stakeAddress, redeemer) .withdraw(stakeAddress, rewardAmount, redeemer) ``` -------------------------------- ### Lock Funds at Plutus Script Address with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/smart-contract.md Shows how to create a transaction to lock funds (lovelace) at a specific Plutus script address. It includes paying to the contract with an inline datum and committing the transaction. ```js const tx = await lucid .newTx() .payToContract(matchingNumberAddress, { Inline: Data.to(100n) }, { lovelace: 20000000n, }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Send Native Tokens with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/make-payments.md This snippet shows how to send custom native tokens using Lucid. Lucid automatically handles the minimum ADA requirement necessary when sending native tokens. It requires specifying the policy ID and asset name. ```js const policyId = "00..."; const assetName = "MyToken"; const tx = await lucid.newTx() .payTo("addr_test...", { [policyId + fromText(assetName)]: 10n }) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Burn Native Tokens (JavaScript) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/mint-assets.md This snippet demonstrates how to burn existing native tokens. It reuses the asset unit and minting policy, but specifies a negative quantity (`-1n`) in the minting transaction to effectively remove the tokens from circulation. ```js const unit = policyId + fromText("MyMintedToken"); const tx = await lucid .newTx() .mint({ [unit]: -1n }) .validTo(Date.now() + 200000) .attachScript(mintingPolicy) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Retire Cardano Stake Pool with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/create-stakepool.md This JavaScript snippet illustrates how to retire an existing stake pool on the Cardano blockchain using the Lucid library. It specifies the pool ID and the epoch at which the pool should be retired, then commits and submits the retirement transaction. ```js const retirementEpoch = 100; const tx = await lucid.newTx() .retirePool(poolId, retirementEpoch) .commit(); const signedTx = await tx.sign() .signWithPrivateKey(coldKey) .commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Withdraw Staking Rewards with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/delegate.md Withdraws accumulated staking rewards from the wallet's reward address. It first fetches the current delegation information to determine the amount of rewards available for withdrawal. ```js const rewardAddress = await lucid.wallet.rewardAddress(); const delegation = await lucid.wallet.getDelegation(); const tx = await lucid.newTx() .withdraw(rewardAddress, delegation.rewards) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### TypeScript: Preferring Function Declarations over Arrow Functions for Top-Level Exports Source: https://github.com/spacebudz/lucid/blob/main/CONTRIBUTING.md Illustrates the recommended syntax for defining top-level exported functions in TypeScript. Traditional function declarations are preferred over arrow function syntax for consistency and clarity in this context. ```typescript // BAD. export const add = (a: number, b: number): number => a + b; ``` ```typescript // GOOD. export function add(a: number, b: number): number { return a + b; } ``` -------------------------------- ### Deregister Stake Key with Lucid Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/delegate.md Deregisters the stake key, reclaiming the 2 ADA pledge that was required for registration. This action removes the wallet's eligibility for staking rewards. ```js const rewardAddress = await lucid.wallet.rewardAddress(); const tx = await lucid.newTx() .deregisterStake(rewardAddress) .commit(); const signedTx = await tx.sign().commit(); const txHash = await signedTx.submit(); ``` -------------------------------- ### Derive Policy ID from Minting Policy (JavaScript) Source: https://github.com/spacebudz/lucid/blob/main/docs/docs/getting-started/mint-assets.md This snippet shows how to derive the unique policy ID from a previously created minting policy script. The policy ID is a hash of the script and is essential for identifying the token associated with this specific minting policy. ```js const policyId = mintingPolicy.toHash(); ``` -------------------------------- ### TypeScript: Explicitly Defining Function Return Types Source: https://github.com/spacebudz/lucid/blob/main/CONTRIBUTING.md Emphasizes the importance of explicitly defining return types for functions in TypeScript to enhance code clarity and enable robust type checking. However, for functions returning `void`, explicit mention is not necessary. ```typescript // BAD. Return type is only implicitly determined. export function add(a: number, b: number) { return a + b; } ``` ```typescript // GOOD. export function add(a: number, b: number): number { return a + b; } ``` ```typescript // GOOD. export function log(str: string) { console.log(str); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.