What are Sub Accounts?
Sub accounts in Swig allow an authority to create and manage multiple sub account addresses (up to 255 per role) and perform any actions on behalf of those sub accounts. This is a super high level of permission so use with care. It works for use cases like portfolio management, where you want to allow an unlimited complexity of actions on behalf of specific addresses, but you don’t want to give the authority control over all the assets of the swig. The root authority can always pull funds (SOL and tokens) from the sub accounts back to the main Swig wallet.New in v1.4.0: Each role can now have up to 255 subaccounts (indices 0-254). This enables advanced use cases like:
- Multiple isolated trading strategies per authority
- Separate subaccounts for different asset classes
- Per-client subaccounts in custodial scenarios
How Sub Accounts Work
To work with sub accounts, there are several key steps:- Sub Account Permission: The authority must have the sub account permission(s) to create sub account(s). This permission is set when creating or modifying a role. Each subaccount index (0-254) requires its own permission.
- Create Sub Account: Once the authority has the permission, they can create a sub account using
getCreateSubAccountInstructionswith an optionalsubAccountIndexparameter. - Sub Account Sign: Once created, sub accounts can perform on-chain actions using the
getSignInstructionswith theisSubAccountparameter set totrueand thesubAccountIndexspecified.
Creating a Sub Account Authority
First, you need to add an authority with sub account permissions:- Classic
- Kit
- Rust
Creating Sub Accounts
Once you have an authority with sub account permissions, you can create the sub account(s). Each subaccount is identified by an index (0-254).- Classic
- Kit
- Rust
Creating Swig Account and SubAccount in One Transaction
If you’re creating a new Swig wallet and want to set up a subaccount immediately, you can create both the Swig account with subaccount authority and the subaccount itself in a single transaction. This reduces the number of transactions needed from 3 to 1.- Rust
Using Sub Accounts
Once created, you can use the sub account to perform transactions:- Classic
- Kit
- Rust
Key Features of Sub Accounts
Sub accounts in Swig have several important characteristics:- Dedicated Addresses: Each sub account has its own unique address derived from the Swig ID, role ID, and index
- Multiple Per Role: Each role can have up to 255 subaccounts (indices 0-254), enabling parallel operations
- Isolated Operations: Sub accounts can perform complex operations without affecting the main Swig balance
- Backwards Compatible: Index 0 maintains the same address derivation as v1.3.x for seamless upgrades
- Root Authority Control: The root authority can always reclaim funds from sub accounts
- Permission-Based: Only authorities with sub account permissions can create and manage sub accounts
- Unlimited Actions: Sub accounts can perform any on-chain action within their permission scope
- Index-Specific Permissions: Each subaccount index requires its own SubAccount action/permission
Testing Environment Options
These examples can be run in different environments:-
Local Validator: Use the examples above
- Requires running a local Solana validator with
bun start-validator - Real blockchain environment
- Good for final testing
- Requires running a local Solana validator with
-
LiteSVM: For rapid development and testing
- No validator required
- Instant transaction confirmation
- Perfect for rapid development
- Simulates the Solana runtime
-
Devnet: All examples work with devnet
- Change connection URL to
https://api.devnet.solana.com - Real network environment
- Free to use with airdropped SOL
- Change connection URL to
- Classic
- Kit
Use Cases
Sub accounts are perfect for:- Portfolio Management: Allow complex DeFi operations without full wallet access
- Yield Optimization: Automated strategies with isolated risk
- Multi-Strategy Trading: Separate subaccounts for different trading strategies (now with multiple per role!)
- Asset Segregation: Different subaccounts for different asset classes or risk profiles
- Custodial Services: Per-client subaccounts with a single authority managing multiple clients
- Parallel Operations: Execute independent operations simultaneously across different subaccounts
- Delegation: Give specific permissions for particular use cases
Additional Resources
You can find more working examples in our repositories:- Rust Examples: Swig test suite
- Classic TypeScript (Single): transfer-svm-subaccount.ts
- Kit TypeScript (Single): transfer-local-subaccount.ts
- Classic TypeScript (Multiple): multi-subaccount-svm.ts
- Kit TypeScript (Multiple): multi-subaccount-local.ts
- All Examples: Swig TypeScript examples
Migration Guide
If you’re upgrading from v1.3.x to v1.4.x, your existing subaccounts will continue to work without any changes. The default subaccount (index 0) uses the same address derivation as before, ensuring backwards compatibility. To take advantage of multiple subaccounts, simply add additionalSubAccount actions with different indices to your roles.
