Skip to content

Commit cd21a4c

Browse files
committed
feat: new sub account config
1 parent abe55a3 commit cd21a4c

2 files changed

Lines changed: 96 additions & 9 deletions

File tree

docs/base-account/reference/core/createBaseAccount.mdx

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,31 @@ Whether to enable functional telemetry. Defaults to `true`.
5555
</Expandable>
5656
</ParamField>
5757

58-
<ParamField body="subAccounts" type="Omit<SubAccountOptions, 'enableAutoSubAccounts'>">
58+
<ParamField body="subAccounts" type="SubAccountOptions">
5959
Sub-account configuration options.
6060

6161
<Expandable title="SubAccountOptions properties">
62+
<ParamField body="creation" type="'on-connect' | 'manual'">
63+
Controls when sub-accounts are created. Defaults to `'manual'`.
64+
65+
- `'on-connect'`: Automatically creates a sub-account when connecting to the wallet (automatically injects `addSubAccount` capability to `wallet_connect`)
66+
- `'manual'`: Requires explicit `wallet_addSubAccount` call to create a sub-account
67+
</ParamField>
68+
69+
<ParamField body="defaultAccount" type="'sub' | 'universal'">
70+
Controls which account is used by default when no account is specified. Defaults to `'universal'`.
71+
72+
- `'sub'`: Sub-account is the default account (first in accounts array)
73+
- `'universal'`: Universal account is the default account (first in accounts array)
74+
</ParamField>
75+
76+
<ParamField body="funding" type="'spend-permissions' | 'manual'">
77+
Controls how sub-accounts are funded. Defaults to `'spend-permissions'`.
78+
79+
- `'spend-permissions'`: Routes through universal account if no spend permissions exist, handles insufficient balance errors automatically. Learn more in [Auto Spend Permissions](/base-account/improve-ux/sub-accounts#auto-spend-permissions)
80+
- `'manual'`: Direct execution from sub-account without automatic fallbacks
81+
</ParamField>
82+
6283
<ParamField body="toOwnerAccount" type="ToOwnerAccountFn">
6384
Function that returns the owner account for signing sub-account transactions.
6485

@@ -72,9 +93,6 @@ Where `OwnerAccount` is a union type of:
7293
- `WebAuthnAccount` (from viem) - A WebAuthn-based account for passkey authentication
7394
</Expandable>
7495
</ParamField>
75-
<ParamField body="unstable_enableAutoSpendPermissions" type="boolean">
76-
When <code>true</code> (default), enables Auto Spend Permissions for sub-accounts. This allows automatic transfers from the user's Base Account to the sub-account when funds are missing and attempts background transactions using existing spend permissions. Set to <code>false</code> to disable this behavior. Learn more in [Auto Spend Permissions](/base-account/improve-ux/sub-accounts#auto-spend-permissions).
77-
</ParamField>
7896
</Expandable>
7997
</ParamField>
8098
@@ -147,6 +165,9 @@ const sdk = createBaseAccountSDK({
147165
telemetry: true
148166
},
149167
subAccounts: {
168+
creation: 'on-connect', // Auto-create sub-account on connection
169+
defaultAccount: 'sub', // Use sub-account by default
170+
funding: 'spend-permissions', // Auto-handle funding
150171
toOwnerAccount: async () => ({
151172
account: cryptoAccount?.account || null
152173
})
@@ -158,13 +179,16 @@ const sdk = createBaseAccountSDK({
158179
});
159180
```
160181

161-
```typescript With Sub-Accounts
182+
```typescript With Sub-Accounts (Manual Creation)
162183
import { createBaseAccountSDK } from '@base-org/account';
163184

164185
const sdk = createBaseAccountSDK({
165186
appName: 'Sub-Account App',
166187
appChainIds: [8453],
167188
subAccounts: {
189+
creation: 'manual', // Explicitly create sub-accounts when needed
190+
defaultAccount: 'universal', // Universal account is default
191+
funding: 'spend-permissions', // Auto-handle insufficient balance
168192
toOwnerAccount: async () => {
169193
// Return the owner account that will sign sub-account transactions
170194
// mainAccount should be a LocalAccount or WebAuthnAccount from viem
@@ -173,17 +197,38 @@ const sdk = createBaseAccountSDK({
173197
}
174198
});
175199

176-
// Create a sub-account
200+
// Manually create a sub-account when needed
177201
const subAccount = await sdk.subAccount.create({
178202
type: 'create',
179203
keys: [{
180204
type: 'p256',
181205
publicKey: '0x...'
182206
}]
183207
});
208+
```
209+
210+
```typescript With Auto-Created Sub-Accounts
211+
import { createBaseAccountSDK } from '@base-org/account';
212+
213+
const sdk = createBaseAccountSDK({
214+
appName: 'Auto Sub-Account App',
215+
appChainIds: [8453],
216+
subAccounts: {
217+
creation: 'on-connect', // Auto-create on wallet connection
218+
defaultAccount: 'sub', // Sub-account is default
219+
funding: 'spend-permissions', // Auto-handle insufficient balance
220+
toOwnerAccount: async () => {
221+
return { account: mainAccount || null };
222+
}
223+
}
224+
});
184225

185-
// Get existing sub-account
186-
const existingSubAccount = await sdk.subAccount.get();
226+
// Sub-account is automatically created on connection
227+
const provider = sdk.getProvider();
228+
await provider.request({ method: 'eth_requestAccounts' });
229+
230+
// Sub-account is automatically available
231+
const subAccount = await sdk.subAccount.get();
187232
```
188233
</RequestExample>
189234

@@ -233,6 +278,48 @@ const config = createConfig({
233278

234279
## Configuration Options
235280

281+
### Sub-Account Configuration
282+
283+
Configure sub-account behavior with three independent options:
284+
285+
```typescript
286+
const sdk = createBaseAccountSDK({
287+
appName: 'My App',
288+
appChainIds: [8453],
289+
subAccounts: {
290+
creation: 'on-connect' | 'manual', // When to create
291+
defaultAccount: 'sub' | 'universal', // Which is default
292+
funding: 'spend-permissions' | 'manual', // How to fund transactions
293+
toOwnerAccount: async () => ({ account }) // Owner for signing
294+
}
295+
});
296+
```
297+
298+
**Common Configurations:**
299+
300+
```typescript
301+
// Most seamless UX: Auto-create, use sub-account by default, auto-fund
302+
subAccounts: {
303+
creation: 'on-connect',
304+
defaultAccount: 'sub',
305+
funding: 'spend-permissions'
306+
}
307+
308+
// Manual control: Create when needed, universal default, auto-fund
309+
subAccounts: {
310+
creation: 'manual',
311+
defaultAccount: 'universal',
312+
funding: 'spend-permissions'
313+
}
314+
315+
// Full manual: Complete developer control
316+
subAccounts: {
317+
creation: 'manual',
318+
defaultAccount: 'universal',
319+
funding: 'manual'
320+
}
321+
```
322+
236323
### Attribution
237324

238325
Configure transaction attribution for analytics and tracking:

docs/base-account/reference/core/provider-rpc-methods/request-overview.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface ProviderInterface {
3232

3333
type CreateProviderOptions = Partial<AppMetadata> & {
3434
preference?: Preference;
35-
subAccounts?: Omit<SubAccountOptions, 'enableAutoSubAccounts'>;
35+
subAccounts?: SubAccountOptions;
3636
paymasterUrls?: Record<number, string>;
3737
};
3838

0 commit comments

Comments
 (0)