The Polkadot API Playground is an interactive web application designed for developers to learn, test, and experiment with the Polkadot API (PAPI) library. This playground provides a user-friendly interface for interacting with Polkadot and its test networks through a code editor, console output, and pre-built examples.
- Multi-Network Support: Connect to different Polkadot networks (Polkadot, Westend, Paseo, Rococo)
- Interactive Code Editor: Write and edit TypeScript/JavaScript code with syntax highlighting
- Pre-built Examples: Learn from curated examples covering common blockchain operations
- Live Console Output: View execution results in real-time
- Dark/Light Theme Switching: Choose your preferred UI theme
- Responsive Design: Use on desktop or mobile devices
# Clone the repository
git clone https://github.com/developerfred/papi-simulator.git
cd papi-simulator
# Install dependencies
npm install
# Start development server
npm run devThen open http://localhost:3000 in your browser.
The project is built with Next.js and follows a well-organized structure:
/src
/app # Next.js App Router pages
/components # React components
/common # Shared utility components
/layouts # Layout components
/LivePreview # Component preview functionality
/Playground # Main playground components
/ui # UI components (buttons, cards, etc.)
/lib # Utilities and business logic
/constants # Application constants
/editor # Code editor configuration
/examples # Example code templates
/hooks # React hooks
/simulation # Code execution simulation
/theme # Theme configuration
/types # TypeScript type definitions
/utils # Utility functions
- Choose a network from the Network Selector panel:
- Westend: Test network with similar parameters to Polkadot
- Paseo: Testing network for parachains
- Polkadot: Main production network
Each network has its own token symbol, endpoint, and parameter configuration.
The playground includes several pre-built examples:
- Simple Transfer: Create a basic balance transfer between accounts
- Query Balance: Check an account's balance and format it for display
- Watch Blocks: Subscribe to finalized blocks and monitor the chain
- Network Dashboard: Interactive component showing network details
- Test Component: Simple React component for testing the live preview
Select an example to load its code into the editor.
- After selecting a network and example (or writing your own code):
- Click the "Run Code" button
- View the simulated execution output in the Console panel
- For React components, toggle "Live Preview" to see rendered output
The playground supports React component rendering in the "Live Preview" mode:
- Select a component example like "Network Dashboard" or "Test Component"
- Toggle the editor to "Live Preview" mode
- Make changes to the component code and see them rendered in real-time
The code editor is powered by Monaco Editor (the same engine as VS Code) and provides:
- Syntax highlighting for TypeScript/JavaScript
- Auto-completion for Polkadot API functions
- Error checking and linting
- Keyboard shortcuts for common operations
Configuration options for the editor can be found in src/lib/editor/types.ts.
The console displays simulated execution results including:
- Log messages (
console.log()output) - Warnings (
console.warn()output) - Errors (
console.error()output) - Execution status and timing information
Each message is timestamped and color-coded based on its type.
Network endpoints and parameters are defined in src/lib/constants/networks.ts. Each network includes:
- ID: Unique identifier for the network
- Name: Display name
- Endpoint: WebSocket RPC endpoint for connection
- Faucet: URL to get test tokens
- Explorer: Block explorer URL
- Token Symbol: The network's native token
- Token Decimals: Decimal places for the token
Examples are organized using a factory pattern in src/lib/examples/:
- BaseExampleFactory: Abstract base class for creating examples
- Example Implementations: Individual classes extending the base factory
- ExampleRegistry: Central registry managing all available examples
To add a new example, create a new class extending ExampleFactory and register it with the example registry.
The project includes several testing capabilities:
# Run all tests
npm test
# Run with coverage
npm test -- --coverage- Unit Tests: Test individual components and utilities
- Integration Tests: Test interactions between components
- E2E Tests: Test full workflows from UI interactions to simulated results
- Connection errors: Ensure you have internet access and the selected network is available
- Code execution errors: Check console output for detailed error messages
- UI layout issues: Try refreshing or toggling between desktop/mobile views
If you encounter issues not covered in this documentation:
- Check the GitHub Issues for similar problems
- Join the Polkadot Discord and ask in the development channels
- Review the Polkadot-API Documentation for reference
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-feature) - Make your changes
- Run tests to ensure everything works (
npm test) - Commit your changes (
git commit -m 'Add my feature') - Push to your branch (
git push origin feature/my-feature) - Create a new Pull Request
- Follow TypeScript best practices and type all functions/components
- Use functional components with hooks for React code
- Document new features thoroughly
- Write tests for any new functionality
This project is licensed under the MIT License - see the LICENSE file for details.
To add custom networks to the playground:
- Edit
src/lib/constants/networks.tsand add your network configuration - Generate the necessary descriptors using the Polkadot-API CLI:
npx papi add <your-network-key> -w wss://your-network-endpoint npx papi generate
- Import the generated descriptors in your code
To create a new example:
- Create a new file in
src/lib/examples/extending theExampleFactoryclass - Implement the
generateCodemethod to return code specific to your example - Register your example in
src/lib/examples/index.ts
Example:
// src/lib/examples/MyCustomExample.ts
import type { Network } from "../types/network";
import { ExampleFactory } from "./factory";
export class MyCustomExample extends ExampleFactory {
constructor() {
super({
id: "my-custom-example",
name: "My Custom Example",
description: "Description of what this example does",
level: "beginner", // or "intermediate" or "advanced"
categories: ["category1", "category2"],
});
}
generateCode(network: Network): string {
return `// Custom example code for ${network.name}
// Your code here
`;
}
}
// Then register in src/lib/examples/index.ts
import { MyCustomExample } from "./MyCustomExample";
exampleRegistry.register(new MyCustomExample());The Live Preview feature renders React components in real-time. Understanding its limitations is important:
- Supported libraries include React core and some specific UI libraries
- Component props are limited to primitive types and basic objects
- External data fetching requires mocking or simulation
- State is reset when code changes
For more advanced component testing, consider using Storybook alongside the playground.
The application uses a theme provider in src/lib/theme/ThemeProvider.tsx. To customize:
- Edit the color constants in
src/lib/theme/themeConstants.ts - Update the CSS variables in
src/app/globals.cssfor consistent styling - Network-specific colors are defined in the
NETWORK_COLORSconstant
The playground interacts with the Polkadot API through several abstraction layers:
// Creating a client connection
import { createClient } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws-provider/web";
const client = createClient(
getWsProvider("wss://endpoint.example.com")
);// Getting a typed API for a specific chain
import { chainDescriptor } from "@polkadot-api/descriptors";
const typedApi = client.getTypedApi(chainDescriptor);// Query chain storage
const value = await typedApi.query.System.Account.getValue("address");// Create a transaction
const tx = typedApi.tx.Balances.transfer_keep_alive({
dest: MultiAddress.Id("recipient-address"),
value: 1_000_000_000n
});
// Get encoded data for simulation
const encodedData = await tx.getEncodedData();
// In a real application, sign and submit:
// const result = await tx.signAndSubmit(signer);// Subscribe to events
typedApi.event.Balances.Transfer.watch().subscribe(event => {
console.log("Transfer detected:", event);
});For the complete API reference, see the official Polkadot-API Documentation.