Type Generator
Type generation is a crucial part of working with Polkadot and Substrate-based chains. Each network has its own unique set of types, APIs, and extrinsics. Polkadart’s CLI tool automatically generates these types from chain metadata, providing:
- ✅ Type safety - Catch errors at compile time
- 💡 Auto-completion - IDE support for all chain-specific APIs
- 🔄 Always up-to-date - Regenerate when chains upgrade
- 🎯 Chain-specific - Accurate types for each network
Configuration
Section titled “Configuration”Setting Up Type Generation
Section titled “Setting Up Type Generation”Add the following configuration to your pubspec.yaml
file:
polkadart: output_dir: lib/generated # Where to generate the code chains: polkadot: wss://rpc.polkadot.io # Polkadot mainnet kusama: wss://kusama-rpc.polkadot.io # Kusama canary network # Add more chains as needed: # moonbeam: wss://wss.api.moonbeam.network # acala: wss://acala-rpc.dwellir.com
Generating Types
Section titled “Generating Types”-
Run the generator command:
Terminal window dart run polkadart_cli:generate -vThe
-v
flag enables verbose output to see the generation progress. -
Check the generated files:
A
lib/generated
folder will be created with a subfolder for each chain:lib/└── generated/├── polkadot/│ ├── polkadot.dart # Main entry point│ ├── types/ # Chain-specific types│ ├── pallets/ # Pallet APIs│ └── extrinsics/ # Transaction builders└── kusama/└── ... # Similar structure -
Import and use the generated code:
import 'package:your_app/generated/polkadot/polkadot.dart';
Advanced Configuration
Section titled “Advanced Configuration”Custom Output Directory
Section titled “Custom Output Directory”polkadart: output_dir: lib/blockchain # Custom output path
Using Environment Variables
Section titled “Using Environment Variables”For security, you can use environment variables for RPC endpoints:
polkadart: output_dir: lib/generated chains: polkadot: ${POLKADOT_RPC_URL} kusama: ${KUSAMA_RPC_URL}
Updating Generated Types
Section titled “Updating Generated Types”When a chain performs a runtime upgrade, regenerate the types:
# Clean old generated filesrm -rf lib/generated
# Regenerate with latest metadatadart run polkadart_cli:generate -v