How to Build a Research Swarm with EchoRift

Research swarms are one of the most practical applications of agent coordination. Multiple agents monitor blockchain activity, research new contracts, and produce analysis reports—all coordinated through EchoRift infrastructure.

What You'll Build

A research swarm with three types of agents:

  • Watcher Agent: Monitors BlockWire for new contract deployments
  • Researcher Agents: Claim research tasks and analyze contracts
  • Analyst Agent: Produces daily reports from completed research

Step 1: Set Up BlockWire Subscription

First, subscribe to BlockWire for new contract events:

typescript
import { BlockWireClient } from '@echorift/blockwire';

const blockwire = new BlockWireClient({
  signer: agentWallet,
});

// Subscribe for 24 hours
const subscription = await blockwire.subscribe({
  webhookUrl: 'https://your-agent.com/blockwire/events',
  eventTypes: ['new_contract'],
  hours: 24,
});

console.log('Subscribed:', subscription.id);

Step 2: Create Switchboard Swarm

Create a swarm for coordination:

typescript
import { SwitchboardClient } from '@echorift/switchboard';

const switchboard = new SwitchboardClient({
  signer: agentWallet,
});

const swarm = await switchboard.swarms.create({
  name: 'research-swarm',
  membership: {
    type: 'allowlist',
    allowlist: [agentWallet.address],
  },
});

console.log('Swarm created:', swarm.id);

Step 3: Build the Watcher Agent

The watcher receives BlockWire events and creates research tasks:

typescript
// Webhook handler for BlockWire events
app.post('/blockwire/events', async (req, res) => {
  // Verify HMAC signature
  const isValid = verifyWebhook(req);
  if (!isValid) {
    return res.status(401).send('Invalid signature');
  }
  
  // Acknowledge immediately
  res.status(200).send('OK');
  
  // Process events async
  const { events } = req.body;
  
  for (const event of events) {
    if (event.type === 'new_contract') {
      // Create research task
      await switchboard.tasks.create({
        swarmId: 'research-swarm',
        type: 'research_contract',
        payload: {
          address: event.address,
          deployer: event.data.deployer,
          block: event.block,
        },
        priority: 1,
      });
    }
  }
});

Step 4: Build Researcher Agents

Researchers claim tasks and perform analysis:

typescript
async function researcherLoop() {
  while (true) {
    // Claim next available task
    const task = await switchboard.tasks.claim({
      swarmId: 'research-swarm',
      agentId: 'researcher-001',
      types: ['research_contract'],
    });
    
    if (task) {
      try {
        // Perform research
        const research = await analyzeContract(task.payload.address);
        
        // Store findings in shared state
        await switchboard.state.set({
          swarmId: 'research-swarm',
          key: `research:${task.payload.address}`,
          value: research,
        });
        
        // Mark task complete
        await switchboard.tasks.complete({
          taskId: task.id,
          result: { researchKey: `research:${task.payload.address}` },
        });
      } catch (error) {
        // Handle errors, release task
        console.error('Research failed:', error);
      }
    } else {
      // No tasks available, wait
      await sleep(5000);
    }
  }
}

Step 5: Set Up Daily Reports with CronSynth

Schedule daily report generation:

typescript
import { CronSynthClient } from '@echorift/cronsynth';

const cronsynth = new CronSynthClient({
  signer: agentWallet,
});

// Schedule daily report at 9 AM UTC
await cronsynth.createSchedule({
  webhookUrl: 'https://your-agent.com/cron/daily-report',
  cron: '0 9 * * *', // 9 AM daily
});

Report handler:

typescript
app.post('/cron/daily-report', async (req, res) => {
  // Acknowledge immediately
  res.status(200).send('OK');
  
  // Get all research from today
  const research = await getTodayResearch();
  
  // Generate report
  const report = await generateReport(research);
  
  // Broadcast to swarm
  await switchboard.messages.broadcast({
    swarmId: 'research-swarm',
    agentId: 'analyst-001',
    message: {
      type: 'DAILY_REPORT_READY',
      reportUrl: report.url,
    },
  });
});

Why This Works

This architecture provides:

  • No duplicate work: Atomic task claims ensure each contract is researched once
  • Scalable: Add more researcher agents without changing architecture
  • Reliable: Circuit breakers prevent runaway agents, treasury controls spending
  • Verifiable: BlockWire attestations prove events were real

Next Steps

Extend this pattern:

  • Add priority scoring for contracts
  • Add risk analysis agents
  • Add notification agents for important findings
  • Add treasury for paying external APIs

The coordination infrastructure handles the hard parts. You focus on agent logic.


Part of the EchoRift infrastructure series. Learn more about EchoRift architecture.