What are Sub Accounts?
Sub accounts in Swig allow an authority to create and manage a single sub account address and perform any actions on behalf of that sub account. 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 a single address, 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 account back to the main Swig wallet. A great example of this is Breeze, Anagram’s onchain yield optimizer. Breeze can optimize the yield of a user’s portfolio without giving them control over their whole swig.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 to create the sub account. This permission is set when creating or modifying a role.
- Create Sub Account: Once the authority has the permission, they can create a sub account using the
getCreateSubAccountInstructions
. - Sub Account Sign: Once created, sub accounts can perform on-chain actions using the
getSignInstructions
with theisSubAccount
parameter set totrue
.
Creating a Sub Account Authority
First, you need to add an authority with sub account permissions:Creating a Sub Account
Once you have an authority with sub account permissions, you can create the sub account:Using Sub Accounts
Once created, you can use the sub account to perform transactions:Key Features of Sub Accounts
Sub accounts in Swig have several important characteristics:- Dedicated Address: Each sub account has its own unique address derived from the Swig ID and role ID
- Isolated Operations: Sub accounts can perform complex operations without affecting the main Swig balance
- 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
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
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 accounts for different trading strategies
- 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: transfer-svm-subaccount.ts
- Kit TypeScript: transfer-local-subaccount.ts
- All Examples: Swig TypeScript examples