Frontend Integration

useCounterDeal Hook

Encrypt investor cUSDC intent and submit counterDeal with safe gas handling.

The counter flow must encrypt the fund amount immediately before submission because Nox handle proofs expire and must target the DealRoom address.

Force gas when estimation fails.

Nox internal calls can cause estimation to fail or return MAX_UINT64. Use gas: 1_500_000n as a direct-write fallback.

hooks/useCounterDeal.ts
export function useCounterDeal() {
  const { writeContractAsync } = useWriteContract();

  const counterDeal = async ({ dealId, fundAmount }) => {
    const walletClient = await getWalletClient(wagmiConfig);
    const handleClient = await createViemHandleClient(walletClient);

    // ATOMIC: encrypt immediately before submit
    const { handle, handleProof } = await handleClient.encryptInput(
      fundAmount,
      "uint256",
      DEAL_ROOM_ADDRESS
    );

    const hash = await writeContractAsync({
      address: DEAL_ROOM_ADDRESS,
      abi: dealRoomAbi,
      functionName: "counterDeal",
      args: [dealId, handle, handleProof],
      gas: 1_500_000n, // required - Nox calls break gas estimation
    });

    return waitForTransactionReceipt(wagmiConfig, { hash });
  };

  return { counterDeal };
}