Getting Started
Overview
Khizab CLI is a command line interface for managing ABIs, generating code (e.g. React Hooks), and much more. It makes working with abis easier by automating manual work so you can build faster. You can learn more about the rationale behind the project in the Why Khizab CLI section.
Manual Installation
To manually add Khizab CLI to your project, install the required packages.
pnpm add -D @khizab/clipnpm add -D @khizab/clinpm install --save-dev @khizab/clinpm install --save-dev @khizab/cliyarn add -D @khizab/cliyarn add -D @khizab/clibun add -D @khizab/clibun add -D @khizab/cliCreate Config File
Run the init command to generate a configuration file: either khizab.config.ts if TypeScript is detected, otherwise khizab.config.js. You can also create the configuration file manually. See Configuring CLI for more info.
pnpm khizab initpnpm khizab initnpx khizab initnpx khizab inityarn khizab inityarn khizab initbun khizab initbun khizab initThe generated configuration file will look something like this:
import { defineConfig } from '@khizab/cli'
export default defineConfig({
out: 'src/generated.ts',
contracts: [],
plugins: [],
})import { defineConfig } from '@khizab/cli'
export default defineConfig({
out: 'src/generated.ts',
contracts: [],
plugins: [],
})Add Contracts And Plugins
Once the configuration file is set up, you can add contracts and plugins to it. These contracts and plugins are used to manage ABIs, generate code (React hooks, etc.), and much more!
For example, we can add ABI file, and the react plugins.
import { defineConfig } from '@khizab/cli'
import { react } from '@khizab/cli/plugins'
import { abi } from './abi'
import { mainnet, sepolia } from 'khizab/chains'
export default defineConfig({
out: 'src/generated.ts',
contracts: [
{
name: 'TodoList',
abi,
},
],
plugins: [
react()
],
})import { defineConfig } from '@khizab/cli'
import { react } from '@khizab/cli/plugins'
import { abi } from './abi'
import { mainnet, sepolia } from 'khizab/chains'
export default defineConfig({
out: 'src/generated.ts',
contracts: [
{
name: 'TodoList',
abi,
},
],
plugins: [
react()
],
})Run Code Generation
Now that we added a few contracts and plugins to the configuration file, we can run the generate command to resolve ABIs and generate code to the out file.
pnpm khizab generatepnpm khizab generatenpx khizab generatenpx khizab generateyarn khizab generateyarn khizab generatebun khizab generatebun khizab generateIn this example, the generate command will do the following:
- Validate the
reactplugin - Pull in the
./abifile using the name'TodoList' - Generate React Hooks for the ABI
- Save the ABI and React Hooks to the
outfile
Use Generated Code
Once out is created, you can start using the generated code in your project.
import { useReadTodoList, useReadTodoListGetTodoListCounter } from './generated'
// Use the generated TodoList read hook
const { data } = useReadTodoList({
functionName: 'get_todo_list_counter',
args: ['0xa0f18535a2d899a57befc280c953cc4a46a9b75d1f5e20337941a5f01ee79138'],
})
// Use the generated TodoList "get_todo_list_counter" hook
const { data } = useReadTodoListGetTodoListCounter({
args: ['0xa0f18535a2d899a57befc280c953cc4a46a9b75d1f5e20337941a5f01ee79138'],
})import { useReadTodoList, useReadTodoListGetTodoListCounter } from './generated'
// Use the generated TodoList read hook
const { data } = useReadTodoList({
functionName: 'get_todo_list_counter',
args: ['0xa0f18535a2d899a57befc280c953cc4a46a9b75d1f5e20337941a5f01ee79138'],
})
// Use the generated TodoList "get_todo_list_counter" hook
const { data } = useReadTodoListGetTodoListCounter({
args: ['0xa0f18535a2d899a57befc280c953cc4a46a9b75d1f5e20337941a5f01ee79138'],
})TIP
Instead of committing the out file, you likely want to add out to your .gitignore and run generate during the build process or before you start your dev server in a "predev" script.
Next Steps
For more information on what to do next, check out the following topics.
- Configuring CLI Learn how to configure the CLI to work best for your project.
- Commands Learn more about the CLI commands and how to use them.
- Plugins Browse the collection of plugins and set them up with your config.