# Deposit from a Safe

> Treasuries deposit from a multisig. Approve and deposit go out as one Safe transaction.

- Status: live. Compiles on Composer today.
- Who it's for: DAOs, funds and treasury tools
- Page: https://composer-experiences.li.quest/templates/safe-deposit
- Working example, run with your own key and wallet: https://composer-experiences.li.quest/templates/safe-deposit/live

## What you get

- One proposal for the owners to sign
- Works with the Safe Transaction Service

## What runs, in order

A step without ops is work outside the flow, such as the user paying in. `×n` marks an op the flow repeats.

1. Safe pays (Base)
2. Approve and deposit, batched (Base). Ops: `lifi.zap`
3. Owners confirm (Base): Safe signatures
4. Position in the Safe (Base)

## What you control

| Parameter | Set by |
| --- | --- |
| Safe address | Passed by your user |
| Vault | Fixed by you |

## Build it

1. Get an API key at https://portal.li.fi. Keep it on your server.
2. Install these exact versions; npm's `latest` tag lags production:

```sh
npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0
```

3. Add the module below as `safe-deposit.ts`. It names the flow `tpl:safe-deposit@1`, so LI.FI can see which template it came from.
4. Compile with `simulationPolicy: 'allow-revert'`. Before the user signs, show `outputs[<key>].amount.minimum.value`: the least they receive. It is optional; when it is absent, the flow has no floor.

### The module: `safe-deposit.ts`

```ts
/**
 * Deposit a Safe's tokens into a vault: the Safe itself is the signer, so the
 * vault shares land back in the Safe and no owner wallet ever holds the funds.
 * The compile result's `actions` (approve, then execute) are what the Safe's
 * owners confirm, batched into one Safe transaction. Call it with
 * `const { flow, run } = buildFlow(sdk, inputs); const result = await flow.compile(run);`.
 */
import {
  type Address,
  type ComposeSdk,
  guards,
  materialisers,
  resources,
} from '@lifi/composer-sdk';

export interface SafeDepositInputs {
  chainId: number;
  /** The Safe: it signs the compile, grants the approval and receives the shares. */
  safe: Address;
  /** The token the Safe spends, e.g. USDC. */
  token: Address;
  /** In `token`'s smallest unit. */
  amount: bigint;
  /** The vault's share token. */
  vault: Address;
  slippageBps: number;
}

export const buildFlow = (
  sdk: ComposeSdk,
  inputs: SafeDepositInputs,
  name = 'tpl:safe-deposit@1'
) => {
  const { chainId, safe } = inputs;
  const flow = sdk.flow(chainId, {
    name,
    inputs: { funding: resources.erc20(inputs.token, chainId) },
  });

  // One zap: the Safe's token goes in, the vault's share token comes out.
  flow.lifi.zap('deposit', {
    bind: { amountIn: flow.inputs.funding },
    config: { resourceOut: resources.erc20(inputs.vault, chainId) },
    guards: [guards.slippage({ port: 'amountOut', bps: inputs.slippageBps })],
  });

  // `signer: safe` gives the Safe its own proxy; the approval it has to grant
  // that proxy comes back as the first action, and the shares sweep to the Safe.
  const run = {
    signer: safe,
    inputs: {
      funding: materialisers.directDeposit({ amount: inputs.amount }),
    },
    sweepTo: safe,
  };
  return { flow, run };
};
```

### Call it

```ts
import { createComposeSdk } from '@lifi/composer-sdk';

import { buildFlow } from './safe-deposit';

const sdk = createComposeSdk({
  baseUrl: 'https://composer.li.quest',
  apiKey: process.env.LIFI_API_KEY!,
});

// inputs: the values your user picks; see the module's inputs interface.
const { flow, run } = buildFlow(sdk, inputs);
const result = await flow.compile({ ...run, simulationPolicy: 'allow-revert' });
// Show the outputs' amount.minimum to the user, then have them sign
// result.transaction.
```

### The same call over REST

Compile and simulate in one call, for sample values:

```http
POST https://composer.li.quest/compose
content-type: application/json
x-lifi-api-key: <your key>

{
  "flow": {
    "version": 1,
    "id": "tpl:safe-deposit@1",
    "chainId": 8453,
    "inputs": [
      {
        "name": "funding",
        "resource": {
          "kind": "erc20",
          "token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
          "chainId": 8453
        }
      }
    ],
    "nodes": [
      {
        "id": "deposit",
        "op": "lifi.zap",
        "bind": {
          "amountIn": {
            "$ref": "input.funding"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0xbeeF010f9cb27031ad51e3333f9aF9C6B1228183",
            "chainId": 8453
          }
        },
        "guards": [
          {
            "kind": "slippage",
            "port": "amountOut",
            "bps": 50
          }
        ]
      }
    ]
  },
  "run": {
    "inputs": {
      "funding": {
        "kind": "directDeposit",
        "amount": "50000000000"
      }
    },
    "signer": "0xbeeB9eeE061925cC6d2122F05a4e6536F0FEB000",
    "sweepTo": "0xbeeB9eeE061925cC6d2122F05a4e6536F0FEB000"
  }
}
```

### Prompt for a coding agent

```text
Goal: add "Deposit from a Safe" to my app with LI.FI Composer.
1. Install exactly: npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0
2. Read https://composer.li.quest/llms.txt. The manifest at https://composer.li.quest/compose/manifest is the source of truth for operations.
3. The flow: Safe pays → Approve and deposit, batched (lifi.zap) → Owners confirm → Position in the Safe.
   What can change: Safe address, Vault.
4. Name the flow "tpl:safe-deposit@1" so LI.FI can see which template it came from.
5. Compile with POST /compose and simulationPolicy: 'allow-revert'.
   Before the user signs, show outputs[<key>].amount.minimum.value: the least they receive.
   It is optional; when it is absent, the flow has no floor.
6. The API key comes from https://portal.li.fi. Keep it on the server.
7. If you get stuck, POST https://composer.li.quest/feedback with what you tried.
```

## Composer references

- [Composer API llms.txt](https://composer.li.quest/llms.txt)
- [Operation manifest](https://composer.li.quest/compose/manifest): the source of truth for operations
- [Docs](https://docs.li.fi/composer/overview.md)
- Stuck? `POST https://composer.li.quest/feedback` with what you tried; it needs no key.
