Integration examples

End-to-end code samples for the most common Chamber integrations

Minimal, runnable examples for the flows integrators hit most often. All SDK examples assume @dhedge/v2-sdk and ethers v5. Error handling is kept terse — add production niceties (retries, user prompts, logging) in real code.

Read a vault's current composition

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

const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const chamber = new Dhedge(new ethers.Wallet(PK, provider), Network.POLYGON);

const vault = await chamber.loadPool(VAULT_ADDRESS);
const composition = await vault.getComposition();
console.log(composition);

For read-only analytics across many vaults, use the subgraph instead — it's faster and doesn't require an RPC per query.

Deposit into a vault

const vault = await chamber.loadPool(VAULT_ADDRESS);

// ERC20 approve → deposit
await vault.approveDeposit(USDC_ADDRESS, "1000000000"); // 1000 USDC (6 decimals)
const tx = await vault.deposit(USDC_ADDRESS, "1000000000");
await tx.wait();

The deposit asset must be enabled on the vault (manager-controlled). If it isn't, the call reverts with dh8arrow-up-right. All revert codes (dh3, dh8, etc.) are catalogued in the V2-Public error codes JSONarrow-up-right — that file is the source of truth and may change. See deposit for the depositor-facing story, and fees for how entry fees are settled (in shares, not the deposit asset).

Withdraw

Simple withdrawal (assets fit the basic ERC20 path):

If the vault holds complex assets (LPs, perps, lending positions), the SDK's withdraw() may not cleanly unwind them. For those vaults, call the contract's withdrawSafe(shareAmount, complexAssets[]) directly via an ethers Contract instance — each entry tells the asset guard how to unwind. The SDK doesn't currently wrap this.

Note that the depositor's lockup cooldown must have elapsed, or the call reverts with dh3arrow-up-right. See lockup & withdrawals.

Create a vault

For a walkthrough of the product decisions behind each parameter (denomination asset, fee structure, lockup), see create a vault.

Execute a trade

Only the manager or delegated trader can execute trades. The aggregator routes must compose into a guard-compliant call — if the destination contract isn't guarded, the transaction reverts with dh23.

Prepare calldata without sending

Useful for multisig flows, or handing the tx to a user's wallet to sign:

Check if a protocol is supported

Before building a call, verify the guard exists:

See guard system for the full enumeration surface.

Query the subgraph

See subgraph for schema and per-chain endpoints.

Set a trader

A manager delegates day-to-day trading to a second EOA (hot wallet, bot, or agent):

The trader can execute trades and change vault assets (default ON) but cannot change fees or the trader address itself. See trader delegation for the full permission split.

See also

Last updated