Integration Guide

This section walks through integrating ZKGate into an existing dApp.


5.1 Step 1 – Choose Integration Model

You can integrate ZKGate as:

  1. Full Privacy Backend

    • Your dApp runs entirely on ZKGate pools (wallet, DEX, launchpad, etc.).

  2. Hybrid Privacy Layer

    • You keep your existing public contracts.

    • You add ZKGate as an optional private mode for specific actions (e.g. private swaps, private invest).


5.2 Step 2 – Onboard & Get Your PoolKey

  • Apply to ZKGate to create a pool.

  • Receive:

    • projectId

    • poolId

    • project API key(s)


5.3 Step 3 – Frontend Integration (SDK)

Example React hook:

import { useEffect, useState } from "react";
import { ZKGateClient } from "@zkgate/sdk";

export function useZKGate(projectId: string, poolId: string) {
  const [client, setClient] = useState<ZKGateClient | null>(null);

  useEffect(() => {
    const c = new ZKGateClient({
      projectId,
      poolId,
      env: "testnet",
    });
    setClient(c);
  }, [projectId, poolId]);

  return client;
}

Usage:

const zkClient = useZKGate("my-project-001", "pool-DEX-001");

async function onPrivateSwap() {
  if (!zkClient) return;

  await zkClient.executePrivate({
    action: "swap",
    poolId: "pool-DEX-001",
    payload: {
      fromAsset: "USDC",
      toAsset: "ZK",
      amountIn: "100",
      minAmountOut: "98"
    }
  });
}

5.4 Step 4 – Backend Integration (Optional)

Use backend for:

  • automated market making

  • treasury operations

  • fund strategies

  • advanced analytics

Example Node.js script:

import { ZKGateAdminClient } from "@zkgate/sdk/admin";

const admin = new ZKGateAdminClient({
  projectId: process.env.PROJECT_ID!,
  apiKey: process.env.ZKGATE_API_KEY!,
  env: "mainnet",
});

async function rebalance() {
  const state = await admin.getPoolState("pool-DEX-001");
  console.log("Pool stats:", state);

  // Example: add liquidity under certain conditions using private actions
  // ...
}

rebalance().catch(console.error);

5.5 Step 5 – UX Considerations

To keep privacy UX smooth:

  • Show a “Private Mode” toggle in UI (Public / Private).

  • Clearly label actions as “Private Swap”, “Private Deposit”, etc.

  • Add visual indicators when:

    • Proof is being generated

    • Batch is pending

    • Transaction is finalized

Last updated