Skip to main content

Wallet integration

Use the connection your app already owns​

The SDK has no dependency on wagmi or RainbowKit and does not open a connect modal. Accept a connected viem wallet client and a public client for the same chain:

import { createT3trisClient } from "@t3tris-labs/sdk";
import type { PublicClient, WalletClient } from "viem";

export function createVaultClient(walletClient: WalletClient, publicClient: PublicClient) {
if (!walletClient.account) throw new Error("Connect an account first.");
return createT3trisClient({ walletClient, publicClient });
}

For wagmi, obtain walletClient from useWalletClient or getWalletClient, then pass it with your chain's public client. Recreate the SDK client when the selected account, connector or chain changes. Clear pending confirmations and previews too.

Injected public clients remain your application's responsibility. Create viem clients with ccipRead: false (or configure the wagmi client with that setting) when calling untrusted contracts. Otherwise an OffchainLookup revert can make viem fetch a contract-selected HTTP(S) URL outside the configured RPC transport, including an internal destination when running on a backend. API-mode SDK clients do not use this on-chain path.

With an existing EIP-1193 provider, you can create both viem clients on that provider's transport:

import { createT3trisClient } from "@t3tris-labs/sdk";
import { createPublicClient, createWalletClient, custom, type Address, type EIP1193Provider } from "viem";

export function fromProvider(provider: EIP1193Provider, account: Address) {
const transport = custom(provider);
const walletClient = createWalletClient({ account, transport });
const publicClient = createPublicClient({ ccipRead: false, transport });
const client = createT3trisClient({ walletClient, publicClient });
return { client, publicClient };
}

createT3trisClient({ provider, account }) is the shorter alternative. SDK 0.8.2 disables CCIP Read on the public client it creates, so contract-selected gateway URLs are not fetched. Injected public clients are not reconfigured; keep ccipRead: false on those clients as shown above. Keep a public client available if your app needs receipt polling or block timestamps.

Select the network before the action​

Every contract read and write takes chainId. Reads check the RPC chain. Writes check the wallet chain, RPC chain and account before simulation, then again before submission. A mismatch throws ChainMismatchError.

Your application owns switching or adding networks through its wallet connector. Do it before requesting confirmation. The SDK will not switch as a side effect of an action, and a chain ID in a payload does not redirect the provider.

One write, one explicit action​

A write simulates the transaction and submits it once. It does not approve tokens, sign permits, claim shares, switch networks, retry or wait for mining. Batch methods submit one requested atomic multicall.

Keep each transaction visible to the user. Check inputs and permissions, show the expected action, request confirmation, then submit. Use separate states for waiting on a wallet signature, broadcast, mined execution and indexed data refresh.

Handle the result​

Every on-chain write returns TransactionResult:

StatusMeaningNext step
submittedA transaction hash was returnedWait for a receipt and check for a revert
proposedA contract wallet returned a proposal identifierComplete execution through that wallet and track its eventual transaction
import type { TransactionResult } from "@t3tris-labs/sdk";
import type { PublicClient } from "viem";

export async function waitForExecution(publicClient: PublicClient, result: TransactionResult) {
if (result.status === "proposed") {
return { status: "awaiting-wallet-execution" as const, proposalId: result.hash };
}
const receipt = await publicClient.waitForTransactionReceipt({ hash: result.hash });
if (receipt.status === "reverted") throw new Error("Transaction reverted.");
return { status: "executed" as const, receipt };
}

Do not treat a Safe proposal identifier as a mined transaction hash. Later steps, such as depositing after an approval, must wait until that approval has actually executed on-chain.

Amounts and units​

Contract amounts are raw integer units represented as bigint. Use the underlying token's decimals for assets and the vault share token's decimals for shares. They can differ.

import { parseUnits, formatUnits } from "viem";

const assets = parseUnits("1.25", 6); // 1_250_000n for a 6-decimal token
const displayedAssets = formatUnits(assets, 6);
const onePercentWad = 10n ** 16n; // 10^18 is 100%

On-chain fee and oracle deviation inputs use WAD. API Vault.fees uses basis points, where 100 bps is 1%. Preserve exact API amount strings and consult the entity's field unit before formatting. Decimal strings from the API and contract bigint inputs are separate representations.

When sending a wallet payload through your own JSON editor, serialize bigint amounts as integer strings and validate them before converting back to BigInt. JSON cannot encode a bigint directly.

Approvals and Permit2​

The funding examples include copyable native approval and Permit2 preparation/signing functions.

For a native allowance path, read tokens.allowance for the expected spender, submit tokens.approve if necessary and wait for execution before the funding action. Prefer an amount scoped to the user's operation over unlimited approvals. Some tokens require resetting an existing allowance to zero first.

Import Permit2 helpers from @t3tris-labs/sdk/onchain. Your application must prepare each step:

  1. Ensure the ERC-20 allowance to PERMIT2_ADDRESS is sufficient. Submit and confirm a separate approval when needed.
  2. Read client.permit2.allowance({ chainId, owner, token, spender }) for the vault's nonce.
  3. Read the latest block timestamp with your public client. permitDeadlines(Number(block.timestamp)) derives the helper's expiration and signature deadline from chain time.
  4. Build a PermitSingle with the token, intended amount, expiration, nonce and vault spender. Use buildPermitSingleTypedData for the signing payload.
  5. Ask the wallet to sign. Encode it with encodePermit2Data(permit, signature) and pass the result as permit2Data to the funding action.

Verify Permit2 deployment and the funding path for your target vault. The helper does not submit an approval or obtain a signature. Contract-wallet typed-data signing has wallet-specific behavior, so never silently fall back to a different funding path after a rejection.