Skip to main content

Funding and Permit2 examples

Funding is separate from a vault action. Select the target chain, verify the spender and show each approval or signature to the user. These examples never switch networks or fall back to another approval path automatically.

Native allowance​

For vaults supporting native allowance funding, the spender is the vault. Read first, then submit only the approval requested by the user:

import type { T3trisWalletClient } from "@t3tris-labs/sdk";
import type { Address } from "viem";

export async function allowanceForDeposit(
client: T3trisWalletClient, chainId: number, token: Address,
owner: Address, vault: Address, amount: bigint,
) {
const allowance = await client.tokens.allowance({ chainId, token, owner, spender: vault });
return { allowance, approvalRequired: allowance < amount };
}

export function approveDeposit(
client: T3trisWalletClient, chainId: number, token: Address,
vault: Address, amount: bigint,
) {
if (amount <= 0n) throw new Error("Approval amount must be positive.");
return client.tokens.approve({ chainId, token, spender: vault, amount });
}

Wait for a successful approval receipt before invoking requestDeposit. SDK 0.8.2 exposes no synchronous vaults.deposit method. If the result is proposed, wait for actual contract-wallet execution first. Some tokens require an allowance reset to zero before a new non-zero approval; support this as an explicit, separately confirmed flow for those tokens.

Prepare Permit2​

First ensure the ERC-20 allowance from the owner to PERMIT2_ADDRESS is sufficient, using tokens.allowance and a separate tokens.approve. Confirm that approval's execution before requesting the Permit2 signature.

The following function prepares a signature payload without signing or submitting anything:

import type { T3trisWalletClient } from "@t3tris-labs/sdk";
import {
buildPermitSingleTypedData, permitDeadlines, type PermitSingle,
} from "@t3tris-labs/sdk/onchain";
import type { Address, PublicClient } from "viem";

export async function preparePermit(
client: T3trisWalletClient, publicClient: PublicClient,
chainId: number, token: Address, owner: Address, vault: Address, amount: bigint,
) {
if (amount <= 0n || amount >= 2n ** 160n) throw new Error("Amount is outside Permit2 uint160 range.");
const allowance = await client.permit2.allowance({ chainId, token, owner, spender: vault });
const block = await publicClient.getBlock();
const deadlines = permitDeadlines(Number(block.timestamp));
const permit: PermitSingle = {
details: { token, amount, nonce: allowance.nonce, expiration: deadlines.expiration },
spender: vault,
sigDeadline: deadlines.sigDeadline,
};
return { permit, typedData: buildPermitSingleTypedData(permit, chainId) };
}

Use the same chain for the public client and connected wallet. Deadlines come from chain time. The helper's expiration is 30 days and signature deadline is 30 minutes; choose shorter values in your own PermitSingle when your policy requires them. A delayed multisig execution can outlive a signature deadline.

Sign and encode​

This step requests an EIP-712 signature from the connected wallet, not a transaction:

import { buildPermitSingleTypedData, encodePermit2Data, type PermitSingle } from "@t3tris-labs/sdk/onchain";
import type { WalletClient } from "viem";

export async function signPermit(walletClient: WalletClient, chainId: number, permit: PermitSingle) {
if (!walletClient.account) throw new Error("Connect an account first.");
if (await walletClient.getChainId() !== chainId) throw new Error("Select the target network first.");
const signature = await walletClient.signTypedData({
account: walletClient.account,
...buildPermitSingleTypedData(permit, chainId),
});
return encodePermit2Data(permit, signature);
}

Review the token, amount, vault spender and deadlines before signing. Recreate the payload if its nonce or deadline is stale. Contract-wallet signature support and aggregation are wallet-specific; verify that the final signature is valid for the target Permit2 deployment before submitting a funded action.

Submit the funded request​

import type { T3trisWalletClient } from "@t3tris-labs/sdk";
import type { Address, Hex } from "viem";

export function requestWithPermit(
client: T3trisWalletClient, chainId: number, vault: Address,
controller: Address, amount: bigint, permit2Data: Hex,
) {
return client.vaults.requestDeposit({ chainId, address: vault, controller, amount, permit2Data });
}

Obtain a transaction confirmation after the permit signature. A rejected signature, failed approval read or uncertain submission must stop the workflow. Inspect wallet and chain state before deciding whether to retry; never silently add an unlimited approval as recovery.