stacksdapp-codegen 0.1.2

TypeScript code generation engine for Stacks dApps, using Tera templates to create type-safe contract hooks.
Documentation
// AUTO-GENERATED by scaffold-stacks. DO NOT EDIT.
// Re-run `stacksdapp generate` to update.

"use client";
import { useState, useCallback } from 'react';
import { ClarityValue } from '@stacks/transactions';
import * as contracts from './contracts';

{% for contract in contracts %}
{% for fn in contract.functions %}
{% if fn.access == "public" or fn.access == "read_only" %}
export function use{{ contract.contract_name | upper_camel }}_{{ fn.name | upper_camel }}() {
  const [data, setData] = useState<unknown>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<Error | null>(null);
  const [txid, setTxid] = useState<string | null>(null);

  
  const isReadOnly = {% if fn.access == "read_only" %}true{% else %}false{% endif %};

  const call = useCallback(async (functionArgs: ClarityValue[] = []) => {
    setLoading(true);
    setError(null);
    setTxid(null);
    try {
      // Use the generated contract function from the index
      const fn = (contracts as any).{{ contract.contract_name | camel }}_{{ fn.name | camel }};
      
      const result = isReadOnly 
        ? await fn(functionArgs) 
        : await fn(functionArgs, []); // Default empty post-conditions for public

      if (!isReadOnly && result?.txid) {
        setTxid(result.txid);
      }
      
      setData(result ?? null);
      return result;
    } catch (e) {
      console.error(e);
      setError(e as Error);
      throw e; // Re-throw so the caller can handle it if needed
    } finally {
      setLoading(false);
    }
    // Dependency array is stable because isReadOnly is a constant literal
  }, [isReadOnly]); 

  return { data, loading, error, txid, call };
}
{% endif %}
{% endfor %}
{% endfor %}