77 *
88 * Default: scenario 1
99 *
10- * Scenarios:
11- * 1. Subscription Offering
12- * - Seller creates PAYABLE_REQUEST_SUBSCRIPTION memo
13- * - Buyer pays subscription; job proceeds to delivery
14- *
15- * 2. Non-Subscription Offering (Fixed-Price)
16- * - Uses a non-subscription offering (jobOfferings[1])
17- * - Seller accepts and creates a payable requirement
18- * - Buyer pays and advances to delivery
10+ * Assumption:
11+ * - chosenAgent.jobOfferings[0] is a subscription offering
12+ * - chosenAgent.jobOfferings[1] is a non-subscription (fixed-price) offering
1913 */
2014import AcpClient , {
2115 AcpContractClientV2 ,
@@ -39,7 +33,8 @@ const SUBSCRIPTION_TIER = "sub_premium";
3933
4034// Parse --scenario N from argv
4135const scenarioArg = process . argv . indexOf ( "--scenario" ) ;
42- const SCENARIO = scenarioArg !== - 1 ? parseInt ( process . argv [ scenarioArg + 1 ] , 10 ) : 1 ;
36+ const SCENARIO =
37+ scenarioArg !== - 1 ? parseInt ( process . argv [ scenarioArg + 1 ] , 10 ) : 1 ;
4338
4439async function buyer ( ) {
4540 console . log ( `=== Subscription Example - Buyer (Scenario ${ SCENARIO } ) ===\n` ) ;
@@ -66,37 +61,55 @@ async function buyer() {
6661 console . log (
6762 `Buyer: Job ${ job . id } — Subscription payment requested: ${ memoToSign . content } ` ,
6863 ) ;
69- console . log ( `Buyer: Job ${ job . id } — Amount: ${ memoToSign . payableDetails ?. amount } ` ) ;
64+ console . log (
65+ `Buyer: Job ${ job . id } — Amount: ${ memoToSign . payableDetails ?. amount } ` ,
66+ ) ;
7067 const { txnHash : subPayTx } = await job . paySubscription (
7168 `Subscription payment for ${ SUBSCRIPTION_TIER } ` ,
7269 ) ;
73- console . log ( `Buyer: Job ${ job . id } — Subscription paid (tx: ${ subPayTx } )` ) ;
70+ console . log (
71+ `Buyer: Job ${ job . id } — Subscription paid (tx: ${ subPayTx } )` ,
72+ ) ;
7473
75- // Fixed-price requirement — pay and advance to delivery (Scenario 3 )
74+ // Fixed-price requirement — pay and advance to delivery (Scenario 2 )
7675 } else if (
7776 job . phase === AcpJobPhases . NEGOTIATION &&
7877 memoToSign ?. type === MemoType . PAYABLE_REQUEST
7978 ) {
80- console . log ( `Buyer: Job ${ job . id } — Fixed-price requirement, paying now` ) ;
79+ console . log (
80+ `Buyer: Job ${ job . id } — Fixed-price requirement, paying now` ,
81+ ) ;
8182 const payResult = await job . payAndAcceptRequirement ( "Payment for job" ) ;
82- console . log ( `Buyer: Job ${ job . id } — Paid and advanced to TRANSACTION phase (tx: ${ payResult ?. txnHash } )` ) ;
83+ console . log (
84+ `Buyer: Job ${ job . id } — Paid and advanced to TRANSACTION phase (tx: ${ payResult ?. txnHash } )` ,
85+ ) ;
8386
84- // Valid subscription — accept requirement without payment (Scenario 2)
87+ // Active subscription path — accept requirement without payment
8588 } else if (
8689 job . phase === AcpJobPhases . NEGOTIATION &&
8790 memoToSign ?. type === MemoType . MESSAGE &&
8891 memoToSign ?. nextPhase === AcpJobPhases . TRANSACTION
8992 ) {
90- console . log ( `Buyer: Job ${ job . id } — Subscription active, accepting without payment` ) ;
93+ console . log (
94+ `Buyer: Job ${ job . id } — Subscription active, accepting without payment` ,
95+ ) ;
9196 const { txnHash : signMemoTx } = await job . acceptRequirement (
9297 memoToSign ,
9398 "Subscription verified, proceeding to delivery" ,
9499 ) ;
95- console . log ( `Buyer: Job ${ job . id } — Advanced to TRANSACTION phase (tx: ${ signMemoTx } )` ) ;
100+ console . log (
101+ `Buyer: Job ${ job . id } — Advanced to TRANSACTION phase (tx: ${ signMemoTx } )` ,
102+ ) ;
96103 } else if ( job . phase === AcpJobPhases . COMPLETED ) {
97- console . log ( `Buyer: Job ${ job . id } — Completed! Deliverable:` , job . deliverable ) ;
104+ console . log (
105+ `Buyer: Job ${ job . id } — Completed! Deliverable:` ,
106+ job . deliverable ,
107+ ) ;
98108 } else if ( job . phase === AcpJobPhases . REJECTED ) {
99- console . log ( `Buyer: Job ${ job . id } — Rejected. Reason:` , job . rejectionReason ) ;
109+ console . log (
110+ `Buyer: Job ${ job . id } — Rejected. Reason:` ,
111+ job . rejectionReason ,
112+ ) ;
100113 } else {
101114 console . log (
102115 `Buyer: Job ${ job . id } — Unhandled event (phase: ${ AcpJobPhases [ job . phase ] } , ` +
@@ -116,36 +129,47 @@ async function buyer() {
116129 showHiddenOfferings : true ,
117130 } ) ;
118131
132+ console . log ( "Relevant agents:" , relevantAgents ) ;
133+
119134 if ( ! relevantAgents || relevantAgents . length === 0 ) {
120135 console . error ( "No agents found" ) ;
121136 return ;
122137 }
123138
139+ // Pick one of the agents based on your criteria (in this example we just pick the first one)
124140 const chosenAgent = relevantAgents [ 0 ] ;
141+
142+ // Pick one of the service offerings based on your criteria:
143+ // - index 0: subscription offering
144+ // - index 1: non-subscription (fixed-price) offering
125145 const subscriptionOffering = chosenAgent . jobOfferings [ 0 ] ;
126146 const fixedOffering = chosenAgent . jobOfferings [ 1 ] ;
127147
128148 switch ( SCENARIO ) {
129149 case 1 : {
130- console . log ( "--- Scenario 1: Subscription Offering ---\n" ) ;
131- const jobId1 = await subscriptionOffering . initiateJob (
150+ const chosenJobOffering = subscriptionOffering ;
151+ const jobId = await chosenJobOffering . initiateJob (
152+ // Requirement payload schema depends on your ACP service configuration.
153+ // If your service requires fields, replace {} with the expected schema payload.
132154 { } ,
133- undefined ,
134- new Date ( Date . now ( ) + 1000 * 60 * 15 ) , // 15 min job expiry
155+ undefined , // evaluator address, undefined fallback to empty address
156+ new Date ( Date . now ( ) + 1000 * 60 * 15 ) , // job expiry duration, minimum 5 minutes
135157 SUBSCRIPTION_TIER ,
136158 ) ;
137- console . log ( `\nBuyer : [Scenario 1 — Subscription Offering] Job ${ jobId1 } initiated` ) ;
159+ console . log ( `Buyer : [Scenario 1 — Subscription Offering] Job ${ jobId } initiated` ) ;
138160 break ;
139161 }
140162
141163 case 2 : {
142- console . log ( "--- Scenario 2: Non-Subscription Offering (Fixed-Price) ---\n" ) ;
143- const jobId2 = await fixedOffering . initiateJob (
164+ const chosenJobOffering = fixedOffering ;
165+ const jobId = await chosenJobOffering . initiateJob (
166+ // Requirement payload schema depends on your ACP service configuration.
167+ // If your service requires fields, replace {} with the expected schema payload.
144168 { } ,
145- undefined ,
146- new Date ( Date . now ( ) + 1000 * 60 * 15 ) , // 15 min job expiry
169+ undefined , // evaluator address, undefined fallback to empty address
170+ new Date ( Date . now ( ) + 1000 * 60 * 15 ) , // job expiry duration, minimum 5 minutes
147171 ) ;
148- console . log ( `\nBuyer : [Scenario 2 — Fixed-Price Job] Job ${ jobId2 } initiated` ) ;
172+ console . log ( `Buyer : [Scenario 2 — Fixed-Price Job] Job ${ jobId } initiated` ) ;
149173 break ;
150174 }
151175
0 commit comments