Services

Service modules that interact with the Stacks blockchain and external APIs.

escrow-service.ts

Core service for reading escrow data from the blockchain and Supabase.

Key Functions

typescript
import { getEscrow, getEscrows, getUserEscrows, getEscrowStats, } from "@/lib/escrow-service";
FunctionDescriptionReturns
getEscrow(id)Fetch single escrow by IDEscrow
getEscrows(filters)Fetch escrows with optional filtersEscrow[]
getUserEscrows(address)Fetch escrows for a specific userEscrow[]
getEscrowStats()Aggregate statisticsEscrowStats

Supabase vs On-Chain

The service primarily reads from Supabase (indexed data) for performance. For critical operations, it verifies against on-chain data:

typescript
// Fast: Read from Supabase index const escrow = await getEscrow(id); // Verified: Compare with on-chain state const onChainEscrow = await getEscrowOnChain(id);

admin-service.ts

Admin-only operations for contract management.

typescript
import { getContractOwner, getPlatformFee, getDisputeTimeout, setPlatformFee, setDisputeTimeout, resolveDispute, } from "@/lib/admin-service";
FunctionDescriptionAuth
getContractOwner()Get contract owner addressPublic
getPlatformFee()Get current fee percentagePublic
getDisputeTimeout()Get timeout in blocksPublic
setPlatformFee(fee)Update platform feeAdmin
setDisputeTimeout(blocks)Update dispute timeoutAdmin
resolveDispute(id, resolution)Resolve a disputed escrowAdmin

post-conditions.ts

Builds Stacks post-conditions for safe transactions.

typescript
import { makeFundPostConditions, makeReleasePostConditions, } from "@/lib/post-conditions";

Post-conditions ensure that a transaction only executes if certain conditions are met. This prevents unexpected token transfers:

typescript
// Funding: sender must send exactly the escrow amount const postConditions = makeFundPostConditions( senderAddress, amount, tokenType // "STX" | "sBTC" ); // Release: contract must send exactly the escrow amount to recipient const releaseConditions = makeReleasePostConditions( escrowId, amount, recipientAddress, tokenType );

stacks-config.ts

Network configuration for Stacks blockchain connections.

typescript
import { network, contractAddress, contractName } from "@/lib/stacks-config";

Configuration is determined by VITE_STACKS_NETWORK environment variable:

VariableValues
VITE_STACKS_NETWORKmainnet, testnet, devnet
VITE_CONTRACT_ADDRESSDeployer address
VITE_CONTRACT_NAMEContract name (default: escrow-v6)

generate-receipt.ts

Generates PDF receipts for completed escrows.

typescript
import { generateReceipt } from "@/lib/generate-receipt"; // Download a PDF receipt await generateReceipt(escrow);

supabase.ts

Supabase client initialization.

typescript
import { supabase } from "@/lib/supabase"; // Direct queries (use sparingly - prefer hooks) const { data } = await supabase .from("escrows") .select("*") .eq("status", "funded");

types.ts

TypeScript type definitions shared across the frontend.

typescript
import type { Escrow, EscrowStatus, TokenType, EscrowStats, DisputeResolution, } from "@/lib/types";

Key types:

typescript
type EscrowStatus = | "pending" | "funded" | "completed" | "refunded" | "disputed" | "resolved" | "expired"; type TokenType = "STX" | "sBTC"; interface Escrow { id: number; sender: string; recipient: string; amount: number; tokenType: TokenType; status: EscrowStatus; createdAt: number; expiresAt: number; description?: string; }