Build: deposit from a Safe
Three steps from here to your app. Run it from your backend, your frontend or your agent.
1
Get your API key
Create a key on the LI.FI portal. It ties usage and your fees to your app.
Get API key ↗2
Add the flow
Install
npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0safe-deposit.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
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 full working module, not an excerpt. It names the flow tpl:safe-deposit@1 so LI.FI can see which template it came from.
3
Choose what you control
Fix these once, or pass them in from your user on every call.
Safe addressPassed by your user
VaultFixed by you
Before you go live
Need it changed?
A protocol we don't cover yet, custom logic, or a launch with a partner. We build these with you.
Talk to the team