Skip to content

Task Pool Factory

The TaskPoolFactory is the factory contract for creating and managing multiple TaskPool instances using the minimal proxy pattern. It provides efficient pool creation, management, and integration with the ModelRegistry for automatic task creation.

The TaskPoolFactory contract serves as the central factory for task pools, providing:

  • Pool Creation: Create new task pools using minimal proxy pattern
  • Pool Management: Manage pool lifecycle (active/inactive)
  • Gas Efficiency: Minimal proxy pattern for gas-efficient pool creation
  • Integration: Seamless integration with ModelRegistry
  • Pool Discovery: Efficient pool discovery and management
  • Gas Efficiency: Create pools with minimal gas costs
  • Single Implementation: One implementation contract for all pools
  • Clone Creation: Efficient pool cloning
  • Storage Optimization: Optimized storage layout
  • Lifecycle Management: Activate/deactivate pools
  • Pool Discovery: Track and discover active pools
  • Category Organization: Organize pools by category
  • Metadata Support: Store pool metadata and information
  • ModelRegistry Integration: Automatic task creation
  • TaskRouter Integration: Pool discovery and routing
  • Owner Management: Owner and user pool creation
  • Access Control: Proper authorization and permissions
// Implementation and registry
address public immutable implementation;
address public modelRegistry;
// Pool management
address[] public allPools;
mapping(address => PoolInfo) public poolInfo;
mapping(string => address[]) public poolsByCategory;
mapping(address => string) public poolCategory;
// Pool discovery optimization
address[] public activePools;
mapping(address => uint256) public activePoolIndex;
mapping(address => bool) public isRegisteredPool;
struct PoolInfo {
address owner; // Pool owner
uint256 createdAt; // Creation timestamp
bool isActive; // Pool status
string metadata; // Pool metadata
}
function createPool(
string calldata category,
string calldata metadata
) external returns (address)

Parameters:

  • category: Pool category (e.g., “AI Tasks”, “Image Processing”)
  • metadata: Pool metadata and description

Returns:

  • address: Address of the created pool

Example:

// Create a new task pool
const tx = await taskPoolFactory.createPool(
"AI Tasks",
"Pool for AI inference tasks"
);
const receipt = await tx.wait();
// Get the pool address from the event
const poolAddress = receipt.logs[0].args.pool;
console.log(`Created pool: ${poolAddress}`);
function createPoolFor(
address user,
string calldata category,
string calldata metadata
) external onlyOwner returns (address)

Parameters:

  • user: Address of the user to create pool for
  • category: Pool category
  • metadata: Pool metadata

Access Control: Only owner can create pools for specific users

Example:

// Owner creates pool for specific user
const poolAddress = await taskPoolFactory.createPoolFor(
userAddress,
"AI Tasks",
"Pool for specific user"
);
function getActivePools() external view returns (address[] memory)

Returns:

  • address[]: Array of active pool addresses

Example:

// Get all active pools
const activePools = await taskPoolFactory.getActivePools();
console.log(`Found ${activePools.length} active pools`);
activePools.forEach((pool, index) => {
console.log(`Pool ${index + 1}: ${pool}`);
});
function getPoolsByCategory(string calldata category) external view returns (address[] memory)

Parameters:

  • category: Pool category

Returns:

  • address[]: Array of pool addresses in the category

Example:

// Get pools by category
const aiPools = await taskPoolFactory.getPoolsByCategory("AI Tasks");
console.log(`Found ${aiPools.length} AI task pools`);
function getPoolInfo(address pool) external view returns (PoolInfo memory)

Parameters:

  • pool: Pool address

Returns:

  • PoolInfo: Pool information struct

Example:

// Get pool information
const poolInfo = await taskPoolFactory.getPoolInfo(poolAddress);
console.log(`Owner: ${poolInfo.owner}`);
console.log(`Created: ${new Date(poolInfo.createdAt * 1000)}`);
console.log(`Active: ${poolInfo.isActive}`);
console.log(`Metadata: ${poolInfo.metadata}`);
function activatePool(address pool) external onlyOwner

Parameters:

  • pool: Pool address to activate

Access Control: Only owner can activate pools

Example:

// Activate a pool
await taskPoolFactory.activatePool(poolAddress);
console.log("Pool activated");
function deactivatePool(address pool) external onlyOwner

Parameters:

  • pool: Pool address to deactivate

Access Control: Only owner can deactivate pools

Example:

// Deactivate a pool
await taskPoolFactory.deactivatePool(poolAddress);
console.log("Pool deactivated");
function setModelRegistry(address _modelRegistry) external onlyOwner

Parameters:

  • _modelRegistry: ModelRegistry contract address

Access Control: Only owner can set ModelRegistry

Example:

