Skip to content

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

Add the following configuration to your pubspec.yaml file:

pubspec.yaml
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
  1. Run the generator command:

    Terminal window
    dart run polkadart_cli:generate -v

    The -v flag enables verbose output to see the generation progress.

  2. 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
  3. Import and use the generated code:

    import 'package:your_app/generated/polkadot/polkadot.dart';
pubspec.yaml
polkadart:
output_dir: lib/blockchain # Custom output path

For security, you can use environment variables for RPC endpoints:

pubspec.yaml
polkadart:
output_dir: lib/generated
chains:
polkadot: ${POLKADOT_RPC_URL}
kusama: ${KUSAMA_RPC_URL}

When a chain performs a runtime upgrade, regenerate the types:

Terminal window
# Clean old generated files
rm -rf lib/generated
# Regenerate with latest metadata
dart run polkadart_cli:generate -v