1+ import AcpClient , {
2+ AcpContractClient ,
3+ AcpJobPhases ,
4+ AcpJob ,
5+ baseSepoliaAcpConfig
6+ } from '@virtuals-protocol/acp-node' ;
7+
8+ import * as dotenv from "dotenv" ;
9+ import { Address } from "viem" ;
10+
11+ // Load environment variables
12+ dotenv . config ( { override : true } ) ;
13+
14+ async function testBuyer ( ) {
15+ console . log ( "Starting buyer test..." ) ;
16+
17+ // Define the onNewTask callback
18+ const onNewTask = async ( job : AcpJob ) => {
19+ console . log ( `Received job update for job ${ job . id } , phase: ${ job . phase } ` ) ;
20+
21+ if ( job . phase === AcpJobPhases . NEGOTIATION ) {
22+ // Check if there's a memo that indicates next phase is TRANSACTION
23+ for ( const memo of job . memos ) {
24+ if ( memo . nextPhase === AcpJobPhases . TRANSACTION ) {
25+ console . log ( "Paying job" , job . id ) ;
26+ // await job.pay(2); // Pay 2 ETH
27+ break ;
28+ }
29+ }
30+ } else if ( job . phase === AcpJobPhases . COMPLETED ) {
31+ console . log ( "Job completed" , job ) ;
32+ }
33+ } ;
34+
35+ // Initialize AcpClient
36+ const acpClient = new AcpClient ( {
37+ acpContractClient : await AcpContractClient . build (
38+ process . env . WHITELISTED_WALLET_PRIVATE_KEY as `0x${string } `,
39+ Number ( process . env . WHITELISTED_WALLET_ENTITY_ID ) ,
40+ process . env . BUYER_WALLET_ADDRESS as Address ,
41+ baseSepoliaAcpConfig
42+ ) ,
43+ onNewTask : onNewTask
44+ } ) ;
45+
46+ // Browse agents
47+ console . log ( "Browsing agents..." ) ;
48+ const agents = await acpClient . browseAgents ( "aixbt" , "hedgefund" ) ;
49+ console . log ( `Found ${ agents . length } agents` ) ;
50+
51+ // Look for aixbt agent
52+ let aixbtAgent = null ;
53+ let selectedOffering = null ;
54+
55+ // Iterate through all agents to find aixbt
56+ for ( const agent of agents ) {
57+ console . log ( `Checking agent: ${ agent . name } ` ) ;
58+
59+ // Check if this agent has "aixbt" in its name or description
60+ if (
61+ agent . name . toLowerCase ( ) . includes ( "aixbt" ) ||
62+ ( agent . description && agent . description . toLowerCase ( ) . includes ( "aixbt" ) )
63+ ) {
64+ console . log ( `Found aixbt agent: ${ agent . name } ` ) ;
65+ aixbtAgent = agent ;
66+
67+ // If the agent has offerings, select the first one
68+ if ( agent . offerings && agent . offerings . length > 0 ) {
69+ selectedOffering = agent . offerings [ 0 ] ;
70+ console . log ( "Selected offering:" , selectedOffering ) ;
71+ break ;
72+ }
73+ }
74+ }
75+
76+ if ( aixbtAgent && selectedOffering ) {
77+ // Calculate expiration date (1 day from now)
78+ const expiredAt = new Date ( ) ;
79+ expiredAt . setDate ( expiredAt . getDate ( ) + 1 ) ;
80+
81+ // Initiate job with the correct parameters
82+ const serviceRequirement = "Get top 1 project pick from aixbt agent to invest in" ;
83+ const jobId = await acpClient . initiateJob (
84+ aixbtAgent . walletAddress ,
85+ serviceRequirement ,
86+ 0.02 ,
87+ undefined ,
88+ expiredAt
89+ ) ;
90+
91+ console . log ( `Job ${ jobId } initiated with aixbt agent` ) ;
92+ } else {
93+ console . log ( "No aixbt agent or offerings found" ) ;
94+ }
95+
96+ // Keep the process running to listen for events
97+ console . log ( "Listening for next steps..." ) ;
98+ while ( true ) {
99+ await new Promise ( resolve => setTimeout ( resolve , 30000 ) ) ; // Wait 30 seconds
100+ console . log ( "Still listening..." ) ;
101+ }
102+ }
103+
104+ // Run the test
105+ testBuyer ( ) . catch ( error => {
106+ console . error ( "Error in buyer test:" , error ) ;
107+ process . exit ( 1 ) ;
108+ } ) ;
0 commit comments