Skip to content

Commit a89341b

Browse files
authored
feat: sub accounts quickstart, new config (#463)
* feat: sub accounts quickstart * feat: new sub account config
1 parent 2d00ee3 commit a89341b

File tree

3 files changed

+139
-25
lines changed

3 files changed

+139
-25
lines changed

docs/base-account/improve-ux/sub-accounts.mdx

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ bun add @base-org/account
5151
```
5252
</CodeGroup>
5353

54+
## Quickstart
55+
56+
The fastest way to adopt Sub Accounts is to set `creation` to `on-connect` and `defaultAccount` to `sub` in the SDK configuration.
57+
58+
```tsx
59+
const sdk = createBaseAccountSDK({
60+
// ...
61+
subAccounts: {
62+
creation: 'on-connect',
63+
defaultAccount: 'sub',
64+
}
65+
});
66+
```
67+
68+
This will automatically create a Sub Account for the user when they connect their Base Account and transactions will automatically be sent from the Sub Account unless you specify the `from` parameter in your transaction request to be the universal account address. Spend Permissions will also be automatically requested for the Sub Account as your app needs them.
69+
70+
This is what the user will see when they connect their Base Account and automatic Sub Accounts are enabled:
71+
72+
<div style={{ display: 'flex', justifyContent: 'center'}}>
73+
<img src="/images/base-account/SubAccountCreationConnect.png" alt="Sub Account Creation Flow" style={{ width: '300px', height: 'auto' }} />
74+
</div>
75+
76+
<Tip>
77+
We recommend using a [Paymaster](/base-account/improve-ux/sponsor-gas/paymasters) to sponsor gas to ensure the best user experience when integrating Sub Accounts. You can set a paymaster to be used for all transactions by configuring the `paymasterUrls` parameter in the SDK configuration. See the [createBaseAccount](/base-account/reference/core/createBaseAccount#param-paymaster-urls) reference for more information.
78+
</Tip>
79+
5480
## Using Sub Accounts
5581

5682
### Initialize the SDK
@@ -270,22 +296,7 @@ Ensure you do not lose your app's Sub Account signer keys when using the SDK on
270296

271297
Auto Spend Permissions allows Sub Accounts to access funds from their parent Base Account when transaction balances are insufficient. This feature can also establish ongoing spend permissions, enabling future transactions to execute without user approval prompts, reducing friction in your app's transaction flow.
272298

273-
### Configuration
274-
275-
Auto Spend Permissions is only available in SDK version `2.1.0` or later. This feature is **enabled by default** when using Sub Accounts.
276-
277-
To disable Auto Spend Permissions when using Sub Accounts, set `unstable_enableAutoSpendPermissions` to `false` in your SDK configuration:
278-
279-
```tsx
280-
const sdk = createBaseAccountSDK({
281-
appName: 'Base Account SDK Demo',
282-
appLogoUrl: 'https://base.org/logo.png',
283-
appChainIds: [base.id],
284-
subAccounts: {
285-
unstable_enableAutoSpendPermissions: false, // Disable auto spend permissions
286-
}
287-
});
288-
```
299+
This feature is **enabled by default** when using Sub Accounts.
289300

290301
### How it works
291302

@@ -307,6 +318,22 @@ Spend permission requests are limited to the first token when multiple transfers
307318
</Warning>
308319

309320

321+
### Configuration
322+
323+
If your users' Sub Accounts will be funded manually, you can disable Auto Spend Permissions by setting `funding` to `manual` in your SDK configuration:
324+
325+
```tsx
326+
const sdk = createBaseAccountSDK({
327+
appName: 'Base Account SDK Demo',
328+
appLogoUrl: 'https://base.org/logo.png',
329+
appChainIds: [base.id],
330+
subAccounts: {
331+
funding: 'manual', // Disable auto spend permissions
332+
}
333+
});
334+
```
335+
336+
310337
## Technical Details
311338

312339
Base Account's self-custodial design requires a user passkey prompt for each wallet interaction, such as transactions or message signing. While this ensures user awareness and approval of every wallet interaction, it can impact user experience in applications requiring frequent wallet interactions.

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)