@arkade-os/sdk Documentation - v0.4.21
    Preparing search index...

    Class ContractManager

    Central manager for contract lifecycle and operations.

    Responsibilities:

    • Create and persist contracts
    • Query stored contracts (optionally with their virtual outputs)
    • Provide spendable path selection for a contract
    • Emit contract-related events (virtual output received/spent, connection reset)

    Notes:

    • Implementations typically start watching automatically during initialization (so onContractEvent() is just for subscribing).
    const manager = await ContractManager.create({
    indexerProvider: wallet.indexerProvider,
    contractRepository: wallet.contractRepository,
    });

    // Create a new VHTLC contract
    const contract = await manager.createContract({
    label: "Lightning Receive",
    type: "vhtlc",
    params: { sender: "ark1q...", receiver: "ark1q...", ... },
    script: "5120...",
    address: "ark1q...",
    });

    // Start watching for events
    const unsubscribe = manager.onContractEvent((event) => {
    console.log(`${event.type} on ${event.contractScript}`);
    });

    // Query contracts together with their current virtual outputs
    const contractsWithVtxos = await manager.getContractsWithVtxos();

    // Get balance across all contracts
    const balances = contractsWithVtxos.flatMap(({vtxos}) => vtxos).reduce((acc, vtxo) => acc + vtxo.value, 0)

    // Later: unsubscribe from events
    unsubscribe();

    // Clean up
    manager.dispose();

    Implements

    Index

    Methods

    • Symbol.dispose implementation for using with using keyword.

      Returns void

      {
      using manager = await wallet.getContractManager();
      // ... use manager
      } // automatically disposed
    • Stamp raw virtual outputs with the correct per-contract tapscripts (forfeit, intent, tap tree).

      Resolves each vtxo's script to its owning contract via the contract repository and attaches the matching tapscripts. Throws when any vtxo references a script with no registered contract — callers are expected to register the contract before asking for annotation. This is the single shared path that replaces scattered extendVirtualCoin* calls in wallet/handler code, and keeps the wallet from silently stamping the default tapscript onto a non-default vtxo.

      Parameters

      Returns Promise<ExtendedVirtualCoin[]>

    • Dispose of the ContractManager and release all resources.

      Stops the watcher, clears callbacks, and marks the manager as uninitialized.

      Implements the disposable pattern for cleanup.

      Returns void

    • Get contracts with optional filters.

      Parameters

      • Optionalfilter: ContractFilter

        Optional filter criteria

      Returns Promise<Contract[]>

      Filtered contracts TODO: filter spent/unspent

      // Get all VHTLC contracts
      const vhtlcs = await manager.getContracts({ type: 'vhtlc' });

      // Get all active contracts
      const active = await manager.getContracts({ state: 'active' });
    • Register a callback for contract events.

      The manager automatically watches after initialize(). This method allows registering callbacks to receive events.

      Parameters

      Returns () => void

      Unsubscribe function to remove this callback

      const unsubscribe = manager.onContractEvent((event) => {
      console.log(`${event.type} on ${event.contractScript}`);
      });

      // Later: stop receiving events
      unsubscribe();
    • Force refresh virtual outputs from the indexer.

      Without options, re-fetches every contract and advances the global cursor. With options, narrows the refresh to specific scripts and/or a time window. Subset refreshes (scripts filter) intentionally do not advance the cursor.

      Parameters

      • Optionalopts: RefreshVtxosOptions

      Returns Promise<void>

    • Update a contract. Nested fields like params and metadata are replaced with the provided values. If you need to preserve existing fields, merge them manually.

      Parameters

      • script: string

        Contract script

      • updates: Partial<Omit<Contract, "script" | "createdAt">>

        Fields to update

      Returns Promise<Contract>

    • Update a contract's params. This method preserves existing params by merging the provided values.

      Parameters

      • script: string

        Contract script

      • updates: Record<string, string>

        The new values to merge with existing params

      Returns Promise<Contract>