“Its sub accounts all the way down” - Ancient Swig Proverb
This guide explains how to work with sub accounts in Swig, which allow authorities to create and manage dedicated sub account addresses with specific permissions. Sub accounts are perfect for use cases like portfolio management where you want to delegate complex operations without giving control over all assets.You can find complete working examples in the Swig repository:
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.
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 the isSubAccount parameter set to true.
Once created, you can use the sub account to perform transactions:
Copy
Ask AI
import { SystemProgram, LAMPORTS_PER_SOL,} from '@solana/web3.js';import { getSignInstructions,} from '@swig-wallet/classic';// Fund the sub accountawait connection.requestAirdrop(subAccountAddress, LAMPORTS_PER_SOL);// Create a transfer from the sub accountconst recipient = Keypair.generate().publicKey;const transfer = SystemProgram.transfer({ fromPubkey: subAccountAddress, toPubkey: recipient, lamports: 0.1 * LAMPORTS_PER_SOL,});// Sign with sub account (note: isSubAccount = true)const signIx = await getSignInstructions( swig, subAccountAuthRole.id, [transfer], true, // isSubAccount flag);await sendTransaction(connection, signIx, subAccountAuthority);
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
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
Copy
Ask AI
// Local validatorconst connection = new Connection('http://localhost:8899', 'confirmed');// Devnetconst connection = new Connection('https://api.devnet.solana.com', 'confirmed');