Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ai agent 1.2 #2

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ai-agents/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ async function promptUser() {
});
}

console.log('Payment CLI started. Type your payment command (e.g., "transfer 100 rs to user@upi")');
console.log('Payment CLI started. Type your payment command (e.g., "transfer 100 rs to rahul@icicbank")');
promptUser();
44 changes: 22 additions & 22 deletions packages/ai-agents/src/cli/paymentCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ import { keccak256, toBytes } from 'viem';

// ASCII Art for CLI
console.log(`

$$$$$$$$\ $$\ $$\ $$$$$$$\ $$$$$$\ $$$$$$\ $$\ $$$$$$\
\____$$ |$$ | $$ | $$ __$$\ $$ __$$\ \_$$ _|$$ | $$ __$$\
$$ / $$ |$$ / $$ | $$ |$$ / $$ | $$ | $$ | $$ / \__|
$$ / $$$$$ / $$$$$$$ |$$$$$$$$ | $$ | $$ | \$$$$$$\
$$ / $$ $$< $$ __$$< $$ __$$ | $$ | $$ | \____$$\
$$ / $$ |\$$\ $$ | $$ |$$ | $$ | $$ | $$ | $$\ $$ |
$$$$$$$$\ $$ | \$$\ $$ | $$ |$$ | $$ |$$$$$$\ $$$$$$$$\\$$$$$$ |
\________|\__| \__| \__| \__|\__| \__|\______|\________|\______/

\________|\__| \__| \__| \__|\__| \__|\______|\________|\______/


`);

console.log(`AI Agent ready for payments powered by the CDP AgentKit 🤖

`)

const INTENT_AGGREGATOR_URL = 'https://zkrail-intent-aggregator.d4mr.workers.dev';

const ERC20_ABI = [
Expand Down Expand Up @@ -116,9 +118,9 @@ export class PaymentCli {
throw new Error('Failed to approve USDC spend');
}

return `Approved ${amount * 3.5} USDC for ${USDC_CONTRACT_ADDRESS}. Transaction hash: ${approveTx.getTransactionHash()}`;
return `Successfully approved ${amount * 3.5} USDC for ${USDC_CONTRACT_ADDRESS}. Transaction hash: ${approveTx.getTransactionHash()}`;
} catch (error) {
console.error('Approval failed:', error);
console.error('Error during approval:', error);
throw new Error('Failed to approve USDC transfer');
}
}
Expand All @@ -143,9 +145,9 @@ export class PaymentCli {
throw new Error('Failed to transfer USDC');
}

return `Transferred ${amount} USDC to ${recipientAddress}. Transaction hash: ${transferTx.getTransactionHash()}`;
return `Successfully transferred ${amount} USDC to ${recipientAddress}. Transaction hash: ${transferTx.getTransactionHash()}`;
} catch (error) {
console.error('Transfer failed:', error);
console.error('Error during transfer:', error);
throw new Error('Failed to transfer USDC');
}
}
Expand Down Expand Up @@ -177,7 +179,7 @@ export class PaymentCli {
upiId: result.upiId
};
} catch (error) {
console.error('Failed to parse LLM response:', content);
console.error('Failed to parse the response:', content);
throw new Error('Invalid response format from LLM');
}
}
Expand All @@ -196,12 +198,12 @@ export class PaymentCli {
}

if (attempt < maxRetries) {
console.log(`No solutions yet, waiting ${delaySeconds} seconds before retry...`);
console.log(`No solutions found yet, waiting ${delaySeconds} seconds before retry...`);
await new Promise(resolve => setTimeout(resolve, delaySeconds * 1000));
}
}

throw new Error(`No solutions available after ${maxRetries} attempts`);
throw new Error(`Could not find solutions after ${maxRetries} attempts`);
}

private async pollIntentState(intentId: string, targetState: string, maxAttempts = 100): Promise<void> {
Expand Down Expand Up @@ -315,9 +317,9 @@ export class PaymentCli {

async processPaymentPrompt(prompt: string) {
try {
console.log("Processing payment prompt...");
console.log("Processing the payment prompt...");

// Step 1: Extract payment details using AI
// Step 1: Extract payment details
const details = await this.extractPaymentDetails(prompt);
console.log("Extracted payment details:", details);

Expand All @@ -337,7 +339,7 @@ export class PaymentCli {
intent
);
const { intentId } = intentResponse.data;
console.log("Intent created:", intentId);
console.log("Created intent:", intentId);

// Step 3: Get solutions (quotations) with retry
console.log("Fetching solutions...");
Expand All @@ -362,15 +364,15 @@ export class PaymentCli {
parseFloat(totalAmount.toString()) / 1e6, // Use the total amount from contract
ZKRAIL_UPI_ADDRESS // Approve the ZKRail contract to spend
);
console.log("Approval completed:", approvalResult);
console.log("Completed the approval:", approvalResult);

// Step 5: Transfer USDC to solver
// console.log("Transferring USDC...");
// const transferResult = await this.transferUSDC(
// parseFloat(bestSolution.amountWei) / 1e6,
// bestSolution.solverAddress
// );
// console.log("Transfer completed:", transferResult);
// console.log("Completed the transfer:", transferResult);
console.log({bestSolution})

// Step 4: Commit to solution
Expand All @@ -384,7 +386,7 @@ export class PaymentCli {
`${INTENT_AGGREGATOR_URL}/api/solutions/${bestSolution.id}/accept`,
{ commitmentTxHash: commitTxHash }
);
console.log("Solution accepted");
console.log("Accepted the solution");

// Step 6: Wait for payment to be claimed
console.log("Waiting for payment to be claimed...");
Expand All @@ -393,7 +395,7 @@ export class PaymentCli {
// Step 7: Settle the solution
console.log("Settling solution...");
const settleTxHash = await this.settleSolution(bestSolution.intentId);
console.log("Solution settled:", settleTxHash);
console.log("Settled the solution:", settleTxHash);

// Step 8: Notify API about settlement
try {
Expand All @@ -408,17 +410,15 @@ export class PaymentCli {
}
}
);
console.log("Settlement recorded with API");
console.log("Recorded the settlement with API");
} catch (error) {
console.error("Failed to record settlement with API:", error);
// Don't throw here as the on-chain settlement was successful
}

} catch (error) {
console.error("Error processing payment prompt:", error);
console.error("Encountered an error processing payment prompt:", error);
throw error;
}
}
}


}