> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zyf.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Nested Safe integration

> Connect an existing Safe treasury, deploy its Zyfai child, deposit, and withdraw.

Use this guide if your customers already manage funds in Safe{Wallet}. Zyfai creates a dedicated yield account (the **child Safe**) controlled by their existing **parent Safe**. Connect that parent in your app; Safe{Wallet} collects owner approvals.

The same sequence is implemented in the public demo: [sdk.demo.zyf.ai/nested-safe](https://sdk.demo.zyf.ai/nested-safe).

| Demo step | SDK call |
| - | - |
| 1. Connect parent Safe | `connectAccount` |
| 2. Deploy child | `deploySafe` |
| 3. Deposit from parent | `sendDeposit` |
| 4. Withdraw | `withdrawFunds` |

When the connected owner is a parent Safe, `deploySafe(parent, chainId)` deploys the child. That call does not put the on-chain session enable in the `safe-deploy` typed-data intent. Include it by passing `true` as the last argument: `deploySafe(parent, chainId, undefined, true)`. Without that flag, the account record (`hasActiveSessionKey`) is written when you call `sendDeposit` afterwards. The SDK posts `POST /session-keys/add` during that deposit, so you do not add a separate register call in your UI. `createSessionKey` stays available for manual retries or non-deploy flows. EOA callers are unchanged: the fourth `deploySafe` argument still defaults to `false`.

## Check status

After connect, call [`getNestedSafeOnboardingStatus`](https://docs.zyf.ai/docs/sdk/api/get-nested-safe-onboarding-status) with the parent address and the same `chainId` you use for deploy and deposit. Use it to label progress in your UI and to enable the right action.

```typescript theme={null}
const { status } = await sdk.getNestedSafeOnboardingStatus(parent, chainId);
// status.suggestedStep → "deploy" | "deposit" | "withdraw"
```

| Field | Use in your UI |
| - | - |
| `childRegistered`, `childDeployed` | Child setup progress |
| `sessionActive` | Agent session ready **on this chain** (not account-wide alone) |
| `hasPortfolioBalance` | Whether the child holds funds or positions |
| `suggestedStep` | Which primary button to highlight |
| `child` | Child address and deploy flags (same as `getSmartWalletAddress` when registered) |

| `suggestedStep` | Meaning |
| - | - |
| `deploy` | Run [`deploySafe`](#2-deploy-the-child) (child missing, not on chain, or session inactive on this chain) |
| `deposit` | Session ready; fund with [`sendDeposit`](#3-deposit-from-the-parent) |
| `withdraw` | Withdrawable balance; call [`withdrawFunds`](#4-withdraw-to-the-parent) |

`sessionActive` is for the `chainId` you pass. If the child is deployed but the session is inactive on that chain, `suggestedStep` is still `deploy` (call `deploySafe` again). Re-fetch status after deploy, deposit credit, or when the user changes chain.

Full method reference: [getNestedSafeOnboardingStatus](https://docs.zyf.ai/docs/sdk/api/get-nested-safe-onboarding-status.md).

## 1. Connect the parent Safe

Use WalletConnect (wagmi or Reown AppKit), or the Safe App provider when your app is embedded in Safe{Wallet}. Open the **parent** Safe before approving the connection. Do not connect an owner EOA or the Zyfai child.

```typescript theme={null}
const chainId = 8453; // Must match the connected Safe's network
const provider = await connector.getProvider();
const parent = await sdk.connectAccount(provider, chainId);
```

Wait for `connectAccount` before enabling the later buttons. Use this same address for deploy, deposit, portfolio, and withdraw. When the account or chain changes, call `disconnectAccount()` and connect again. The parent must already have code on the selected chain. See [API key and CORS](https://docs.zyf.ai/docs/sdk/getting-started.md#api-key-and-cors).

## 2. Deploy the child

```typescript theme={null}
const deployed = await sdk.deploySafe(parent, chainId);
```

Owners sign EIP-712 typed data from the execution API (`safe-deploy`), not a raw userOp `personal_sign`. The child address comes back on `deployed.safeAddress`. For a parent Safe, this step deploys the child. The on-chain session enable is in that typed data only when the fourth argument is `true`. Otherwise the session record is written on the following `sendDeposit`. A second call on an already deployed child finishes any remaining enable and does not deploy again.

## 3. Deposit from the parent

Amounts are least units (100 USDC is `"100000000"`). Check [supported assets and minimums](https://docs.zyf.ai/docs/sdk/supported-assets.md). The parent proposes an ERC-20 transfer into the child.

```typescript theme={null}
import type { NestedSafeDepositReference } from "@zyfai/sdk";

const sent = await sdk.sendDeposit({
  userAddress: parent,
  chainId,
  amount: "100000000",
  asset: "USDC",
  onSafeProposal: async (reference: NestedSafeDepositReference) => {
    await savePendingProposal(reference);
  },
});

await saveDepositId(sent.registration.id);
await clearPendingProposal();
```

`savePendingProposal`, `saveDepositId`, and `clearPendingProposal` are your storage, not SDK methods. A returned `sendDeposit` is the registered transfer, not a finished yield allocation.

| What owners see | What to show |
| - | - |
| Sign-in message | SIWE for the parent Safe |
| Deploy typed data | Child deploy and account prompts. The on-chain session enable is in this prompt only when the fourth `deploySafe` argument is `true` |
| Deposit proposal | Parent → child token transfer; other owners confirm the same Safe transaction |
| After execution | Track credit, then allocation |

For a threshold above 1, one owner cannot finish deploy or deposit alone. Co-owners confirm the same item in Safe{Wallet}. The SDK waits on the [Safe Transaction Service](https://app.safe.global/) before it treats the deposit as submitted.

If `onSafeProposal` already returned a reference, a timeout is not a failure. Reconnect as the same parent on the same chain and resume that proposal. Do not send a second transfer for the same operation.

```typescript theme={null}
const reference = await loadPendingProposal();
if (reference) {
  const sent = await sdk.resumeNestedSafeDeposit(reference);
  await saveDepositId(sent.registration.id);
}
```

Then:

```typescript theme={null}
await sdk.waitForDepositCredit(sent.registration.id, chainId);
await sdk.waitForDepositPosition(sent.registration.id, chainId);
```

Use [getDepositStatus](https://docs.zyf.ai/docs/sdk/api/get-deposit-status.md) for a one-off refresh. A polling timeout is not a reason to submit another transfer.

## 4. Withdraw to the parent

Once the session is active, the agent submits the withdrawal. Owners do not normally sign another parent-Safe transaction for that payout. Omit the amount for a full withdraw.

```typescript theme={null}
const result = await sdk.withdrawFunds(parent, chainId, "50000000", "USDC");
const withdrawalId = result.lifecycle!.id;
const settled = await sdk.waitForWithdrawSettlement(withdrawalId, chainId);
if (settled.status !== "completed") {
  await sdk.waitForWithdrawComplete(withdrawalId, chainId);
}
```

Use [getPortfolio](https://docs.zyf.ai/docs/sdk/api/get-portfolio.md) and [getSmartWalletAddress](https://docs.zyf.ai/docs/sdk/api/get-smart-wallet-address.md) with the parent address to show balances and the child. Do not ask the customer to connect the child.

## Methods

| Purpose | SDK |
| - | - |
| Connect | [connectAccount](https://docs.zyf.ai/docs/sdk/api/connect-account.md), [disconnectAccount](https://docs.zyf.ai/docs/sdk/api/disconnect-account.md) |
| Child and session | `deploySafe`, `createSessionKey` (optional retry) |
| Status | [getNestedSafeOnboardingStatus](https://docs.zyf.ai/docs/sdk/api/get-nested-safe-onboarding-status.md) |
| Deposit | [sendDeposit](https://docs.zyf.ai/docs/sdk/api/send-deposit.md), `resumeNestedSafeDeposit`, [getDepositStatus](https://docs.zyf.ai/docs/sdk/api/get-deposit-status.md), [waitForDepositCredit](https://docs.zyf.ai/docs/sdk/api/wait-for-deposit-credit.md), [waitForDepositPosition](https://docs.zyf.ai/docs/sdk/api/wait-for-deposit-position.md) |
| Portfolio | [getPortfolio](https://docs.zyf.ai/docs/sdk/api/get-portfolio.md), [getSmartWalletAddress](https://docs.zyf.ai/docs/sdk/api/get-smart-wallet-address.md) |
| Withdraw | [withdrawFunds](https://docs.zyf.ai/docs/sdk/api/withdraw-funds.md), [getWithdrawStatus](https://docs.zyf.ai/docs/sdk/api/get-withdraw-status.md), [waitForWithdrawSettlement](https://docs.zyf.ai/docs/sdk/api/wait-for-withdraw-settlement.md), [waitForWithdrawComplete](https://docs.zyf.ai/docs/sdk/api/wait-for-withdraw-complete.md) |

Working UI: [nested Safe demo](https://sdk.demo.zyf.ai/nested-safe).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.