@@ -2,6 +2,8 @@ import { Command } from 'commander';
22import { Commands } from './commands.js' ;
33import { ethers } from 'ethers' ;
44import chalk from 'chalk' ;
5+ import { stdin as input , stdout as output } from 'node:process' ;
6+ import { createInterface } from 'readline/promises' ;
57
68async function initializeSigner ( ) {
79
@@ -174,38 +176,6 @@ export async function createCLI() {
174176 const commands = new Commands ( signer , chainId ) ;
175177 await commands . allowAlgo ( [ null , dsDid , aDid , options . encrypt . toString ( ) ] ) ;
176178 } ) ;
177- // initializeCommand command
178- program
179- . command ( 'initializeCompute' )
180- . description ( 'Initialises provider fees and payment for a compute job' )
181- . argument ( '<datasetDids>' , 'Dataset DIDs (comma-separated) OR (empty array for none)' )
182- . argument ( '<algoDid>' , 'Algorithm DID' )
183- . argument ( '<computeEnvId>' , 'Compute environment ID' )
184- . argument ( '<computeValidUntil>' , 'Valid Until for fees availability' )
185- . argument ( '<paymentToken>' , 'Payment token for compute' )
186- . argument ( '<resources>' , 'Resources of compute environment stringified' )
187- . option ( '-d, --datasets <datasetDids>' , 'Dataset DIDs (comma-separated) OR (empty array for none)' )
188- . option ( '-a, --algo <algoDid>' , 'Algorithm DID' )
189- . option ( '-e, --env <computeEnvId>' , 'Compute environment ID' )
190- . option ( '--validUntil <validUntil>' , 'Compute fees valid until' )
191- . option ( '-t, --token <paymentToken>' , 'Compute payment token' )
192- . option ( '--resources <resources>' , 'Compute resources' )
193- . action ( async ( datasetDids , algoDid , computeEnvId , computeValidUntil , paymentToken , resources , options ) => {
194- const dsDids = options . datasets || datasetDids ;
195- const aDid = options . algo || algoDid ;
196- const envId = options . env || computeEnvId ;
197- const validUntil = options . validUntil || computeValidUntil ;
198- const token = options . token || paymentToken ;
199- const res = options . resources || resources ;
200- if ( ! dsDids || ! aDid || ! envId || ! validUntil || ! token || ! res ) {
201- console . error ( chalk . red ( 'Missing required arguments' ) ) ;
202- // process.exit(1);
203- return
204- }
205- const { signer, chainId } = await initializeSigner ( ) ;
206- const commands = new Commands ( signer , chainId ) ;
207- await commands . initializeCompute ( [ null , dsDids , aDid , envId , validUntil . toString ( ) , token , JSON . stringify ( res ) ] ) ;
208- } ) ;
209179
210180 // startCompute command
211181 program
@@ -222,31 +192,56 @@ export async function createCLI() {
222192 . option ( '-d, --datasets <datasetDids>' , 'Dataset DIDs (comma-separated) OR (empty array for none)' )
223193 . option ( '-a, --algo <algoDid>' , 'Algorithm DID' )
224194 . option ( '-e, --env <computeEnvId>' , 'Compute environment ID' )
225- . option ( '--init <initializeResponse>' , 'Initialize response' )
226195 . option ( '--maxJobDuration <maxJobDuration>' , 'Compute maxJobDuration' )
227196 . option ( '-t, --token <paymentToken>' , 'Compute payment token' )
228197 . option ( '--resources <resources>' , 'Compute resources' )
229198 . option ( '--amountToDeposit [amountToDeposit]' , 'Amount to deposit in escrow' )
230- . action ( async ( datasetDids , algoDid , computeEnvId , initializeResponse , maxJobDuration , paymentToken , resources , amountToDeposit , options ) => {
199+ . option ( '-y, --yes' , 'Accept payment from initialize compute' )
200+ . action ( async ( datasetDids , algoDid , computeEnvId , maxJobDuration , paymentToken , resources , amountToDeposit , options ) => {
231201 const dsDids = options . datasets || datasetDids ;
232202 const aDid = options . algo || algoDid ;
233203 const envId = options . env || computeEnvId ;
234- const initResp = options . init || initializeResponse ;
235204 const jobDuration = options . maxJobDuration || maxJobDuration ;
236205 const token = options . token || paymentToken ;
237206 const res = options . resources || resources ;
238207 const amount = options . amountToDeposit || amountToDeposit ;
239- if ( ! dsDids || ! aDid || ! envId || ! initResp || ! jobDuration || ! token || ! res ) {
208+ if ( ! dsDids || ! aDid || ! envId || ! jobDuration || ! token || ! res ) {
240209 console . error ( chalk . red ( 'Missing required arguments' ) ) ;
241210 // process.exit(1);
242211 return
243212 }
244213 const { signer, chainId } = await initializeSigner ( ) ;
245- const commands = new Commands ( signer , chainId ) ;
246- const args = [ null , dsDids , aDid , envId , JSON . stringify ( initResp ) , jobDuration . toString ( ) , token , JSON . stringify ( res ) ]
247- if ( amount ) args . push ( amount . toString ( ) )
248- await commands . computeStart ( args ) ;
249- } ) ;
214+ const commands = new Commands ( signer , chainId ) ;
215+
216+ const initArgs = [ null , dsDids , aDid , envId , jobDuration , token , res ] ;
217+ const initResp = await commands . initializeCompute ( initArgs ) ;
218+
219+ if ( ! initResp ) {
220+ console . error ( chalk . red ( 'Initialization failed. Aborting.' ) ) ;
221+ return ;
222+ }
223+
224+ console . log ( chalk . yellow ( '\n--- Payment Details ---' ) ) ;
225+ console . log ( JSON . stringify ( initResp , null , 2 ) ) ;
226+
227+ const proceed = options . yes ;
228+
229+ if ( ! proceed ) {
230+ const rl = createInterface ( { input, output } ) ;
231+ const confirmation = await rl . question ( `\nProceed with payment for starting compute job at price ${ ethers . BigNumber . from ( initResp . payment . amount ) } in tokens from address ${ initResp . payment . token } ? (y/n): ` ) ;
232+ rl . close ( ) ;
233+ if ( confirmation . trim ( ) . toLowerCase ( ) !== 'y' ) {
234+ console . log ( chalk . red ( 'Compute job canceled by user.' ) ) ;
235+ return ;
236+ }
237+ }
238+
239+ const computeArgs = [ null , dsDids , aDid , envId , JSON . stringify ( initResp ) , jobDuration , token , res ] ;
240+ if ( amount ) computeArgs . push ( amount . toString ( ) ) ;
241+
242+ await commands . computeStart ( computeArgs ) ;
243+ console . log ( chalk . green ( 'Compute job started successfully.' ) ) ;
244+ } ) ;
250245
251246 // startFreeCompute command
252247 program
0 commit comments