Understanding the Architecture
Before adding features, understand SubWallet’s architecture:- Background Environment: Handles API calls, state management, and cron jobs
- Extension UI: React-based frontend (popup, portfolio pages)
- Injected Scripts: Provides wallet functionality to dApps
- Message Passing: Communication between components
Adding an API
APIs are defined inpackages/extension-koni-base/src/api and handle external data fetching.
Create API File
Add a new file based on the API type. For example, create
packages/extension-koni-base/src/api/nft.ts:Adding a Store
Stores persist data to Chrome local storage and are defined inpackages/extension-koni-base/src/store.
Create Store Class
Create a new store file, e.g., Store Types:
packages/extension-koni-base/src/store/Nft.ts:BaseStore: Basic persistence to Chrome storageSubscribableStore: ExtendsBaseStore, includes RxJS subject for subscriptions
Adding a Message Handler
Message handlers enable communication between the background, extension UI, and web pages.Define Request Type
Add the message type to Message Type Format:
KoniRequestSignatures interface:pri(...)- Messages from extension pagespub(...)- Messages from web pages/tabs- Array format:
[RequestType, ResponseType]or[RequestType, SubscriptionBool, SubscriptionType]
Add Handler in Background
Implement the handler in For Tab Messages (KoniTabs):
KoniExtension or KoniTabs:For Extension Messages (KoniExtension):Adding a Cron Job
Cron jobs run periodic tasks in the background, such as price updates or chain synchronization.Register in KoniCron
Add the cron job to
KoniCron.init() in packages/extension-koni-base/src/background/KoniCron.ts:Developing UI Components
SubWallet Extension UI is built with React and Redux Toolkit.UI Structure
packages/extension-koni-ui/src/Popup.tsx- Main extension popupcomponents/- Reusable componentshooks/- Custom React hooksstores/- Redux storespartials/- Header and layout componentsmessaging.ts- Message passing functions
Code Quality
Before committing your feature:Best Practices
API Guidelines
- Keep API logic separate from business logic
- Use simple functions for single-purpose APIs
- Use objects for grouped related API calls
- Always handle errors gracefully
- Add appropriate TypeScript types
Store Guidelines
- Use
BaseStorefor simple data persistence - Use
SubscribableStorewhen UI needs real-time updates - Always initialize stores in
KoniState.init() - Use consistent naming:
${EXTENSION_PREFIX}-${storeName}
Message Handler Guidelines
- Prefix extension messages with
pri - Prefix tab messages with
pub - Define all types in
KoniRequestSignatures - Keep handlers focused and simple
- Log errors appropriately
Cron Job Guidelines
- Make intervals configurable
- Add error handling for network failures
- Log start and completion
- Consider rate limiting for external APIs
- Clean up resources on extension unload
UI Guidelines
- Use Redux Toolkit for state management
- Subscribe to background data instead of polling
- Handle loading and error states
- Keep components focused and reusable
- Follow existing styling patterns