@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
136 lines (135 loc) • 5.22 kB
TypeScript
/**
* Provides type-safe contract interaction utilities for the Vana protocol.
*
* @remarks
* This module enables strongly-typed smart contract interactions with automatic
* ABI loading, address resolution, and instance caching. It follows viem's patterns
* for contract typing while providing Vana-specific conveniences.
*
* @category Contracts
* @module contractController
*/
import type { Abi } from "abitype";
import { type GetContractReturnType, type PublicClient, type WalletClient } from "viem";
import { type ContractAbis, type VanaContract } from "../generated/abi";
import type { VanaChainId, ContractInfo } from "../types/index";
import { createClient } from "../core/client";
export declare const contractCacheForTesting: Map<string, {
address: `0x${string}`;
abi: Abi;
}>;
/**
* Gets a typed contract instance for the specified contract name.
*
* @remarks
* Provides complete type safety following viem's patterns with automatic
* ABI loading and address resolution. Contract instances are cached per
* chain for performance. Use const assertion for full type inference.
*
* @param contract - Name of the contract to instantiate.
* Use const assertion for full typing: `"DataRegistry" as const`
* @param client - Optional viem client instance.
* Defaults to auto-configured client. Obtain via `createClient()` or viem.
* @returns A fully typed contract instance with methods corresponding to the contract's ABI
*
* @example
* ```typescript
* // Full type inference with const assertion
* const dataRegistry = getContractController("DataRegistry" as const, client);
*
* // Now dataRegistry has full type inference for all methods
* const result = await dataRegistry.read.getFileCount(); // Type: bigint
* await dataRegistry.write.addFile([url, proof]); // Typed parameters
*
* // Auto-configured client
* const permissions = getContractController("DataPortabilityPermissions" as const);
* const granted = await permissions.read.hasPermission([grantor, grantee]);
* ```
*
* @category Contracts
*/
export declare function getContractController<T extends VanaContract>(contract: T, client?: PublicClient | WalletClient | ReturnType<typeof createClient>): GetContractReturnType<ContractAbis[T]>;
/**
* Gets contract information without creating a contract instance.
*
* @remarks
* Returns contract address and ABI for manual contract interaction or
* custom client configuration. Useful when you need contract details
* but don't want to create a client connection.
*
* @param contract - Name of the contract.
* Use const assertion for typed ABI.
* @param chainId - Chain ID to get contract info for.
* Defaults to Vana mainnet (1480).
* @returns Contract information with typed ABI
*
* @example
* ```typescript
* const info = getContractInfo("DataRegistry" as const, 14800);
* console.log(info.address); // Typed as Address
* console.log(info.abi); // Fully typed ABI
*
* // Use with custom viem client
* const contract = getContract({
* ...info,
* client: customClient
* });
* ```
*
* @category Contracts
*/
export declare function getContractInfo<T extends VanaContract>(contract: T, chainId?: VanaChainId): ContractInfo<ContractAbis[T]>;
/**
* Provides type-safe contract factory for creating multiple contract instances.
*
* @remarks
* Alternative API for applications that need to create multiple contracts
* with the same client. The factory pattern reduces boilerplate and ensures
* consistent client configuration across contracts.
*
* @example
* ```typescript
* const factory = new ContractFactory(client);
*
* const dataRegistry = factory.create("DataRegistry" as const);
* const permissions = factory.create("DataPortabilityPermissions" as const);
*
* // List available contracts
* const contracts = factory.getAvailableContracts();
* ```
*
* @category Contracts
*/
export declare class ContractFactory {
private readonly client;
private readonly chainId;
constructor(client: PublicClient | WalletClient | ReturnType<typeof createClient>);
/**
* Creates a typed contract instance
*
* @param contract - Contract name (use const assertion for full typing)
* @returns Fully typed contract instance
*/
create<T extends VanaContract>(contract: T): GetContractReturnType<ContractAbis[T]>;
/**
* Gets contract information without creating an instance
*
* @param contract - Contract name
* @returns Contract information with typed ABI
*/
getInfo<T extends VanaContract>(contract: T): ContractInfo<ContractAbis[T]>;
/**
* Lists all available contracts for the current chain
*
* @returns Array of contract names available on this chain
*/
getAvailableContracts(): VanaContract[];
}
/**
* Clears the contract cache. Useful for testing or when chain configurations change.
*
* @param contract - Optional specific contract to clear, or clear all if not provided
* @param chainId - Optional specific chain to clear, or clear all if not provided
*/
export declare function clearContractCache(contract?: VanaContract, chainId?: number): void;
export type { GetContractReturnType } from "viem";