# SDK

The Chamber SDK is the TypeScript wrapper over the vault contracts. It covers vault creation, deposits, withdrawals, trading across supported protocols, and LP/lending/perp operations — all composed into guard-compliant transactions.

**Package:** `@dhedge/v2-sdk` (npm name is staying — code identifiers don't rebrand) **Install:** `npm install @dhedge/v2-sdk` **Requires:** `ethers` v5 with a signer connected to an RPC for a supported chain.

## Supported chains

The SDK tracks contract deployments but typically trails by a release. If you need a chain, asset, or protocol that's live in the contracts but not in the installed SDK version, call the contracts directly. The [deployment matrix](/build/deployment-matrix.md), [contract addresses](/build/contract-addresses.md), and [Guard System](/build/guard-system.md) pages are the authoritative sources for what Chamber supports — not the SDK.

The SDK's `Network` enum currently covers **Polygon, Optimism, Arbitrum, Base, Ethereum, Plasma, and HyperEVM** (exposed in code as `Network.HYPERLIQUID`). Pin a version and verify the enum on it before targeting newer chains.

## Quick start

```ts
import { Dhedge, Dapp, Network, ethers } from "@dhedge/v2-sdk";

const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const wallet = new ethers.Wallet(privateKey, provider);
const chamber = new Dhedge(wallet, Network.POLYGON);
```

The `Dhedge` client needs a wallet with a connected provider — a read-only provider is not enough for state-changing calls.

## Creating a vault

```ts
const vault = await chamber.createPool(
  "Alice", // manager name (display)
  "My Vault", // vault name
  "MVLT", // token symbol
  [
    // enabled assets: [address, isDepositAsset]
    [usdcAddress, true],
    [wethAddress, false],
  ],
  10, // performance fee (percent, 0–50)
  2, // management fee (percent/yr, 0–3)
  0, // entry fee (percent, 0–2)
  0, // exit fee (percent, 0–2)
);
```

See [create a vault](/manage/create-a-vault.md) for the manager-facing walkthrough of what these parameters mean. Fees are entered as percentages (e.g. `10` = 10%, matching the UI). Respect the [fee caps](/manage/fees-performance.md#fee-caps).

## Loading an existing vault

```ts
const vault = await chamber.loadPool(vaultAddress);
```

For non-Chamber contracts (other pools not created by the factory), pass a second argument: `loadPool(address, false)`.

## Core vault operations

Every method returns either a sent transaction or, when you pass `sdkOptions`, the prepared calldata so you can sign externally.

**Composition and state**

```ts
await vault.getComposition();     // current holdings
await vault.changeAssets([...]);  // add/remove tradeable assets (manager only)
await vault.setTrader(address);   // delegate trading to another EOA (manager only)
```

A few manager-side contract functions don't yet have SDK wrappers — notably `setMaxSupplyCap` (deposit limit) and `setPoolLogic`. Use the contracts directly for those; the [Chamber app](https://chamberfi.com/) UI also exposes them. See [permissions & access](/manage/permissions-access.md) and [create a vault](/manage/create-a-vault.md).

**Deposits and withdrawals**

```ts
await vault.approveDeposit(usdcAddress, amount);
await vault.deposit(usdcAddress, amount);
await vault.withdraw(shareAmount);
```

`deposit` and `withdraw` wrap the vault's underlying `deposit` / `withdraw` calls. The SDK's `withdraw` returns a pro-rata slice of the vault's holdings. For withdrawals that involve complex assets (LPs, perps, lending positions), you currently need to call `withdrawSafe` on the contract directly with an encoded `ComplexAsset[]` argument — the SDK doesn't yet wrap this. See [Lockup & withdrawals](/deposit/lockup-withdrawals.md) for the underlying semantics.

**Trading**

```ts
await vault.trade(Dapp.ONE_INCH, fromAsset, toAsset, amountIn, slippagePercent);
```

The `Dapp` enum covers the aggregators, DEXes, and protocols the SDK wraps — currently including Uniswap V3, 1inch, Odos, KyberSwap, CoW Swap, Pendle, Aave (V2 and V3), Compound V3, Fluid (via the Compound V3 surface), Velodrome (V2 and CL), Aerodrome (and CL), PancakeSwap CL, Toros, and Hyperliquid.

**Flat Money** has contract-level guards but isn't yet wrapped in the SDK — trades on it go through the contracts directly.

Each SDK route is composed into a guard-compliant call. Check `Dapp` in the installed version for the exact list, and the [Guard System](/build/guard-system.md) page for the full set of protocols Chamber allows at the contract level.

**LP, lending, perps**

* `addLiquidityUniswapV3` — concentrated liquidity mint (Uniswap V3, Velodrome CL, Aerodrome CL, Pancake CL)
* `addLiquidityV2` — Velodrome V2, Aerodrome
* `lend` / `withdrawDeposit` / `borrow` / `repay` — Aave V2 / V3
* `lendCompoundV3` / `withdrawCompoundV3` / `harvestCompoundV3Rewards` — Compound V3 and Fluid
* `depositHyperliquid` / `perpToSpotHyperliquid` / `spotToPerpHyperliquid` / `withdrawHyperliquid` — Hyperliquid wallet/bridge ops
* `openMarketOrderHyperliquid` / `closePositionHyperliquid` — Hyperliquid perps
* `approveTorosLimitOrder` / `createTorosLimitOrder` / `modifyTorosLimitOrder` / `deleteTorosLimitOrder` — Toros stop-loss / take-profit
* `mintUnitViaFlatMoney` / `redeemUnitViaFlatMoney` / `cancelOrderViaFlatMoney` — Flat Money

Method surface and supported protocols move with releases. Check the SDK's exported types on the installed version.

## Getting calldata without sending

Most methods accept an `sdkOptions` argument for dry-runs:

```ts
const txData = await vault.trade(Dapp.ODOS, fromAsset, toAsset, amountIn, 0.5, {
  estimateGas: true,
  onlyGetTxData: true,
});
```

Pass `onlyGetTxData: true` to return the encoded transaction rather than broadcasting it. This is how you'd integrate the SDK into a multisig flow, or hand the calldata to a user's wallet to sign.

## Version pinning

* The SDK tracks the contracts repo's version tags. Pin to a minor version in production and test across upgrades.
* Breaking changes to method signatures are documented in the repo's `CHANGELOG.md`.

## See also

* [Contract addresses](/build/contract-addresses.md) — underlying contracts the SDK calls
* [Guard system](/build/guard-system.md) — what the SDK's composed calls need to satisfy
* [Integration examples](/build/integration-examples.md) — end-to-end SDK flows


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.chamberfi.com/build/sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
