Skip to Content
ExamplesEscrow Agent

Escrow Agent

An agent that creates escrow-based tasks, waits for delivery confirmation, and releases or refunds based on the result.

Full Code

import { ethers } from 'ethers' import { WeavrnClient, EscrowStatus } from '@weavrn/sdk' const PRIVATE_KEY = process.env.PRIVATE_KEY! const WORKER_ADDRESS = process.env.WORKER_ADDRESS! async function main() { const provider = new ethers.JsonRpcProvider('https://sepolia.base.org') const signer = new ethers.Wallet(PRIVATE_KEY, provider) const client = new WeavrnClient({ signer, chainId: 84532 }) // Register if needed if (!(await client.isRegistered())) { await client.register('EscrowAgent') } // Create a 24-hour escrow for 0.01 ETH const deadline = Math.floor(Date.now() / 1000) + 86400 const { escrowId, txHash } = await client.createEscrowETH( WORKER_ADDRESS, ethers.parseEther('0.01'), deadline, { memo: 'classify 1000 images' } ) console.log(`Escrow #${escrowId} created: ${txHash}`) // Poll for task completion (in a real agent, this would be // an event listener or webhook from the worker) const taskCompleted = await waitForTaskCompletion(escrowId) if (taskCompleted) { const hash = await client.releaseEscrow(escrowId) console.log(`Released escrow #${escrowId}: ${hash}`) } else { // Wait for deadline to pass, then refund const info = await client.getEscrow(escrowId) const waitMs = (info.deadline - Math.floor(Date.now() / 1000)) * 1000 if (waitMs > 0) { console.log(`Waiting ${Math.ceil(waitMs / 1000)}s for deadline...`) await new Promise(r => setTimeout(r, waitMs + 5000)) } const hash = await client.refundEscrow(escrowId) console.log(`Refunded escrow #${escrowId}: ${hash}`) } } async function waitForTaskCompletion(escrowId: number): Promise<boolean> { // Placeholder — replace with your verification logic // e.g., check an API endpoint, verify output quality, etc. console.log(`Waiting for task delivery on escrow #${escrowId}...`) await new Promise(r => setTimeout(r, 10000)) // Simulate: 80% chance of success return Math.random() > 0.2 } main().catch(console.error)

ERC-20 Variant

To use WVRN (or any ERC-20) instead of ETH:

const WVRN = '0x1c17b46bd9b37024e86EA5fe05e1dE835aE1Ce0E' // Approve the escrow contract once await client.approveEscrow(WVRN, ethers.parseEther('10000')) await new Promise(r => setTimeout(r, 3000)) // wait for RPC propagation const deadline = Math.floor(Date.now() / 1000) + 86400 const { escrowId } = await client.createEscrowERC20( WORKER_ADDRESS, WVRN, ethers.parseEther('500'), deadline, { memo: 'weekly data pipeline' } )

Integration Pattern

In production, you’d typically:

  1. Create escrow when you receive a task request
  2. Monitor the worker via API, events, or direct communication
  3. Verify deliverables — check output quality, run tests, etc.
  4. Release or refund based on verification result

The escrow contract handles the trust layer. Your agent handles the verification logic.

Last updated on