UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

1 lines 9.97 kB
{"version":3,"sources":["../../src/contracts/contractController.ts"],"sourcesContent":["/**\n * Provides type-safe contract interaction utilities for the Vana protocol.\n *\n * @remarks\n * This module enables strongly-typed smart contract interactions with automatic\n * ABI loading, address resolution, and instance caching. It follows viem's patterns\n * for contract typing while providing Vana-specific conveniences.\n *\n * @category Contracts\n * @module contractController\n */\n\nimport type { Abi } from \"abitype\";\nimport {\n getContract,\n type GetContractReturnType,\n type PublicClient,\n type WalletClient,\n} from \"viem\";\nimport { type ContractAbis, getAbi, type VanaContract } from \"../generated/abi\";\nimport type { VanaChainId, ContractInfo } from \"../types/index\";\nimport { getContractAddress, CONTRACT_ADDRESSES } from \"../generated/addresses\";\nimport { createClient } from \"../core/client\";\nimport { vanaMainnet } from \"../config/chains\";\n\n// Cache for contract instances - keyed by contract name and chain ID\nconst contractCache = new Map<string, GetContractReturnType<Abi>>();\n\n// Export cache for testing\nexport const contractCacheForTesting = contractCache;\n\n/**\n * Creates a cache key for contract instances.\n *\n * @remarks\n * Generates unique keys for caching contract instances per chain to prevent\n * cross-chain contamination and improve performance.\n *\n * @param contract - The contract name to create a cache key for\n * @param chainId - The chain ID to include in the cache key\n * @returns A string cache key combining contract name and chain ID\n *\n * @internal\n */\nfunction createCacheKey(contract: VanaContract, chainId: number): string {\n return `${contract}:${chainId}`;\n}\n\n/**\n * Gets a typed contract instance for the specified contract name.\n *\n * @remarks\n * Provides complete type safety following viem's patterns with automatic\n * ABI loading and address resolution. Contract instances are cached per\n * chain for performance. Use const assertion for full type inference.\n *\n * @param contract - Name of the contract to instantiate.\n * Use const assertion for full typing: `\"DataRegistry\" as const`\n * @param client - Optional viem client instance.\n * Defaults to auto-configured client. Obtain via `createClient()` or viem.\n * @returns A fully typed contract instance with methods corresponding to the contract's ABI\n *\n * @example\n * ```typescript\n * // Full type inference with const assertion\n * const dataRegistry = getContractController(\"DataRegistry\" as const, client);\n *\n * // Now dataRegistry has full type inference for all methods\n * const result = await dataRegistry.read.getFileCount(); // Type: bigint\n * await dataRegistry.write.addFile([url, proof]); // Typed parameters\n *\n * // Auto-configured client\n * const permissions = getContractController(\"DataPortabilityPermissions\" as const);\n * const granted = await permissions.read.hasPermission([grantor, grantee]);\n * ```\n *\n * @category Contracts\n */\nexport function getContractController<T extends VanaContract>(\n contract: T,\n client:\n | PublicClient\n | WalletClient\n | ReturnType<typeof createClient> = createClient(),\n): GetContractReturnType<ContractAbis[T]> {\n const chainId = client.chain?.id ?? vanaMainnet.id;\n const cacheKey = createCacheKey(contract, chainId);\n\n let controller = contractCache.get(cacheKey);\n\n if (!controller) {\n controller = getContract({\n address: getContractAddress(chainId, contract),\n abi: getAbi(contract),\n client,\n }) as GetContractReturnType<ContractAbis[T]>;\n\n contractCache.set(cacheKey, controller);\n }\n\n return controller as GetContractReturnType<ContractAbis[T]>;\n}\n\n/**\n * Gets contract information without creating a contract instance.\n *\n * @remarks\n * Returns contract address and ABI for manual contract interaction or\n * custom client configuration. Useful when you need contract details\n * but don't want to create a client connection.\n *\n * @param contract - Name of the contract.\n * Use const assertion for typed ABI.\n * @param chainId - Chain ID to get contract info for.\n * Defaults to Vana mainnet (1480).\n * @returns Contract information with typed ABI\n *\n * @example\n * ```typescript\n * const info = getContractInfo(\"DataRegistry\" as const, 14800);\n * console.log(info.address); // Typed as Address\n * console.log(info.abi); // Fully typed ABI\n *\n * // Use with custom viem client\n * const contract = getContract({\n * ...info,\n * client: customClient\n * });\n * ```\n *\n * @category Contracts\n */\nexport function getContractInfo<T extends VanaContract>(\n contract: T,\n chainId: VanaChainId = vanaMainnet.id as VanaChainId,\n): ContractInfo<ContractAbis[T]> {\n return {\n address: getContractAddress(chainId, contract),\n abi: getAbi(contract),\n };\n}\n\n/**\n * Provides type-safe contract factory for creating multiple contract instances.\n *\n * @remarks\n * Alternative API for applications that need to create multiple contracts\n * with the same client. The factory pattern reduces boilerplate and ensures\n * consistent client configuration across contracts.\n *\n * @example\n * ```typescript\n * const factory = new ContractFactory(client);\n *\n * const dataRegistry = factory.create(\"DataRegistry\" as const);\n * const permissions = factory.create(\"DataPortabilityPermissions\" as const);\n *\n * // List available contracts\n * const contracts = factory.getAvailableContracts();\n * ```\n *\n * @category Contracts\n */\nexport class ContractFactory {\n private readonly client:\n | PublicClient\n | WalletClient\n | ReturnType<typeof createClient>;\n private readonly chainId: number;\n\n constructor(\n client: PublicClient | WalletClient | ReturnType<typeof createClient>,\n ) {\n this.client = client;\n try {\n this.chainId = client.chain?.id ?? vanaMainnet.id;\n } catch {\n this.chainId = vanaMainnet.id;\n }\n }\n\n /**\n * Creates a typed contract instance\n *\n * @param contract - Contract name (use const assertion for full typing)\n * @returns Fully typed contract instance\n */\n create<T extends VanaContract>(\n contract: T,\n ): GetContractReturnType<ContractAbis[T]> {\n return getContractController(contract, this.client);\n }\n\n /**\n * Gets contract information without creating an instance\n *\n * @param contract - Contract name\n * @returns Contract information with typed ABI\n */\n getInfo<T extends VanaContract>(contract: T): ContractInfo<ContractAbis[T]> {\n return getContractInfo(contract, this.chainId as VanaChainId);\n }\n\n /**\n * Lists all available contracts for the current chain\n *\n * @returns Array of contract names available on this chain\n */\n getAvailableContracts(): VanaContract[] {\n // Return all contract names that have addresses on this chain\n const chainAddresses = CONTRACT_ADDRESSES[this.chainId];\n if (!chainAddresses) return [];\n\n return Object.keys(chainAddresses) as VanaContract[];\n }\n}\n\n/**\n * Clears the contract cache. Useful for testing or when chain configurations change.\n *\n * @param contract - Optional specific contract to clear, or clear all if not provided\n * @param chainId - Optional specific chain to clear, or clear all if not provided\n */\nexport function clearContractCache(\n contract?: VanaContract,\n chainId?: number,\n): void {\n if (contract && chainId) {\n const cacheKey = createCacheKey(contract, chainId);\n contractCache.delete(cacheKey);\n } else if (contract) {\n // Clear all instances of this contract across all chains\n for (const key of contractCache.keys()) {\n if (key.startsWith(`${contract}:`)) {\n contractCache.delete(key);\n }\n }\n } else if (chainId) {\n // Clear all contracts for this chain\n for (const key of contractCache.keys()) {\n if (key.endsWith(`:${chainId}`)) {\n contractCache.delete(key);\n }\n }\n } else {\n // Clear entire cache\n contractCache.clear();\n }\n}\n\n// Function is already exported above, no need for redundant export\n\n// Type-only exports for enhanced type safety\nexport type { GetContractReturnType } from \"viem\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,kBAKO;AACP,iBAA6D;AAE7D,uBAAuD;AACvD,oBAA6B;AAC7B,oBAA4B;AAG5B,MAAM,gBAAgB,oBAAI,IAAwC;AAG3D,MAAM,0BAA0B;AAevC,SAAS,eAAe,UAAwB,SAAyB;AACvE,SAAO,GAAG,QAAQ,IAAI,OAAO;AAC/B;AAgCO,SAAS,sBACd,UACA,aAGsC,4BAAa,GACX;AACxC,QAAM,UAAU,OAAO,OAAO,MAAM,0BAAY;AAChD,QAAM,WAAW,eAAe,UAAU,OAAO;AAEjD,MAAI,aAAa,cAAc,IAAI,QAAQ;AAE3C,MAAI,CAAC,YAAY;AACf,qBAAa,yBAAY;AAAA,MACvB,aAAS,qCAAmB,SAAS,QAAQ;AAAA,MAC7C,SAAK,mBAAO,QAAQ;AAAA,MACpB;AAAA,IACF,CAAC;AAED,kBAAc,IAAI,UAAU,UAAU;AAAA,EACxC;AAEA,SAAO;AACT;AA+BO,SAAS,gBACd,UACA,UAAuB,0BAAY,IACJ;AAC/B,SAAO;AAAA,IACL,aAAS,qCAAmB,SAAS,QAAQ;AAAA,IAC7C,SAAK,mBAAO,QAAQ;AAAA,EACtB;AACF;AAuBO,MAAM,gBAAgB;AAAA,EACV;AAAA,EAIA;AAAA,EAEjB,YACE,QACA;AACA,SAAK,SAAS;AACd,QAAI;AACF,WAAK,UAAU,OAAO,OAAO,MAAM,0BAAY;AAAA,IACjD,QAAQ;AACN,WAAK,UAAU,0BAAY;AAAA,IAC7B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA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