← Back
import { useState, useCallback } from 'react';
import { BuilderConfig } from '@polymarket/builder-signing-sdk';
import { RelayClient } from '@polymarket/builder-relayer-client';
import { usePrivySigner } from './usePrivySigner';

const RELAYER_URL = 'https://relayer-v2.polymarket.com/';
const POLYGON_CHAIN_ID = 137;
// Our server-side builder sign-route (HMAC with SZHub creds) — keeps builder
// secrets off the client. Mirrors the official example's REMOTE_SIGNING_URL.
const REMOTE_SIGNING_URL = () => `${window.location.origin}/api/polymarket/sign`;

/**
 * RelayClient for gasless Safe deploy + approvals + CTF ops, signed by the
 * Privy embedded wallet, with builder attribution via our remote sign-route.
 */
export default function useRelayClient() {
  const [relayClient, setRelayClient] = useState<RelayClient | null>(null);
  const { eoaAddress, ethersSigner } = usePrivySigner();

  const initializeRelayClient = useCallback(async () => {
    if (!eoaAddress || !ethersSigner) throw new Error('Wallet not connected');

    const builderConfig = new BuilderConfig({
      remoteBuilderConfig: { url: REMOTE_SIGNING_URL() },
    });
    const client = new RelayClient(
      RELAYER_URL,
      POLYGON_CHAIN_ID,
      ethersSigner,
      builderConfig,
    );
    setRelayClient(client);
    return client;
  }, [eoaAddress, ethersSigner]);

  const clearRelayClient = useCallback(() => setRelayClient(null), []);

  return { relayClient, initializeRelayClient, clearRelayClient };
}

📜 Git History

6c47fa4chore: local Polikopi project home + Phase 1 redesign artifacts12 days ago
Show last diff
Loading...