At a glance
- Factory:
createViemSdk(client) → ViemSdk
- Composed resources:
sdk.deposits, sdk.withdrawals, sdk.helpers
- Client vs SDK: the client wires RPC/signing; the sdk adds high-level flows (quote → prepare → create → wait) and convenience helpers.
- Wallets by flow:
- Deposits (L1 tx):
l1Wallet required
- Withdrawals (L2 tx):
l2Wallet required
- Finalize (L1 tx):
l1Wallet required
Import
Quick start
You can construct the client with only the wallets you need for a given flow (e.g., just
l2Wallet to create withdrawals; add l1Wallet when you plan to finalize).
createViemSdk(client) → ViemSdk
Returns: ViemSdk
The SDK composes the client with resources: deposits, withdrawals, and
convenience helpers.
ViemSdk interface
deposits: DepositsResource
L1 → L2 flows. See Deposits.
withdrawals: WithdrawalsResource
L2 → L1 flows. See Withdrawals.
helpers
Utilities for chain addresses, connected contracts, and L1↔L2 token mapping.
addresses() → Promise<ResolvedAddresses>
Resolve core addresses (Bridgehub, routers, vaults, base-token system).
contracts() → Promise<{ ...contracts }>
Typed Viem contracts for all core components (each exposes .read / .write / .simulate).
One-off contract getters
l1AssetRouter() → Promise<Contract>
l1NativeTokenVault() → Promise<Contract>
l1Nullifier() → Promise<Contract>
baseToken(chainId?: bigint) → Promise<Address>
L1 address of the base token for the current (or supplied) L2 chain.
l2TokenAddress(l1Token: Address) → Promise<Address>
L2 token address for an L1 token.
- Handles ETH special case (L2 ETH placeholder).
- If the token is the chain’s base token, returns the L2 base-token system address.
- Otherwise queries
IL2NativeTokenVault.l2TokenAddress.
l1TokenAddress(l2Token: Address) → Promise<Address>
L1 token for an L2 token via IL2AssetRouter.l1TokenAddress. ETH placeholder resolves to canonical ETH.
assetId(l1Token: Address) → Promise<Hex>
bytes32 asset ID via L1NativeTokenVault.assetId (ETH handled canonically).
Notes & pitfalls
- Wallet placement matters: deposits sign on L1; withdrawals sign on L2; finalization signs on L1.
- Chain-derived behavior: helpers read from on-chain sources; results depend on connected networks.
- Error model: resource methods throw typed errors; prefer
try* variants on resources for result objects.