========================
CODE SNIPPETS
========================
TITLE: Install Dependencies and Start Development Server
DESCRIPTION: This snippet provides commands to set up the development environment for the CKB Academy project. It includes installing dependencies, setting up Git hooks for code formatting, and starting the local development server using either `yarn dev:site` or `turbo run dev`.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/README.md#_snippet_0
LANGUAGE: bash
CODE:
```
# install dependencies
yarn
# Install Git hook to achieve code formatting during code submission.
yarn run prepare
# start a development server: https://start.solidjs.com/api/dev
yarn dev:site
# or
turbo run dev
```
----------------------------------------
TITLE: Start Production Server
DESCRIPTION: This command starts the production server after the project has been built. It serves the bundled application from the `output` directory.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/README.md#_snippet_2
LANGUAGE: bash
CODE:
```
# https://start.solidjs.com/api/start
yarn start
```
----------------------------------------
TITLE: Integrating ConnectWallet Component for MetaMask
DESCRIPTION: This example demonstrates how to embed the `ConnectWallet` React component into a page. Its primary function is to facilitate the connection to a MetaMask wallet, which is the initial step for all on-chain operations in the course.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_1/chapter_1.mdx#_snippet_1
LANGUAGE: JSX
CODE:
```
```
----------------------------------------
TITLE: Simple User Defined Token (SUDT) Type Script Parameters Example
DESCRIPTION: Provides a real-world example of parameters used to locate the Simple User Defined Token (SUDT) type script on the CKB testnet. This demonstrates the values for `code_hash`, `hash_type`, `tx_hash`, `index`, and `dep_type` that point to the actual script code.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-theory/chapter_5.mdx#_snippet_2
LANGUAGE: APIDOC
CODE:
```
SUDT Type Script Location Parameters:
code_hash: 0xc5e5dcf215925f7ef4dfaf5f4b4f105bc321c02776d6e7d52a1db3fcd9d011a4
hash_type: type
tx_hash: 0xe12877ebd2c3c364dc46c5c992bcfaf4fee33fa13eebdf82c591fc9825aab769
index: 0x0
dep_type: code
```
----------------------------------------
TITLE: Displaying Latest Blocks with BlocksSection Component
DESCRIPTION: This example shows how to integrate the `BlocksSection` component, wrapped by `WalletReady`, to present the nine most recent blocks on the CKB testnet. It ensures that block data is fetched and displayed only when the wallet is ready.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_1/chapter_1.mdx#_snippet_3
LANGUAGE: JSX
CODE:
```
```
----------------------------------------
TITLE: Build Project for Production
DESCRIPTION: This snippet shows the commands to build the CKB Academy project for production. It uses `solid-start` and `Vite` to bundle server and client code. The output will be located in `site/dist/public`.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/README.md#_snippet_1
LANGUAGE: bash
CODE:
```
# https://start.solidjs.com/api/build
yarn build
# or
turbo run build
```
----------------------------------------
TITLE: Displaying Testnet Configuration with TestnetConfigSection Component
DESCRIPTION: This snippet demonstrates the use of the `TestnetConfigSection` component, enclosed within `WalletReady`, to display crucial configuration information of the CKB testnet, such as the chain prefix and details about built-in smart contracts.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_1/chapter_1.mdx#_snippet_4
LANGUAGE: JSX
CODE:
```
```
----------------------------------------
TITLE: Importing Core React Components for CKB Operations
DESCRIPTION: This snippet lists the essential React components imported from local paths, which are utilized throughout the CKB basic operations course to manage wallet connections, display cells, show blocks, and configure testnet settings.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_1/chapter_1.mdx#_snippet_0
LANGUAGE: TypeScript
CODE:
```
import { ConnectWallet } from "~/content/courses/basic-operation/chapter_1/ConnectWallet.tsx";
import { CellsSection } from "~/content/courses/basic-operation/chapter_1/CellsSection.tsx";
import { BlocksSection } from "~/content/courses/basic-operation/chapter_1/BlocksSection.tsx";
import { TestnetConfigSection } from "~/content/courses/basic-operation/chapter_1/TestnetConfigSection.tsx";
import { WalletReady } from "~/content/components/WalletReady.tsx";
```
----------------------------------------
TITLE: Displaying Live Cells with CellsSection Component
DESCRIPTION: This snippet illustrates the usage of the `CellsSection` component, nested within `WalletReady`, to dynamically display the user's live CKB cells. It requires the wallet to be connected and ready before rendering the cell list.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_1/chapter_1.mdx#_snippet_2
LANGUAGE: JSX
CODE:
```
```
----------------------------------------
TITLE: CKB Testnet Built-in Smart Contracts Reference
DESCRIPTION: This section provides a reference for the pre-deployed built-in smart contracts available on the CKB testnet. It includes details on their purpose and typical use cases, such as protecting cell ownership or enabling multi-signature functionalities.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_1/chapter_1.mdx#_snippet_5
LANGUAGE: APIDOC
CODE:
```
Testnet Configuration:
PREFIX: ckt (Indicates test chain)
Built-in Smart Contracts (SCRIPTS):
- SECP256K1_BLAKE160:
Purpose: The system's default smart contract for the lock script of cells, used to protect the ownership of the cells.
- SECP256K1_BLAKE160_MULTISIG:
Purpose: The multi-signature version of SECP256K1_BLAKE160.
- DAO:
Purpose: NervosDAO contract.
```
----------------------------------------
TITLE: Sending a Transaction to CKB Testnet
DESCRIPTION: This snippet demonstrates how to send a transaction to the CKB testnet using the `SendTransaction` component, wrapped within a `WalletReady` context. It highlights that the returned transaction hash (`tx_hash`) remains consistent with the one generated beforehand, ensuring CKB's certainty.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_3/chapter_3.mdx#_snippet_1
LANGUAGE: JSX
CODE:
```
```
----------------------------------------
TITLE: CKB Transaction Inputs and Outputs Visualization
DESCRIPTION: Visualizes how inputs (existing cells) are transformed into outputs (new cells) within a CKB transaction.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-theory/chapter_7.mdx#_snippet_1
LANGUAGE: ts
CODE:
```
inputs:
some cells...
|
|
|
\/
outputs:
some new cells...
```
----------------------------------------
TITLE: CKB Transaction Rule: Miner Fee Calculation
DESCRIPTION: Explains the second rule for CKB transactions, where the difference between input and output capacities constitutes the miner's fee.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-theory/chapter_7.mdx#_snippet_3
LANGUAGE: text
CODE:
```
sum(cell's capacity for each cell in inputs)
- sum(cell's capacity for each cell in outputs)
= fee
```
----------------------------------------
TITLE: CKB Transaction Essence
DESCRIPTION: Illustrates the fundamental concept of a transaction in CKB, showing it as a transformation from inputs to outputs.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-theory/chapter_7.mdx#_snippet_0
LANGUAGE: ts
CODE:
```
transaction: inputs -> outputs
```
----------------------------------------
TITLE: WitnessArgs Structure Definition for CKB Transactions
DESCRIPTION: Defines the `WitnessArgs` structure, an essential component in CKB transactions for providing additional arguments or data, such as signatures. It includes fields for lock script arguments, and optional input/output type script arguments.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_3/chapter_3.mdx#_snippet_0
LANGUAGE: APIDOC
CODE:
```
table WitnessArgs {
lock: BytesOpt, // Lock Script args, which holds the lock-script-related arguments or data
input_type: BytesOpt, // Type Script args for input (optional)
output_type: BytesOpt, // Type Script args for output (optional)
}
```
----------------------------------------
TITLE: CKB Script Structure Definition
DESCRIPTION: Defines the structure of a CKB Script, including `code_hash` for referencing code, `args` for script arguments, and `hash_type` to specify how `code_hash` is interpreted for code retrieval. The `hash_type` can be 'data', 'type', or 'data1'.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-theory/chapter_5.mdx#_snippet_0
LANGUAGE: ts
CODE:
```
Script: {
code_hash: HexString
args: HexString
hash_type: Uint8, there are three allowed values: {0: "data", 1: "type", 2: "data1"}
}
```
----------------------------------------
TITLE: CKB Script `code_hash` Interpretation by `hash_type`
DESCRIPTION: Explains how the `code_hash` field in a CKB Script is interpreted based on the `hash_type` value. It specifies that `code_hash` matches the Blake2b hash of either the data field or the type script of a dependency cell (`CellDep`), depending on the `hash_type`.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-theory/chapter_5.mdx#_snippet_1
LANGUAGE: APIDOC
CODE:
```
hash_type interpretations for code_hash:
- If hash_type is "data" or "data1": code_hash should match blake2b_ckbhash(data) of a dep cell.
- If hash_type is "type": code_hash should instead match blake2b_ckbhash(type script) of a dep cell.
```
----------------------------------------
TITLE: CKB Cell Field Specifications
DESCRIPTION: Detailed specifications for each field within the CKB cell structure. `capacity` defines the cell's size in shannon, `lock` and `type` are scripts governing permissions and data interpretation respectively, and `data` holds the arbitrary byte content. These definitions are essential for interacting with and understanding CKB's on-chain data model.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-theory/chapter_2.mdx#_snippet_1
LANGUAGE: APIDOC
CODE:
```
capacity:
description: The space size of the cell, i.e. the integer number of native tokens represented by this cell, usually expressed in hexadecimal. The basic unit for capacity is shannon, 1 CKB equals 10**8 shannon.
type: HexString
lock:
description: A script, which is essentially the equivalent of a lock.
type: Script
type:
description: A script, same as the lock but for a different purpose.
type: Script
data:
description: This field can store arbitrary bytes, which means any type of data.
type: HexString
```
----------------------------------------
TITLE: CKB Script Structure Definition
DESCRIPTION: Defines the fundamental structure of a CKB Script, which acts as a lock on a cell. It comprises `code_hash` to reference the executable code, `args` for arguments passed to that code, and `hash_type` to specify the interpretation method of the `code_hash`. This structure is crucial for determining cell ownership and validating transactions.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-theory/chapter_3.mdx#_snippet_0
LANGUAGE: TypeScript
CODE:
```
Script: {
code_hash: HexString
args: HexString
hash_type: Uint8, there are three allowed values: {0: "data", 1: "type", 2: "data1"}
}
```
----------------------------------------
TITLE: Checking CKB Transaction Status On-Chain
DESCRIPTION: This snippet illustrates how to check the on-chain status of a previously sent CKB transaction using the `CheckBlock` component, also within a `WalletReady` context. It explains that a 'pending' `tx_status` indicates ongoing verification, requiring a retry later.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_3/chapter_3.mdx#_snippet_2
LANGUAGE: JSX
CODE:
```
```
----------------------------------------
TITLE: CKB Transaction JSON Structure
DESCRIPTION: Defines the JSON structure for manually constructing a CKB transaction, including its inputs, outputs, and other critical fields. This structure is used for off-chain assembly and on-chain verification.
SOURCE: https://github.com/flouse/ckb-academy/blob/dev/site/src/content/courses/basic-operation/chapter_2/chapter_2.mdx#_snippet_0
LANGUAGE: APIDOC
CODE:
```
CKB Transaction (JSON Object):
version: string
Description: The transaction version, currently '0x0'.
header_deps: array (nullable)
Description: List of header dependencies, currently null.
cell_deps: array