// Set ModelRegistry for automatic task creation
await taskPoolFactory.setModelRegistry(modelRegistryAddress);
console.log("ModelRegistry set");
// Deploy the implementation contract
const TaskPoolImplementation = await ethers.getContractFactory(
"TaskPoolImplementation"
);
const taskPoolImplementation = await TaskPoolImplementation.deploy();
// Deploy the factory with implementation address
const TaskPoolFactory = await ethers.getContractFactory("TaskPoolFactory");
const taskPoolFactory = await TaskPoolFactory.deploy(
await taskPoolImplementation.getAddress()
);
// Create multiple pools
const pool1 = await taskPoolFactory.createPool("AI Tasks", "Pool 1");
const pool2 = await taskPoolFactory.createPool("AI Tasks", "Pool 2");
const pool3 = await taskPoolFactory.createPool("Image Processing", "Pool 3");
// Fund the pools with HYRA tokens
await owner.sendTransaction({
to: pool1Address,
value: ethers.parseEther("1.0"),
});
await owner.sendTransaction({
to: pool2Address,
value: ethers.parseEther("1.0"),
});
await owner.sendTransaction({
to: pool3Address,
value: ethers.parseEther("1.0"),
});
event PoolCreated(
address indexed pool,
address indexed owner,
uint256 poolId,
string category
);
event PoolActivated(address indexed pool);
event PoolDeactivated(address indexed pool);
event ModelRegistryUpdated(address indexed oldRegistry, address indexed newRegistry);
event PoolCategoryUpdated(address indexed pool, string oldCategory, string newCategory);
// ModelRegistry creates tasks in pools
await modelRegistry.requestInference("llm-chat-v1", "Hello world");
// Factory manages pool creation and discovery
const activePools = await taskPoolFactory.getActivePools();
// TaskRouter uses factory to discover pools
const activePools = await taskPoolFactory.getActivePools();
// TaskRouter routes tasks to pools
await taskRouter.claimBestTask();
// TaskPoolViewer reads pool data
const poolInfo = await taskPoolFactory.getPoolInfo(poolAddress);
const poolStats = await taskPoolViewer.getPoolStatistics(poolAddress);
  • Single Implementation: One implementation contract for all pools
  • Clone Creation: Efficient pool cloning
  • Storage Optimization: Minimal storage overhead
  • Gas Efficiency: Reduced deployment costs
  • Active Pool Tracking: Optimized active pool management
  • Category Organization: Efficient category-based organization
  • Batch Operations: Support for batch pool operations
  • Use custom errors instead of string messages
  • Reduced gas costs for error handling
// 1. Deploy implementation
const TaskPoolImplementation = await ethers.getContractFactory(
"TaskPoolImplementation"
);
const taskPoolImplementation = await TaskPoolImplementation.deploy();
// 2. Deploy factory
const TaskPoolFactory = await ethers.getContractFactory("TaskPoolFactory");
const taskPoolFactory = await TaskPoolFactory.deploy(
await taskPoolImplementation.getAddress()
);
// 3. Set ModelRegistry
await taskPoolFactory.setModelRegistry(modelRegistryAddress);
// 4. Create pools
const pool1 = await taskPoolFactory.createPool(
"AI Tasks",
"Pool for AI inference"
);
const pool2 = await taskPoolFactory.createPool(
"Image Processing",
"Pool for image tasks"
);
// 5. Fund pools
await owner.sendTransaction({ to: pool1, value: ethers.parseEther("1.0") });
await owner.sendTransaction({ to: pool2, value: ethers.parseEther("1.0") });
// 6. Get active pools
const activePools = await taskPoolFactory.getActivePools();
console.log(`Active pools: ${activePools.length}`);
// Get all pools
const allPools = await taskPoolFactory.getAllPools();
console.log(`Total pools: ${allPools.length}`);
// Get pools by category
const aiPools = await taskPoolFactory.getPoolsByCategory("AI Tasks");
console.log(`AI pools: ${aiPools.length}`);
// Get pool information
const poolInfo = await taskPoolFactory.getPoolInfo(poolAddress);
console.log(`Pool owner: ${poolInfo.owner}`);
console.log(`Pool active: ${poolInfo.isActive}`);
// Activate/deactivate pools
await taskPoolFactory.activatePool(poolAddress);
await taskPoolFactory.deactivatePool(poolAddress);
error PoolNotFound(); // Pool doesn't exist
error PoolAlreadyActive(); // Pool already active
error PoolAlreadyInactive(); // Pool already inactive
error InvalidCategory(); // Invalid category
error Unauthorized(); // Access denied
  • Pool existence checks
  • Category validation
  • Access control checks
  • Parameter validation
  • Owner-only pool management
  • Proper authorization checks
  • Secure pool creation and management
  • Comprehensive parameter validation
  • Pool existence checks
  • Category validation
  • All external functions use ReentrancyGuard
  • Safe external calls
  • Efficient active pool tracking
  • Optimized pool scanning
  • Cached pool data
  • Efficient category-based organization
  • Optimized pool filtering
  • Fast pool lookup

Ready to learn about other smart contracts? Check out: